如果十进制更长,则Fortran-逆矩阵结果不相同 [英] Fortran- Inverse Matrix result not same if Decimal is longer

查看:122
本文介绍了如果十进制更长,则Fortran-逆矩阵结果不相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的真实数据是第一个输入的,但是结果的倒数却是如此之大.当您与第一和第二输入进行比较时,它们是相同的数据.只有十进制大小差异.为什么会有不同的结果?因为它们是相同的数据.他们怎么会有不同的结果?您可以看到结果和输入.太奇怪了.

My real data is first input but inverse of result is so big. They are same data when you compare with first and second input. There is only difference decimal size. Why is there different result? Because they are same data. How can they have different result? You can see result and input. It is so strange.

program test

Implicit none

double precision,allocatable,dimension(:,:)         :: A       
double precision,allocatable,dimension(:)           :: WORK
integer ,allocatable,dimension(:)       :: ipiv
integer                                 :: n,info,M
 external     DGETRF,DGETRI
M=8
allocate(A(M,M),WORK(M),IPIV(M))
!!! First Input !!!!
A(1,:)=(/3.740486048842566D-4, 0.0D0, 0.0D0, 4.987315029057229D-5, 0.0D0, 0.0D0, 0.0D0, 0.0D0/)
A(2,:)=(/0.0D0 , 3.740486048842566D-4, 0.0D0, 0.0D0, 4.987315029057229D-5 ,0.0D0 ,0.0D0 ,0.0D0 /)
A(3,:)=(/0.0D0 , 0.0D0 ,3.740486048842566D-4, 0.0D0 ,0.0D0, 4.987315029057229D-5, 0.0D0 ,0.0D0/)
A(4,:)=(/4.987315029057229D-5 ,0.0D0 ,0.0D0 ,6.649753768432517D-6, 0.0D0 ,0.0D0, 0.0D0, 0.0D0 /)
A(5,:)=(/0.0D0 , 4.987315029057229D-5, 0.0D0, 0.0D0 ,6.649753768432517D-6 ,0.0D0 ,0.0D0 ,0.0D0 /)
A(6,:)=(/0.0D0, 0.0D0, 4.987315029057229D-5, 0.0D0 ,0.0D0, 6.649753768432517D-6, 0.0D0 ,0.0D0 /)
A(7,:)=(/0.0D0, 0.0D0 ,0.0D0, 0.0D0 ,0.0D0 ,0.0D0 ,1.499999910593033D-11, 0.0D0 /)
A(8,:)=(/0.0D0 ,0.0D0 ,0.0D0 ,0.0D0 ,0.0D0 ,0.0D0, 0.0D0 ,1.499999910593033D-11 /)
 !!!! Second Input !!!! 
!A(1,:)=(/3.74D-4, 0.0D0, 0.0D0, 4.98D-5, 0.0D0, 0.0D0, 0.0D0, 0.0D0/)
!A(2,:)=(/0.0D0 , 3.74D-4, 0.0D0, 0.0D0, 4.98D-5 ,0.0D0 ,0.0D0 ,0.0D0 /)
!A(3,:)=(/0.0D0 , 0.0D0 ,3.74D-4, 0.0D0 ,0.0D0, 4.98D-5, 0.0D0 ,0.0D0/)
!A(4,:)=(/4.98D-5 ,0.0D0 ,0.0D0 ,6.64D-6, 0.0D0 ,0.0D0, 0.0D0, 0.0D0 /)
!A(5,:)=(/0.0D0 , 4.98D-5, 0.0D0, 0.0D0 ,6.64D-6 ,0.0D0 ,0.0D0 ,0.0D0 /)
!A(6,:)=(/0.0D0, 0.0D0, 4.98D-5, 0.0D0 ,0.0D0, 6.64D-6, 0.0D0 ,0.0D0 /)
!A(7,:)=(/0.0D0, 0.0D0 ,0.0D0, 0.0D0 ,0.0D0 ,0.0D0 ,1.49D-11, 0.0D0 /)
!A(8,:)=(/0.0D0 ,0.0D0 ,0.0D0 ,0.0D0 ,0.0D0 ,0.0D0, 0.0D0 ,1.49D-11 /)


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

call DGETRI(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 
!!!!! Second Input 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

!!! First Input Result
!   1.0e+21 *

!-0.0238         0         0    0.1783         0         0         0         0
!     0   -0.0238         0         0    0.1783         0         0         0
!     0         0    0.0000         0         0   -0.0000         0         0
! 0.1783         0         0   -1.3375         0         0         0         0
!     0    0.1783         0         0   -1.3375         0         0         0
!     0         0   -0.0000         0         0    0.0000         0         0
!     0         0         0         0         0         0    0.0000         0
!     0         0         0         0         0         0         0    0.0000

推荐答案

创建矩阵逆并不是一个难题. 我将您先前的示例转换为使用一种简单的方法,该方法基于带阴影身份矩阵的高斯消除,在大多数情况下效果很好.附带的程序可以反转您之前的对称矩阵,而无需旋转行.它不需要黑匣子".

Creating a matrix inverse is not a difficult problem. I converted your earlier example to using a simple approach, based on Gaussian elimination with a shadowed identity matrix, which works well for most cases. The attached program inverts your earlier symmetric matrix, without resorting to pivoting of the rows. It does not need a "black-box".

以不同的系数得到不同的结果不足为奇.对于输入值的很小变化,结果的显着变化表明了您正在使用的方程式关系的敏感性和可能的​​不良条件.

That you get different results with different coefficients is hardly surprising. With the significant change in results for apparently small changes of input values, shows the sensitivity and possibly poor conditioning of the equation relationship you are using.

https://www.dropbox.com/s/ssotjx45yrz5sf9 /dgetri.f90?dl=0

第一个输入"的附加响应

Additional response re "First Input"

https://www.dropbox.com/s/hximfoin977rmov /dgetri_piv4.f90?dl=0

此最新链接(16-6)包含两个数据集.在首次输入"中,您的方程式基本上是4:6行,1:3/7.5行和small_noise.

This latest link (16-6) has both data sets included. In "First Input", your equations basically are rows 4:6 are rows 1:3 / 7.5 + small_noise.

这个最新的代码示例在矩阵求逆过程中和之后都进行了准确性检查.测试期间检查行更改正确,而之后检查为"A.A ^ -1-I"和"A-(A ^ -1)^-1",这更好地表明了准确性差.

This latest code example has accuracy checks both during the matrix inversion and also after. The during test checks the row changes are correct, while the after checks are "A.A^-1 - I" and "A - (A^-1)^-1", which better indicate poor accuracy.

有趣的是,第二个输入"(带有更多噪音)报告了相当准确的结果.未能获得8字节实数的逆数,需要一个相当人为的矩阵!同样,随机数衍生系数示例也显示出良好的准确性.

It is interesting that "Second Input" (with more noise) reports a reasonably accurate outcome. Failing to get an inverse with 8-byte reals needs a fairly contrived matrix ! Similarly, the random number derived coefficients examples shows good accuracy.

这些示例表明,我提出的精度测试并不总是能够识别定义不明确的方程式关系.通过检查逆数以识别值的大变化也很有用.

These examples show that the accuracy tests I have presented don't always identify poorly defined equation relationships. Your inspection of the inverse to identify large variation in values is also useful.

鉴于方程式的定义方式,我不确定您想要的结果是什么.

Given the way the equations appear to have been defined, I am not sure what is the outcome you are wanting.

这篇关于如果十进制更长,则Fortran-逆矩阵结果不相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆