使用 SGETR(F,I) 的 Fortran 逆矩阵计算仅适用于单精度

Fortran inverse matrix computation using SGETR(F,I) only works in single precision

我正在尝试计算方阵的逆,但它不起作用。我检查了以前的帖子,但逻辑是一样的,但我还没有找到问题出在哪里。我还分享了 Matlab 结果,例如矩阵。

program test

Implicit none

real,allocatable,dimension(:,:)         :: A       
real,allocatable,dimension(:)           :: WORK
integer ,allocatable,dimension(:)       :: ipiv
integer                                 :: n,info,M
external     SGETRF,SGETRI
M=8
allocate(A(M,M),WORK(M),IPIV(M))

A(1,:)=(/3.74E-4, 0.0, 0.0, 4.98E-5, 0.0, 0.0, 0.0, 0.0/)
A(2,:)=(/0.0 , 3.74E-4, 0.0, 0.0, 4.98E-5 ,0.0 ,0.0 ,0.0 /)
A(3,:)=(/0.0 , 0.0 ,3.74E-4, 0.0 ,0.0, 4.98E-5, 0.0 ,0.0/)
A(4,:)=(/4.98E-5 ,0.0 ,0.0 ,6.64e-6, 0.0 ,0.0, 0.0, 0.0 /)
A(5,:)=(/0.0 , 4.98E-5, 0.0, 0.0 ,6.64E-6 ,0.0 ,0.0 ,0.0 /)
A(6,:)=(/0.0, 0.0, 4.98E-5, 0.0 ,0.0, 6.64E-6, 0.0 ,0.0 /)
A(7,:)=(/0.0, 0.0 ,0.0, 0.0 ,0.0 ,0.0 ,1.49E-11, 0.0 /)
A(8,:)=(/0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0, 0.0 ,1.49E-11 /)


call SGETRF(M,M,A,M,IPIV,info)
if(info .eq. 0) then
   Print *,'succeded'
else
   Print *,'failed'
end if

call SGETRI(M,A,M,IPIV,WORK,M,info)
if(info .eq. 0) then
  Print *,'succeded'
else
  Print *,'failed'
end if
Print *,A

deallocate(A,IPIV,WORK)

end 

!!!!! Matlab Result
!1.0e+10 *
! 0.0002     0       0   -0.0015       0      0        0   0
!     0      0.0002  0       0       -0.0015  0        0   0
!     0      0    0.0002     0         0     -0.0015   0   0
! -0.0015    0       0     0.0113      0      0        0   0
!     0     -0.0015  0       0       0.0113   0        0   0
!     0      0   -0.0015     0         0    0.0113     0   0
!     0      0       0       0         0      0     6.7114 0
!     0      0       0       0         0      0        0   6.7114

你的reals只是单精度的。 lapack D 前缀表示双精度。两个修复:

  1. 将您的 DG 更改为 SG
  2. 保留您的 DG 并使用双精度