我正在尝试使用LAPACK库来解决一个简单的三对角方程组。下面的代码解释了这一切。
我得到的数组中充满了零(初始化的),而不是正确的答案。
我检查了输入,试图用两个编译器编译,一切似乎都很好。有什么问题?
编译行是:
ifort -L/usr/local/lib/ -llapack -lblas tLapack.f90 -o tlapack
gfortran -L/usr/local/lib/ -llapack -lblas tLapack.f90 -o tlapack
代码是:
program lapackT
implicit none
! dgtsv( integer(4) :: N,
! integer(4) :: NRHS,
! real(8) :: DL[],
! real(8) :: D [],
! real(8) :: DU[],
! real(8) :: B [],
! integer(4) :: LDB ,
! integer(4) :: info )
! [A][x] = [b]
! N - The order of matrix [A]
! NRHS - Number of coluns in [b]
! DL - Array with the subdiag.
! D - Main diagonal.
! DU - Upper Diagonal.
! B - Answer !!
! LDB - length of array [B].
! INFO - If = 0 .. Uhul !!.
real(8), dimension(3) :: mainDiag
real(8), dimension(2) :: lowerDiag
real(8), dimension(2) :: upperDiag
real(8), dimension(3) :: unknow
real(8), dimension(3) :: equalty
integer(4) :: info = 0
integer(4) :: i = 0
integer(4) :: N = 3
integer(4) :: NRHS = 1
integer(4) :: LDB = 3
mainDiag(1) = 2.0d0
mainDiag(2) = 2.0d0
mainDiag(3) = 2.0d0
upperDiag(1) = 3.0d0
upperDiag(2) = 3.0d0
lowerDiag(1) = 1.0d0
lowerDiag(2) = 1.0d0
equalty(1) = 1.0d0
equalty(2) = 1.0d0
equalty(3) = 1.0d0
unknow = 0.0d0 ! answer
call dgtsv(N,NRHS,lowerDiag,mainDiag,upperDiag,equalty,LDB,info)
write(*,*) info
do i = 1,size(unknow)
write(*,*) unknow(i)
end do
! Correct answer: unknow = (/-1,1,0/) ! real(8) values
! Answer Im getting: unknow = (/0,0,0/) ! real(8) values
end program lapackT
答案 0 :(得分:3)
如果您查看文档,您会看到答案是在您的等号参数中返回的(即它会覆盖RHS) - 因为未知未通过该怎么能被呼叫影响?我同意,我认为这是有史以来最伟大的设计......
虽然我在这里请了解种类,你正在做的一些事情是在四分之一世纪前。请查看Fortran 90 kind parameter。无论如何,在这里我将如何编写你的程序(有些人会说它现在也略显过时),以及它给出的答案:
ian-admin@agon ~/test/stack $ cat dt.f90
program lapackT
implicit none
Integer, Parameter :: wp = Selected_real_kind( 13, 70 )
real(wp), dimension(3) :: mainDiag
real(wp), dimension(2) :: lowerDiag
real(wp), dimension(2) :: upperDiag
real(wp), dimension(3) :: unknow
real(wp), dimension(3) :: equalty
integer :: info = 0
integer :: i = 0
integer :: N = 3
integer :: NRHS = 1
integer :: LDB = 3
mainDiag(1) = 2.0_wp
mainDiag(2) = 2.0_wp
mainDiag(3) = 2.0_wp
upperDiag(1) = 3.0_wp
upperDiag(2) = 3.0_wp
lowerDiag(1) = 1.0_wp
lowerDiag(2) = 1.0_wp
equalty(1) = 1.0_wp
equalty(2) = 1.0_wp
equalty(3) = 1.0_wp
unknow = 0.0_wp ! answer
call dgtsv(N,NRHS,lowerDiag,mainDiag,upperDiag,equalty,LDB,info)
write(*,*) info
do i = 1,size(unknow)
write(*,*) equalty(i)
end do
! Correct answer: unknow = (/-1,1,0/) ! real(8) values
! Answer Im getting: unknow = (/0,0,0/) ! real(8) values
end program lapackT
ian-admin@agon ~/test/stack $ gfortran -Wall -Wextra -fcheck=all -O -std=f95 dt.f90 -llapack
ian-admin@agon ~/test/stack $ ./a.out
0
-1.0000000000000000
1.0000000000000000
0.0000000000000000E+000
ian-admin@agon ~/test/stack $
答案 1 :(得分:2)
除非dgtsv
通过副作用进行操作,否则这段语句(您的代码,没有空行):
unknow = 0.0d0 ! answer
call dgtsv(N,NRHS,lowerDiag,mainDiag,upperDiag,equalty,LDB,info)
write(*,*) info
do i = 1,size(unknow)
write(*,*) unknow(i)
end do
不会更新unknow
。怎么可能不是全部0.0
?
通过equalty
致电dgtsv
时,结果是否未通过UIActivityViewController
返回?