SCM Repository

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

Diff of /pkg/tests/indexing.R

revision 2267, Wed Sep 17 11:55:57 2008 UTC revision 2323, Wed Jan 21 20:50:12 2009 UTC
# Line 14  Line 14
14  validObject(m)  validObject(m)
15  stopifnot(identical(m, m[]),  stopifnot(identical(m, m[]),
16            identical(m[2, 3],  16), # simple number            identical(m[2, 3],  16), # simple number
17            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
18              identical(m[NA,NA], as(Matrix(NA, 7,4), "dMatrix")))
19
20  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
21  m[-(4:7), 3:4]        # ditto; the upper right corner of 'm'  m[-(4:7), 3:4]        # ditto; the upper right corner of 'm'
# Line 52  Line 53
53  m. <- as.matrix(m)  m. <- as.matrix(m)
54
55  ## m[ cbind(i,j) ] indexing:  ## m[ cbind(i,j) ] indexing:
56  ij <- cbind(1:6, 2:3)  iN <- ij <- cbind(1:6, 2:3)
57  stopifnot(identical(m[ij], m.[ij]))  iN[2:3,] <- iN[5,2] <- NA
58    stopifnot(identical(m[ij], m.[ij]),
59              identical(m[iN], m.[iN]))
60
61  ## testing operations on logical Matrices rather more than indexing:  ## testing operations on logical Matrices rather more than indexing:
62  g10 <- m [ m > 10 ]  g10 <- m [ m > 10 ]
# Line 92  Line 95
95  j <- c(2:4, 4:3)  j <- c(2:4, 4:3)
96  assert.EQ.mat(mC[i,], mm[i,])  assert.EQ.mat(mC[i,], mm[i,])
97  assert.EQ.mat(mC[,j], mm[,j])  assert.EQ.mat(mC[,j], mm[,j])
98    ## 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  assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])  assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])
101  assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])  assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])
102  assert.EQ.mat(mC[i,j], mm[i,j])  assert.EQ.mat(mC[i,j], mm[i,j])
# Line 106  Line 111
111  m. <- mC; m.[, c(2, 7:12)] <- 0  m. <- mC; m.[, c(2, 7:12)] <- 0
113  ss <- as(S, "matrix")  ss <- as(S, "matrix")
114    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  T <- as(S, "TsparseMatrix")  T <- as(S, "TsparseMatrix")
122  ## non-repeated indices:  ## non-repeated indices:
123  i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])  i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])
124    ## 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  N <- nrow(T)  N <- nrow(T)
130  set.seed(11)  set.seed(11)
131  for(n in 1:50) {  for(n in 1:50) {
# Line 146  Line 163
163
164
165  stopifnot(all.equal(mC[,3], mm[,3]),  stopifnot(all.equal(mC[,3], mm[,3]),
166            identical(mC[ij], mm[ij]))            identical(mC[ij], mm[ij]),
167              identical(mC[iN], mm[iN]))
168
169  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
170
171  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings

Legend:
 Removed from v.2267 changed lines Added in v.2323