# SCM Repository

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

# Diff of /pkg/tests/indexing.R

revision 1710, Tue Dec 26 15:57:06 2006 UTC revision 1724, Sat Jan 13 21:06:51 2007 UTC
# Line 169  Line 169
169  stopifnot(is(T, "dtCMatrix"), identical(T[,3], c(10,10,10,0,0)))  stopifnot(is(T, "dtCMatrix"), identical(T[,3], c(10,10,10,0,0)))
170
171

172  ## --- negative indices ----------  ## --- negative indices ----------
173  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
174  mt <- mT[1:5, 1:7]  mt <- mT[1:5, 1:7]
# Line 230  Line 229
229            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier
230
231  mc0 <- mc  mc0 <- mc
232    mt0 <- as(mc0, "TsparseMatrix")
233    m0  <- as(mc0, "matrix")
234  set.seed(1)  set.seed(1)
235  for(i in 1:20) {  for(i in 1:50) {
236      mc <- mc0      mc <- mc0; mt <- mt0 ; m <- m0
237      ev <- 1:5 %% 2 == round(runif(1))# 0 or 1      ev <- 1:5 %% 2 == round(runif(1))# 0 or 1
238      j <- sample(ncol(mc), 1 + round(runif(1)))      j <- sample(ncol(mc), 1 + round(runif(1)))
239      nv <- rpois(sum(ev) * length(j), lambda = 1)      nv <- rpois(sum(ev) * length(j), lambda = 1)
240      mc[ev, j] <- nv      mc[ev, j] <- nv
241      if(i < 5) print(mc[ev,j, drop = FALSE])       m[ev, j] <- nv
242      stopifnot(as.vector(mc[ev, j]) == nv) ## failed earlier...      mt[ev, j] <- nv
243      validObject(mc)      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  }  }
249
250  mc # no longer has non-structural zeros  mc # no longer has non-structural zeros
# Line 263  Line 268
268  stopifnot(as.matrix(Hc.[i,j]) == 0)  stopifnot(as.matrix(Hc.[i,j]) == 0)
269  Hc.[, 1:6]  Hc.[, 1:6]
270
271  ## an example that failed long  ## an example that failed for a long time
272  sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))  sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7))
273  validObject(dm <- kronecker(Diagonal(2), sy3))  validObject(dm <- kronecker(Diagonal(2), sy3))
274  (s2 <- as(dm, "sparseMatrix"))  (s2 <- as(dm, "sparseMatrix"))
# Line 286  Line 291
291  ## symmetric subassign should keep symmetry  ## symmetric subassign should keep symmetry
292  st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))  st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))
293  s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))  s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))
294    ##
295  m <- as.mat(st)  m <- as.mat(st)
296   m[2:1,2:1] <- 4:1   m[2:1,2:1] <- 4:1
297  st[2:1,2:1] <- 4:1  st[2:1,2:1] <- 4:1
# Line 295  Line 300
300            1:4 == as.vector(s2[1:2,1:2]),            1:4 == as.vector(s2[1:2,1:2]),
301            identical(m, as.mat(s2)))            identical(m, as.mat(s2)))
302
303    ## 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  ## m[cbind(i,j)] <- value:  ## m[cbind(i,j)] <- value:
322  m.[ cbind(3:5, 1:3) ] <- 1:3  m.[ cbind(3:5, 1:3) ] <- 1:3
323  stopifnot(m.[3,1] == 1, m.[4,2] == 2)  stopifnot(m.[3,1] == 1, m.[4,2] == 2)

Legend:
 Removed from v.1710 changed lines Added in v.1724