我尝试使用来自de Lapack库的ZGETRI和ZGETREF来创建复杂矩阵并计算其逆。 这是我尝试做的测试代码
module inversion_test
use, intrinsic :: iso_fortran_env, only: dp => real64
implicit none
real(dp), public, parameter :: wl = 1d0
real(dp), public, parameter :: wr = 1d0
integer, public, parameter :: matrix_size = 5
contains
function Green0inv(w) result(G0inv)
real(dp), intent(in) :: w
real(dp), dimension(matrix_size,matrix_size) :: Omega
complex(dp), dimension(matrix_size, matrix_size) :: Sigma
complex(dp),dimension(matrix_size,matrix_size) :: G0inv
integer :: i,j
complex(dp) :: S11, Snn
real(dp):: aux_wr1, aux_wl1
aux_wr1 = 1-w**2/(2d0*wr)
aux_wl1 = 1-w**2/(2d0*wl)
S11 = dcmplx(.5*(w**2-2d0*wl), 2.0*wL*dsqrt(1-aux_wL1**2))
Snn = dcmplx(.5*(w**2-2d0*wr), 2.0*wr*dsqrt(1-aux_wr1**2))
do i = 1,matrix_size
do j=i,matrix_size
Omega(i,j) = 0d0
Omega(j,i) = Omega(i,j)
Sigma(i,j) = dcmplx(0d0,0d0)
Sigma(j,i) = Sigma(i,j)
end do
end do
Sigma(1,1) = S11
Sigma(matrix_size,matrix_size) = Snn
do i = 1,matrix_size
Omega(i,i) = w**2
end do
G0inv = Omega - Sigma
end function Green0inv
function Green0(w) result(G0)
implicit none
external liblapack
real(dp), intent(in) :: w
complex(dp), dimension(matrix_size,matrix_size) :: G0, Gaux
! Down here all these variables are needed by ZGETRF and ZGETRI
DOUBLE PRECISION, ALLOCATABLE, DIMENSION(:) :: WORK
Integer:: LWORK = matrix_size*matrix_size
Integer, Allocatable, dimension(:) :: IPIV
Integer :: INFO, LDA = matrix_size, M = matrix_size, N = matrix_size
Integer DeAllocateStatus
allocate(work(Lwork))
allocate(IPIV(N))
Gaux = Green0inv(w)
CALL ZGETRF (M, N, Gaux, LDA, IPIV, INFO)
! This calculates LU descomposition of a matrix and overwrites it
CALL ZGETRI(N, Gaux, N, IPIV, WORK, LWORK, INFO)
! This calculates the inverse of a matrix knowing its LU descomposition and overwrites it
G0 = Gaux
DEALLOCATE (IPIV, STAT = DeAllocateStatus)
DEALLOCATE (WORK, STAT = DeAllocateStatus)
end function Green0
end module inversion_test
program test_inversion
use, intrinsic :: iso_fortran_env, only: dp => real64
use inversion_test
implicit none
complex(dp),dimension(matrix_size,matrix_size) :: G
real(dp) :: w
w = 1.0d0
G = Green0(w)
print*,G
end program test_inversion
但是当我将其复制如下时
gfortran -llapack test_inversion.f03 -o ti.out
我有这条消息
/tmp/ccxJ8QzV.o: In function `__inversion_test_MOD_green0':
test_inversion.f03:(.text+0x37a): undefined reference to `zgetrf_'
test_inversion.f03:(.text+0x3be): undefined reference to `zgetri_'
collect2: error: ld returned 1 exit status
我不知道为什么会这样。我在一个模块之外使用这个库,在主程序中,并没有任何反应,但如果我这样做,我就会收到这条消息。
我该如何解决这个问题?
编辑:提供的解决方案 Can't get past "undefined reference to `XXXX'" 在我的测试程序中正常工作。但是当它复制时,但是当我运行时,我有这个错误信息
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0x7F3AA5CD5E08
#1 0x7F3AA5CD4F90
#2 0x7F3AA54074AF
#3 0x7FFD573AEED8
Segmentation fault (core dumped)
所以我决定运行以下
gfortran fulltest1.f03 -g -fcheck=all -Wall
它提出了几个警告。所有人都说
(1)[ - WTA]
的不合格标签字符但它仍然出现
/tmp/ccUJiu8P.o: In function `__greenes_MOD_greenfrozen':
/home/danniel/fulltest1.f03:179: undefined reference to `zgetrf_'
/home/danniel/fulltest1.f03:179: undefined reference to `zgetri_'
/tmp/ccUJiu8P.o: In function `__greenes_MOD_green0':
/home/danniel/fulltest1.f03:143: undefined reference to `zgetrf_'
/home/danniel/fulltest1.f03:143: undefined reference to `zgetri_'
collect2: error: ld returned 1 exit status
此错误与我在测试程序中的错误相同,我在两个程序中使用相同的模块。