[Fortran] 纯文本查看 复制代码
program aaa
implicit none
integer, parameter :: n = 3
integer :: info,i,sig
integer :: ipv(n)
complex(16) :: a(n,n)
complex(8) :: det1 ,det2
! 矩阵赋值
a(1,1)=(1.0,1.0)
a(1,2)=(2.0,2.0)
a(1,3)=(0.0,3.0)
a(2,1)=(3.0,4.0)
a(2,2)=(5.0,5.0)
a(2,3)=(5.0,6.0)
a(3,1)=(7.0,7.0)
a(3,2)=(8.0,8.0)
a(3,3)=(9.0,9.0)
!解析解
det1=a(1,1)*(a(2,2)*a(3,3)-a(2,3)*a(3,2))-a(1,2)*(a(2,1)*a(3,3)-a(2,3)*a(3,1))+a(1,3)*(a(2,1)*a(3,2)-a(2,2)*a(3,1))
! 符号及行列式初始值
sig=1
det2=(1,0)
! 使用 LAPACK 函数求解行列式
call zgetrf(n, n, a, n, ipiv, info)
do i = 1, 3
if (ipiv(i) /= i) then
sig=-1*sig
end if
det2=det2*a(i,i)
end do
write(*,*) "det1=", det1
write(*,*) "det2=", det2*sig
end program aaa