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 1710, Tue Dec 26 15:57:06 2006 UTC revision 1835, Fri May 11 10:41:21 2007 UTC
# Line 81  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    
85    ## *repeated* (aka 'duplicated') indices - did not work at all ...
86    i <- rep(8:10,2)
87    j <- c(2:4, 4:3)
88    assert.EQ.mat(mC[i,], mm[i,])
89    assert.EQ.mat(mC[,j], mm[,j])
90    assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])
91    assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])
92    assert.EQ.mat(mC[i,j], mm[i,j])
93    set.seed(7)
94    for(n in 1:50) {
95        i <- sample(sample(nrow(mC), 7), 20, replace = TRUE)
96        j <- sample(sample(ncol(mC), 6), 17, replace = TRUE)
97        assert.EQ.mat(mC[i,j], mm[i,j])
98    }
99    
100    ##---- Symmetric indexing of symmetric Matrix ----------
101    m. <- mC; m.[, c(2, 7:12)] <- 0
102    validObject(S <- crossprod(add.simpleDimnames(m.) %% 100))
103    ss <- as(S, "matrix")
104    T <- as(S, "TsparseMatrix")
105    ## non-repeated indices:
106    i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])
107    N <- nrow(T)
108    set.seed(11)
109    for(n in 1:50) {
110        i <- sample(N, max(2, sample(N,1)), replace = FALSE)
111        validObject(Tii <- T[i,i])
112        stopifnot(is(Tii, "dsTMatrix"), # remained symmetric Tsparse
113                  identical(t(Tii), t(T)[i,i]))
114        assert.EQ.mat(Tii, ss[i,i])
115    }
116    
117    ## repeated ones ``the challenge'' (to do smartly):
118    j <- c(4, 4, 9, 12, 9, 4, 17, 3, 18, 4, 12, 18, 4, 9)
119    assert.EQ.mat(T[j,j], ss[j,j])
120    ## and another two sets  (a, A) &  (a., A.) :
121    a <- matrix(0, 6,6)
122    a[upper.tri(a)] <- (utr <- c(2, 0,-1, 0,0,5, 7,0,0,0, 0,0,-2,0,8))
123    ta <- t(a); ta[upper.tri(a)] <- utr; a <- t(ta)
124    diag(a) <- c(0,3,0,4,6,0)
125    A <- as(Matrix(a), "TsparseMatrix")
126    A. <- A
127    diag(A.) <- 10 * (1:6)
128    a. <- as(A., "matrix")
129    ## More testing {this was not working for a long time..}
130    set.seed(1)
131    for(n in 1:100) {
132        i <- sample(1:nrow(A), 3+2*rpois(1, lam=3), replace=TRUE)
133        Aii  <- A[i,i]
134        A.ii <- A.[i,i]
135        stopifnot(class(Aii) == class(A),
136                  class(A.ii) == class(A.))
137        assert.EQ.mat(Aii , a [i,i])
138        assert.EQ.mat(A.ii, a.[i,i])
139        assert.EQ.mat(T[i,i], ss[i,i])
140    }
141    
142    
143  stopifnot(all.equal(mC[,3], mm[,3]),  stopifnot(all.equal(mC[,3], mm[,3]),
144            identical(mC[ij], mm[ij]))            identical(mC[ij], mm[ij]))
145  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
# Line 125  Line 184 
184            identical(as.mat(l3 ), m.x[1:3, ] != 0)            identical(as.mat(l3 ), m.x[1:3, ] != 0)
185            )            )
186    
187    ##-- Sub*assignment* with repeated / duplicated index:
188    A <- Matrix(0,4,3) ; A[c(1,2,1), 2] <- 1 ; A
189    B <- A;              B[c(1,2,1), 2] <- 1:3; B
190    stopifnot(identical(unname(as.matrix(A)),
191                        local({a <- matrix(0,4,3); a[c(1,2,1), 2] <-  1 ; a})),
192              identical(unname(as.matrix(B)),
193                        local({a <- matrix(0,4,3); a[c(1,2,1), 2] <- 1:3; a})))
194    
195    
196  ## used to fail  ## used to fail
197  n <- 5 ## or much larger  n <- 5 ## or much larger
198  sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),  sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),
# Line 142  Line 210 
210  m0 <- Diagonal(5)  m0 <- Diagonal(5)
211  (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix  (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix
212  (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)  (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)
213    m1g <- as(m1, "generalMatrix")
214    stopifnot(is(m1g, "dgTMatrix"))
215    assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])
216    assert.EQ.mat(m2[,c(4,1)], diag(5)[,c(4,1)])
217    stopifnot(identical(m2[1:3,], as(m1[1:3,], "CsparseMatrix")),
218              identical(Matrix:::uniqTsparse(m1[, c(4,2)]),
219                        Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))
220              )## failed in 0.9975-11
221    
222  M <- m0; M[1,] <- 0  M <- m0; M[1,] <- 0
223  stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))  stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
# Line 169  Line 245 
245  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)))
246    
247    
   
248  ## --- negative indices ----------  ## --- negative indices ----------
249  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
250  mt <- mT[1:5, 1:7]  mt <- mT[1:5, 1:7]
# Line 230  Line 305 
305            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier            mc[-1,3] == -2:1, mt[-1,3] == -2:1) ## failed earlier
306    
307  mc0 <- mc  mc0 <- mc
308    mt0 <- as(mc0, "TsparseMatrix")
309    m0  <- as(mc0, "matrix")
310  set.seed(1)  set.seed(1)
311  for(i in 1:20) {  for(i in 1:50) {
312      mc <- mc0      mc <- mc0; mt <- mt0 ; m <- m0
313      ev <- 1:5 %% 2 == round(runif(1))# 0 or 1      ev <- 1:5 %% 2 == round(runif(1))# 0 or 1
314      j <- sample(ncol(mc), 1 + round(runif(1)))      j <- sample(ncol(mc), 1 + round(runif(1)))
315      nv <- rpois(sum(ev) * length(j), lambda = 1)      nv <- rpois(sum(ev) * length(j), lambda = 1)
316      mc[ev, j] <- nv      mc[ev, j] <- nv
317      if(i < 5) print(mc[ev,j, drop = FALSE])       m[ev, j] <- nv
318      stopifnot(as.vector(mc[ev, j]) == nv) ## failed earlier...      mt[ev, j] <- nv
319      validObject(mc)      if(i %% 10 == 1) print(mc[ev,j, drop = FALSE])
320        stopifnot(as.vector(mc[ev, j]) == nv, ## failed earlier...
321                  as.vector(mt[ev, j]) == nv)
322        validObject(mc) ; assert.EQ.mat(mc, m)
323        validObject(mt) ; assert.EQ.mat(mt, m)
324  }  }
325    
326  mc # no longer has non-structural zeros  mc # no longer has non-structural zeros
# Line 263  Line 344 
344  stopifnot(as.matrix(Hc.[i,j]) == 0)  stopifnot(as.matrix(Hc.[i,j]) == 0)
345  Hc.[, 1:6]  Hc.[, 1:6]
346    
347  ## an example that failed long  ## an example that failed for a long time
348  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))
349  validObject(dm <- kronecker(Diagonal(2), sy3))  validObject(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker
350    dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"
351  (s2 <- as(dm, "sparseMatrix"))  (s2 <- as(dm, "sparseMatrix"))
352  validObject(st <- as(s2, "TsparseMatrix"))  validObject(st <- as(s2, "TsparseMatrix"))
353    stopifnot(is(s2, "symmetricMatrix"),
354              is(st, "symmetricMatrix"))
355  validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix  validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
356  validObject(s2.32 <- s2[1:3,1:2])  validObject(s2.32 <- s2[1:3,1:2])
357  I <- c(1,4:3)  I <- c(1,4:3)
# Line 286  Line 370 
370  ## symmetric subassign should keep symmetry  ## symmetric subassign should keep symmetry
371  st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))  st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))
372  s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))  s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))
373    ##
374  m <- as.mat(st)  m <- as.mat(st)
375   m[2:1,2:1] <- 4:1   m[2:1,2:1] <- 4:1
376  st[2:1,2:1] <- 4:1  st[2:1,2:1] <- 4:1
# Line 295  Line 379 
379            1:4 == as.vector(s2[1:2,1:2]),            1:4 == as.vector(s2[1:2,1:2]),
380            identical(m, as.mat(s2)))            identical(m, as.mat(s2)))
381    
382    ## now a slightly different situation for 's2' (had bug)
383    s2 <- as(dm, "sparseMatrix")
384    s2[I,I] <- 0; diag(s2)[2:3] <- -(1:2)
385    stopifnot(is(s2,"symmetricMatrix"), diag(s2) == c(0:-2,0))
386    t2 <- as(s2, "TsparseMatrix")
387    m <- as.mat(s2)
388    s2[2:1,2:1] <- 4:1
389    t2[2:1,2:1] <- 4:1
390     m[2:1,2:1] <- 4:1
391    assert.EQ.mat(t2, m)
392    assert.EQ.mat(s2, m)
393    ## and the same (for a different s2 !)
394    s2[2:1,2:1] <- 4:1
395    t2[2:1,2:1] <- 4:1
396    assert.EQ.mat(t2, m)# ok
397    assert.EQ.mat(s2, m)# failed in 0.9975-8
398    
399    
400  ## m[cbind(i,j)] <- value:  ## m[cbind(i,j)] <- value:
401  m.[ cbind(3:5, 1:3) ] <- 1:3  m.[ cbind(3:5, 1:3) ] <- 1:3
402  stopifnot(m.[3,1] == 1, m.[4,2] == 2)  stopifnot(m.[3,1] == 1, m.[4,2] == 2)
# Line 302  Line 404 
404  validObject(x.x)  validObject(x.x)
405  stopifnot(class(x.x) == "dsCMatrix",  stopifnot(class(x.x) == "dsCMatrix",
406            12:16 == as.mat(x.x)[cbind(2:6, 2:6)])            12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
407    (ne1 <- (mc - m.) != 0)
408    stopifnot(identical(ne1, 0 != abs(mc - m.)))
409    (ge <- m. >= mc) # contains "=" -> result is dense
410    ne. <- mc != m.  # was wrong (+ warning)
411    stopifnot(identical(!(m. < mc), m. >= mc),
412              identical(m. < mc, as(!ge, "sparseMatrix")),
413              identical(ne., Matrix:::drop0(ne1)))
414    
415    (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
416    T3 <- as(M3, "TsparseMatrix")
417    stopifnot(identical(drop(M3), M3),
418              identical4(drop(M3[,2, drop = FALSE]), M3[,2, drop = TRUE],
419                         drop(T3[,2, drop = FALSE]), T3[,2, drop = TRUE]),
420              is(T3, "triangularMatrix"),
421              !is(T3[,2, drop=FALSE], "triangularMatrix")
422              )
423    
424  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''

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

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