I am trying to compute the m
first eigenvectors of a large sparse matrix in R. Using eigen()
is not realistic because large means N > 106 here.
So far I figured out that I should use ARPACK from the igraph
package, which can deal with sparse matrices. However I can't get it to work on a very simple (3x3) matrix:
library(Matrix)
library(igraph)
TestDiag <- Diagonal(3, 3:1)
TestMatrix <- t(sparseMatrix(i = c(1, 1, 2, 2, 3), j = c(1, 2, 1, 2, 3), x = c(3/5, 4/5, -4/5, 3/5, 1)))
TestMultipliedMatrix <- t(TestMatrix) %*% TestDiag %*% TestMatrix
And then using the code given in example of the help of the arpack()
function to extract the 2 first eigenvectors :
func <- function(x, extra=NULL) { as.vector(TestMultipliedMatrix %*% x) }
arpack(func, options=list(n = 3, nev = 2, ncv = 3, sym=TRUE, which="LM", maxiter=200), complex = FALSE)
I get an error message:
Error in arpack(func, options = list(n = 3, nev = 2, ncv = 3, sym = TRUE, :
At arpack.c:1156 : ARPACK error, NCV must be greater than NEV and less than or equal to N
I don't understand this error, as ncv (3) is greater than nev (2) here, and equal to N (3).
Am I making some stupid mistake or is there a better way to compute eigenvectors of a sparse matrix in R?
Update
This error is apparently due to a bug in the arpack()
function with uppercase / lowercase NCV and NEV.
Any suggestions to solve the bug (I tried to have a look at the package code but it is far too complex for me to understand) or compute the eigenvectors in an other way are welcome.