当我使用抽象/黑盒线性运算符时,上述函数会失败。这是一个最小的例子:
import numpy as np
import scipy.sparse.linalg as la
# Just generate an n X n matrix
n = 9
a = np.random.normal( size = n * n )
a = a.reshape( (n,n) )
# A is a black-box linear operator
def A(v):
global a
return np.dot( a, v )
# If you don't define a shpae for A you get an error
A.shape = ( n,n )
# This works
success = la.eigs( a )
# This throws an error.
failure = la.eigs( A )
这种情况发生在带有 scipy 0.13.3 的 python 3.2.2 以及带有 scipy 0.16.0 的 python 2.7.3 中。
错误消息:
File "/home/daon/.local/lib/python2.7/site-packages/scipy/sparse/linalg/eigen/arpack/arpack.py", line 1227, in eigs
matvec = _aslinearoperator_with_dtype(A).matvec
File "/home/daon/.local/lib/python2.7/site-packages/scipy/sparse/linalg/eigen/arpack/arpack.py", line 885, in _aslinearoperator_with_dtype
m = aslinearoperator(m)
File "/home/daon/.local/lib/python2.7/site-packages/scipy/sparse/linalg/interface.py", line 682, in aslinearoperator
raise TypeError('type not understood')
TypeError: type not understood
最佳答案
好吧,这很尴尬:只需以不同的方式定义A
:
def f(v):
global a
return np.dot( a, v )
A = la.LinearOperator( a.shape, f )
这使得一切正常。
关于python - scipy.sparse.linalg.eigs 因抽象线性运算符而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32832766/