# SCM Repository

[matrix] Annotation of /pkg/tests/indexing.R
 [matrix] / pkg / tests / indexing.R

# Annotation of /pkg/tests/indexing.R

 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 : maechler 873 ### Dense Matrices 8 : 9 : maechler 1673 m <- Matrix(1:28 +0, nrow = 7) 10 : validObject(m) 11 : maechler 873 stopifnot(identical(m, m[]), 12 : identical(m[2, 3], 16), # simple number 13 : identical(m[2, 3:4], c(16,23))) # simple numeric of length 2 14 : maechler 687 15 : maechler 873 m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix' 16 : m[-(4:7), 3:4] # dito; the upper right corner of 'm' 17 : maechler 687 18 : maechler 873 ## rows or columns only: 19 : m[1,] # first row, as simple numeric vector 20 : m[,2] # 2nd column 21 : m[,1:2] # sub matrix of first two columns 22 : m[-(1:6),, drop=FALSE] # not the first 6 rows, i.e. only the 7th 23 : maechler 1331 m[integer(0),] #-> 0 x 4 Matrix 24 : m[2:4, numeric(0)] #-> 3 x 0 Matrix 25 : maechler 687 26 : maechler 873 ## logical indexing 27 : stopifnot(identical(m[2,3], m[(1:nrow(m)) == 2, (1:ncol(m)) == 3]), 28 : identical(m[2,], m[(1:nrow(m)) == 2, ]), 29 : identical(m[,3:4], m[, (1:4) >= 3])) 30 : 31 : maechler 886 ## dimnames indexing: 32 : mn <- m 33 : dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""), 34 : LETTERS[1:ncol(mn)]) 35 : mn["rd", "D"] 36 : maechler 1226 stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24, 37 : identical(mn[, "A"], mn[,1]), mn[,1] == 1:7, 38 : maechler 886 identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2]) 39 : ) 40 : maechler 873 41 : maechler 886 mo <- m 42 : m[2,3] <- 100 43 : m[1:2, 4] <- 200 44 : m[, 1] <- -1 45 : m[1:3,] 46 : 47 : maechler 1673 m. <- as.matrix(m) 48 : 49 : ## m[ cbind(i,j) ] indexing: 50 : ij <- cbind(1:6, 2:3) 51 : stopifnot(identical(m[ij], m.[ij])) 52 : 53 : maechler 1599 ## testing operations on logical Matrices rather more than indexing: 54 : maechler 1226 g10 <- m [ m > 10 ] 55 : stopifnot(18 == length(g10)) 56 : maechler 1362 stopifnot(10 == length(m[ m <= 10 ])) 57 : maechler 1599 sel <- (20 < m) & (m < 150) 58 : maechler 1655 sel.<- (20 < m.)& (m.< 150) 59 : maechler 1599 nsel <-(20 >= m) | (m >= 150) 60 : maechler 1575 (ssel <- as(sel, "sparseMatrix")) 61 : stopifnot(is(sel, "lMatrix"), is(ssel, "lsparseMatrix"), 62 : maechler 1655 identical3(as.mat(sel.), as.mat(sel), as.mat(ssel)), 63 : identical3(!sel, !ssel, nsel), # ! is typically dense 64 : maechler 1599 identical3(m[ sel], m[ ssel], as.matrix(m)[as.matrix( ssel)]), 65 : identical3(m[!sel], m[!ssel], as.matrix(m)[as.matrix(!ssel)]) 66 : maechler 1575 ) 67 : maechler 873 68 : maechler 1575 ## more sparse Matrices -------------------------------------- 69 : maechler 873 70 : maechler 687 m <- 1:800 71 : set.seed(101) ; m[sample(800, 600)] <- 0 72 : m <- Matrix(m, nrow = 40) 73 : mm <- as(m, "matrix") 74 : dimnames(mm) <- NULL ## << workaround: as(, "matrix") has NULL dimnames 75 : str(mC <- as(m, "dgCMatrix")) 76 : str(mT <- as(m, "dgTMatrix")) 77 : stopifnot(identical(mT, as(mC, "dgTMatrix")), 78 : identical(mC, as(mT, "dgCMatrix"))) 79 : 80 : mC[,1] 81 : mC[1:2,] 82 : maechler 925 mC[7, drop = FALSE] 83 : maechler 1226 assert.EQ.mat(mC[1:2,], mm[1:2,]) 84 : maechler 1673 stopifnot(all.equal(mC[,3], mm[,3]), 85 : identical(mC[ij], mm[ij])) 86 : maechler 1226 assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE]) 87 : 88 : maechler 1331 stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings 89 : dim(mC[, integer(0)]) == c(40,0), 90 : identical(mC[, integer(0)], mC[, FALSE]), 91 : identical(mC[7, drop = FALSE], 92 : maechler 925 mC[7,, drop = FALSE])) 93 : maechler 1269 validObject(print(mT[,c(2,4)])) 94 : stopifnot(all.equal(mT[2,], mm[2,]), 95 : ## row or column indexing in combination with t() : 96 : identical(mT[2,], t(mT)[,2]), 97 : identical(mT[-2,], t(t(mT)[,-2])), 98 : identical(mT[c(2,5),], t(t(mT)[,c(2,5)])) 99 : ) 100 : maechler 1226 assert.EQ.mat(mT[4,, drop = FALSE], mm[4,, drop = FALSE]) 101 : maechler 687 stopifnot(identical3(mm[,1], mC[,1], mT[,1]), 102 : identical3(mm[3,], mC[3,], mT[3,]), 103 : identical3(mT[2,3], mC[2,3], 0), 104 : identical(mT[], mT), 105 : maechler 1665 identical4( mm[c(3,7), 2:4], as.mat( m[c(3,7), 2:4]), 106 : as.mat(mT[c(3,7), 2:4]), as.mat(mC[c(3,7), 2:4])) 107 : bates 1367 ) 108 : maechler 687 109 : maechler 1331 x.x <- crossprod(mC) 110 : stopifnot(class(x.x) == "dsCMatrix", 111 : maechler 1673 class(x.x. <- round(x.x / 10000)) == "dsCMatrix", 112 : identical(x.x[cbind(2:6, 2:6)], 113 : diag(x.x [2:6, 2:6]))) 114 : maechler 1331 head(x.x.) # Note the *non*-structural 0's printed as "0" 115 : maechler 1575 tail(x.x., -3) # all but the first three lines 116 : maechler 1331 117 : lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0 118 : maechler 1665 (l10 <- lx.x[1:10, 1:10])# "lsC" 119 : (l3 <- lx.x[1:3, ]) 120 : m.x <- as(x.x, "matrix") 121 : stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric ! 122 : identical(as.mat(lx.x), m.x != 0), 123 : identical(as.logical(lx.x), as.logical(m.x)), 124 : identical(as.mat(l10), m.x[1:10, 1:10] != 0), 125 : identical(as.mat(l3 ), m.x[1:3, ] != 0) 126 : ) 127 : maechler 1331 128 : maechler 1705 ## used to fail 129 : n <- 5 ## or much larger 130 : sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1), 131 : Dim=as.integer(c(n,n)), x = 1) 132 : (cm <- as(sm, "CsparseMatrix")) 133 : sm[2,] 134 : stopifnot(sm[2,] == c(0:1, rep.int(0,ncol(sm)-2)), 135 : sm[2,] == cm[2,], 136 : sm[,3] == sm[3,], 137 : all(sm[,-(1:3)] == t(sm[-(1:3),])), # all() 138 : all(sm[,-(1:3)] == 0) 139 : ) 140 : maechler 1665 141 : maechler 1710 ### Diagonal -- Sparse: 142 : m0 <- Diagonal(5) 143 : (m1 <- as(m0, "sparseMatrix")) # dtTMatrix 144 : (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning) 145 : maechler 1705 146 : maechler 1710 M <- m0; M[1,] <- 0 147 : stopifnot(identical(M, Diagonal(x=c(0, rep(1,4))))) 148 : M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3) 149 : validObject(M) 150 : M <- m0; M[1:3, 3] <- 0 ;M 151 : T <- m0; T[1:3, 3] <- 10 152 : stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))), 153 : is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0))) 154 : 155 : M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0) 156 : M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3) 157 : validObject(M) 158 : M <- m1; M[1:3, 3] <- 0 ;M 159 : assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0) 160 : T <- m1; T[1:3, 3] <- 10; validObject(T) 161 : stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0))) 162 : 163 : M <- m2; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0) 164 : M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3) 165 : validObject(M) 166 : M <- m2; M[1:3, 3] <- 0 ;M 167 : assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0) 168 : T <- m2; T[1:3, 3] <- 10; validObject(T) 169 : stopifnot(is(T, "dtCMatrix"), identical(T[,3], c(10,10,10,0,0))) 170 : 171 : 172 : maechler 925 ## --- negative indices ---------- 173 : mc <- mC[1:5, 1:7] 174 : mt <- mT[1:5, 1:7] 175 : ## sub matrix 176 : maechler 1226 assert.EQ.mat(mC[1:2, 0:3], mm[1:2, 0:3]) # test 0-index 177 : maechler 925 stopifnot(identical(mc[-(3:5), 0:2], mC[1:2, 0:2]), 178 : maechler 1226 identical(mt[-(3:5), 0:2], mT[1:2, 0:2]), 179 : identical(mC[2:3, 4], mm[2:3, 4])) 180 : assert.EQ.mat(mC[1:2,], mm[1:2,]) 181 : maechler 925 ## sub vector 182 : stopifnot(identical4(mc[-(1:4), ], mC[5, 1:7], 183 : mt[-(1:4), ], mT[5, 1:7])) 184 : stopifnot(identical4(mc[-(1:4), -(2:4)], mC[5, c(1,5:7)], 185 : mt[-(1:4), -(2:4)], mT[5, c(1,5:7)])) 186 : 187 : ## mixing of negative and positive must give error 188 : assertError(mT[-1:1,]) 189 : 190 : maechler 1226 ## Sub *Assignment* ---- now works (partially): 191 : mt0 <- mt 192 : mt[1, 4] <- -99 193 : mt[2:3, 1:6] <- 0 194 : mt 195 : m2 <- mt+mt 196 : m2[1,4] <- -200 197 : m2[c(1,3), c(5:6,2)] <- 1:6 198 : stopifnot(m2[1,4] == -200, 199 : as.vector(m2[c(1,3), c(5:6,2)]) == 1:6) 200 : mt[,3] <- 30 201 : mt[2:3,] <- 250 202 : mt[1:5 %% 2 == 1, 3] <- 0 203 : mt[3:1, 1:7 > 5] <- 0 204 : mt 205 : maechler 1215 206 : maechler 1226 tt <- as(mt,"matrix") 207 : ii <- c(0,2,5) 208 : jj <- c(2:3,5) 209 : tt[ii, jj] <- 1:6 # 0 is just "dropped" 210 : mt[ii, jj] <- 1:6 211 : assert.EQ.mat(mt, tt) 212 : maechler 1215 213 : maechler 1226 mt[1:5, 2:6] 214 : as((mt0 - mt)[1:5,], "dsparseMatrix")# [1,5] and lines 2:3 215 : 216 : maechler 1315 mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0) 217 : maechler 1226 mt[2:3, 4:7] <- 33 218 : validObject(mt) 219 : mt 220 : 221 : maechler 1315 mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99) 222 : mc[1,4] <- 00 ; stopifnot(mc[1,4] == 00) 223 : mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99) 224 : mc[1:2,4:3] <- 4:1; stopifnot(as.matrix(mc[1:2,4:3]) == 4:1) 225 : 226 : maechler 1226 mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled 227 : mt[-1, 3] <- -2:1 228 : maechler 1315 stopifnot(mc@x != 0, mt@x != 0, 229 : maechler 1707 mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier 230 : maechler 1315 231 : maechler 1707 mc0 <- mc 232 : maechler 1724 mt0 <- as(mc0, "TsparseMatrix") 233 : m0 <- as(mc0, "matrix") 234 : maechler 1707 set.seed(1) 235 : maechler 1724 for(i in 1:50) { 236 : mc <- mc0; mt <- mt0 ; m <- m0 237 : maechler 1707 ev <- 1:5 %% 2 == round(runif(1))# 0 or 1 238 : j <- sample(ncol(mc), 1 + round(runif(1))) 239 : nv <- rpois(sum(ev) * length(j), lambda = 1) 240 : mc[ev, j] <- nv 241 : maechler 1724 m[ev, j] <- nv 242 : mt[ev, j] <- nv 243 : if(i %% 10 == 1) print(mc[ev,j, drop = FALSE]) 244 : stopifnot(as.vector(mc[ev, j]) == nv, ## failed earlier... 245 : as.vector(mt[ev, j]) == nv) 246 : validObject(mc) ; assert.EQ.mat(mc, m) 247 : validObject(mt) ; assert.EQ.mat(mt, m) 248 : maechler 1707 } 249 : 250 : mc # no longer has non-structural zeros 251 : maechler 1226 mc[ii, jj] <- 1:6 252 : mc[c(2,5), c(3,5)] <- 3.2 253 : validObject(mc) 254 : maechler 1600 m. <- mc 255 : mc[4,] <- 0 256 : mc 257 : maechler 1226 258 : maechler 1331 H <- Hilbert(9) 259 : maechler 1374 Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ... 260 : (trH <- tril(Hc[1:5, 1:5])) 261 : stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L") 262 : maechler 1331 263 : maechler 1374 i <- c(1:2, 4, 6:7); j <- c(2:4,6) 264 : H[i,j] <- 0 265 : maechler 1331 (H. <- round(as(H, "sparseMatrix"), 3)[ , 2:7]) 266 : Hc. <- Hc 267 : maechler 1374 Hc.[i,j] <- 0 ## now "works", but setting "non-structural" 0s 268 : stopifnot(as.matrix(Hc.[i,j]) == 0) 269 : maechler 1331 Hc.[, 1:6] 270 : 271 : maechler 1724 ## an example that failed for a long time 272 : maechler 1673 sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7)) 273 : validObject(dm <- kronecker(Diagonal(2), sy3)) 274 : (s2 <- as(dm, "sparseMatrix")) 275 : validObject(st <- as(s2, "TsparseMatrix")) 276 : validObject(s.32 <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix 277 : validObject(s2.32 <- s2[1:3,1:2]) 278 : I <- c(1,4:3) 279 : stopifnot(is(s2.32, "generalMatrix"), 280 : is(s.32, "generalMatrix"), 281 : identical(as.mat(s.32), as.mat(s2.32)), 282 : identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])), 283 : identical4(2, dm[4,3], s2[4,3], st[4,3]), 284 : identical3(diag(dm), diag(s2), diag(st)), 285 : is((cI <- s2[I,I]), "dsCMatrix"), 286 : is((tI <- st[I,I]), "dsTMatrix"), 287 : identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI)) 288 : ) 289 : 290 : ## now sub-assign and check for consistency 291 : ## symmetric subassign should keep symmetry 292 : st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix")) 293 : s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix")) 294 : maechler 1724 ## 295 : maechler 1673 m <- as.mat(st) 296 : m[2:1,2:1] <- 4:1 297 : st[2:1,2:1] <- 4:1 298 : s2[2:1,2:1] <- 4:1 299 : stopifnot(identical(m, as.mat(st)), 300 : 1:4 == as.vector(s2[1:2,1:2]), 301 : identical(m, as.mat(s2))) 302 : 303 : maechler 1724 ## now a slightly different situation for 's2' (had bug) 304 : s2 <- as(dm, "sparseMatrix") 305 : s2[I,I] <- 0; diag(s2)[2:3] <- -(1:2) 306 : stopifnot(is(s2,"symmetricMatrix"), diag(s2) == c(0:-2,0)) 307 : t2 <- as(s2, "TsparseMatrix") 308 : m <- as.mat(s2) 309 : s2[2:1,2:1] <- 4:1 310 : t2[2:1,2:1] <- 4:1 311 : m[2:1,2:1] <- 4:1 312 : assert.EQ.mat(t2, m) 313 : assert.EQ.mat(s2, m) 314 : ## and the same (for a different s2 !) 315 : s2[2:1,2:1] <- 4:1 316 : t2[2:1,2:1] <- 4:1 317 : assert.EQ.mat(t2, m)# ok 318 : assert.EQ.mat(s2, m)# failed in 0.9975-8 319 : 320 : 321 : maechler 1673 ## m[cbind(i,j)] <- value: 322 : m.[ cbind(3:5, 1:3) ] <- 1:3 323 : stopifnot(m.[3,1] == 1, m.[4,2] == 2) 324 : x.x[ cbind(2:6, 2:6)] <- 12:16 325 : validObject(x.x) 326 : stopifnot(class(x.x) == "dsCMatrix", 327 : 12:16 == as.mat(x.x)[cbind(2:6, 2:6)]) 328 : 329 : maechler 1226 cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''