I am trying to find the top k leading eigenvalues of a NumPy matrix (using python dot product notation) L@L + a*X@X.T, where $L$ and $X$ are a symmetric $n \times n$ and an $n \times d$ matrix, respectively, and $a$ is a scalar.

According to the below text from this paper, I should be able to calculate these leading eigenvalues with L@(L@v) + a*X@(X.T@v), where I guess v is an arbitrary vector. The Lanczos paper they cite is here.

I'm not quite sure where to start. I know that scipy has scipy.sparse.linalg.eigsh here, and from the notes it looks like it uses the Lanczos algorithm - but I am at a loss as to whether it's possible to use sparse.linalg.eigsh for my specific use case. I googled around and didn't find a Python implementation for this very quickly -- does anybody know if I can use sparse.linalg.eigsh to calculate this somehow? I definitely don't want to write this algorithm out myself if I can avoid it.

enter image description here


You could define a linear opearator and pass it to the function eigsh. Ideally, your matrices $L$ and $X$ are sparse so you can take advantage of the matrix-vector product.

In your case, you would have something like the following.

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

def mv(v):
    a = 2.3
    return L@(L@v) + a*X@(X.T@v)

n = L.shape[0]
k = 10
A = LinearOperator((n, n), matvec=mv)
eigenvalues, eigenvectors = eigsh(A, k=k, which="LM")
  • $\begingroup$ I'll play around with this, thank you so much! $\endgroup$ – Alex L Apr 8 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.