SCM

SCM Repository

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

Diff of /pkg/tests/indexing.R

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1705, Sat Dec 16 12:37:25 2006 UTC revision 1724, Sat Jan 13 21:06:51 2007 UTC
# Line 1  Line 1 
1  #### For both 'Extract' ("[") and 'Replace' ("[<-") Method testing  ## For both 'Extract' ("[") and 'Replace' ("[<-") Method testing
2    
3  library(Matrix)  library(Matrix)
4    
# Line 138  Line 138 
138            all(sm[,-(1:3)] == 0)            all(sm[,-(1:3)] == 0)
139            )            )
140    
141    ### Diagonal -- Sparse:
142    m0 <- Diagonal(5)
143    (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix
144    (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)
145    
146    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  ## --- negative indices ----------  ## --- negative indices ----------
173  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
# Line 196  Line 226 
226  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled  mc[-1, 3] <- -2:1 # 0 should not be entered; 'value' recycled
227  mt[-1, 3] <- -2:1  mt[-1, 3] <- -2:1
228  stopifnot(mc@x != 0, mt@x != 0,  stopifnot(mc@x != 0, mt@x != 0,
229            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ##--> BUG -- fixed            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier
230    
231  ev <- 1:5 %% 2 == 0  mc0 <- mc
232  mc[ev, 3] <- 0:1  mt0 <- as(mc0, "TsparseMatrix")
233  if(FALSE)## FIXME  m0  <- as(mc0, "matrix")
234   stopifnot(mc[ev, 3] == 0:1) ##-> BUG  {very peculiar; the 2nd time it works ...}  set.seed(1)
235  validObject(mc)  for(i in 1:50) {
236  mc # now shows a non-structural zeros      mc <- mc0; mt <- mt0 ; m <- m0
237        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         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    }
249    
250    mc # no longer has non-structural zeros
251  mc[ii, jj] <- 1:6  mc[ii, jj] <- 1:6
252  mc[c(2,5), c(3,5)] <- 3.2  mc[c(2,5), c(3,5)] <- 3.2
253  validObject(mc)  validObject(mc)
# Line 224  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 247  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 256  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.1705  
changed lines
  Added in v.1724

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge