SCM Repository

[matrix] View of /pkg/Matrix/man/graph2T.Rd
ViewVC logotype

View of /pkg/Matrix/man/graph2T.Rd

Parent Directory Parent Directory | Revision Log Revision Log

Revision 2935 - (download) (as text) (annotate)
Mon Dec 2 15:59:47 2013 UTC (6 years ago) by mmaechler
File size: 3761 byte(s)
oops! forgotten to add before commit
\title{Conversions "graph" <--> (sparse) Matrix}
% graph stuff
  The \pkg{Matrix} package has supported conversion from and to
  \code{"\link[graph:graph-class]{graph}"} objects from (Bioconductor) package
  \pkg{graph} since summer 2005, via the usual \code{as(., "<class>")}
  coercion, \preformatted{  as(from, Class)
  Since 2013, this functionality is further exposed as the
  \code{graph2T()} and \code{T2graph()} functions (with further
  arguments than just \code{from}), which convert a graphs to and from
  the triplet form of sparse matrices (of class
  \code{"\linkS4class{TsparseMatrix}"}) .
graph2T(from, use.weights = )
T2graph(from, need.uniq = is_not_uniqT(from), edgemode = NULL)
  \item{from}{for \code{graph2T()}, an \R object of class
    \code{"graph"}; \cr
    for \code{T2graph()}, a sparse matrix inheriting from
  \item{use.weights}{logical indicating if weights should be used, i.e.,
    equivalently the result will be numeric, i.e. of class
    \code{\linkS4class{dgTMatrix}}; otherwise the result will be
    \code{\linkS4class{ngTMatrix}} or \code{\linkS4class{nsTMatrix}},
    the latter if the graph is undirected.  The default looks if there
    are weights in the graph, and if any differ from \code{1}, weights
    are used.}
  \item{need.uniq}{a logical indicating if \code{from} may need to be
    internally \dQuote{uniqified}; do not set this and hence use the
    default, unless you know what you are doing!}
  \item{edgemode}{one of \code{NULL}, \code{"directed"}, or
    \code{"undirected"}.  The default \code{NULL} looks if the matrix is
    symmetric and assumes \code{"undirected"} in that case.}
  For \code{graph2T()}, a sparse matrix  inheriting from

  For \code{T2graph()} an \R object of class \code{"graph"}.
  Note that the CRAN package \pkg{igraph} also provides conversions from
  and to sparse matrices (of package \pkg{Matrix}) via its
  \code{\link[igraph]{graph.adjacency}()} and
if(isTRUE(try(require(graph)))) { ## super careful .. for "checking reasons"
  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(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)
   identical(as(g3,"Matrix"), as(as(a2 + t(a2), "nMatrix"),"symmetricMatrix"))
   identical(tg3 <- graph2T(g3), graph2T(g3, use.weights=FALSE))
   identical(as(m3,"TsparseMatrix"), Matrix:::uniqTsparse(tg3))
  a. <- sparseMatrix(i= 4:1, j=1:4, dimnames=list(n4,n4), giveC=FALSE) # no 'x' 
  show(g. <- as(a., "graph")) 
  stopifnot(edgemode(g.) == "undirected", numEdges(g.) == 2,
            as(g.,"Matrix"), as(a., "symmetricMatrix"))
}% only if( graph )
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge