【问题标题】:Issues using the scipy.sparse.linalg linear system solvers使用 scipy.sparse.linalg 线性系统求解器的问题
【发布时间】:2018-03-23 05:53:27
【问题描述】:

我要解决由大型稀疏矩阵组成的线性系统。

我一直在使用 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)

运行它会产生以下错误

    raise ValueError('A and b have incompatible dimensions')
ValueError: A and b have incompatible dimensions

对于minres 的调用,尽管尺寸明显 兼容的。 scipy.sparse.linalg 中的其他求解器,例如 cglsqrgmres 都抛出相同的错误。

这是在带有 SciPy 0.19 的 python 3.6.1 上运行的。

有人知道这里发生了什么吗?

谢谢!

【问题讨论】:

    标签: python scipy sparse-matrix linear-algebra


    【解决方案1】:

    您的使用与 API 不兼容!

    spsolveb:

    b : ndarray 或稀疏矩阵

    表示等式右侧的矩阵或向量。如果是向量,b.shape 必须是 (n,) 或 (n, 1)。

    允许稀疏 b

    minresb:

    b : {数组,矩阵}

    线性系统的右侧。形状为 (N,) 或 (N,1)。

    此处不允许使用稀疏 b!

    这同样适用于提到的非工作求解器(其中 lsqr 可能有点不同 -> array_like 与数组)。

    这并不罕见,因为稀疏 rhs 向量在许多情况下没有帮助,因此许多数值优化开发人员放弃了支持!

    这行得通:

    sol2 = minres(A, b.todense())
    

    (你得到了我的赞成和赞扬,因为这个很好的可重复的例子!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-03
      • 2017-12-13
      • 2021-05-18
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      相关资源
      最近更新 更多