python - scipy.sparse.linalg.eigs 因抽象线性运算符而失败

标签 python scipy linear-algebra eigenvalue

当我使用抽象/黑盒线性运算符时,上述函数会失败。这是一个最小的例子:

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/

相关文章:

javascript - 您可以使用 python 中的映射或其他高阶函数来跟踪索引吗?

python - 寻找最大和距离

java - 将android-opengl上的触摸转换为ray/vector并检查是否碰到飞机

python - PyInstaller Winerror3 : System cannot find path specified

python - scipy curve_fit 提高 "OptimizeWarning: Covariance of the parameters could not be estimated"

python - 值错误 : cannot switch from manual field specification to automatic field numbering

python - 围绕中心单元旋转 3x3 阵列的每个单元的模块(不是矩阵旋转)

python - 是否可以在 Scipy 的 Mann-Whitney U 检验中指定替代假设?

python - 相同的 SciPy.linalg.eigh() 特征值不相等

algorithm - 如何在 O(m+n) 中获取两个稀疏向量的点积,其中 m 和 n 是两个向量中的元素数