英文:
Wrong Results in from LAPACK in Fortran
问题
I installed LAPACK in Msys2 by pasting the command from this link: https://packages.msys2.org/package/mingw-w64-x86_64-lapack. I then linked the library with -llapack -lblas
. Everything seems to work fine, but every time I use the subroutines from LAPACK, I get incorrect results.
For example, the subroutine DGESV. I tried many different input values, but all resulted in incorrect answers. Does anyone know why I'm getting these results?
I've tried different values (different matrices) and I've searched for answers online, without success.
relevant Code:
call DGESV(N, 1, K, N, U_step, FRes, N, info)
if (info /= 0) then
write(*,*) "Error while Solving Matrix"
stop
end if
- K is a quadratic Matrix (N x N)
- U_step is the result vector
- FRes is the right side Vector
Basically in the Form Ax = B --> KU_step = FRes
Example of wrong results:
K =
FRes =
I just didn't trust Matlab, I crosschecked the results in Wolfram Alpha.
英文:
I installed LAPACK in Msys2 by pasting the command from this link: https://packages.msys2.org/package/mingw-w64-x86_64-lapack. I then linked the library with -llapack -lblas
. Everything seems to work fine, but every time I use the subroutines from LAPACK, I get incorrect results.
For example, the subroutine DGESV. I tried many different input values, but all resulted in incorrect answers. Does anyone know why I'm getting these results?
I've tried different values (different matrices) and I've searched for answers online, without succes.
relevant Code:
call DGESV(N, 1, K, N, U_step, FRes, N, info)
if (info /= 0) then
write(*,*) "Error while Solving Matrix"
stop
end if
- K is a quadratic Matrix (N x N)
- U_step is the result vector
- FRes is the right side Vector
Basically in the Form Ax = B --> KU_step = FRes
Example of wrong results:
K =
FRes =
I just didn't trusted Matlab, I crosschecked the results in Wolfram Alpha.
答案1
得分: 1
你期望在DGESV
的第5个参数中获得结果,但实际上DGESV
将结果输出到第6个参数中,从而覆盖了输入的右侧向量。
第5个参数是一个工作整数数组,DGESV
用它来存储LU分解的主元索引。
所以你的代码应该像这样:
integer :: ipiv(N)
...
U_step(:) = FRes(:)
call DGESV(N, 1, K, N, ipiv, U_step, N, info)
DGESV
文档:https://netlib.org/lapack/explore-html/d7/d3b/group__double_g_esolve_ga5ee879032a8365897c3ba91e3dc8d512.html
英文:
You are visibly expecting the result in the 5th argument of DGESV
, but DEGSV actually outputs the result in the 6th argument, hence overwriting the input right hand side vector(s).
The 5th argument is a work integer array where DEGSV
stores the pivot indices of the LU decomposition.
So your code should be something like:
integer :: ipiv(N)
...
U_step(:) = FRes(:)
call DGESV(N, 1, K, N, ipiv, U_step, N, info)
DGESV
documentation: https://netlib.org/lapack/explore-html/d7/d3b/group__double_g_esolve_ga5ee879032a8365897c3ba91e3dc8d512.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论