[英]Preconditioned Conjugate Gradient and LinearOperator in python
[Homework] I am going to solve the linear system Ax=b by the Preconditioned Conjugate Gradient method, and I use spilu function from scipy.sparse.linalg for the preconditioner. 【作业】我打算用预处理共轭梯度法求解线性系统Ax=b,预处理器使用scipy.sparse.linalg中的spilu函数。 A is a sparse symmetric 162*162 matrix.
A 是稀疏对称的 162*162 矩阵。 Since the spilu gives an approximation to the inverse of A, say M approximates A, and so spilu(A) gives M^-1, which is the preconditioner.
由于 spilu 给出了 A 的倒数的近似值,假设 M 逼近 A,因此 spilu(A) 给出 M^-1,这是预处理器。 I find that we can directly gives the preconditioner in the python Conjugate Gradient function, but my code below does not work.
我发现我们可以直接在python Conjugate Gradient函数中给出预处理器,但是我下面的代码不起作用。
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)
<ipython-input-84-86f8f91df8d2> in <module>()
----> 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
/Users/ruobinghan/anaconda/lib/python3.4/site-packages/scipy/sparse/linalg/isolve/iterative.py in cg(A, b, x0, tol, maxiter, xtype, M, callback)
219 @non_reentrant
220 def cg(A, b, x0=None, tol=1e-5, maxiter=None, xtype=None, M=None, callback=None):
--> 221 A,M,x,b,postprocess = make_system(A,M,x0,b,xtype)
222
223 n = len(b)
/Users/ruobinghan/anaconda/lib/python3.4/site-packages/scipy/sparse/linalg/isolve/utils.py in make_system(A, M, x0, b, xtype)
108 x = zeros(N, dtype=xtype)
109 else:
--> 110 x = array(x0, dtype=xtype)
111 if not (x.shape == (N,1) or x.shape == (N,)):
112 raise ValueError('A and x have incompatible dimensions')
TypeError: float() argument must be a string or a number, not 'LinearOperator'
Also, the question hints I will need to use LinearOperator interface, I do not understand what is exactly LinearOperator doing and why we need it here.此外,问题提示我需要使用 LinearOperator 接口,我不明白 LinearOperator 到底在做什么以及我们为什么需要它。
Any suggestion would be appreciated!任何建议将不胜感激! Thanks in advance!
提前致谢!
I think the parameters are in wrong order,我认为参数顺序错误,
x3=scipy.sparse.linalg.cg(A,b,M2)
In the Error message:在错误消息中:
220 def cg(A, b, x0=None, tol=1e-5, maxiter=None, xtype=None, M=None,
callback=None):
--> 221 A,M,x,b,postprocess = make_system(A,M,x0,b,xtype)
M2 is in the place of x0 - the initial guess of the solution but not the preconditioner. M2 代替 x0 - 解决方案的初始猜测,但不是预处理器。 In my host, with correct order, class- LinearOperator is functioning well.
在我的主机中,以正确的顺序,类LinearOperator运行良好。
correct version正确的版本
x3=scipy.sparse.linalg.cg(A,b,M=M2)
Please use "key word" arguments as often as possible.请尽可能多地使用“关键字”参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.