Scipy LinearOperator dtype unspecified

I am trying to use the scipy command gmres. My matrix is ​​dense and large, so my plan was to use the command LinearOperatorto return only the vector matrix product. See my previous question here LinearOperator with two inputs . With the help of this question, I was able to build an object LinearOperatorthat successfully performs the calculation A*x, where Ais the matrix, and xis the vector.

The problem is what I'm calling gmres. I ran the command:

x, info = scipy.sparse.linalg.gmres(A, b)

and it returns an error that the operator Adoes not have dtype. This is true because it A.dtypereturns an error. My problem is that I have no idea what to specify for dtype. When I build my linear operator, dtypethere is an optional parameter for, but I do not know what to give it.
I tried to skip dtype='float64'and it froze my IDE, so I suspect I'm wrong there.
The attempt dtype = Nonesimply returns the default value, where dtype is not defined.

I also tried just determining Ato leave dtypeblank and then type A.dtype = None. This actually gives the attribute A.dtypeand gives a different error when called A.

This seems to be related to another problem, which is that gmres seems to want it to be given a precondition. I actually do not have the pre-conditioner that I want to give him, so he tries to build one, and he tries to use the same dtype type as A, but since A does not have the dtype attribute, it throws errors. Any suggestion would be greatly appreciated.

A = sparse.scipy.linalg.linearoperator(shape = (n,n), matvec = mv, dtype = 'float64')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/iterative.py", line 393, 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 119, in make_system

M = LinearOperator(A.shape, matvec=psolve, rmatvec=rpsolve, dtype=A.dtype)
AttributeError: LinearOperator instance has no attribute 'dtype'
+4
source share
1 answer

(This is more of an extended comment than an answer.)

What version of scipy are you using? I am using scipy 0.13.0. If I do not specify dtype when creating LinearOperator, I get the error dtypeyou get. But the hint dtype='float64'works for me:

In [1]: import numpy as np

In [2]: from scipy.sparse.linalg import LinearOperator, gmres

In [3]: def mymatvec(v):
   ...:     a = np.array([[4,2,1],[2,2,1],[1,1,1]])
   ...:     return a.dot(v)
   ...: 

In [4]: A = LinearOperator((3,3), mymatvec, dtype='float64')

In [5]: b = np.array([1,2,3])

In [6]: gmres(A, b)
Out[6]: (array([-0.5, -0.5,  4. ]), 0)
+3

All Articles