python稀疏gmres与输入参数混淆

时间:2014-12-14 00:30:41

标签: python numpy scipy linear-algebra sparse-matrix

我有一个简单的代码来解决使用scipy.sparse.linalg.gmres的稀疏线性系统

W, S  = load_data()
M = normalize(W.T.astype('float64'),'l1')
S = normalize(S.astype('float64'),'l1')
rhs = S[cat_id,:].T
print M.shape
print rhs.shape 
p = gmres(M, rhs)

function load_data从matlab的.mat文件加载两个稀疏矩阵并省略。 输出结果令人惊讶:

(150495, 150495)
(150495, 1)
Traceback (most recent call last):
  File "explain.py", line 54, in <module>
    pr(1)
  File "explain.py", line 42, in pr
    p = gmres(M, rhs)
  File "<string>", line 2, in gmres
  File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/iterative.py", line 85, in non_reentrant
    return func(*a, **kw)
  File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/iterative.py", line 418, in gmres
    A,M,x,b,postprocess = make_system(A,M,x0,b,xtype)
  File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/utils.py", line 78, in make_system
    raise ValueError('A and b have incompatible dimensions')
ValueError: A and b have incompatible dimensions

但我按照文档

运行gmres
  

A:{稀疏矩阵,密集矩阵,LinearOperator}

The real or complex N-by-N matrix of the linear system.
     

b:{array,matrix}

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

我根本不明白这段代码有什么问题,想要任何想法。

1 个答案:

答案 0 :(得分:2)

gmres的参数b不能是稀疏矩阵;它可以是一个numpy数组或矩阵。尝试

p = gmres(M, rhs.A)