LAPACK在Fortran中产生错误结果。

huangapple go评论54阅读模式
英文:

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 = LAPACK在Fortran中产生错误结果。

FRes = LAPACK在Fortran中产生错误结果。

LAPACK在Fortran中产生错误结果。

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 = LAPACK在Fortran中产生错误结果。

FRes = LAPACK在Fortran中产生错误结果。

LAPACK在Fortran中产生错误结果。

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

huangapple
  • 本文由 发表于 2023年5月15日 03:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定