【问题标题】:scipy.sparse.linalg.eigs fails with abstract linear operatorscipy.sparse.linalg.eigs 因抽象线性运算符而失败
【发布时间】:2015-12-26 06:31:54
【问题描述】:

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

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 )    

这发生在 python 3.2.2 和 scipy 0.13.3 以及 python 2.7.3 和 scipy 0.16.0。

错误信息:

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

【问题讨论】:

    标签: python scipy linear-algebra eigenvalue


    【解决方案1】:

    好吧,这很尴尬:只需不同地定义A

    def f(v):
        global a   
        return np.dot( a, v )
    
    A = la.LinearOperator( a.shape, f )
    

    这使一切正常。

    【讨论】:

    • 这里只是提一下,如果你在函数f(v)之外定义a = a.reshape( (n,n) ),那么python会将它解释为一个全局变量。所以函数内部的声明global a是不必要的。
    猜你喜欢
    • 2011-03-12
    • 1970-01-01
    • 2020-06-05
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多