我在使用 numpy 1.3.0 和 MATLAB 7.9.0 时遇到以下问题:python 代码
import numpy as np
Lu = [[1.01250000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[-0.00250000000000000,1.01250000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,-0.00250000000000000,1.01250000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,-0.00250000000000000,1.01250000000000,0,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0,0,0],[-0.00250000000000000,0,0,0,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0,0],[0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0,0],[0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0,0],[0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,0,0,0,-0.00250000000000000,0,0,0,0,0,0,0,0],[0,0,0,0,-0.00250000000000000,0,0,0,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0,0],[0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0,0],[0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0,0,0],[0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,0,0,0,-0.00250000000000000,0,0,0,0],[0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,0,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0,0],[0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0,0],[0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,-0.00250000000000000,0,0,-0.00250000000000000,0],[0,0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01000000000000,0,0,0,-0.00250000000000000],[0,0,0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,0,1.01250000000000,-0.00250000000000000,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01250000000000,-0.00250000000000000,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01250000000000,-0.00250000000000000],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-0.00250000000000000,0,0,-0.00250000000000000,1.01250000000000]]
rhs = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0.0050, 0.0050, 0.0050, 0.0050]
Lu = np.array(Lu)
rhs = np.array(rhs)
ans = np.linalg.solve(Lu,rhs)
print ans
产生输出
[ 1.87241716e-13 1.89545264e-13 1.89545264e-13 1.87241716e-13
7.56433496e-11 7.63890449e-11 7.63890449e-11 7.56433496e-11
3.04833369e-08 3.07089522e-08 3.07089522e-08 3.04833369e-08
1.22844835e-05 1.23451480e-05 1.23451480e-05 1.22844835e-05
4.95055571e-03 4.96277946e-03 4.96277946e-03 4.95055571e-03]
而在 MATLAB 中使用反斜杠会产生输出
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0050 0.0050 0.0050 0.0050
我没有找到任何其他线性代数方程系统,其中 numpy 和 matlab 产生不同的解决方案。我目前不在城里,因此无法检查不同版本的 numpy(在不同的计算机上)是否会给出正确的结果。np.linalg.solve 不是解决这个系统的正确函数吗(系统矩阵 Lu 是稀疏的)?这是我的 numpy 版本中的错误吗?我的代码有问题吗?
谢谢!