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

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