2017-10-11 59 views
2

我有線性系統來解決這個問題,它由大型稀疏矩陣組成。使用scipy.sparse.linalg線性系統求解器的問題

我一直在使用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中的其他解算器,如cg,lsqrgmres都會引發相同的錯誤。

這是python 3.6.1與SciPy 0.19上運行。

任何人都知道這裏發生了什麼?

謝謝!

回答

2

您的使用情況與API不兼容!

​​3210上b

B:ndarray或稀疏矩陣

表示等式的右邊的矩陣或向量。如果一個向量b.shape必須是(n,)或(n,1)。

稀疏b爲允許

minresb

B:{陣列,矩陣}的線性系統的

右側。有形狀(N,)或(N,1)。

稀疏b不允許在這裏!

這同樣適用於提到的非工作求解器(其中lsqr可能有點不同 - > array_like與數組)。

這並不罕見,因爲稀疏的rhs-vectors在很多情況下並不能提供幫助,因此很多數值優化開發人員因此放棄了支持!

這工作:(!你有我給予好評和讚譽爲漂亮的重複的例子)

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