scipy - Preconditioned Conjugate Gradient and LinearOperator in python -
[homework] going solve linear system ax=b preconditioned conjugate gradient method, , use spilu function scipy.sparse.linalg preconditioner. sparse symmetric 162*162 matrix. since spilu gives approximation inverse of a, m approximates a, , spilu(a) gives m^-1, preconditioner. find can directly gives preconditioner in python conjugate gradient function, code below not work.
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 , x have incompatible dimensions') typeerror: float() argument must string or number, not 'linearoperator'
also, question hints need use linearoperator interface, not understand linearoperator doing , why need here.
any suggestion appreciated! in advance!
i think parameters in wrong order,
x3=scipy.sparse.linalg.cg(a,b,m2)
in 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 in place of x0 - initial guess of solution not preconditioner. in host, correct order, class-linearoperator functioning well.
correct version
x3=scipy.sparse.linalg.cg(a,b,m=m2)
please use "key word" arguments possible.
Comments
Post a Comment