SCM

SCM Repository

[matrix] Annotation of /pkg/tests/other-pkgs.R
ViewVC logotype

Annotation of /pkg/tests/other-pkgs.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1765 - (view) (download)

1 : maechler 1327 ####--------- Test interfaces to other non-standard Packages ---------------
2 :    
3 : maechler 1350 library(Matrix)
4 :    
5 : maechler 1765 source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
6 : maechler 1467
7 :     MatrixRversion <- pkgRversion("Matrix")
8 :    
9 : maechler 1327 ###-- 1) 'graph' (from Bioconductor) ---------------------------
10 :     ###-- == ======= ---------------------------
11 : maechler 1407 if(isTRUE(try(require(graph)))) { # may be there and fail (with R-devel)
12 : maechler 1327
13 : maechler 1350 if(packageDescription("graph")$Version <= "1.10.2") {
14 :     ## graph 1.10.x for x <= 2 had too many problems as(<graph>, "matrix")
15 :     cat("Version of 'graph' is too old --- no tests done here!\n")
16 : maechler 886
17 : maechler 1467 } else if(pkgRversion("graph") != MatrixRversion) {
18 : maechler 909
19 : maechler 1467 cat(sprintf("The R version (%s) of 'graph' installation differs from the Matrix one (%s)\n",
20 :     pkgRversion("graph"), MatrixRversion))
21 :    
22 :     } else { ## do things
23 :    
24 : maechler 1350 ## 1) undirected
25 : maechler 886
26 : maechler 1350 V <- LETTERS[1:4]
27 :     edL <- vector("list", length=4)
28 :     names(edL) <- V
29 :     ## 1a) unweighted
30 :     for(i in 1:4)
31 :     edL[[i]] <- list(edges = 5-i)
32 :     gR <- new("graphNEL", nodes=V, edgeL=edL)
33 :     str(edges(gR))
34 :     sm.g <- as(gR, "sparseMatrix")
35 : maechler 1565 str(sm.g) ## dgC: TODO: want 'ds.' (symmetric)
36 : maechler 1350 validObject(sm.g)
37 : maechler 1565 sm.g ## TODO: should also show the colnames
38 : maechler 886
39 : maechler 1350 ## 1b) weighted
40 :     set.seed(123)
41 :     for(i in 1:4)
42 :     edL[[i]] <- list(edges = 5-i, weights=runif(1))
43 :     gRw <- new("graphNEL", nodes=V, edgeL=edL)
44 :     str(edgeWeights(gRw))
45 :     sm.gw <- as(gRw, "sparseMatrix")
46 : maechler 1565 str(sm.gw) ## *numeric* dgCMatrix
47 : maechler 1350 validObject(sm.gw)
48 : maechler 1565 sm.gw ## U[0,1] numbers in anti-diagonal
49 : maechler 907
50 : maechler 1350 ## 2) directed
51 :     gU <- gR; edgemode(gU) <- "directed"
52 :     sgU <- as(gU, "sparseMatrix")
53 : maechler 1565 str(sgU) ## 'dgC'
54 : maechler 1350 validObject(sgU)
55 :     sgU
56 : maechler 909
57 : maechler 1350 ## Reverse : sparseMatrix -> graph
58 : maechler 1565 sm.g[1,2] <- 1
59 : maechler 1350 gmg <- as(sm.g, "graph")
60 :     validObject(gmg2 <- as(sm.g, "graphNEL"))
61 :     gmgw <- as(sm.gw, "graph")
62 :     validObject(gmgw2 <- as(sm.gw, "graphNEL"))
63 :     gmgU <- as(sgU, "graph")
64 :     validObject(gmgU2 <- as(sgU, "graphNEL"))
65 :     stopifnot(identical(gmg, gmg2),
66 :     identical(gmgw, gmgw2),
67 :     identical(gmgU, gmgU2))
68 : maechler 1327
69 : maechler 1350 detach("package:graph")
70 : maechler 1467 }
71 : maechler 1350
72 :     } ## end{graph}
73 :    
74 : maechler 1327 ###-- 2) 'SparseM' ---------------------------------------------
75 :     ###-- == ======== ---------------------------------------------
76 :    
77 : maechler 1571 if(isTRUE(try(require(SparseM)))) { # may be there and fail
78 : maechler 1327
79 : maechler 1467 if(pkgRversion("SparseM") != MatrixRversion) {
80 :    
81 :     cat(sprintf("The R version (%s) of 'SparseM' installation differs from the Matrix one (%s)\n",
82 :     pkgRversion("SparseM"), MatrixRversion))
83 :    
84 :     } else { ## do things
85 :    
86 : maechler 1765 set.seed(1)
87 :     a <- round(rnorm(5*4), 2)
88 :     a[abs(a) < 0.7] <- 0
89 :     A <- matrix(a,5,4)
90 :     print(M <- Matrix(A))
91 :     stopifnot(
92 :     validObject(A.csr <- as.matrix.csr(A)),
93 :     validObject(At.csr <- as.matrix.csr(t(A))),
94 :     identical(At.csr, t(A.csr)),
95 :     identical(A, as.matrix(A.csr)),
96 :     identical(M, as(A.csr, "CsparseMatrix")),
97 :     identical(t(M), as(At.csr, "CsparseMatrix"))
98 :     )
99 : maechler 1327
100 : maechler 1765 ## More tests, notably for triplets
101 :     A.coo <- as.matrix.coo(A)
102 :     str(T <- as(M, "TsparseMatrix")) # has 'j' sorted
103 :     str(T. <- as(A.coo, "TsparseMatrix")) # has 'i' sorted
104 : maechler 1327
105 : maechler 1765 T3 <- as(as(T, "matrix.coo"), "Matrix") # dgT
106 :     M3 <- as(as(M, "matrix.csr"), "Matrix") # dgC
107 :     M4 <- as(as(M, "matrix.csc"), "Matrix") # dgC
108 :     M5 <- as(as(M, "matrix.coo"), "Matrix") # dgT
109 :     uniqT <- Matrix:::uniqTsparse
110 :     stopifnot(identical4(uniqT(T), uniqT(T.), uniqT(T3), uniqT(M5)),
111 :     identical3(M, M3, M4))
112 : maechler 1350
113 : maechler 1765 if(FALSE) # detaching the package gives error ".GenericTable" not found
114 :     detach("package:SparseM")
115 :    
116 : maechler 1467 }
117 :    
118 : maechler 1327 }## end{SparseM}

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