function zdet(A) result(x)
! compute the determinant of a complex matrix using an LU factorization
complex(8), intent(in) :: A(:, :)
complex(8) :: x
integer :: i
integer :: info, n
integer, allocatable :: ipiv(:)
complex(8), allocatable :: At(:,:)
n = size(A(1,:))
call assert_shape(A, [n, n], "det", "A")
allocate(At(n,n), ipiv(n))
At = A
call zgetrf(n, n, At, n, ipiv, info)
if(info /= 0) then
print *, "zgetrf returned info =", info
if (info < 0) then
print *, "the", -info, "-th argument had an illegal value"
else
print *, "U(", info, ",", info, ") is exactly zero; The factorization"
print *, "has been completed, but the factor U is exactly"
print *, "singular, and division by zero will occur if it is used"
print *, "to solve a system of equations."
end if
stop 'zdet error: zgetrf '
end if
! for details on the computation, compare the comment in ddet().
x = one
do i = 1,n
if(ipiv(i) /= i) then ! additional sign change
x = -x*At(i,i)
else
x = x*At(i,i)
endif
end do
end function zdet