Computes matrix inverse using LAPACK Input matrix should be a square matrix
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=wp), | intent(in) | :: | A(:,:) |
input matrix |
FUNCTION INV(A) RESULT(Ainv) ! MODULES IMPLICIT NONE !----------------------------------------------------------------------------------------------------------------------------------- ! INPUT/OUTPUT VARIABLES REAL(wp),INTENT(IN) :: A(:,:) !! input matrix REAL(wp) :: Ainv(SIZE(A,1),SIZE(A,2)) !! result: inverse of A !----------------------------------------------------------------------------------------------------------------------------------- ! External procedures defined in LAPACK EXTERNAL DGETRF EXTERNAL DGETRI ! LOCAL VARIABLES REAL(wp):: work(SIZE(A,1)) ! work array for LAPACK INTEGER :: ipiv(SIZE(A,1)) ! pivot indices INTEGER :: n,info !=================================================================================================================================== ! Store A in Ainv to prevent it from being overwritten by LAPACK Ainv = A n = size(A,1) ! DGETRF computes an LU factorization of a general M-by-N matrix A ! using partial pivoting with row interchanges. CALL DGETRF(n, n, Ainv, n, ipiv, info) IF(info.NE.0)THEN CALL abort(__STAMP__,& 'Matrix is numerically singular!') END IF ! DGETRI computes the inverse of a matrix using the LU factorization ! computed by DGETRF. CALL DGETRI(n, Ainv, n, ipiv, work, n, info) IF(info.NE.0)THEN CALL abort(__STAMP__,& 'Matrix inversion failed!') END IF END FUNCTION INV