(This article was first published on Pirate Science » R, and kindly contributed to R-bloggers)
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 his blog: Pirate Science » R.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...