使用scipy.sparse.linalg线性系统求解器的问题 [英] Issues using the scipy.sparse.linalg linear system solvers

查看:131
本文介绍了使用scipy.sparse.linalg线性系统求解器的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got linear system to solve which consists of large, sparse matrices.

我一直在使用 scipy.sparse 库及其 linalg 子库来执行此操作,但是我无法获得一些线性求解器工作.

I've been using the scipy.sparse library, and its linalg sub-library to do this, but I can't get some of the linear solvers to work.

这是一个有效的示例,可以为我重现此问题:

Here is a working example which reproduces the issue for me:

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)

运行此命令会产生以下错误

Running this produces the following error

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

调用 miners ,即使尺寸显然 兼容. scipy.sparse.linalg 中的其他求解器,例如 cg lsqr gmres 都抛出相同的错误.

for the call to minres, even though the dimensions clearly are compatible. Other solvers in scipy.sparse.linalg, such as cg, lsqr and gmres all throw an identical error.

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

This is being run on python 3.6.1 with SciPy 0.19.

任何人都知道这是怎么回事吗?

Anyone have any idea what's going on here?

谢谢!

推荐答案

您的用法与API不兼容!

Your usage is incompatible with the API!

b 上溶解:

b:ndarray或稀疏矩阵

b : ndarray or sparse matrix

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

The matrix or vector representing the right hand side of the equation. If a vector, b.shape must be (n,) or (n, 1).

允许稀疏b

分钟:

minres on b:

b:{array,matrix}

b : {array, matrix}

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

Right hand side of the linear system. Has shape (N,) or (N,1).

此处不允许稀疏b!

上述提到的无效求解器也是如此(其中lsqr可能有点不同-> array_like vs. array).

The same applies to the mentioned non-working solvers (where lsqr might be a bit different -> array_like vs. array).

这并不是罕见的情况,因为稀疏的rhs-vector在许多情况下都无济于事,因此许多数值优化开发人员都放弃了支持!

This is not that uncommon as sparse rhs-vectors are not helping in many cases and a lot of numerical-optimization devs therefore drop support!

这有效:

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

(您对这个可复制的好例子赞不绝口!)

(you got my upvote and praise for the nice reproducible example!)

这篇关于使用scipy.sparse.linalg线性系统求解器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆