2015-09-28 30 views
0

當我使用抽象/黑盒線性運算符時,上述函數失敗。這裏有一個小例子:scipy.sparse.linalg.eigs與抽象線性運算符失敗

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)  

發生這種情況的蟒蛇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 

任何幫助,將不勝感激。

回答

0

OK,這是尷尬的:只要定義A不同:

def f(v): 
    global a 
    return np.dot(a, v) 

A = la.LinearOperator(a.shape, f) 

這使得一切工作就好了。