[作业]我要用预处理的共轭梯度法求解线性方程组Ax=b,我使用scipy.sparse.linalg先决条件。A是稀疏对称162×162矩阵。因为spilu给出了A的逆的近似值,比如M近似A,所以spilu(A)给出了M^-1,这是预处理条件。我发现我们可以在python共轭梯度函数中直接给出预处理条件,但是我下面的代码不起作用。在M_inverse=scipy.sparse.linalg.spilu(A)
M2=scipy.sparse.linalg.LinearOperator((162,162),M_inverse.solve)
x3=scipy.sparse.linalg.cg(A,b,M2)
TypeError Traceback (most recent call last)
in ()
----> 1 x3=scipy.sparse.linalg.cg(A,b,M2)
/Users/ruobinghan/anaconda/lib/python3.4/site-packages/scipy/sparse/linalg/isolve/iterative.py in cg(A, b, x0, tol, maxiter, xtype, M, callback)
/Users/ruobinghan/anaconda/lib/python3.4/site-packages/scipy/sparse/linalg/isolve/iterative.py in non_reentrant(func, *a, **kw)
83 try:
84 d['__entered'] = True
---> 85 return func(*a, **kw)
86 finally:
87 d['__entered'] = False<