SUBROUTINE d_inv(mat, matinv)
implicit none
real(dp), INTENT(IN) :: mat(:, :)
real(dp), dimension(size(mat, 1), size(mat, 2)), intent(out) :: matinv
real(dp), dimension(size(mat, 1), size(mat, 2)) :: matdum
integer, dimension(size(mat, 1)) :: ipiv
integer :: nsize, msize, i, info
msize = size(mat, 1)
nsize = size(mat, 2)
matdum = mat
matinv = 0.0_dp
do i = 1, msize
matinv(i, i) = 1.0_dp
end do
info = 0
call dGETRF(msize, nsize, matdum, nsize, ipiv, info)
! IF (INFO /= 0) STOP 'Error with d_inv 1'
if (info /= 0) then
write(stdout, *) 'Warning from d_inv 1', info
call stop_all("d_inv", "Warning from d_inv 1")
end if
call dGETRS('n', msize, nsize, matdum, nsize, IPIV, matinv, msize, info)
if (info /= 0) call stop_all("d_inv", 'Error with d_inv 2')
END SUBROUTINE d_inv