简体   繁体   中英

python scipy eigs : return eigenvector after maximum number of iterations whatever the convergence tolerance

I would like to get eigenvectors of a sparse symmetric matrix with the best precision affordable in a given time.
Currently I use the following with scipy.sparse.eigsh :

evals, evecs = eigsh(MyMatrix, 2,which='LM' ,tol=1.e-15, maxiter=1000000)

If it does not converge to tol precision by maxiter iterations, it raises an ArpackNoConvergence error which contains the eigenvectors/values that have converged, but not the ones that did not. Yet, I prefer to have the vector with precision 1.e-14 instead of 1.e-15 rather than no vector at all. Is there a way to force returning the eigenvectors that have not converged yet (perhaps with another library) ?
Like in Matlab, where the eigs function returns the eigenvectors anyway, with just an additional Warning if the desired precision is not reached.

Thanks !

The ArpackNoConvergence exception has .eigenvalues and .eigenvectors attributes that contain the partial results:

import numpy as np
from scipy.sparse.linalg import eigsh, ArpackNoConvergence

M = np.random.RandomState(0).randn(100, 100)

try:
    w, v = eigsh(M, 5, maxiter=20)
except ArpackNoConvergence as e:
    print(e)
    w = e.eigenvalues
    v = e.eigenvectors
    print(w.shape, v.shape)

Prints:

ARPACK error -1: No convergence (21 iterations, 2/5 eigenvectors converged)
((2,), (100, 2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM