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

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