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 2363 - (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 : 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 : mmaechler 2363 identical (mC[7, drop=FALSE], mm[7, drop=FALSE]) # *vector* indexing
171 : maechler 1226
172 : maechler 1331 stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
173 :     dim(mC[, integer(0)]) == c(40,0),
174 : mmaechler 2363 identical(mC[, integer(0)], mC[, FALSE]))
175 : maechler 1269 validObject(print(mT[,c(2,4)]))
176 :     stopifnot(all.equal(mT[2,], mm[2,]),
177 :     ## row or column indexing in combination with t() :
178 :     identical(mT[2,], t(mT)[,2]),
179 :     identical(mT[-2,], t(t(mT)[,-2])),
180 :     identical(mT[c(2,5),], t(t(mT)[,c(2,5)]))
181 :     )
182 : maechler 1226 assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE])
183 : maechler 687 stopifnot(identical3(mm[,1], mC[,1], mT[,1]),
184 :     identical3(mm[3,], mC[3,], mT[3,]),
185 :     identical3(mT[2,3], mC[2,3], 0),
186 :     identical(mT[], mT),
187 : maechler 1665 identical4( mm[c(3,7), 2:4], as.mat( m[c(3,7), 2:4]),
188 :     as.mat(mT[c(3,7), 2:4]), as.mat(mC[c(3,7), 2:4]))
189 : bates 1367 )
190 : maechler 687
191 : maechler 1331 x.x <- crossprod(mC)
192 :     stopifnot(class(x.x) == "dsCMatrix",
193 : maechler 1673 class(x.x. <- round(x.x / 10000)) == "dsCMatrix",
194 :     identical(x.x[cbind(2:6, 2:6)],
195 :     diag(x.x [2:6, 2:6])))
196 : maechler 1331 head(x.x.) # Note the *non*-structural 0's printed as "0"
197 : maechler 1575 tail(x.x., -3) # all but the first three lines
198 : maechler 1331
199 :     lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
200 : maechler 1665 (l10 <- lx.x[1:10, 1:10])# "lsC"
201 :     (l3 <- lx.x[1:3, ])
202 : maechler 2115 m.x <- as.mat(x.x) # as.mat() *drops* (NULL,NULL) dimnames
203 : maechler 1665 stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
204 :     identical(as.mat(lx.x), m.x != 0),
205 :     identical(as.logical(lx.x), as.logical(m.x)),
206 :     identical(as.mat(l10), m.x[1:10, 1:10] != 0),
207 :     identical(as.mat(l3 ), m.x[1:3, ] != 0)
208 :     )
209 : maechler 1331
210 : maechler 1833 ##-- Sub*assignment* with repeated / duplicated index:
211 :     A <- Matrix(0,4,3) ; A[c(1,2,1), 2] <- 1 ; A
212 : maechler 2062 B <- A; B[c(1,2,1), 2] <- 1:3; B; B. <- B
213 :     B.[3,] <- rbind(4:2)
214 :     diag(B.) <- 10 * diag(B.)
215 :     C <- B.; C[,2] <- C[,2]; C[1,] <- C[1,]; C[2:3,2:1] <- C[2:3,2:1]
216 : maechler 1833 stopifnot(identical(unname(as.matrix(A)),
217 :     local({a <- matrix(0,4,3); a[c(1,2,1), 2] <- 1 ; a})),
218 :     identical(unname(as.matrix(B)),
219 : maechler 2062 local({a <- matrix(0,4,3); a[c(1,2,1), 2] <- 1:3; a})),
220 :     identical(C, drop0(B.)))
221 : maechler 1833
222 :    
223 : maechler 1705 ## used to fail
224 :     n <- 5 ## or much larger
225 :     sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),
226 :     Dim=as.integer(c(n,n)), x = 1)
227 :     (cm <- as(sm, "CsparseMatrix"))
228 :     sm[2,]
229 :     stopifnot(sm[2,] == c(0:1, rep.int(0,ncol(sm)-2)),
230 :     sm[2,] == cm[2,],
231 :     sm[,3] == sm[3,],
232 :     all(sm[,-(1:3)] == t(sm[-(1:3),])), # all(<lge.>)
233 :     all(sm[,-(1:3)] == 0)
234 :     )
235 : maechler 1665
236 : maechler 2120 m0 <- Diagonal(5)
237 :     stopifnot(identical(m0[2,], m0[,2]),
238 :     identical(m0[,1], c(1,0,0,0,0)))
239 : maechler 1710 ### Diagonal -- Sparse:
240 : mmaechler 2239 (m1 <- as(m0, "TsparseMatrix")) # dtTMatrix
241 : mmaechler 2198 (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix
242 : maechler 1799 m1g <- as(m1, "generalMatrix")
243 :     stopifnot(is(m1g, "dgTMatrix"))
244 :     assert.EQ.mat(m2[1:3,], diag(5)[1:3,])
245 :     assert.EQ.mat(m2[,c(4,1)], diag(5)[,c(4,1)])
246 :     stopifnot(identical(m2[1:3,], as(m1[1:3,], "CsparseMatrix")),
247 :     identical(Matrix:::uniqTsparse(m1[, c(4,2)]),
248 :     Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))
249 :     )## failed in 0.9975-11
250 : maechler 1705
251 : maechler 2120 (uTr <- new("dtTMatrix", Dim = c(3L,3L), diag="U"))
252 :     uTr[1,] <- 0
253 :     assert.EQ.mat(uTr, cbind(0, rbind(0,diag(2))))
254 :    
255 : maechler 1710 M <- m0; M[1,] <- 0
256 :     stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
257 :     M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3)
258 : mmaechler 2175 checkMatrix(M)
259 : maechler 1710 M <- m0; M[1:3, 3] <- 0 ;M
260 :     T <- m0; T[1:3, 3] <- 10
261 :     stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
262 :     is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))
263 :    
264 :     M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
265 :     M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
266 : mmaechler 2175 checkMatrix(M)
267 : maechler 1710 M <- m1; M[1:3, 3] <- 0 ;M
268 :     assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
269 : mmaechler 2175 T <- m1; T[1:3, 3] <- 10; checkMatrix(T)
270 : maechler 1710 stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))
271 :    
272 :     M <- m2; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
273 :     M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
274 : mmaechler 2175 checkMatrix(M)
275 : maechler 1710 M <- m2; M[1:3, 3] <- 0 ;M
276 :     assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
277 : mmaechler 2175 T <- m2; T[1:3, 3] <- 10; checkMatrix(T)
278 : maechler 1710 stopifnot(is(T, "dtCMatrix"), identical(T[,3], c(10,10,10,0,0)))
279 :    
280 :    
281 : maechler 2098 ## "Vector indices" -------------------
282 : mmaechler 2363 .iniDiag.example <- expression({
283 :     D <- Diagonal(6)
284 :     M <- as(D,"dgeMatrix")
285 :     m <- as(D,"matrix")
286 :     s <- as(D,"TsparseMatrix")
287 :     S <- as(s,"CsparseMatrix")
288 :     })
289 :     eval(.iniDiag.example)
290 : maechler 2098 i <- c(3,1,6); v <- c(10,15,20)
291 :     ## (logical,value) which both are recycled:
292 :     L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)
293 : maechler 2096
294 : maechler 2098 ## vector subassignment, both with integer & logical
295 :     ## these now work correctly {though not very efficiently; hence warnings}
296 :     m[i] <- v # the role model: only first column is affected
297 :     M[i] <- v; assert.EQ.mat(M,m) # dge
298 :     D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT
299 :     s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT
300 :     S[i] <- v; assert.EQ.mat(S,m); S # dtC -> dtT -> dgT -> dgC
301 : mmaechler 2363 stopifnot(identical(s,D))
302 : maechler 2098 ## logical
303 : mmaechler 2363 eval(.iniDiag.example)
304 : maechler 2098 m[L] <- z
305 :     M[L] <- z; assert.EQ.mat(M,m)
306 :     D[L] <- z; assert.EQ.mat(D,m)
307 :     s[L] <- z; assert.EQ.mat(s,m)
308 :     S[L] <- z; assert.EQ.mat(S,m) ; S
309 : maechler 2096
310 : maechler 2098 ## indexing [i] vs [i,] --- now ok
311 : mmaechler 2363 eval(.iniDiag.example)
312 :     stopifnot(identical5(m[i], M[i], D[i], s[i], S[i]))
313 :     stopifnot(identical5(m[L], M[L], D[L], s[L], S[L]))
314 :     ## bordercase ' drop = .' *vector* indexing {failed till 2009-04-..)
315 :     stopifnot(identical5(m[i,drop=FALSE], M[i,drop=FALSE], D[i,drop=FALSE],
316 :     s[i,drop=FALSE], S[i,drop=FALSE]))
317 :     stopifnot(identical5(m[L,drop=FALSE], M[L,drop=FALSE], D[L,drop=FALSE],
318 :     s[L,drop=FALSE], S[L,drop=FALSE]))
319 :     ##
320 : maechler 2098 assert.EQ.mat(D[i,], m[i,])
321 :     assert.EQ.mat(M[i,], m[i,])
322 :     assert.EQ.mat(s[i,], m[i,])
323 :     assert.EQ.mat(S[i,], m[i,])
324 : maechler 2096
325 : maechler 2098 assert.EQ.mat(D[,i], m[,i])
326 :     assert.EQ.mat(M[,i], m[,i])
327 :     assert.EQ.mat(s[,i], m[,i])
328 :     assert.EQ.mat(S[,i], m[,i])
329 :    
330 :    
331 : maechler 925 ## --- negative indices ----------
332 :     mc <- mC[1:5, 1:7]
333 :     mt <- mT[1:5, 1:7]
334 :     ## sub matrix
335 : maechler 1226 assert.EQ.mat(mC[1:2, 0:3], mm[1:2, 0:3]) # test 0-index
336 : maechler 925 stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]),
337 : maechler 1226 identical(mt[-(3:5), 0:2], mT[1:2, 0:2]),
338 :     identical(mC[2:3, 4], mm[2:3, 4]))
339 :     assert.EQ.mat(mC[1:2,], mm[1:2,])
340 : maechler 925 ## sub vector
341 :     stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7],
342 :     mt[-(1:4), ], mT[5, 1:7]))
343 :     stopifnot(identical4(mc[-(1:4), -(2:4)], mC[5, c(1,5:7)],
344 :     mt[-(1:4), -(2:4)], mT[5, c(1,5:7)]))
345 :    
346 :     ## mixing of negative and positive must give error
347 :     assertError(mT[-1:1,])
348 :    
349 : maechler 1226 ## Sub *Assignment* ---- now works (partially):
350 :     mt0 <- mt
351 :     mt[1, 4] <- -99
352 :     mt[2:3, 1:6] <- 0
353 :     mt
354 :     m2 <- mt+mt
355 :     m2[1,4] <- -200
356 :     m2[c(1,3), c(5:6,2)] <- 1:6
357 :     stopifnot(m2[1,4] == -200,
358 :     as.vector(m2[c(1,3), c(5:6,2)]) == 1:6)
359 :     mt[,3] <- 30
360 :     mt[2:3,] <- 250
361 :     mt[1:5 %% 2 == 1, 3] <- 0
362 :     mt[3:1, 1:7 > 5] <- 0
363 :     mt
364 : maechler 1215
365 : maechler 1226 tt <- as(mt,"matrix")
366 :     ii <- c(0,2,5)
367 :     jj <- c(2:3,5)
368 :     tt[ii, jj] <- 1:6 # 0 is just "dropped"
369 :     mt[ii, jj] <- 1:6
370 :     assert.EQ.mat(mt, tt)
371 : maechler 1215
372 : maechler 1226 mt[1:5, 2:6]
373 :     as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3
374 :    
375 : maechler 1315 mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
376 : maechler 1226 mt[2:3, 4:7] <- 33
377 : mmaechler 2175 checkMatrix(mt)
378 : maechler 1226 mt
379 :    
380 : maechler 1315 mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
381 :     mc[1,4] <- 00 ; stopifnot(mc[1,4] == 00)
382 :     mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
383 :     mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1)
384 :    
385 : maechler 1226 mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
386 :     mt[-1, 3] <- -2:1
387 : maechler 1315 stopifnot(mc@x != 0, mt@x != 0,
388 : maechler 1707 mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier
389 : maechler 1315
390 : maechler 1707 mc0 <- mc
391 : maechler 1724 mt0 <- as(mc0, "TsparseMatrix")
392 :     m0 <- as(mc0, "matrix")
393 : maechler 1707 set.seed(1)
394 : maechler 1724 for(i in 1:50) {
395 :     mc <- mc0; mt <- mt0 ; m <- m0
396 : maechler 1707 ev <- 1:5 %% 2 == round(runif(1))# 0 or 1
397 :     j <- sample(ncol(mc), 1 + round(runif(1)))
398 :     nv <- rpois(sum(ev) * length(j), lambda = 1)
399 :     mc[ev, j] <- nv
400 : maechler 1724 m[ev, j] <- nv
401 :     mt[ev, j] <- nv
402 :     if(i %% 10 == 1) print(mc[ev,j, drop = FALSE])
403 :     stopifnot(as.vector(mc[ev, j]) == nv, ## failed earlier...
404 :     as.vector(mt[ev, j]) == nv)
405 :     validObject(mc) ; assert.EQ.mat(mc, m)
406 :     validObject(mt) ; assert.EQ.mat(mt, m)
407 : maechler 1707 }
408 :    
409 :     mc # no longer has non-structural zeros
410 : maechler 1226 mc[ii, jj] <- 1:6
411 :     mc[c(2,5), c(3,5)] <- 3.2
412 : mmaechler 2175 checkMatrix(mc)
413 : maechler 1600 m. <- mc
414 :     mc[4,] <- 0
415 :     mc
416 : maechler 1226
417 : mmaechler 2239 S <- as(Diagonal(5),"TsparseMatrix")
418 : maechler 1331 H <- Hilbert(9)
419 : maechler 1374 Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
420 :     (trH <- tril(Hc[1:5, 1:5]))
421 : maechler 2110 stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L",
422 :     is(S, "triangularMatrix"))
423 : maechler 1331
424 : maechler 2110 ## triangular assignment
425 :     ## the slick (but inefficient in case of sparse!) way to assign sub-diagonals:
426 :     ## equivalent to tmp <- `diag<-`(S[,-1], -2:1); S[,-1] <- tmp
427 :     ## which dispatches to (x="TsparseMatrix", i="missing",j="index", value="replValue")
428 :     diag(S[,-1]) <- -2:1 # used to give a wrong warning
429 : maechler 2113 S <- as(S,"triangularMatrix")
430 :     assert.EQ.mat(S, local({s <- diag(5); diag(s[,-1]) <- -2:1; s}))
431 : maechler 2110
432 :     trH[c(1:2,4), c(2:3,5)] <- 0 # gave an *error* upto Jan.2008
433 :     trH[ lower.tri(trH) ] <- 0 # ditto, because of callNextMethod()
434 :    
435 :     m <- Matrix(0+1:28, nrow = 4)
436 :     m[-3,c(2,4:5,7)] <- m[ 3, 1:4] <- m[1:3, 6] <- 0
437 :     mT <- as(m, "dgTMatrix")
438 :     stopifnot(identical(mT[lower.tri(mT)],
439 :     m [lower.tri(m) ]))
440 :     lM <- upper.tri(mT, diag=TRUE)
441 :     mT[lM] <- 0
442 :     m[lM] <- 0
443 :     assert.EQ.mat(mT, as(m,"matrix"))
444 :     mT[lM] <- -1:0
445 :     m[lM] <- -1:0
446 :     assert.EQ.mat(mT, as(m,"matrix"))
447 :     (mT <- drop0(mT))
448 :    
449 : maechler 1374 i <- c(1:2, 4, 6:7); j <- c(2:4,6)
450 :     H[i,j] <- 0
451 : maechler 1331 (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7])
452 :     Hc. <- Hc
453 : maechler 1374 Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s
454 :     stopifnot(as.matrix(Hc.[i,j]) == 0)
455 : maechler 1331 Hc.[, 1:6]
456 :    
457 : maechler 1724 ## an example that failed for a long time
458 : maechler 1673 sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
459 : mmaechler 2175 checkMatrix(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker
460 : maechler 1825 dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"
461 : maechler 1673 (s2 <- as(dm, "sparseMatrix"))
462 : mmaechler 2175 checkMatrix(st <- as(s2, "TsparseMatrix"))
463 : maechler 1725 stopifnot(is(s2, "symmetricMatrix"),
464 :     is(st, "symmetricMatrix"))
465 : mmaechler 2175 checkMatrix(s.32 <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
466 :     checkMatrix(s2.32 <- s2[1:3,1:2])
467 : maechler 1673 I <- c(1,4:3)
468 :     stopifnot(is(s2.32, "generalMatrix"),
469 :     is(s.32, "generalMatrix"),
470 :     identical(as.mat(s.32), as.mat(s2.32)),
471 :     identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])),
472 :     identical4(2, dm[4,3], s2[4,3], st[4,3]),
473 :     identical3(diag(dm), diag(s2), diag(st)),
474 :     is((cI <- s2[I,I]), "dsCMatrix"),
475 :     is((tI <- st[I,I]), "dsTMatrix"),
476 :     identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI))
477 :     )
478 :    
479 :     ## now sub-assign and check for consistency
480 :     ## symmetric subassign should keep symmetry
481 : mmaechler 2175 st[I,I] <- 0; checkMatrix(st); stopifnot(is(st,"symmetricMatrix"))
482 :     s2[I,I] <- 0; checkMatrix(s2); stopifnot(is(s2,"symmetricMatrix"))
483 : maechler 1724 ##
484 : maechler 1673 m <- as.mat(st)
485 :     m[2:1,2:1] <- 4:1
486 :     st[2:1,2:1] <- 4:1
487 :     s2[2:1,2:1] <- 4:1
488 :     stopifnot(identical(m, as.mat(st)),
489 :     1:4 == as.vector(s2[1:2,1:2]),
490 :     identical(m, as.mat(s2)))
491 :    
492 : maechler 1724 ## now a slightly different situation for 's2' (had bug)
493 :     s2 <- as(dm, "sparseMatrix")
494 :     s2[I,I] <- 0; diag(s2)[2:3] <- -(1:2)
495 :     stopifnot(is(s2,"symmetricMatrix"), diag(s2) == c(0:-2,0))
496 :     t2 <- as(s2, "TsparseMatrix")
497 :     m <- as.mat(s2)
498 :     s2[2:1,2:1] <- 4:1
499 :     t2[2:1,2:1] <- 4:1
500 :     m[2:1,2:1] <- 4:1
501 :     assert.EQ.mat(t2, m)
502 :     assert.EQ.mat(s2, m)
503 :     ## and the same (for a different s2 !)
504 :     s2[2:1,2:1] <- 4:1
505 :     t2[2:1,2:1] <- 4:1
506 :     assert.EQ.mat(t2, m)# ok
507 :     assert.EQ.mat(s2, m)# failed in 0.9975-8
508 :    
509 :    
510 : maechler 1673 ## m[cbind(i,j)] <- value:
511 :     m.[ cbind(3:5, 1:3) ] <- 1:3
512 :     stopifnot(m.[3,1] == 1, m.[4,2] == 2)
513 :     x.x[ cbind(2:6, 2:6)] <- 12:16
514 : mmaechler 2175 stopifnot(isValid(x.x, "dsCMatrix"),
515 : maechler 1673 12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
516 : maechler 1725 (ne1 <- (mc - m.) != 0)
517 :     stopifnot(identical(ne1, 0 != abs(mc - m.)))
518 :     (ge <- m. >= mc) # contains "=" -> result is dense
519 :     ne. <- mc != m. # was wrong (+ warning)
520 :     stopifnot(identical(!(m. < mc), m. >= mc),
521 :     identical(m. < mc, as(!ge, "sparseMatrix")),
522 : maechler 2113 identical(ne., drop0(ne1)))
523 : maechler 1673
524 : mmaechler 2192 d6 <- Diagonal(6)
525 :     ii <- c(1:2, 4:5)
526 :     d6[cbind(ii,ii)] <- 7*ii
527 :     stopifnot(is(d6, "ddiMatrix"), identical(d6, Diagonal(x=c(7*1:2,1,7*4:5,1))))
528 :    
529 : mmaechler 2267 for(j in 3:6) { ## even and odd j used to behave differently
530 :     M <- Matrix(0, j,j); m <- matrix(0, j,j)
531 :     T <- as(M, "TsparseMatrix")
532 :     TG <- as(T, "generalMatrix")
533 :     G <- as(M, "generalMatrix")
534 :     id <- cbind(1:j,1:j)
535 :     i2 <- cbind(1:j,j:1)
536 :     m[id] <- 1:j
537 :     M[id] <- 1:j ; stopifnot(is(M,"symmetricMatrix"))
538 :     T[id] <- 1:j ; stopifnot(is(T,"symmetricMatrix"))
539 :     G[id] <- 1:j
540 :     TG[id]<- 1:j
541 :     m[i2] <- 10
542 :     M[i2] <- 10 ; stopifnot(is(M,"symmetricMatrix"))
543 :     T[i2] <- 10 ; stopifnot(is(T,"symmetricMatrix"))
544 :     G[i2] <- 10
545 :     TG[i2]<- 10
546 :     ##
547 :     assert.EQ.mat(M, m)
548 :     assert.EQ.mat(T, m)
549 :     assert.EQ.mat(G, m)
550 :     assert.EQ.mat(TG,m)
551 :     }
552 :    
553 :    
554 :     ## drop, triangular, ...
555 : maechler 1751 (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
556 :     T3 <- as(M3, "TsparseMatrix")
557 :     stopifnot(identical(drop(M3), M3),
558 :     identical4(drop(M3[,2, drop = FALSE]), M3[,2, drop = TRUE],
559 :     drop(T3[,2, drop = FALSE]), T3[,2, drop = TRUE]),
560 :     is(T3, "triangularMatrix"),
561 :     !is(T3[,2, drop=FALSE], "triangularMatrix")
562 :     )
563 : maechler 1725
564 : mmaechler 2186 (T6 <- as(as(kronecker(Matrix(c(0,0,1,0),2,2), t(T3)), "lMatrix"),
565 :     "triangularMatrix"))
566 :     T6[1:4, -(1:3)] # failed (trying to coerce back to ltTMatrix)
567 :     stopifnot(identical(T6[1:4, -(1:3)][2:3, -3],
568 :     spMatrix(2,2, i=c(1,2,2), j=c(1,1,2), x=rep(TRUE,3))))
569 :    
570 : maechler 2113 M <- Diagonal(4); M[1,2] <- 2
571 :     M. <- as(M, "CsparseMatrix")
572 :     (R <- as(M., "RsparseMatrix"))
573 : mmaechler 2175 (Ms <- symmpart(M.))
574 :     Rs <- as(Ms, "RsparseMatrix")
575 :     stopifnot(isValid(M, "triangularMatrix"),
576 :     isValid(M.,"triangularMatrix"),
577 :     isValid(Ms, "dsCMatrix"),
578 :     isValid(R, "dtRMatrix"),
579 :     isValid(Rs, "dsRMatrix") )
580 : maechler 2113 stopifnot(dim(M[2:3, FALSE]) == c(2,0),
581 :     dim(R[2:3, FALSE]) == c(2,0),
582 :     identical(M [2:3,TRUE], M [2:3,]),
583 :     identical(M.[2:3,TRUE], M.[2:3,]),
584 :     identical(R [2:3,TRUE], R [2:3,]),
585 :     dim(R[FALSE, FALSE]) == c(0,0))
586 :    
587 : mmaechler 2203 n <- 50000L
588 :     Lrg <- new("dgTMatrix", Dim = c(n,n))
589 :     diag(Lrg) <- 1:n
590 :     dLrg <- as(Lrg, "diagonalMatrix")
591 :     stopifnot(identical(Diagonal(x = 1:n), dLrg))
592 :     diag(dLrg) <- 1 + diag(dLrg)
593 :     Clrg <- as(Lrg,"CsparseMatrix")
594 :     Ctrg <- as(Clrg, "triangularMatrix")
595 :     diag(Ctrg) <- 1 + diag(Ctrg)
596 :     stopifnot(identical(Diagonal(x = 1+ 1:n), dLrg),
597 :     identical(Ctrg, as(dLrg,"CsparseMatrix")))
598 :    
599 :     cc <- capture.output(show(dLrg))# show(<diag>) used to error for large n
600 :    
601 : mmaechler 2335 ## Large Matrix indexing / subassignment
602 :     ## ------------------------------------- (from ex. by Imran Rashid)
603 : mmaechler 2341 n <- 7000000
604 :     m <- 100000
605 : mmaechler 2335 nnz <- 20000
606 :    
607 :     set.seed(12)
608 :     f <- sparseMatrix(i = sample(n, size=nnz, replace=TRUE),
609 :     j = sample(m, size=nnz, replace=TRUE))
610 :     str(f)
611 : mmaechler 2357 dim(f) # 6999863 x 99992
612 :     prod(dim(f)) # 699930301096 == 699'930'301'096 (~ 700'000 millions)
613 :     str(thisCol <- f[,5000])# logi [~ 7 mio....]
614 :     sv <- as(thisCol, "sparseVector")
615 :     str(sv) ## "empty" !
616 :     validObject(spCol <- f[,5000, drop=FALSE])
617 :     ## *not* identical(): as(spCol, "sparseVector")@length is "double"prec:
618 :     stopifnot(all.equal(as(spCol, "sparseVector"),
619 :     as(sv, "nsparseVector"), tol=0))
620 :     f[,5762] <- thisCol # now "fine" <<<<<<<<<< FIXME uses LARGE objects
621 :     ## is using replCmat() in ../R/Csparse.R, then
622 :     ## replTmat() in ../R/Tsparse.R
623 : mmaechler 2335
624 :     fx <- sparseMatrix(i = sample(n, size=nnz, replace=TRUE),
625 :     j = sample(m, size=nnz, replace=TRUE),
626 :     x = round(10*rnorm(nnz)))
627 :     class(fx)## dgCMatrix
628 :     fx[,6000] <- (tC <- rep(thisCol, length=nrow(fx)))
629 :     thCol <- fx[,2000]
630 :     fx[,5762] <- thCol
631 :     stopifnot(is(f, "ngCMatrix"), is(fx, "dgCMatrix"),
632 :     identical(thisCol, f[,5762]),# perfect
633 :     identical(as.logical(fx[,6000]), tC),
634 :     identical(thCol, fx[,5762]))
635 :    
636 : mmaechler 2175 cat('Time elapsed: ', (.pt <- proc.time()),'\n') # "stats"
637 :     ##
638 :     cat("checkMatrix() of all: \n---------\n")
639 :     Sys.setlocale("LC_COLLATE", "C")# to keep ls() reproducible
640 :     for(nm in ls()) if(is(.m <- get(nm), "Matrix")) {
641 :     cat(nm, "\n")
642 : mmaechler 2186 checkMatrix(.m, verbose = FALSE)
643 : mmaechler 2175 }
644 :     cat('Time elapsed: ', proc.time() - .pt,'\n') # "stats"
645 :    
646 :     if(!interactive()) warnings()
647 :    

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