I have a symmetric matrix (adjacency matrix for an undirected graph) and I have a particular eigenvalue (the maximum eigenvalue) and I want the eigenvector associated with it (left or right, either one, since I believe the left is simply the transpose of the right for symmetric matrices).
The graphs I am running can go from thousands to hundreds of thousands of nodes so the corresponding adjacency matrix will be large. The density however is sparse, so the corresponding matrix will be sparse as well.
Is there an efficient way to do this in SciPy? Better yet, is there a way to compute only the leading eigenvalue and the corresponding eigenvector for a given symmetric matrix (meaning I don’t have to compute the leading eigenvalue myself explicitly with linalg.eigvals).
Yes there is,
scipy.sparse.linalg.eigsh, thehat the end standing for Hermitian, there is also a version for non-symmetrical matrices,scipy.sparse.linalg.eigs.If
ais your matrix, sparse or not, your call would look something like:Your
evalsandevecsare arrays of eigenvalues and corresponding eigenvectors, since you could ask for more than one if settingkto something other than 1. Which eigenvalues and vectors are returned is controlled with thewhichparameter, that defaults toLM, standing for largest magnitude.