inverse Subroutine

public subroutine inverse(a, ainv, status_ok)

inverse of a 2x2 matrix.

See: https:

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in), dimension(2,2) :: a
real(kind=wp), intent(out), dimension(2,2) :: ainv
logical, intent(out) :: status_ok

Source Code

    subroutine inverse (a, ainv, status_ok)

        real(wp), dimension(2,2), intent(in)  :: a
        real(wp), dimension(2,2), intent(out) :: ainv
        logical, intent(out) :: status_ok

        real(wp), parameter :: eps = 1.0e-10_wp
        real(wp), dimension(2,2) :: cofactor

        associate( det => a(1,1)*a(2,2) - a(1,2)*a(2,1) )
            if (abs(det) <= eps) then
                ainv = 0.0_wp
                status_ok = .false.
            else
                cofactor(1,1) = +a(2,2)
                cofactor(1,2) = -a(2,1)
                cofactor(2,1) = -a(1,2)
                cofactor(2,2) = +a(1,1)
                ainv = transpose(cofactor) / det
                status_ok = .true.
            end if
        end associate

    end subroutine inverse