SCM

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3153 - (view) (download) (as text)

1 : mmaechler 2935 \name{graph-sparseMatrix}
2 :     \title{Conversions "graph" <--> (sparse) Matrix}
3 :     \alias{graph2T}
4 :     \alias{T2graph}
5 :     % graph stuff
6 :     \alias{coerce,graph,Matrix-method}
7 :     \alias{coerce,graph,sparseMatrix-method}
8 :     \alias{coerce,graph,CsparseMatrix-method}
9 :     \alias{coerce,graphAM,sparseMatrix-method}
10 :     \alias{coerce,graphNEL,CsparseMatrix-method}
11 :     \alias{coerce,graphNEL,TsparseMatrix-method}
12 :     \alias{coerce,sparseMatrix,graph-method}
13 :     \alias{coerce,sparseMatrix,graphNEL-method}
14 :     \alias{coerce,TsparseMatrix,graphNEL-method}
15 :     \alias{coerce,dgTMatrix,graphNEL-method}
16 :     \description{
17 :     The \pkg{Matrix} package has supported conversion from and to
18 :     \code{"\link[graph:graph-class]{graph}"} objects from (Bioconductor) package
19 : mmaechler 3153 \pkg{graph} since summer 2005, via the usual \code{\link{as}(., "<class>")}
20 : mmaechler 2935 coercion, \preformatted{ as(from, Class)
21 : mmaechler 3153 }%pre
22 : mmaechler 2935 Since 2013, this functionality is further exposed as the
23 :     \code{graph2T()} and \code{T2graph()} functions (with further
24 : mmaechler 2937 arguments than just \code{from}), which convert graphs to and from
25 : mmaechler 2935 the triplet form of sparse matrices (of class
26 :     \code{"\linkS4class{TsparseMatrix}"}) .
27 :     }
28 :     \usage{
29 :     graph2T(from, use.weights = )
30 :     T2graph(from, need.uniq = is_not_uniqT(from), edgemode = NULL)
31 :     }
32 :     \arguments{
33 :     \item{from}{for \code{graph2T()}, an \R object of class
34 :     \code{"graph"}; \cr
35 :     for \code{T2graph()}, a sparse matrix inheriting from
36 :     \code{"\linkS4class{TsparseMatrix}"}.}
37 :     \item{use.weights}{logical indicating if weights should be used, i.e.,
38 :     equivalently the result will be numeric, i.e. of class
39 :     \code{\linkS4class{dgTMatrix}}; otherwise the result will be
40 :     \code{\linkS4class{ngTMatrix}} or \code{\linkS4class{nsTMatrix}},
41 :     the latter if the graph is undirected. The default looks if there
42 :     are weights in the graph, and if any differ from \code{1}, weights
43 :     are used.}
44 :     \item{need.uniq}{a logical indicating if \code{from} may need to be
45 : mmaechler 2937 internally \dQuote{uniqified}; do not set this and hence rather use
46 :     the default, unless you know what you are doing!}
47 : mmaechler 2935 \item{edgemode}{one of \code{NULL}, \code{"directed"}, or
48 :     \code{"undirected"}. The default \code{NULL} looks if the matrix is
49 :     symmetric and assumes \code{"undirected"} in that case.}
50 :     }
51 :     \value{
52 :     For \code{graph2T()}, a sparse matrix inheriting from
53 :     \code{"\linkS4class{TsparseMatrix}"}.
54 :    
55 :     For \code{T2graph()} an \R object of class \code{"graph"}.
56 :     }
57 :     \seealso{
58 :     Note that the CRAN package \pkg{igraph} also provides conversions from
59 :     and to sparse matrices (of package \pkg{Matrix}) via its
60 :     \code{\link[igraph]{graph.adjacency}()} and
61 :     \code{\link[igraph]{get.adjacency}()}.
62 :     }
63 :     \examples{
64 :     if(isTRUE(try(require(graph)))) { ## super careful .. for "checking reasons"
65 :     n4 <- LETTERS[1:4]; dns <- list(n4,n4)
66 :     show(a1 <- sparseMatrix(i= c(1:4), j=c(2:4,1), x = 2, dimnames=dns))
67 :     show(g1 <- as(a1, "graph")) # directed
68 :     unlist(edgeWeights(g1)) # all '2'
69 :    
70 :     show(a2 <- sparseMatrix(i= c(1:4,4), j=c(2:4,1:2), x = TRUE, dimnames=dns))
71 :     show(g2 <- as(a2, "graph")) # directed
72 :     # now if you want it undirected:
73 :     show(g3 <- T2graph(as(a2,"TsparseMatrix"), edgemode="undirected"))
74 :     show(m3 <- as(g3,"Matrix"))
75 :     show( graph2T(g3) ) # a "pattern Matrix" (nsTMatrix)
76 :     \dontshow{
77 :     stopifnot(
78 :     identical(as(g3,"Matrix"), as(as(a2 + t(a2), "nMatrix"),"symmetricMatrix"))
79 :     ,
80 :     identical(tg3 <- graph2T(g3), graph2T(g3, use.weights=FALSE))
81 :     ,
82 : mmaechler 3059 identical(as(m3,"TsparseMatrix"), uniqTsparse(tg3))
83 : mmaechler 2935 )
84 :     }
85 : mmaechler 2945 a. <- sparseMatrix(i= 4:1, j=1:4, dimnames=list(n4,n4), giveC=FALSE) # no 'x'
86 :     show(a.) # "ngTMatrix"
87 :     show(g. <- as(a., "graph"))
88 : mmaechler 2935 \dontshow{
89 :     stopifnot(edgemode(g.) == "undirected", numEdges(g.) == 2,
90 : mmaechler 2945 all.equal(as(g., "TsparseMatrix"),
91 :     as(a., "symmetricMatrix"))
92 :     )
93 : mmaechler 2935 }
94 :     }% only if( graph )
95 :     }
96 :     \keyword{graph}
97 :     \keyword{utilities}

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge