Search code examples
numpyscipyeigenvector

scipy.sparse.linalg.eigen.arpack.eigsh is unstable


I run the following code

import numpy as np  
from scipy.sparse.linalg.eigen.arpack import eigsh


N =3
W = np.random.rand(N,N)

print(W)

Esys2 = eigsh(W, k=2, which = 'LA')
print(Esys2)

Esys = eigsh(W, k=2, which='LA')
print(Esys)

and I get the output:

[[ 0.21158481  0.20105984  0.60153543]
[ 0.53304312  0.5150105   0.49034533]
[ 0.90599546  0.09468583  0.87709113]]

(array([ 0.24450196,  1.58625052]), array([[ 0.07908241,  0.47701961],
       [-0.77316975,  0.58361601],
       [ 0.62924917,  0.65714887]]))

(array([ 0.31766568,  1.85202346]), array([[ 0.02712824,  0.54350609],
       [-0.85016138,  0.45401711],
       [ 0.52582287,  0.70602379]]))        

How is that possible? The output seems to be random. All three Eigenvectors cannot be computed(k=2 is max).

Numpy version: 1.11.1 Python version: 3.5.2

Thank you!


Solution

  • Take another look at the docstring for eigsh. Note, in particular, two items:

    • eigsh assumes that the input is a symmetric matrix. Your W is not symmetric. The behavior of the function is undefined if the input array is not symmetric.
    • eigsh is iterative, and it requires a starting vector for the iteration. If not given with the v0 argument, a random vector is chosen. So you will not necessarily get exactly the same results on repeated calls.