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 2115 - (view) (download)

1 : maechler 1710 ## For both 'Extract' ("[") and 'Replace' ("[<-") Method testing
2 : maechler 687
3 :     library(Matrix)
4 :    
5 : maechler 907 source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
6 : maechler 687
7 : maechler 2113 options(verbose = TRUE)# to show message()s
8 :    
9 : maechler 873 ### Dense Matrices
10 :    
11 : maechler 1673 m <- Matrix(1:28 +0, nrow = 7)
12 :     validObject(m)
13 : maechler 873 stopifnot(identical(m, m[]),
14 :     identical(m[2, 3], 16), # simple number
15 :     identical(m[2, 3:4], c(16,23))) # simple numeric of length 2
16 : maechler 687
17 : maechler 873 m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
18 : maechler 2113 m[-(4:7), 3:4] # ditto; the upper right corner of 'm'
19 : maechler 687
20 : maechler 873 ## rows or columns only:
21 :     m[1,] # first row, as simple numeric vector
22 :     m[,2] # 2nd column
23 :     m[,1:2] # sub matrix of first two columns
24 :     m[-(1:6),, drop=FALSE] # not the first 6 rows, i.e. only the 7th
25 : maechler 1331 m[integer(0),] #-> 0 x 4 Matrix
26 :     m[2:4, numeric(0)] #-> 3 x 0 Matrix
27 : maechler 687
28 : maechler 873 ## logical indexing
29 :     stopifnot(identical(m[2,3], m[(1:nrow(m)) == 2, (1:ncol(m)) == 3]),
30 :     identical(m[2,], m[(1:nrow(m)) == 2, ]),
31 :     identical(m[,3:4], m[, (1:4) >= 3]))
32 :    
33 : maechler 886 ## dimnames indexing:
34 :     mn <- m
35 :     dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),
36 :     LETTERS[1:ncol(mn)])
37 :     mn["rd", "D"]
38 : maechler 1226 stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,
39 :     identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,
40 : maechler 886 identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])
41 :     )
42 : maechler 873
43 : maechler 886 mo <- m
44 :     m[2,3] <- 100
45 :     m[1:2, 4] <- 200
46 :     m[, 1] <- -1
47 :     m[1:3,]
48 :    
49 : maechler 1673 m. <- as.matrix(m)
50 :    
51 :     ## m[ cbind(i,j) ] indexing:
52 :     ij <- cbind(1:6, 2:3)
53 :     stopifnot(identical(m[ij], m.[ij]))
54 :    
55 : maechler 1599 ## testing operations on logical Matrices rather more than indexing:
56 : maechler 1226 g10 <- m [ m > 10 ]
57 :     stopifnot(18 == length(g10))
58 : maechler 1362 stopifnot(10 == length(m[ m <= 10 ]))
59 : maechler 1599 sel <- (20 < m) & (m < 150)
60 : maechler 1655 sel.<- (20 < m.)& (m.< 150)
61 : maechler 1599 nsel <-(20 >= m) | (m >= 150)
62 : maechler 1575 (ssel <- as(sel, "sparseMatrix"))
63 :     stopifnot(is(sel, "lMatrix"), is(ssel, "lsparseMatrix"),
64 : maechler 1655 identical3(as.mat(sel.), as.mat(sel), as.mat(ssel)),
65 :     identical3(!sel, !ssel, nsel), # !<sparse> is typically dense
66 : maechler 1599 identical3(m[ sel], m[ ssel], as.matrix(m)[as.matrix( ssel)]),
67 :     identical3(m[!sel], m[!ssel], as.matrix(m)[as.matrix(!ssel)])
68 : maechler 1575 )
69 : maechler 873
70 : maechler 1575 ## more sparse Matrices --------------------------------------
71 : maechler 873
72 : maechler 687 m <- 1:800
73 :     set.seed(101) ; m[sample(800, 600)] <- 0
74 :     m <- Matrix(m, nrow = 40)
75 :     mm <- as(m, "matrix")
76 :     dimnames(mm) <- NULL ## << workaround: as(<sparse>, "matrix") has NULL dimnames
77 :     str(mC <- as(m, "dgCMatrix"))
78 :     str(mT <- as(m, "dgTMatrix"))
79 :     stopifnot(identical(mT, as(mC, "dgTMatrix")),
80 :     identical(mC, as(mT, "dgCMatrix")))
81 :    
82 :     mC[,1]
83 :     mC[1:2,]
84 : maechler 925 mC[7, drop = FALSE]
85 : maechler 1226 assert.EQ.mat(mC[1:2,], mm[1:2,])
86 : maechler 1799
87 : maechler 1833 ## *repeated* (aka 'duplicated') indices - did not work at all ...
88 : maechler 1799 i <- rep(8:10,2)
89 :     j <- c(2:4, 4:3)
90 :     assert.EQ.mat(mC[i,], mm[i,])
91 :     assert.EQ.mat(mC[,j], mm[,j])
92 : maechler 1829 assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])
93 : maechler 1833 assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])
94 : maechler 1829 assert.EQ.mat(mC[i,j], mm[i,j])
95 :     set.seed(7)
96 :     for(n in 1:50) {
97 :     i <- sample(sample(nrow(mC), 7), 20, replace = TRUE)
98 :     j <- sample(sample(ncol(mC), 6), 17, replace = TRUE)
99 :     assert.EQ.mat(mC[i,j], mm[i,j])
100 :     }
101 : maechler 1834
102 :     ##---- Symmetric indexing of symmetric Matrix ----------
103 : maechler 1835 m. <- mC; m.[, c(2, 7:12)] <- 0
104 : maechler 1833 validObject(S <- crossprod(add.simpleDimnames(m.) %% 100))
105 : maechler 1829 ss <- as(S, "matrix")
106 :     T <- as(S, "TsparseMatrix")
107 : maechler 1833 ## non-repeated indices:
108 : maechler 1829 i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])
109 : maechler 1833 N <- nrow(T)
110 :     set.seed(11)
111 :     for(n in 1:50) {
112 :     i <- sample(N, max(2, sample(N,1)), replace = FALSE)
113 :     validObject(Tii <- T[i,i])
114 :     stopifnot(is(Tii, "dsTMatrix"), # remained symmetric Tsparse
115 :     identical(t(Tii), t(T)[i,i]))
116 :     assert.EQ.mat(Tii, ss[i,i])
117 :     }
118 :    
119 : maechler 1834 ## repeated ones ``the challenge'' (to do smartly):
120 : maechler 1833 j <- c(4, 4, 9, 12, 9, 4, 17, 3, 18, 4, 12, 18, 4, 9)
121 : maechler 1829 assert.EQ.mat(T[j,j], ss[j,j])
122 : maechler 1834 ## and another two sets (a, A) & (a., A.) :
123 :     a <- matrix(0, 6,6)
124 :     a[upper.tri(a)] <- (utr <- c(2, 0,-1, 0,0,5, 7,0,0,0, 0,0,-2,0,8))
125 :     ta <- t(a); ta[upper.tri(a)] <- utr; a <- t(ta)
126 :     diag(a) <- c(0,3,0,4,6,0)
127 :     A <- as(Matrix(a), "TsparseMatrix")
128 :     A. <- A
129 :     diag(A.) <- 10 * (1:6)
130 :     a. <- as(A., "matrix")
131 :     ## More testing {this was not working for a long time..}
132 :     set.seed(1)
133 :     for(n in 1:100) {
134 :     i <- sample(1:nrow(A), 3+2*rpois(1, lam=3), replace=TRUE)
135 :     Aii <- A[i,i]
136 :     A.ii <- A.[i,i]
137 :     stopifnot(class(Aii) == class(A),
138 :     class(A.ii) == class(A.))
139 :     assert.EQ.mat(Aii , a [i,i])
140 :     assert.EQ.mat(A.ii, a.[i,i])
141 :     assert.EQ.mat(T[i,i], ss[i,i])
142 :     }
143 : maechler 1799
144 : maechler 1829
145 : maechler 1673 stopifnot(all.equal(mC[,3], mm[,3]),
146 :     identical(mC[ij], mm[ij]))
147 : maechler 1226 assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
148 :    
149 : maechler 1331 stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
150 :     dim(mC[, integer(0)]) == c(40,0),
151 :     identical(mC[, integer(0)], mC[, FALSE]),
152 :     identical(mC[7, drop = FALSE],
153 : maechler 925 mC[7,, drop = FALSE]))
154 : maechler 1269 validObject(print(mT[,c(2,4)]))
155 :     stopifnot(all.equal(mT[2,], mm[2,]),
156 :     ## row or column indexing in combination with t() :
157 :     identical(mT[2,], t(mT)[,2]),
158 :     identical(mT[-2,], t(t(mT)[,-2])),
159 :     identical(mT[c(2,5),], t(t(mT)[,c(2,5)]))
160 :     )
161 : maechler 1226 assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
162 : maechler 687 stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
163 :     identical3(mm[3,], mC[3,], mT[3,]),
164 :     identical3(mT[2,3], mC[2,3], 0),
165 :     identical(mT[], mT),
166 : maechler 1665 identical4( mm[c(3,7), 2:4], as.mat( m[c(3,7), 2:4]),
167 :     as.mat(mT[c(3,7), 2:4]), as.mat(mC[c(3,7), 2:4]))
168 : bates 1367 )
169 : maechler 687
170 : maechler 1331 x.x <- crossprod(mC)
171 :     stopifnot(class(x.x) == "dsCMatrix",
172 : maechler 1673 class(x.x. <- round(x.x / 10000)) == "dsCMatrix",
173 :     identical(x.x[cbind(2:6, 2:6)],
174 :     diag(x.x [2:6, 2:6])))
175 : maechler 1331 head(x.x.) # Note the *non*-structural 0's printed as "0"
176 : maechler 1575 tail(x.x., -3) # all but the first three lines
177 : maechler 1331
178 :     lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
179 : maechler 1665 (l10 <- lx.x[1:10, 1:10])# "lsC"
180 :     (l3 <- lx.x[1:3, ])
181 : maechler 2115 m.x <- as.mat(x.x) # as.mat() *drops* (NULL,NULL) dimnames
182 : maechler 1665 stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
183 :     identical(as.mat(lx.x), m.x != 0),
184 :     identical(as.logical(lx.x), as.logical(m.x)),
185 :     identical(as.mat(l10), m.x[1:10, 1:10] != 0),
186 :     identical(as.mat(l3 ), m.x[1:3, ] != 0)
187 :     )
188 : maechler 1331
189 : maechler 1833 ##-- Sub*assignment* with repeated / duplicated index:
190 :     A <- Matrix(0,4,3) ; A[c(1,2,1), 2] <- 1 ; A
191 : maechler 2062 B <- A; B[c(1,2,1), 2] <- 1:3; B; B. <- B
192 :     B.[3,] <- rbind(4:2)
193 :     diag(B.) <- 10 * diag(B.)
194 :     C <- B.; C[,2] <- C[,2]; C[1,] <- C[1,]; C[2:3,2:1] <- C[2:3,2:1]
195 : maechler 1833 stopifnot(identical(unname(as.matrix(A)),
196 :     local({a <- matrix(0,4,3); a[c(1,2,1), 2] <- 1 ; a})),
197 :     identical(unname(as.matrix(B)),
198 : maechler 2062 local({a <- matrix(0,4,3); a[c(1,2,1), 2] <- 1:3; a})),
199 :     identical(C, drop0(B.)))
200 : maechler 1833
201 :    
202 : maechler 1705 ## used to fail
203 :     n <- 5 ## or much larger
204 :     sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),
205 :     Dim=as.integer(c(n,n)), x = 1)
206 :     (cm <- as(sm, "CsparseMatrix"))
207 :     sm[2,]
208 :     stopifnot(sm[2,] == c(0:1, rep.int(0,ncol(sm)-2)),
209 :     sm[2,] == cm[2,],
210 :     sm[,3] == sm[3,],
211 :     all(sm[,-(1:3)] == t(sm[-(1:3),])), # all(<lge.>)
212 :     all(sm[,-(1:3)] == 0)
213 :     )
214 : maechler 1665
215 : maechler 1710 ### Diagonal -- Sparse:
216 :     m0 <- Diagonal(5)
217 :     (m1 <- as(m0, "sparseMatrix")) # dtTMatrix
218 :     (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)
219 : maechler 1799 m1g <- as(m1, "generalMatrix")
220 :     stopifnot(is(m1g, "dgTMatrix"))
221 :     assert.EQ.mat(m2[1:3,], diag(5)[1:3,])
222 :     assert.EQ.mat(m2[,c(4,1)], diag(5)[,c(4,1)])
223 :     stopifnot(identical(m2[1:3,], as(m1[1:3,], "CsparseMatrix")),
224 :     identical(Matrix:::uniqTsparse(m1[, c(4,2)]),
225 :     Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))
226 :     )## failed in 0.9975-11
227 : maechler 1705
228 : maechler 1710 M <- m0; M[1,] <- 0
229 :     stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
230 :     M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3)
231 :     validObject(M)
232 :     M <- m0; M[1:3, 3] <- 0 ;M
233 :     T <- m0; T[1:3, 3] <- 10
234 :     stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
235 :     is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))
236 :    
237 :     M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
238 :     M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
239 :     validObject(M)
240 :     M <- m1; M[1:3, 3] <- 0 ;M
241 :     assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
242 :     T <- m1; T[1:3, 3] <- 10; validObject(T)
243 :     stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))
244 :    
245 :     M <- m2; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
246 :     M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
247 :     validObject(M)
248 :     M <- m2; M[1:3, 3] <- 0 ;M
249 :     assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
250 :     T <- m2; T[1:3, 3] <- 10; validObject(T)
251 :     stopifnot(is(T, "dtCMatrix"), identical(T[,3], c(10,10,10,0,0)))
252 :    
253 :    
254 : maechler 2098 ## "Vector indices" -------------------
255 : maechler 2096 D <- Diagonal(6)
256 : maechler 2098 M <- as(D,"dgeMatrix")
257 : maechler 2096 m <- as(D,"matrix")
258 : maechler 2098 s <- as(D,"TsparseMatrix")
259 :     S <- as(s,"CsparseMatrix")
260 :     i <- c(3,1,6); v <- c(10,15,20)
261 :     ## (logical,value) which both are recycled:
262 :     L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)
263 : maechler 2096
264 : maechler 2098 ## vector subassignment, both with integer & logical
265 :     ## these now work correctly {though not very efficiently; hence warnings}
266 :     m[i] <- v # the role model: only first column is affected
267 :     M[i] <- v; assert.EQ.mat(M,m) # dge
268 :     D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT
269 :     s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT
270 :     S[i] <- v; assert.EQ.mat(S,m); S # dtC -> dtT -> dgT -> dgC
271 :     ## logical
272 :     m[L] <- z
273 :     M[L] <- z; assert.EQ.mat(M,m)
274 :     D[L] <- z; assert.EQ.mat(D,m)
275 :     s[L] <- z; assert.EQ.mat(s,m)
276 :     S[L] <- z; assert.EQ.mat(S,m) ; S
277 : maechler 2096
278 : maechler 2098 ## indexing [i] vs [i,] --- now ok
279 :     stopifnot(identical4(m[i], M[i], D[i], s[i]), identical(s[i],S[i]))
280 :     stopifnot(identical4(m[L], M[L], D[L], s[L]), identical(s[L],S[L]))
281 :     assert.EQ.mat(D[i,], m[i,])
282 :     assert.EQ.mat(M[i,], m[i,])
283 :     assert.EQ.mat(s[i,], m[i,])
284 :     assert.EQ.mat(S[i,], m[i,])
285 : maechler 2096
286 : maechler 2098 assert.EQ.mat(D[,i], m[,i])
287 :     assert.EQ.mat(M[,i], m[,i])
288 :     assert.EQ.mat(s[,i], m[,i])
289 :     assert.EQ.mat(S[,i], m[,i])
290 :    
291 :    
292 : maechler 925 ## --- negative indices ----------
293 :     mc <- mC[1:5, 1:7]
294 :     mt <- mT[1:5, 1:7]
295 :     ## sub matrix
296 : maechler 1226 assert.EQ.mat(mC[1:2, 0:3], mm[1:2, 0:3]) # test 0-index
297 : maechler 925 stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),
298 : maechler 1226 identical(mt[-(3:5), 0:2], mT[1:2, 0:2]),
299 :     identical(mC[2:3, 4], mm[2:3, 4]))
300 :     assert.EQ.mat(mC[1:2,], mm[1:2,])
301 : maechler 925 ## sub vector
302 :     stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],
303 :     mt[-(1:4), ], mT[5, 1:7]))
304 :     stopifnot(identical4(mc[-(1:4), -(2:4)], mC[5, c(1,5:7)],
305 :     mt[-(1:4), -(2:4)], mT[5, c(1,5:7)]))
306 :    
307 :     ## mixing of negative and positive must give error
308 :     assertError(mT[-1:1,])
309 :    
310 : maechler 1226 ## Sub *Assignment* ---- now works (partially):
311 :     mt0 <- mt
312 :     mt[1, 4] <- -99
313 :     mt[2:3, 1:6] <- 0
314 :     mt
315 :     m2 <- mt+mt
316 :     m2[1,4] <- -200
317 :     m2[c(1,3), c(5:6,2)] <- 1:6
318 :     stopifnot(m2[1,4] == -200,
319 :     as.vector(m2[c(1,3), c(5:6,2)]) == 1:6)
320 :     mt[,3] <- 30
321 :     mt[2:3,] <- 250
322 :     mt[1:5 %% 2 == 1, 3] <- 0
323 :     mt[3:1, 1:7 > 5] <- 0
324 :     mt
325 : maechler 1215
326 : maechler 1226 tt <- as(mt,"matrix")
327 :     ii <- c(0,2,5)
328 :     jj <- c(2:3,5)
329 :     tt[ii, jj] <- 1:6 # 0 is just "dropped"
330 :     mt[ii, jj] <- 1:6
331 :     assert.EQ.mat(mt, tt)
332 : maechler 1215
333 : maechler 1226 mt[1:5, 2:6]
334 :     as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
335 :    
336 : maechler 1315 mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
337 : maechler 1226 mt[2:3, 4:7] <- 33
338 :     validObject(mt)
339 :     mt
340 :    
341 : maechler 1315 mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
342 :     mc[1,4] <- 00 ; stopifnot(mc[1,4] == 00)
343 :     mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
344 :     mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
345 :    
346 : maechler 1226 mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
347 :     mt[-1, 3] <- -2:1
348 : maechler 1315 stopifnot(mc@x != 0, mt@x != 0,
349 : maechler 1707 mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier
350 : maechler 1315
351 : maechler 1707 mc0 <- mc
352 : maechler 1724 mt0 <- as(mc0, "TsparseMatrix")
353 :     m0 <- as(mc0, "matrix")
354 : maechler 1707 set.seed(1)
355 : maechler 1724 for(i in 1:50) {
356 :     mc <- mc0; mt <- mt0 ; m <- m0
357 : maechler 1707 ev <- 1:5 %% 2 == round(runif(1))# 0 or 1
358 :     j <- sample(ncol(mc), 1 + round(runif(1)))
359 :     nv <- rpois(sum(ev) * length(j), lambda = 1)
360 :     mc[ev, j] <- nv
361 : maechler 1724 m[ev, j] <- nv
362 :     mt[ev, j] <- nv
363 :     if(i %% 10 == 1) print(mc[ev,j, drop = FALSE])
364 :     stopifnot(as.vector(mc[ev, j]) == nv, ## failed earlier...
365 :     as.vector(mt[ev, j]) == nv)
366 :     validObject(mc) ; assert.EQ.mat(mc, m)
367 :     validObject(mt) ; assert.EQ.mat(mt, m)
368 : maechler 1707 }
369 :    
370 :     mc # no longer has non-structural zeros
371 : maechler 1226 mc[ii, jj] <- 1:6
372 :     mc[c(2,5), c(3,5)] <- 3.2
373 :     validObject(mc)
374 : maechler 1600 m. <- mc
375 :     mc[4,] <- 0
376 :     mc
377 : maechler 1226
378 : maechler 2110 S <- as(Diagonal(5),"sparseMatrix")
379 : maechler 1331 H <- Hilbert(9)
380 : maechler 1374 Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
381 :     (trH <- tril(Hc[1:5, 1:5]))
382 : maechler 2110 stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L",
383 :     is(S, "triangularMatrix"))
384 : maechler 1331
385 : maechler 2110 ## triangular assignment
386 :     ## the slick (but inefficient in case of sparse!) way to assign sub-diagonals:
387 :     ## equivalent to tmp <- `diag<-`(S[,-1], -2:1); S[,-1] <- tmp
388 :     ## which dispatches to (x="TsparseMatrix", i="missing",j="index", value="replValue")
389 :     diag(S[,-1]) <- -2:1 # used to give a wrong warning
390 : maechler 2113 S <- as(S,"triangularMatrix")
391 :     assert.EQ.mat(S, local({s <- diag(5); diag(s[,-1]) <- -2:1; s}))
392 : maechler 2110
393 :     trH[c(1:2,4), c(2:3,5)] <- 0 # gave an *error* upto Jan.2008
394 :     trH[ lower.tri(trH) ] <- 0 # ditto, because of callNextMethod()
395 :    
396 :     m <- Matrix(0+1:28, nrow = 4)
397 :     m[-3,c(2,4:5,7)] <- m[ 3, 1:4] <- m[1:3, 6] <- 0
398 :     mT <- as(m, "dgTMatrix")
399 :     stopifnot(identical(mT[lower.tri(mT)],
400 :     m [lower.tri(m) ]))
401 :     lM <- upper.tri(mT, diag=TRUE)
402 :     mT[lM] <- 0
403 :     m[lM] <- 0
404 :     assert.EQ.mat(mT, as(m,"matrix"))
405 :     mT[lM] <- -1:0
406 :     m[lM] <- -1:0
407 :     assert.EQ.mat(mT, as(m,"matrix"))
408 :     (mT <- drop0(mT))
409 :    
410 : maechler 1374 i <- c(1:2, 4, 6:7); j <- c(2:4,6)
411 :     H[i,j] <- 0
412 : maechler 1331 (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
413 :     Hc. <- Hc
414 : maechler 1374 Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
415 :     stopifnot(as.matrix(Hc.[i,j]) == 0)
416 : maechler 1331 Hc.[, 1:6]
417 :    
418 : maechler 1724 ## an example that failed for a long time
419 : maechler 1673 sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
420 : maechler 1825 validObject(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker
421 :     dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"
422 : maechler 1673 (s2 <- as(dm, "sparseMatrix"))
423 :     validObject(st <- as(s2, "TsparseMatrix"))
424 : maechler 1725 stopifnot(is(s2, "symmetricMatrix"),
425 :     is(st, "symmetricMatrix"))
426 : maechler 1673 validObject(s.32 <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
427 :     validObject(s2.32 <- s2[1:3,1:2])
428 :     I <- c(1,4:3)
429 :     stopifnot(is(s2.32, "generalMatrix"),
430 :     is(s.32, "generalMatrix"),
431 :     identical(as.mat(s.32), as.mat(s2.32)),
432 :     identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])),
433 :     identical4(2, dm[4,3], s2[4,3], st[4,3]),
434 :     identical3(diag(dm), diag(s2), diag(st)),
435 :     is((cI <- s2[I,I]), "dsCMatrix"),
436 :     is((tI <- st[I,I]), "dsTMatrix"),
437 :     identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI))
438 :     )
439 :    
440 :     ## now sub-assign and check for consistency
441 :     ## symmetric subassign should keep symmetry
442 :     st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))
443 :     s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))
444 : maechler 1724 ##
445 : maechler 1673 m <- as.mat(st)
446 :     m[2:1,2:1] <- 4:1
447 :     st[2:1,2:1] <- 4:1
448 :     s2[2:1,2:1] <- 4:1
449 :     stopifnot(identical(m, as.mat(st)),
450 :     1:4 == as.vector(s2[1:2,1:2]),
451 :     identical(m, as.mat(s2)))
452 :    
453 : maechler 1724 ## now a slightly different situation for 's2' (had bug)
454 :     s2 <- as(dm, "sparseMatrix")
455 :     s2[I,I] <- 0; diag(s2)[2:3] <- -(1:2)
456 :     stopifnot(is(s2,"symmetricMatrix"), diag(s2) == c(0:-2,0))
457 :     t2 <- as(s2, "TsparseMatrix")
458 :     m <- as.mat(s2)
459 :     s2[2:1,2:1] <- 4:1
460 :     t2[2:1,2:1] <- 4:1
461 :     m[2:1,2:1] <- 4:1
462 :     assert.EQ.mat(t2, m)
463 :     assert.EQ.mat(s2, m)
464 :     ## and the same (for a different s2 !)
465 :     s2[2:1,2:1] <- 4:1
466 :     t2[2:1,2:1] <- 4:1
467 :     assert.EQ.mat(t2, m)# ok
468 :     assert.EQ.mat(s2, m)# failed in 0.9975-8
469 :    
470 :    
471 : maechler 1673 ## m[cbind(i,j)] <- value:
472 :     m.[ cbind(3:5, 1:3) ] <- 1:3
473 :     stopifnot(m.[3,1] == 1, m.[4,2] == 2)
474 :     x.x[ cbind(2:6, 2:6)] <- 12:16
475 :     validObject(x.x)
476 :     stopifnot(class(x.x) == "dsCMatrix",
477 :     12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
478 : maechler 1725 (ne1 <- (mc - m.) != 0)
479 :     stopifnot(identical(ne1, 0 != abs(mc - m.)))
480 :     (ge <- m. >= mc) # contains "=" -> result is dense
481 :     ne. <- mc != m. # was wrong (+ warning)
482 :     stopifnot(identical(!(m. < mc), m. >= mc),
483 :     identical(m. < mc, as(!ge, "sparseMatrix")),
484 : maechler 2113 identical(ne., drop0(ne1)))
485 : maechler 1673
486 : maechler 1751 (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
487 :     T3 <- as(M3, "TsparseMatrix")
488 :     stopifnot(identical(drop(M3), M3),
489 :     identical4(drop(M3[,2, drop = FALSE]), M3[,2, drop = TRUE],
490 :     drop(T3[,2, drop = FALSE]), T3[,2, drop = TRUE]),
491 :     is(T3, "triangularMatrix"),
492 :     !is(T3[,2, drop=FALSE], "triangularMatrix")
493 :     )
494 : maechler 1725
495 : maechler 2113 M <- Diagonal(4); M[1,2] <- 2
496 :     M. <- as(M, "CsparseMatrix")
497 :     (R <- as(M., "RsparseMatrix"))
498 :     stopifnot(is(M, "triangularMatrix"),
499 :     is(M.,"triangularMatrix"),
500 :     is(R, "triangularMatrix"))
501 :     stopifnot(dim(M[2:3, FALSE]) == c(2,0),
502 :     dim(R[2:3, FALSE]) == c(2,0),
503 :     identical(M [2:3,TRUE], M [2:3,]),
504 :     identical(M.[2:3,TRUE], M.[2:3,]),
505 :     identical(R [2:3,TRUE], R [2:3,]),
506 :     dim(R[FALSE, FALSE]) == c(0,0))
507 :    
508 : 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