--- pkg/tests/indexing.R 2006/12/16 12:37:25 1705 +++ pkg/tests/indexing.R 2007/01/13 21:06:51 1724 @@ -1,4 +1,4 @@ -#### For both 'Extract' ("[") and 'Replace' ("[<-") Method testing +## For both 'Extract' ("[") and 'Replace' ("[<-") Method testing library(Matrix) @@ -138,6 +138,36 @@ all(sm[,-(1:3)] == 0) ) +### Diagonal -- Sparse: +m0 <- Diagonal(5) +(m1 <- as(m0, "sparseMatrix")) # dtTMatrix +(m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning) + +M <- m0; M[1,] <- 0 +stopifnot(identical(M, Diagonal(x=c(0, rep(1,4))))) +M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3) +validObject(M) +M <- m0; M[1:3, 3] <- 0 ;M +T <- m0; T[1:3, 3] <- 10 +stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))), + is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0))) + +M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0) +M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3) +validObject(M) +M <- m1; M[1:3, 3] <- 0 ;M +assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0) +T <- m1; T[1:3, 3] <- 10; validObject(T) +stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0))) + +M <- m2; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0) +M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3) +validObject(M) +M <- m2; M[1:3, 3] <- 0 ;M +assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0) +T <- m2; T[1:3, 3] <- 10; validObject(T) +stopifnot(is(T, "dtCMatrix"), identical(T[,3], c(10,10,10,0,0))) + ## --- negative indices ---------- mc <- mC[1:5, 1:7] @@ -196,14 +226,28 @@ mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled mt[-1, 3] <- -2:1 stopifnot(mc@x != 0, mt@x != 0, - mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed + mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier -ev <- 1:5 %% 2 == 0 -mc[ev, 3] <- 0:1 -if(FALSE)## FIXME - stopifnot(mc[ev, 3] == 0:1) ##-> BUG {very peculiar; the 2nd time it works ...} -validObject(mc) -mc # now shows a non-structural zeros +mc0 <- mc +mt0 <- as(mc0, "TsparseMatrix") +m0 <- as(mc0, "matrix") +set.seed(1) +for(i in 1:50) { + mc <- mc0; mt <- mt0 ; m <- m0 + ev <- 1:5 %% 2 == round(runif(1))# 0 or 1 + j <- sample(ncol(mc), 1 + round(runif(1))) + nv <- rpois(sum(ev) * length(j), lambda = 1) + mc[ev, j] <- nv + m[ev, j] <- nv + mt[ev, j] <- nv + if(i %% 10 == 1) print(mc[ev,j, drop = FALSE]) + stopifnot(as.vector(mc[ev, j]) == nv, ## failed earlier... + as.vector(mt[ev, j]) == nv) + validObject(mc) ; assert.EQ.mat(mc, m) + validObject(mt) ; assert.EQ.mat(mt, m) +} + +mc # no longer has non-structural zeros mc[ii, jj] <- 1:6 mc[c(2,5), c(3,5)] <- 3.2 validObject(mc) @@ -224,7 +268,7 @@ stopifnot(as.matrix(Hc.[i,j]) == 0) Hc.[, 1:6] -## an example that failed long +## an example that failed for a long time sy3 <- new("dsyMatrix", Dim = as.integer(c(2, 2)), x = c(14, -1, 2, -7)) validObject(dm <- kronecker(Diagonal(2), sy3)) (s2 <- as(dm, "sparseMatrix")) @@ -247,7 +291,7 @@ ## symmetric subassign should keep symmetry st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix")) s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix")) - +## m <- as.mat(st) m[2:1,2:1] <- 4:1 st[2:1,2:1] <- 4:1 @@ -256,6 +300,24 @@ 1:4 == as.vector(s2[1:2,1:2]), identical(m, as.mat(s2))) +## now a slightly different situation for 's2' (had bug) +s2 <- as(dm, "sparseMatrix") +s2[I,I] <- 0; diag(s2)[2:3] <- -(1:2) +stopifnot(is(s2,"symmetricMatrix"), diag(s2) == c(0:-2,0)) +t2 <- as(s2, "TsparseMatrix") +m <- as.mat(s2) +s2[2:1,2:1] <- 4:1 +t2[2:1,2:1] <- 4:1 + m[2:1,2:1] <- 4:1 +assert.EQ.mat(t2, m) +assert.EQ.mat(s2, m) +## and the same (for a different s2 !) +s2[2:1,2:1] <- 4:1 +t2[2:1,2:1] <- 4:1 +assert.EQ.mat(t2, m)# ok +assert.EQ.mat(s2, m)# failed in 0.9975-8 + + ## m[cbind(i,j)] <- value: m.[ cbind(3:5, 1:3) ] <- 1:3 stopifnot(m.[3,1] == 1, m.[4,2] == 2)