numpy Python vs Matlab -为什么我的矩阵在Python中是奇异的

q3aa0525  于 2023-04-21  发布在  Python
关注(0)|答案(1)|浏览(158)

此问题已存在

Python vs Matlab - Why numpy is not accurate? [closed]
7天前关闭
我试图将一些算法从Matlab转换到Python 3.8。在算法中,我试图逆一些矩阵,结果是Matlab逆矩阵,因为它应该这样做,但Python(使用numpy.linalg)说它不能逆奇异矩阵,经过一番调试,我们发现在Matlab中矩阵的行列式是5.79913020654461e-35但在python中是0.非常感谢!
我使用Python 3.8与numpy版本1.20.0和Matlab 2017 a
这是数据:
我的矩阵:

[[3.0322662511118286, 3.645196880210743, 1.3326781661192055, -4.925254309001175],
[3.645196880210743, 4.382022947889959, 1.6020606012651588, -5.920826258432845],
[1.3326781661192055, 1.6020606012651588, 0.5857108009982133, -2.164644637608797],
[-4.925254309001175, -5.920826258432845, -2.164644637608797, 8.]]

我的Python脚本:

import numpy as np

np.set_printoptions(20)  # Matrix dtype is float 64, meaning that there will be up to 15 digits after decimal point
matrix = np.array([[3.0322662511118286, 3.645196880210743, 1.3326781661192055, -4.925254309001175],
                   [3.645196880210743, 4.382022947889959, 1.6020606012651588, -5.920826258432845],
                   [1.3326781661192055, 1.6020606012651588, 0.5857108009982133, -2.164644637608797],
                   [-4.925254309001175, -5.920826258432845, -2.164644637608797, 8.]])
print(f"Matrix is: {matrix}")
print(f"dtype is: {matrix.dtype}")
print(f"Matrix det is: {np.linalg.det(matrix)}")
try:
    print(f"Inverse matrix is: {np.linalg.inv(matrix)}")
except np.linalg.LinAlgError as e:
    print(f"Failed to inverse matrix. Error code: '{e}")

Python脚本的输出:

Matrix is: [[ 3.0322662511118286  3.645196880210743   1.3326781661192055
  -4.925254309001175 ]
 [ 3.645196880210743   4.382022947889959   1.6020606012651588
  -5.920826258432845 ]
 [ 1.3326781661192055  1.6020606012651588  0.5857108009982133
  -2.164644637608797 ]
 [-4.925254309001175  -5.920826258432845  -2.164644637608797
   8.                ]]
dtype is: float64
Matrix det is: 0.0
Failed to inverse matrix. Error code: 'Singular matrix

Matlab脚本+输出:

>> format longg
>> matrix = 
3.0322662511118286      3.645196880210743       1.3326781661192055      -4.925254309001175
3.645196880210743       4.382022947889959       1.6020606012651588      -5.920826258432845
1.3326781661192055      1.6020606012651588      .5857108009982133       -2.164644637608797
-4.925254309001175      -5.920826258432845      -2.164644637608797      8.
>> det (matrix)
ans = 5.79913020654461e-35
>> inv (matrix)
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.480881e-17.
ans =
463831092542248     557587200641207     203852870968935     753391506264877
557587200641207     670315847520521     245068621497740     905696113927055
203852870968935     245068621497740     89607103131709.8    331125436963685
753391506264877     905696113927055     331125436963685     1.2237353746994e+15

Alredy尝试使用其他数据类型,但没有成功

gxwragnw

gxwragnw1#

MATLAB和Python都告诉你逆运算是垃圾。那么你为什么要尝试计算它呢?你会在你的代码中使用这个垃圾结果来做什么?你需要重新考虑你在MATLAB和Python中做了什么,因为现在两者都没有意义。下面的例子表明,即使MATLAB给了你最好的答案,你应该注意MATLAB认为答案是垃圾的警告,因为它是。

相关问题