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

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