scipy分解误差

时间:2012-03-29 05:42:33

标签: python scipy

我正在使用scipy.linalg来求解矩阵方程A * x = b 以下代码不起作用:

from scipy import *
from pylab import *
from scipy.sparse  import lil_matrix 
from scipy.sparse.linalg import spsolve, factorized 
from numpy.random import rand 
from numpy import ones 

def build_matrix(n): 
        A_lil = lil_matrix((n, n)) 
        A_lil.setdiag(rand(n)) 
        A_lil.setdiag(rand(n-10), k = 10) 
        A_lil.setdiag(rand(n-10), k = -10) 
        A_csr = A_lil.tocsr() 
        return A_csr 

A = build_matrix(500)
fsolve = factorized(A)
b = rand(500,1)

fsolve(b)

Traceback (most recent call last):
  File "<pyshell#70>", line 1, in <module>
    fsolve(b)
SystemError: gstrs was called with invalid arguments

(编辑:实际上,控制台没有说fsolve(b,trans),它表示self.solve(b,trans),所以下面的部分可能是错误的)

如果我手动输入fsolve到控制台,我看到fsolve需要两个参数'b'和'trans'。出于某种原因,这里没有提到因素分解的文档:http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.factorized.html。添加参数'N'会出现以下错误

>> fsolve(b, 'N')
Traceback (most recent call last):
  File "<pyshell#55>", line 1, in <module>
    fsolve(b,'N')
SystemError: gstrs was called with invalid arguments

<击> 有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

您似乎遇到错误:

b=rand(500,1)

需要:

b=rand(500)

否则数组会“太深”。希望有所帮助。