# SCM Repository

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

# Diff of /pkg/tests/indexing.R

revision 1672, Mon Nov 6 20:25:10 2006 UTC revision 1673, Mon Nov 6 20:54:26 2006 UTC
# Line 6  Line 6
6
7  ### Dense Matrices  ### Dense Matrices
8
9  m <- Matrix(1:28, nrow = 7)  m <- Matrix(1:28 +0, nrow = 7)
10  validObject(m) ; m@x <- as.double(m@x) ; validObject(m)  validObject(m)
11  stopifnot(identical(m, m[]),  stopifnot(identical(m, m[]),
12            identical(m[2, 3],  16), # simple number            identical(m[2, 3],  16), # simple number
13            identical(m[2, 3:4], c(16,23))) # simple numeric of length 2            identical(m[2, 3:4], c(16,23))) # simple numeric of length 2
# Line 44  Line 44
44  m[, 1] <- -1  m[, 1] <- -1
45  m[1:3,]  m[1:3,]
46
## testing operations on logical Matrices rather more than indexing:
47  m. <- as.matrix(m)  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    ## testing operations on logical Matrices rather more than indexing:
54  g10 <- m [ m > 10 ]  g10 <- m [ m > 10 ]
55  stopifnot(18 == length(g10))  stopifnot(18 == length(g10))
56  stopifnot(10 == length(m[ m <= 10 ]))  stopifnot(10 == length(m[ m <= 10 ]))
# Line 76  Line 81
81  mC[1:2,]  mC[1:2,]
82  mC[7,  drop = FALSE]  mC[7,  drop = FALSE]
83  assert.EQ.mat(mC[1:2,], mm[1:2,])  assert.EQ.mat(mC[1:2,], mm[1:2,])
84  stopifnot(all.equal(mC[,3],   mm[,3]))  stopifnot(all.equal(mC[,3], mm[,3]),
85              identical(mC[ij], mm[ij]))
86  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
87
88  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
# Line 102  Line 108
108
109  x.x <- crossprod(mC)  x.x <- crossprod(mC)
110  stopifnot(class(x.x) == "dsCMatrix",  stopifnot(class(x.x) == "dsCMatrix",
111            class(x.x. <- round(x.x / 10000)) == "dsCMatrix")            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  head(x.x.) # Note the *non*-structural 0's printed as "0"  head(x.x.) # Note the *non*-structural 0's printed as "0"
115  tail(x.x., -3) # all but the first three lines  tail(x.x., -3) # all but the first three lines
116
# Line 203  Line 211
211  stopifnot(as.matrix(Hc.[i,j]) == 0)  stopifnot(as.matrix(Hc.[i,j]) == 0)
212  Hc.[, 1:6]  Hc.[, 1:6]
213
214    ## an example that failed long
215    sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
216    validObject(dm <- kronecker(Diagonal(2), sy3))
217    (s2 <- as(dm, "sparseMatrix"))
218    validObject(st <- as(s2, "TsparseMatrix"))
219    validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
220    validObject(s2.32 <- s2[1:3,1:2])
221    I <- c(1,4:3)
222    stopifnot(is(s2.32, "generalMatrix"),
223              is(s.32,  "generalMatrix"),
224              identical(as.mat(s.32), as.mat(s2.32)),
225              identical3(dm[1:3,-1], asD(s2[1:3,-1]), asD(st[1:3,-1])),
226              identical4(2, dm[4,3], s2[4,3], st[4,3]),
227              identical3(diag(dm), diag(s2), diag(st)),
228              is((cI <- s2[I,I]), "dsCMatrix"),
229              is((tI <- st[I,I]), "dsTMatrix"),
230              identical4(as.mat(dm)[I,I], as.mat(dm[I,I]), as.mat(tI), as.mat(cI))
231              )
232
233    ## now sub-assign  and check for consistency
234    ## symmetric subassign should keep symmetry
235    st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))
236    s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))
237
238    m <- as.mat(st)
239     m[2:1,2:1] <- 4:1
240    st[2:1,2:1] <- 4:1
241    s2[2:1,2:1] <- 4:1
242    stopifnot(identical(m, as.mat(st)),
243              1:4 == as.vector(s2[1:2,1:2]),
244              identical(m, as.mat(s2)))
245
246    ## m[cbind(i,j)] <- value:
247    m.[ cbind(3:5, 1:3) ] <- 1:3
248    stopifnot(m.[3,1] == 1, m.[4,2] == 2)
249    x.x[ cbind(2:6, 2:6)] <- 12:16
250    validObject(x.x)
251    stopifnot(class(x.x) == "dsCMatrix",
252              12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
253
254  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''

Legend:
 Removed from v.1672 changed lines Added in v.1673

 root@r-forge.r-project.org ViewVC Help Powered by ViewVC 1.0.0
Thanks to: