python - 在不反转矩阵的情况下求解 Ax=By
问题描述
我需要以 Ax=By 的形式求解 x 的方程。我知道我不应该通过反转 B 来解决它,但我无法用scipy.gmres
or解决 B^-1Ax=ylinalg.solve
因为当我尝试用 反转 B 时它失败了linalg.inv
。它返回错误消息“ Singular matrix
”。
还有其他方法可以反转矩阵吗?效率并不重要,因为我只需要做一次。我不想像 T=Ax 和 x 一样先求解方程两次。
解决方案
我会加入用户hilberts-drinking-problem的建议,而不是反转B
,而是遵循两步方法:首先相乘B
并y
产生一个向量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 )
推荐阅读
- google-apps-script - Gmail 插件 - 我如何找到当前的草稿 ID
- java - 有限状态机是这个用例的正确选择吗?
- netbeans - 由于来自服务器的文件意外结束,无法连接到 NBAndroid
- r - 错误:ModuleNotFoundError:在 R 中使用 keras 时没有名为“keras”的模块
- seal - 使用 SEAL 将分数转换为二进制
- javascript - 获取本地主机名并使用图像 Javascript 创建链接
- node.js - 从节点服务器发回数据以响应前端
- c - C: hexdump 的格式化字符串(char* 到另一个 char*)
- windows - 如何在 Cygwin 中使用 Python 有效地将 POSIX 路径转换为 Windows 路径?
- dart - 主要获取当前上下文