I want to find the 1st and 2nd largest eigenvalues of a big, sparse and symmetric matrix (in python). scipy.sparse.linalg.eigsh with k=2 gives the second largest eigenvalue with respect to the absolute value - so it's not a good solution. In addition, I can't use numpy methods because my matrix is too big and numpy is too slow...
I am not sure what is the best solution to this problem - any help is welcomed.
Thanks!
tl;dr: You can use the which='LA'
flag as described in the documentation.
I quote:
scipy.sparse.linalg.eigsh(A, k=6, M=None, sigma=None, which='LM', v0=None, ncv=None, maxiter=None, tol=0, return_eigenvectors=True, Minv=None, OPinv=None, mode='normal')
Emphasis mine.
which : str [‘LM’ | ‘SM’ | ‘LA’ | ‘SA’ | ‘BE’]
If A is a complex hermitian matrix, ‘BE’ is invalid. Which k eigenvectors and eigenvalues to find:
‘LM’ : Largest (in magnitude) eigenvalues
‘SM’ : Smallest (in magnitude) eigenvalues
‘LA’ : Largest (algebraic) eigenvalues
‘SA’ : Smallest (algebraic) eigenvalues
‘BE’ : Half (k/2) from each end of the spectrum
So, you can specify which='LA'
instead of the default LM
.
Example:
In [19]: A = numpy.random.randn(5,5)
In [20]: numpy.linalg.eig(A+A.T)[0] #Actual Eigenvalues
Out[20]: array([ 3.32906012, 0.88700157, -1.16620472, -3.54512752, -2.43562899])
In [21]: sp.eigsh(A+A.T,3)[0] #Three Largest (in Magnitude). What you don't want
Out[21]: array([-3.54512752, -2.43562899, 3.32906012])
In [22]: sp.eigsh(A+A.T,3,which='LA')[0] #Three Largest. What you do want
Out[22]: array([-1.16620472, 0.88700157, 3.32906012])