if(requireNamespace("graph")) {
n4 <- LETTERS[1:4]; dns <- list(n4,n4)
show(a1 <- sparseMatrix(i= c(1:4), j=c(2:4,1), x = 2, dimnames=dns))
show(g1 <- as(a1, "graph")) # directed
unlist(graph::edgeWeights(g1)) # all '2'
show(a2 <- sparseMatrix(i= c(1:4,4), j=c(2:4,1:2), x = TRUE, dimnames=dns))
show(g2 <- as(a2, "graph")) # directed
# now if you want it undirected:
show(g3 <- T2graph(as(a2,"TsparseMatrix"), edgemode="undirected"))
show(m3 <- as(g3,"Matrix"))
show( graph2T(g3) ) # a "pattern Matrix" (nsTMatrix)
# \dontshow{
stopifnot(
identical(as(g3,"Matrix"), as(as(a2 + t(a2), "nMatrix"),"symmetricMatrix"))
,
identical(tg3 <- graph2T(g3), graph2T(g3, use.weights=FALSE))
,
identical(as(m3,"TsparseMatrix"), asUniqueT(tg3))
)
# }
a. <- sparseMatrix(i=4:1, j=1:4, dimnames=list(n4, n4), repr="T") # no 'x'
show(a.) # "ngTMatrix"
show(g. <- as(a., "graph"))
# \dontshow{
stopifnot(graph::edgemode(g.) == "undirected",
graph::numEdges(g.) == 2,
all.equal(as(g., "TsparseMatrix"),
as(a., "symmetricMatrix"))
)
# }
}
Run the code above in your browser using DataLab