det Function

public function det(matrix)

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: matrix(:,:)

Return Value real(kind=dp)


Contents

Source Code

det

Source Code

    real(dp) function det(matrix)
        real(dp), intent(in) :: matrix(:,:)

        integer :: n, i, info
        integer, allocatable :: ipiv(:)
        real(dp) :: sgn
        real(dp), allocatable :: tmp_matrix(:,:)

        n = size(matrix,1)
        allocate(tmp_matrix(n,n), source = matrix)
        allocate(ipiv(n))

        ipiv = 0

        call dgetrf(n,n,tmp_matrix,n,ipiv,info)

        det = 1.0_dp

        do i = 1, N
            det = det * tmp_matrix(i,i)
        end do

        sgn = 1.0_dp
        do i = 1, n
            if (ipiv(i) /= i) then
                sgn = -sgn
            end if
        end do

        det = sgn * det

    end function det