Scipy LinearOperator dtype未指定

时间:2013-11-28 01:59:07

标签: python numpy matrix scipy

我正在尝试使用Scipy的gmres命令。我的矩阵密集且大,所以我的计划是使用LinearOperator命令仅返回矩阵向量积。请在此处查看我之前的问题LinearOperator with Two Inputs。在这个问题的帮助下,我能够构建一个成功执行计算的LinearOperator对象A*x,其中A是矩阵,x是向量。

问题在于我致电gmres。我运行命令:

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

并返回运算符A没有dtype的错误。这是事实,因为A.dtype返回错误。我的问题是我不知道要为dtype指定什么。当我构造我的线性运算符时,有一个可选参数可以传递给dtype,但我不知道该给它什么。
我试过通过dtype='float64'并冻结了我的IDE,所以我怀疑我错了。
尝试dtype = None只会产生默认值,其中未定义dtype。

我还尝试过简单地定义Adtype留空,然后输入A.dtype = None。这实际上提供了属性A.dtype,并在调用A时产生了另一个错误。

这似乎与另一个问题有关,即gmres似乎想要给它一个预处理器。我实际上没有一个我想要给它的预处理器,所以它试图构造一个并尝试使用与A相同的dtype,但是,因为A没有dtype属性,所以它出错了。任何建议都将不胜感激。

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'

1 个答案:

答案 0 :(得分:3)

(这更像是一个扩展的评论,而不是答案。)

您使用的是什么版本的scipy?我正在使用scipy 0.13.0。如果我在创建LinearOperator时未指定dtype,则会收到dtype错误。但是指定dtype='float64'对我有用:

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)