1

eigh稀疏和eigsh正常 linalg 库的以下用途不应该给出相同的答案吗?

from numpy import random
from scipy.linalg import eigh as E1
from scipy.sparse.linalg import eigsh as E2

# Number of eigenvectors to check
kv = 4

# Make a symmetric matrix
N = 20
A = random.random((N,N))
A += A.T
assert( (A==A.T).all() )

L1,V1 = E1(A)
L2,V2 = E2(A,k=kv)

print sorted(L1)[::-1][:kv]
print sorted(L2)[::-1]

一些样本值:

[20.189135474050769, 3.1309586179883211, 2.6576577451888599, 2.3435647560235355]
[20.18913547405079, 3.1309586179883317, -2.9218877679802597, -3.2962262932479751]

[19.688806193598253, 3.195683848729701, 3.0987244589789058, 2.5648352930907214]
[19.688806193598261, 3.1956838487296961, 3.0987244589789014, -2.7495588013870975]

[20.482117184188727, 3.3175885619590439, 2.8910051228982252, 2.746127351510173]
[20.482117184188716, 3.3175885619590524, 2.891005122898231, 2.7461273515101809]

在我看来,内部的 Lancoz 例程只是有时会收敛。令人抓狂的是它适用于某些值 - 您可以在第三个示例中看到前四个特征值是正确的,但在其他两个示例中并非如此。

版本: Python 2.7.3、 numpy 1.6.1、 scipy 0.9.0

4

2 回答 2

3

您需要按特征值的绝对值对特征值进行排序,以下代码将给出相同的结果:

print sorted(L1, key=abs)[::-1][:kv]
print sorted(L2, key=abs)[::-1]
于 2012-04-20T01:02:56.187 回答
-1

While sorting by absolute values (as suggested by @HYRY) gives the same result for both tests, it still is unsatisfying since the my underlying intent was to get the largest eigenvalues from my matrix. For real symmetric matrices I thought eigsh would return the largest eigenvalues. It doesn't, it returns the largest eigenvalues sorted by magnitude.

于 2012-04-20T13:41:06.900 回答