Using ARPACK to compute the largest eigenvalue of a matrix

[This article was first published on Pirate Science » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Thanks to Gábor Csárdi, author of the R interface to ARPACK, for this example of using (the R/Igraph interface to) arpack for finding the largest eigenvalue of a matrix. The key insight is that arpack solves the function passed to it; it is this nature of this function which determines if arpack returns eigenvalues or the graph laplacian or etc. Note that the adjacency matrix gets passed to arpack via this function, so there is some dependence on scoping (arpack has an option to specify the environment, so encapsulation is possible if desired).

1
2
3
4
5
6
7
8
require(Matrix) ## for sparse matrices
require(igraph) ## for the function arpack
 
## assume you already have an igraph object, though any (sparse) matrix object should work
mymat <- get.adjacency(mygraph,sparse=TRUE)
func <- function(x, extra=NULL) { as.vector(mymat %*% x) } 
vals <- arpack(func, options=list(n=vcount(mygraph), nev=3, ncv=8, sym=TRUE,
                                  which="LM", maxiter=200))$values

file under "completing the function documentation"

To leave a comment for the author, please follow the link and comment on their blog: Pirate Science » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)