SCM

SCM Repository

[matrix] Annotation of /pkg/tests/indexing.R
ViewVC logotype

Annotation of /pkg/tests/indexing.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1665 - (view) (download)

1 : maechler 687 #### For both 'Extract' ("[") and 'Replace' ("[<-") Method testing
2 :    
3 :     library(Matrix)
4 :    
5 : maechler 907 source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
6 : maechler 687
7 : maechler 873 ### Dense Matrices
8 :    
9 : maechler 687 m <- Matrix(1:28, nrow = 7)
10 : maechler 873 validObject(m) ; m@x <- as.double(m@x) ; validObject(m)
11 :     stopifnot(identical(m, m[]),
12 :     identical(m[2, 3], 16), # simple number
13 :     identical(m[2, 3:4], c(16,23))) # simple numeric of length 2
14 : maechler 687
15 : maechler 873 m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
16 :     m[-(4:7), 3:4] # dito; the upper right corner of 'm'
17 : maechler 687
18 : maechler 873 ## rows or columns only:
19 :     m[1,] # first row, as simple numeric vector
20 :     m[,2] # 2nd column
21 :     m[,1:2] # sub matrix of first two columns
22 :     m[-(1:6),, drop=FALSE] # not the first 6 rows, i.e. only the 7th
23 : maechler 1331 m[integer(0),] #-> 0 x 4 Matrix
24 :     m[2:4, numeric(0)] #-> 3 x 0 Matrix
25 : maechler 687
26 : maechler 873 ## logical indexing
27 :     stopifnot(identical(m[2,3], m[(1:nrow(m)) == 2, (1:ncol(m)) == 3]),
28 :     identical(m[2,], m[(1:nrow(m)) == 2, ]),
29 :     identical(m[,3:4], m[, (1:4) >= 3]))
30 :    
31 : maechler 886 ## dimnames indexing:
32 :     mn <- m
33 :     dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),
34 :     LETTERS[1:ncol(mn)])
35 :     mn["rd", "D"]
36 : maechler 1226 stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,
37 :     identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,
38 : maechler 886 identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])
39 :     )
40 : maechler 873
41 : maechler 886 mo <- m
42 :     m[2,3] <- 100
43 :     m[1:2, 4] <- 200
44 :     m[, 1] <- -1
45 :     m[1:3,]
46 :    
47 : maechler 1599 ## testing operations on logical Matrices rather more than indexing:
48 : maechler 1655 m. <- as.matrix(m)
49 : maechler 1226 g10 <- m [ m > 10 ]
50 :     stopifnot(18 == length(g10))
51 : maechler 1362 stopifnot(10 == length(m[ m <= 10 ]))
52 : maechler 1599 sel <- (20 < m) & (m < 150)
53 : maechler 1655 sel.<- (20 < m.)& (m.< 150)
54 : maechler 1599 nsel <-(20 >= m) | (m >= 150)
55 : maechler 1575 (ssel <- as(sel, "sparseMatrix"))
56 :     stopifnot(is(sel, "lMatrix"), is(ssel, "lsparseMatrix"),
57 : maechler 1655 identical3(as.mat(sel.), as.mat(sel), as.mat(ssel)),
58 :     identical3(!sel, !ssel, nsel), # !<sparse> is typically dense
59 : maechler 1599 identical3(m[ sel], m[ ssel], as.matrix(m)[as.matrix( ssel)]),
60 :     identical3(m[!sel], m[!ssel], as.matrix(m)[as.matrix(!ssel)])
61 : maechler 1575 )
62 : maechler 873
63 : maechler 1575 ## more sparse Matrices --------------------------------------
64 : maechler 873
65 : maechler 687 m <- 1:800
66 :     set.seed(101) ; m[sample(800, 600)] <- 0
67 :     m <- Matrix(m, nrow = 40)
68 :     mm <- as(m, "matrix")
69 :     dimnames(mm) <- NULL ## << workaround: as(<sparse>, "matrix") has NULL dimnames
70 :     str(mC <- as(m, "dgCMatrix"))
71 :     str(mT <- as(m, "dgTMatrix"))
72 :     stopifnot(identical(mT, as(mC, "dgTMatrix")),
73 :     identical(mC, as(mT, "dgCMatrix")))
74 :    
75 :     mC[,1]
76 :     mC[1:2,]
77 : maechler 925 mC[7, drop = FALSE]
78 : maechler 1226 assert.EQ.mat(mC[1:2,], mm[1:2,])
79 :     stopifnot(all.equal(mC[,3], mm[,3]))
80 :     assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
81 :    
82 : maechler 1331 stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
83 :     dim(mC[, integer(0)]) == c(40,0),
84 :     identical(mC[, integer(0)], mC[, FALSE]),
85 :     identical(mC[7, drop = FALSE],
86 : maechler 925 mC[7,, drop = FALSE]))
87 : maechler 1269 validObject(print(mT[,c(2,4)]))
88 :     stopifnot(all.equal(mT[2,], mm[2,]),
89 :     ## row or column indexing in combination with t() :
90 :     identical(mT[2,], t(mT)[,2]),
91 :     identical(mT[-2,], t(t(mT)[,-2])),
92 :     identical(mT[c(2,5),], t(t(mT)[,c(2,5)]))
93 :     )
94 : maechler 1226 assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
95 : maechler 687 stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
96 :     identical3(mm[3,], mC[3,], mT[3,]),
97 :     identical3(mT[2,3], mC[2,3], 0),
98 :     identical(mT[], mT),
99 : maechler 1665 identical4( mm[c(3,7), 2:4], as.mat( m[c(3,7), 2:4]),
100 :     as.mat(mT[c(3,7), 2:4]), as.mat(mC[c(3,7), 2:4]))
101 : bates 1367 )
102 : maechler 687
103 : maechler 1331 x.x <- crossprod(mC)
104 :     stopifnot(class(x.x) == "dsCMatrix",
105 :     class(x.x. <- round(x.x / 10000)) == "dsCMatrix")
106 :     head(x.x.) # Note the *non*-structural 0's printed as "0"
107 : maechler 1575 tail(x.x., -3) # all but the first three lines
108 : maechler 1331
109 :     lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
110 : maechler 1665 (l10 <- lx.x[1:10, 1:10])# "lsC"
111 :     (l3 <- lx.x[1:3, ])
112 :     m.x <- as(x.x, "matrix")
113 :     stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
114 :     identical(as.mat(lx.x), m.x != 0),
115 :     identical(as.logical(lx.x), as.logical(m.x)),
116 :     identical(as.mat(l10), m.x[1:10, 1:10] != 0),
117 :     identical(as.mat(l3 ), m.x[1:3, ] != 0)
118 :     )
119 : maechler 1331
120 : maechler 1665
121 : maechler 925 ## --- negative indices ----------
122 :     mc <- mC[1:5, 1:7]
123 :     mt <- mT[1:5, 1:7]
124 :     ## sub matrix
125 : maechler 1226 assert.EQ.mat(mC[1:2, 0:3], mm[1:2, 0:3]) # test 0-index
126 : maechler 925 stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),
127 : maechler 1226 identical(mt[-(3:5), 0:2], mT[1:2, 0:2]),
128 :     identical(mC[2:3, 4], mm[2:3, 4]))
129 :     assert.EQ.mat(mC[1:2,], mm[1:2,])
130 : maechler 925 ## sub vector
131 :     stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],
132 :     mt[-(1:4), ], mT[5, 1:7]))
133 :     stopifnot(identical4(mc[-(1:4), -(2:4)], mC[5, c(1,5:7)],
134 :     mt[-(1:4), -(2:4)], mT[5, c(1,5:7)]))
135 :    
136 :     ## mixing of negative and positive must give error
137 :     assertError(mT[-1:1,])
138 :    
139 : maechler 1226 ## Sub *Assignment* ---- now works (partially):
140 :     mt0 <- mt
141 :     mt[1, 4] <- -99
142 :     mt[2:3, 1:6] <- 0
143 :     mt
144 :     m2 <- mt+mt
145 :     m2[1,4] <- -200
146 :     m2[c(1,3), c(5:6,2)] <- 1:6
147 :     stopifnot(m2[1,4] == -200,
148 :     as.vector(m2[c(1,3), c(5:6,2)]) == 1:6)
149 :     mt[,3] <- 30
150 :     mt[2:3,] <- 250
151 :     mt[1:5 %% 2 == 1, 3] <- 0
152 :     mt[3:1, 1:7 > 5] <- 0
153 :     mt
154 : maechler 1215
155 : maechler 1226 tt <- as(mt,"matrix")
156 :     ii <- c(0,2,5)
157 :     jj <- c(2:3,5)
158 :     tt[ii, jj] <- 1:6 # 0 is just "dropped"
159 :     mt[ii, jj] <- 1:6
160 :     assert.EQ.mat(mt, tt)
161 : maechler 1215
162 : maechler 1226 mt[1:5, 2:6]
163 :     as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
164 :    
165 : maechler 1315 mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
166 : maechler 1226 mt[2:3, 4:7] <- 33
167 :     validObject(mt)
168 :     mt
169 :    
170 : maechler 1315 mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
171 :     mc[1,4] <- 00 ; stopifnot(mc[1,4] == 00)
172 :     mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
173 :     mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
174 :    
175 : maechler 1226 mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
176 :     mt[-1, 3] <- -2:1
177 : maechler 1315 stopifnot(mc@x != 0, mt@x != 0,
178 :     mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed
179 :    
180 :     ev <- 1:5 %% 2 == 0
181 :     mc[ev, 3] <- 0:1
182 : maechler 1374 if(FALSE)## FIXME
183 :     stopifnot(mc[ev, 3] == 0:1) ##-> BUG {very peculiar; the 2nd time it works ...}
184 : maechler 1226 validObject(mc)
185 : maechler 1315 mc # now shows a non-structural zeros
186 : maechler 1226 mc[ii, jj] <- 1:6
187 :     mc[c(2,5), c(3,5)] <- 3.2
188 :     validObject(mc)
189 : maechler 1600 m. <- mc
190 :     mc[4,] <- 0
191 :     mc
192 : maechler 1226
193 : maechler 1331 H <- Hilbert(9)
194 : maechler 1374 Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
195 :     (trH <- tril(Hc[1:5, 1:5]))
196 :     stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")
197 : maechler 1331
198 : maechler 1374 i <- c(1:2, 4, 6:7); j <- c(2:4,6)
199 :     H[i,j] <- 0
200 : maechler 1331 (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
201 :     Hc. <- Hc
202 : maechler 1374 Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
203 :     stopifnot(as.matrix(Hc.[i,j]) == 0)
204 : maechler 1331 Hc.[, 1:6]
205 :    
206 : maechler 1226 cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''

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