首页 > 解决方案 > 使用 SGETR(F,I) 的 Fortran 逆矩阵计算仅适用于单精度

问题描述

我正在尝试计算方阵的逆,但它不起作用。我检查了以前的帖子,但逻辑相同,但我仍然没有发现问题出在哪里。我还分享了 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

标签: fortranfortran90lapackmatrix-inversefortran95

解决方案


reals只是单精度。lapackD前缀意味着双精度。两个修复:

  1. 将您的 s 更改DGSGs
  2. 保留你DG的 s 并使用双精度

推荐阅读