CBo*_*man 5 python linear-algebra scipy sparse-matrix
我有一个线性系统要解决,它由大型稀疏矩阵组成。
我一直在使用该scipy.sparse库及其linalg子库来执行此操作,但我无法让某些线性求解器工作。
这是一个工作示例,它为我重现了该问题:
from numpy.random import random
from scipy.sparse import csc_matrix
from scipy.sparse.linalg import spsolve, minres
N = 10
A = csc_matrix( random(size = (N,N)) )
A = (A.T).dot(A) # force the matrix to be symmetric, as required by minres
x = csc_matrix( random(size = (N,1)) ) # create a solution vector
b = A.dot(x) # create the RHS vector
# verify shapes and types are correct
print('A', A.shape, type(A))
print('x', x.shape, type(x))
print('b', b.shape, type(b))
# spsolve function works fine
sol1 = spsolve(A, b)
# other solvers throw a incompatible dimensions ValueError
sol2 = minres(A, b)
Run Code Online (Sandbox Code Playgroud)
运行它会产生以下错误
raise ValueError('A and b have incompatible dimensions')
ValueError: A and b have incompatible dimensions
Run Code Online (Sandbox Code Playgroud)
对于调用minres,即使尺寸显然是兼容的。中的其他求解器scipy.sparse.linalg,例如cg,lsqr和gmres都抛出相同的错误。
这是在 python 3.6.1 和 SciPy 0.19 上运行的。
有人知道这里发生了什么吗?
谢谢!
您的用法与 API 不兼容!
SPSOLVE上b:
b : ndarray 或稀疏矩阵
表示等式右侧的矩阵或向量。如果是向量,b.shape 必须是 (n,) 或 (n, 1)。
允许使用稀疏 b
minres上b:
b : {数组,矩阵}
线性系统的右侧。具有形状 (N,) 或 (N,1)。
这里不允许使用稀疏 b!
这同样适用于提到的非工作求解器(其中 lsqr 可能有点不同 - > array_like vs. array)。
这并不少见,因为稀疏的 rhs 向量在许多情况下没有帮助,因此许多数值优化开发人员放弃支持!
这有效:
sol2 = minres(A, b.todense())
Run Code Online (Sandbox Code Playgroud)
(你得到了我的赞成和赞扬,因为这个很好的可复制的例子!)
| 归档时间: |
|
| 查看次数: |
850 次 |
| 最近记录: |