首页 > 解决方案 > 在不反转矩阵的情况下求解 Ax=By

问题描述

我需要以 Ax=By 的形式求解 x 的方程。我知道我不应该通过反转 B 来解决它,但我无法用scipy.gmresor解决 B^-1Ax=ylinalg.solve因为当我尝试用 反转 B 时它失败了linalg.inv。它返回错误消息“ Singular matrix”。

还有其他方法可以反转矩阵吗?效率并不重要,因为我只需要做一次。我不想像 T=Ax 和 x 一样先求解方程两次。

标签: pythonnumpymatrixscipylinear-algebra

解决方案


我会加入用户hilberts-drinking-problem的建议,而不是反转B,而是遵循两步方法:首先相乘By产生一个向量By,然后求解 system A·x=By。下面说明了使用小数组作为测试数据的这种方法:

from numpy import array;
from scipy.sparse import coo_matrix
from scipy.sparse.linalg import gmres

A = coo_matrix((3, 3), dtype=float)
A.setdiag( [ 2, 4, -1 ] )
A.setdiag( [ 2, -0.5 ], 1 )
print( "A", A )

B = coo_matrix((3, 2), dtype=float)
B.setdiag( [ 1, -2 ] )
B.setdiag( [ -0.5, 4 ], -1 )
print( "B", B )

y = array( [ 13, 5 ] )
print( "y", y )

By = B * y
print( "By", By )

x = gmres( A, By )
print( "x", x )

推荐阅读