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 1825, Thu Apr 12 13:21:51 2007 UTC revision 2113, Mon Feb 18 08:27:41 2008 UTC
# Line 4  Line 4 
4    
5  source(system.file("test-tools.R", package = "Matrix"))# identical3() etc  source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
6    
7    options(verbose = TRUE)# to show message()s
8    
9  ### Dense Matrices  ### Dense Matrices
10    
11  m <- Matrix(1:28 +0, nrow = 7)  m <- Matrix(1:28 +0, nrow = 7)
# Line 13  Line 15 
15            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
16    
17  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
18  m[-(4:7), 3:4]        # dito; the upper right corner of 'm'  m[-(4:7), 3:4]        # ditto; the upper right corner of 'm'
19    
20  ## rows or columns only:  ## rows or columns only:
21  m[1,]     # first row, as simple numeric vector  m[1,]     # first row, as simple numeric vector
# Line 82  Line 84 
84  mC[7,  drop = FALSE]  mC[7,  drop = FALSE]
85  assert.EQ.mat(mC[1:2,], mm[1:2,])  assert.EQ.mat(mC[1:2,], mm[1:2,])
86    
87  ## *repeated* indices - did not work at all ...  ## *repeated* (aka 'duplicated') indices - did not work at all ...
88  i <- rep(8:10,2)  i <- rep(8:10,2)
89  j <- c(2:4, 4:3)  j <- c(2:4, 4:3)
90  assert.EQ.mat(mC[i,], mm[i,])  assert.EQ.mat(mC[i,], mm[i,])
91  assert.EQ.mat(mC[,j], mm[,j])  assert.EQ.mat(mC[,j], mm[,j])
92  if(FALSE) ## FIXME  assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])
93    assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])
94    assert.EQ.mat(mC[i,j], mm[i,j])
95    set.seed(7)
96    for(n in 1:50) {
97        i <- sample(sample(nrow(mC), 7), 20, replace = TRUE)
98        j <- sample(sample(ncol(mC), 6), 17, replace = TRUE)
99  assert.EQ.mat(mC[i,j], mm[i,j])  assert.EQ.mat(mC[i,j], mm[i,j])
100    }
101    
102    ##---- Symmetric indexing of symmetric Matrix ----------
103    m. <- mC; m.[, c(2, 7:12)] <- 0
104    validObject(S <- crossprod(add.simpleDimnames(m.) %% 100))
105    ss <- as(S, "matrix")
106    T <- as(S, "TsparseMatrix")
107    ## non-repeated indices:
108    i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])
109    N <- nrow(T)
110    set.seed(11)
111    for(n in 1:50) {
112        i <- sample(N, max(2, sample(N,1)), replace = FALSE)
113        validObject(Tii <- T[i,i])
114        stopifnot(is(Tii, "dsTMatrix"), # remained symmetric Tsparse
115                  identical(t(Tii), t(T)[i,i]))
116        assert.EQ.mat(Tii, ss[i,i])
117    }
118    
119    ## repeated ones ``the challenge'' (to do smartly):
120    j <- c(4, 4, 9, 12, 9, 4, 17, 3, 18, 4, 12, 18, 4, 9)
121    assert.EQ.mat(T[j,j], ss[j,j])
122    ## and another two sets  (a, A) &  (a., A.) :
123    a <- matrix(0, 6,6)
124    a[upper.tri(a)] <- (utr <- c(2, 0,-1, 0,0,5, 7,0,0,0, 0,0,-2,0,8))
125    ta <- t(a); ta[upper.tri(a)] <- utr; a <- t(ta)
126    diag(a) <- c(0,3,0,4,6,0)
127    A <- as(Matrix(a), "TsparseMatrix")
128    A. <- A
129    diag(A.) <- 10 * (1:6)
130    a. <- as(A., "matrix")
131    ## More testing {this was not working for a long time..}
132    set.seed(1)
133    for(n in 1:100) {
134        i <- sample(1:nrow(A), 3+2*rpois(1, lam=3), replace=TRUE)
135        Aii  <- A[i,i]
136        A.ii <- A.[i,i]
137        stopifnot(class(Aii) == class(A),
138                  class(A.ii) == class(A.))
139        assert.EQ.mat(Aii , a [i,i])
140        assert.EQ.mat(A.ii, a.[i,i])
141        assert.EQ.mat(T[i,i], ss[i,i])
142    }
143    
144    
145  stopifnot(all.equal(mC[,3], mm[,3]),  stopifnot(all.equal(mC[,3], mm[,3]),
146            identical(mC[ij], mm[ij]))            identical(mC[ij], mm[ij]))
# Line 134  Line 186 
186            identical(as.mat(l3 ), m.x[1:3, ] != 0)            identical(as.mat(l3 ), m.x[1:3, ] != 0)
187            )            )
188    
189    ##-- Sub*assignment* with repeated / duplicated index:
190    A <- Matrix(0,4,3) ; A[c(1,2,1), 2] <- 1 ; A
191    B <- A;              B[c(1,2,1), 2] <- 1:3; B; B. <- B
192    B.[3,] <- rbind(4:2)
193    diag(B.) <- 10 * diag(B.)
194    C <- B.; C[,2] <- C[,2];  C[1,] <- C[1,]; C[2:3,2:1] <- C[2:3,2:1]
195    stopifnot(identical(unname(as.matrix(A)),
196                        local({a <- matrix(0,4,3); a[c(1,2,1), 2] <-  1 ; a})),
197              identical(unname(as.matrix(B)),
198                        local({a <- matrix(0,4,3); a[c(1,2,1), 2] <- 1:3; a})),
199              identical(C, drop0(B.)))
200    
201    
202  ## used to fail  ## used to fail
203  n <- 5 ## or much larger  n <- 5 ## or much larger
204  sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),  sm <- new("dsTMatrix", i=as.integer(1),j=as.integer(1),
# Line 186  Line 251 
251  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)))
252    
253    
254    ## "Vector indices" -------------------
255    D <- Diagonal(6)
256    M <- as(D,"dgeMatrix")
257    m <- as(D,"matrix")
258    s <- as(D,"TsparseMatrix")
259    S <- as(s,"CsparseMatrix")
260    i <- c(3,1,6); v <- c(10,15,20)
261    ## (logical,value) which both are recycled:
262    L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)
263    
264    ## vector subassignment, both with integer & logical
265    ## these now work correctly {though not very efficiently; hence warnings}
266    m[i] <- v # the role model: only first column is affected
267    M[i] <- v; assert.EQ.mat(M,m) # dge
268    D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT
269    s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT
270    S[i] <- v; assert.EQ.mat(S,m); S # dtC -> dtT -> dgT -> dgC
271    ## logical
272    m[L] <- z
273    M[L] <- z; assert.EQ.mat(M,m)
274    D[L] <- z; assert.EQ.mat(D,m)
275    s[L] <- z; assert.EQ.mat(s,m)
276    S[L] <- z; assert.EQ.mat(S,m) ; S
277    
278    ## indexing [i]  vs  [i,] --- now ok
279    stopifnot(identical4(m[i], M[i], D[i], s[i]), identical(s[i],S[i]))
280    stopifnot(identical4(m[L], M[L], D[L], s[L]), identical(s[L],S[L]))
281    assert.EQ.mat(D[i,], m[i,])
282    assert.EQ.mat(M[i,], m[i,])
283    assert.EQ.mat(s[i,], m[i,])
284    assert.EQ.mat(S[i,], m[i,])
285    
286    assert.EQ.mat(D[,i], m[,i])
287    assert.EQ.mat(M[,i], m[,i])
288    assert.EQ.mat(s[,i], m[,i])
289    assert.EQ.mat(S[,i], m[,i])
290    
291    
292  ## --- negative indices ----------  ## --- negative indices ----------
293  mc <- mC[1:5, 1:7]  mc <- mC[1:5, 1:7]
294  mt <- mT[1:5, 1:7]  mt <- mT[1:5, 1:7]
# Line 272  Line 375 
375  mc[4,] <- 0  mc[4,] <- 0
376  mc  mc
377    
378    S <- as(Diagonal(5),"sparseMatrix")
379  H <- Hilbert(9)  H <- Hilbert(9)
380  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
381  (trH <- tril(Hc[1:5, 1:5]))  (trH <- tril(Hc[1:5, 1:5]))
382  stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L")  stopifnot(is(trH, "triangularMatrix"), trH@uplo == "L",
383              is(S, "triangularMatrix"))
384    
385    ## triangular assignment
386    ## the slick (but inefficient in case of sparse!) way to assign sub-diagonals:
387    ## equivalent to tmp <- `diag<-`(S[,-1], -2:1); S[,-1] <- tmp
388    ## which dispatches to (x="TsparseMatrix", i="missing",j="index", value="replValue")
389    diag(S[,-1]) <- -2:1 # used to give a wrong warning
390    S <- as(S,"triangularMatrix")
391    assert.EQ.mat(S, local({s <- diag(5); diag(s[,-1]) <- -2:1; s}))
392    
393    trH[c(1:2,4), c(2:3,5)] <- 0 # gave an *error* upto Jan.2008
394    trH[ lower.tri(trH) ] <- 0   # ditto, because of callNextMethod()
395    
396    m <- Matrix(0+1:28, nrow = 4)
397    m[-3,c(2,4:5,7)] <- m[ 3, 1:4] <- m[1:3, 6] <- 0
398    mT <- as(m, "dgTMatrix")
399    stopifnot(identical(mT[lower.tri(mT)],
400                        m [lower.tri(m) ]))
401    lM <- upper.tri(mT, diag=TRUE)
402    mT[lM] <- 0
403     m[lM] <- 0
404    assert.EQ.mat(mT, as(m,"matrix"))
405    mT[lM] <- -1:0
406     m[lM] <- -1:0
407    assert.EQ.mat(mT, as(m,"matrix"))
408    (mT <- drop0(mT))
409    
410  i <- c(1:2, 4, 6:7); j <- c(2:4,6)  i <- c(1:2, 4, 6:7); j <- c(2:4,6)
411  H[i,j] <- 0  H[i,j] <- 0
# Line 351  Line 481 
481  ne. <- mc != m.  # was wrong (+ warning)  ne. <- mc != m.  # was wrong (+ warning)
482  stopifnot(identical(!(m. < mc), m. >= mc),  stopifnot(identical(!(m. < mc), m. >= mc),
483            identical(m. < mc, as(!ge, "sparseMatrix")),            identical(m. < mc, as(!ge, "sparseMatrix")),
484            identical(ne., Matrix:::drop0(ne1)))            identical(ne., drop0(ne1)))
485    
486  (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail  (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
487  T3 <- as(M3, "TsparseMatrix")  T3 <- as(M3, "TsparseMatrix")
# Line 362  Line 492 
492            !is(T3[,2, drop=FALSE], "triangularMatrix")            !is(T3[,2, drop=FALSE], "triangularMatrix")
493            )            )
494    
495    M <- Diagonal(4); M[1,2] <- 2
496    M. <- as(M, "CsparseMatrix")
497    (R <- as(M., "RsparseMatrix"))
498    stopifnot(is(M, "triangularMatrix"),
499              is(M.,"triangularMatrix"),
500              is(R, "triangularMatrix"))
501    stopifnot(dim(M[2:3, FALSE]) == c(2,0),
502              dim(R[2:3, FALSE]) == c(2,0),
503              identical(M [2:3,TRUE], M [2:3,]),
504              identical(M.[2:3,TRUE], M.[2:3,]),
505              identical(R [2:3,TRUE], R [2:3,]),
506              dim(R[FALSE, FALSE]) == c(0,0))
507    
508  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''

Legend:
Removed from v.1825  
changed lines
  Added in v.2113

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