Reputation: 79
I am trying to calculate the inverse of the square matrix but It is not working. I checked previous posts but the logic is same but I haven't still found where is the problem. I also share Matlab result for example matrix.
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
Upvotes: 0
Views: 735
Reputation: 2148
Your reals
are only single precision. The lapack D
prefix implies double precision. Two fixes:
DG
s to SG
s DG
s and use double precisionUpvotes: 4