igraph-help
[Top][All Lists]

## [igraph] Sparse Matrix Corrected Code from Martin (Solve Error)

 From: Surendar Swaminathan Subject: [igraph] Sparse Matrix Corrected Code from Martin (Solve Error) Date: Thu, 30 Jul 2009 11:29:50 -0700

Hello Gabor thank you very much for reply on teh post for decay factor. I had sent you a query before when the matrix was crashing on bonpow.sparse Matrix. Martin one of the developer of Matrix package sent back corrected code on them. I tried using them still it does not seem to work may be I am doing mistake with that.I am sending you the code he sent me.If you have time can you check them and see whether it is working for you.

Thank you Gabor.

bonpow.sparse <- function(graph, nodes = V(graph), loops = FALSE,
exponent = 1, adj.type = "both",
trace = TRUE,
rescale=FALSE, tol=1e-07)
{
stopifnot(require("igraph"),
require("Matrix"))
if(trace) {
c.width <- 30
C1 <- function(s) cat(sprintf("%-*s .. ", c.width, s))
C2 <- function() cat("[Ok]\n")
} else { C1 <- C2 <- function(...) {} }
## remove loops if requested
if (!loops) {
C1("simplify()ing graph")
graph <- simplify(graph, remove.multiple=FALSE, remove.loops=TRUE)
C2()
}
if(trace >= 2)
cat("class(d): ", class(d),"\n")
if(!is.directed(graph)) {
## MM: unfortunately  "igraph" does not return a *symmetric*
## --- sparse matrix directly, saving space and time,
## so we at least do it now :
C1("d <- as(d, \"symmetricMatrix\")")
d <- as(d, "symmetricMatrix"); C2()
}
## sparse identity matrix
vg <- vcount(graph)
if(FALSE) { ## "non-sense" :
C1("spMatrix(.) for Diagonal(vg)")
id <- spMatrix(vg, vg, i=1:vg, j=1:vg, x = rep(1, vg)); C2()
C1("    --> as(., \"dgCMatrix\")")
id <- as(id, "dgCMatrix"); C2()
}
else
id <- Diagonal(vg)
C1("M <- (id - exponent * d)")
M <- id - exponent * d ; C2()
C1("b <- degree(graph,.)")
b <- degree(graph, mode="out") ; C2()
if(trace >= 2) {
cat("  M : class ", class(M),";    dim: ", dim(M),"\n")
cat("  b : class ", class(b),"; length: ", length(b),"\n")
}
## solve it
## MM: This is "the horror"  -- ("it's the economy, stupid !!")
##     particularly as  solve(M) is *never* sparse !!!
## He should use  solve(M, b) !!
if(FALSE) {
C1("solve(M) %*% b")
ev <- solve(M, tol=tol) %*% b; C2()
} else {
C1("solve(M, b)")
ev <- solve(M, b) ; C2()
}
if (rescale) {
ev <- ev/sum(ev)
} else {
ev <- ev * sqrt(vg/sum((ev)^2))
- Hide quoted text -
}
ev[as.numeric(nodes) + 1]
}