# Fortran Wiki Matrix inversion

## Using LAPACK (large matrices)

! Returns the inverse of a matrix calculated by finding the LU
! decomposition.  Depends on LAPACK.
function inv(A) result(Ainv)
real(dp), dimension(:,:), intent(in) :: A
real(dp), dimension(size(A,1),size(A,2)) :: Ainv

real(dp), dimension(size(A,1)) :: work  ! work array for LAPACK
integer, dimension(size(A,1)) :: ipiv   ! pivot indices
integer :: n, info

! External procedures defined in LAPACK
external DGETRF
external DGETRI

! 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 /= 0) then
stop '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 /= 0) then
stop 'Matrix inversion failed!'
end if
end function inv

## Direct calculation (small matrices)

In my experience, LAPACK is great when you wish to invert huge N×N matrices, but it can be really slow for inverting smaller 2×2, 3×3, and 4×4 matrices. For my use case, where I need to invert billions of 2×2 and 4×4 matrices instead of a few large N×N matrices, I got a 30% speedup of my program replacing the LAPACK calls by direct calculations of the matrix inversions. I have attached the code that I’ve used for the 2×2, 3×3, and 4×4 cases below. The 2×2 version is quite easy to derive analytically. The 3×3 and 4×4 versions are based on the subroutines M33INV and M44INV by David G. Simpson; I just converted them from subroutines to pure functions.

  pure function matinv2(A) result(B)
!! Performs a direct calculation of the inverse of a 2×2 matrix.
complex(wp), intent(in) :: A(2,2)   !! Matrix
complex(wp)             :: B(2,2)   !! Inverse matrix
complex(wp)             :: detinv

! Calculate the inverse determinant of the matrix
detinv = 1/(A(1,1)*A(2,2) - A(1,2)*A(2,1))

! Calculate the inverse of the matrix
B(1,1) = +detinv * A(2,2)
B(2,1) = -detinv * A(2,1)
B(1,2) = -detinv * A(1,2)
B(2,2) = +detinv * A(1,1)
end function

pure function matinv3(A) result(B)
!! Performs a direct calculation of the inverse of a 3×3 matrix.
complex(wp), intent(in) :: A(3,3)   !! Matrix
complex(wp)             :: B(3,3)   !! Inverse matrix
complex(wp)             :: detinv

! Calculate the inverse determinant of the matrix
detinv = 1/(A(1,1)*A(2,2)*A(3,3) - A(1,1)*A(2,3)*A(3,2)&
- A(1,2)*A(2,1)*A(3,3) + A(1,2)*A(2,3)*A(3,1)&
+ A(1,3)*A(2,1)*A(3,2) - A(1,3)*A(2,2)*A(3,1))

! Calculate the inverse of the matrix
B(1,1) = +detinv * (A(2,2)*A(3,3) - A(2,3)*A(3,2))
B(2,1) = -detinv * (A(2,1)*A(3,3) - A(2,3)*A(3,1))
B(3,1) = +detinv * (A(2,1)*A(3,2) - A(2,2)*A(3,1))
B(1,2) = -detinv * (A(1,2)*A(3,3) - A(1,3)*A(3,2))
B(2,2) = +detinv * (A(1,1)*A(3,3) - A(1,3)*A(3,1))
B(3,2) = -detinv * (A(1,1)*A(3,2) - A(1,2)*A(3,1))
B(1,3) = +detinv * (A(1,2)*A(2,3) - A(1,3)*A(2,2))
B(2,3) = -detinv * (A(1,1)*A(2,3) - A(1,3)*A(2,1))
B(3,3) = +detinv * (A(1,1)*A(2,2) - A(1,2)*A(2,1))
end function

pure function matinv4(A) result(B)
!! Performs a direct calculation of the inverse of a 4×4 matrix.
complex(wp), intent(in) :: A(4,4)   !! Matrix
complex(wp)             :: B(4,4)   !! Inverse matrix
complex(wp)             :: detinv

! Calculate the inverse determinant of the matrix
detinv = &
1/(A(1,1)*(A(2,2)*(A(3,3)*A(4,4)-A(3,4)*A(4,3))+A(2,3)*(A(3,4)*A(4,2)-A(3,2)*A(4,4))+A(2,4)*(A(3,2)*A(4,3)-A(3,3)*A(4,2)))&
- A(1,2)*(A(2,1)*(A(3,3)*A(4,4)-A(3,4)*A(4,3))+A(2,3)*(A(3,4)*A(4,1)-A(3,1)*A(4,4))+A(2,4)*(A(3,1)*A(4,3)-A(3,3)*A(4,1)))&
+ A(1,3)*(A(2,1)*(A(3,2)*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