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

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

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