Skip to content

scipy.sparse.linalg.eigs gives incorrect largest magnitude eigenvalue #8307

@mhauru

Description

@mhauru

For the matrix trouble_matrix.zip, eigs gives the wrong dominant eigenvalue. The result depends on the initial v0. For fun, I added a 6 line piece at the end that finds the correct dominant eigenvalue with a dead simple power iteration.

import sys
import pickle
import numpy as np
import scipy as sp
import scipy.sparse.linalg as spsla

print(sp.__version__, np.__version__, sys.version_info)

with open("trouble_matrix.p", "rb") as f:
    M = pickle.load(f)

print(type(M), M.shape)

nev = 1
d = M.shape[0]

# Exact, full eig.
S, U = np.linalg.eig(M)
S = S[np.argsort(-np.abs(S))]
print(S[:nev])

# Sparse eig, with different initial guesses.
v0 = np.random.rand(d)
S, U = spsla.eigs(M, k=nev, v0=v0)
print(S)
S, U = spsla.eigs(M, k=nev, v0=v0)
print(S)
v0 = np.random.rand(d)
S, U = spsla.eigs(M, k=nev, v0=v0)
print(S)

# Finding the dominant eigenvalue with a simple power iteration.
v = np.random.rand(d)
for _ in range(100):
    v /= np.linalg.norm(v)
    old_v = v
    v = np.dot(M, v)
print(np.average(v/old_v))

Output:

1.0.0 1.14.0 sys.version_info(major=3, minor=6, micro=3, releaselevel='final', serial=0)
<class 'numpy.ndarray'> (1296, 1296)
[0.65419337-8.24548726e-17j]
[0.84454248+22.62373266j]
[0.84454248+22.62373266j]
[23.2905353-0.77272713j]
(0.6541933722683013+1.0231927548628299e-16j)

One would expect all five numbers above to be the same (to precision). If M is picked to be a random matrix instead, this indeed happens. Asking for more than one dominant eigenvalue goes equally wrong, I'm just choosing to showcase the simplest possible thing. I tried increasing ncv and maxiter, but to no help.

There shouldn't be anything weird about the matrix. The dominant eigenvalue is not degenerate (not shown in the output below). It's not just this one matrix for which this happens, I found a bunch and just picked this as an example.

I found a similar sounding issue for which="LR", but I don't know if I should expect that to be related: #6827

I assume I'm using the ARPACK version bundled with scipy 1.0.0, which I think is supposed to be 3.3.0. I can check if you let me know how.

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectA clear bug or issue that prevents SciPy from being installed or used as expectedscipy.sparse.linalg

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions