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 2186, Mon Apr 28 12:37:01 2008 UTC revision 2505, Wed Dec 9 17:33:23 2009 UTC
# Line 2  Line 2 
2    
3  library(Matrix)  library(Matrix)
4    
5  source(system.file("test-tools.R", package = "Matrix"))# identical3() etc  source(system.file("test-tools.R", package = "Matrix"), keep.source = FALSE)
6    ##-> identical3() etc
7    
8  if(interactive()) {  if(interactive()) {
9      options(error = recover)      options(error = recover, warn = 1)
10  } else options(verbose = TRUE)# to show message()s  } else if(FALSE) { ## MM @ testing
11        options(error = recover, Matrix.verbose = TRUE, warn = 1)
12    } else options(Matrix.verbose = TRUE, warn = 1)
13    
14    
15  ### Dense Matrices  ### Dense Matrices
16    
# Line 14  Line 18 
18  validObject(m)  validObject(m)
19  stopifnot(identical(m, m[]),  stopifnot(identical(m, m[]),
20            identical(m[2, 3],  16), # simple number            identical(m[2, 3],  16), # simple number
21            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
22              identical(m[NA,NA], as(Matrix(NA, 7,4), "dMatrix")))
23    
24  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'  m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
25  m[-(4:7), 3:4]        # ditto; the upper right corner of 'm'  m[-(4:7), 3:4]        # ditto; the upper right corner of 'm'
# Line 38  Line 43 
43                       LETTERS[1:ncol(mn)])                       LETTERS[1:ncol(mn)])
44  checkMatrix(mn)  checkMatrix(mn)
45  mn["rd", "D"]  mn["rd", "D"]
46    ## Printing sparse colnames:
47    ms <- as(mn,"sparseMatrix")
48    ms[sample(28, 20)] <- 0
49    ms <- t(rbind2(ms, 3*ms))
50    cnam1 <- capture.output(show(ms))[2] ; op <- options("sparse.colnames" = "abb3")
51    cnam2 <- capture.output(show(ms))[2] ; options(op) # revert
52  stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,  stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,
53            identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,            identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,
54            identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2])            identical(mn[c("re", "rb"), "B"], mn[c(5,2), 2]),
55            )            ## sparse printing
56              grep("^ +$", cnam1) == 1, # cnam1 is empty
57              identical(cnam2,
58                        paste(" ", paste(rep(rownames(mn), 2), collapse=" "))))
59    
60  mo <- m  mo <- m
61  m[2,3] <- 100  m[2,3] <- 100
# Line 52  Line 66 
66  m. <- as.matrix(m)  m. <- as.matrix(m)
67    
68  ## m[ cbind(i,j) ] indexing:  ## m[ cbind(i,j) ] indexing:
69  ij <- cbind(1:6, 2:3)  iN <- ij <- cbind(1:6, 2:3)
70  stopifnot(identical(m[ij], m.[ij]))  iN[2:3,] <- iN[5,2] <- NA
71    stopifnot(identical(m[ij], m.[ij]),
72              identical(m[iN], m.[iN]))
73    
74  ## testing operations on logical Matrices rather more than indexing:  ## testing operations on logical Matrices rather more than indexing:
75  g10 <- m [ m > 10 ]  g10 <- m [ m > 10 ]
# Line 92  Line 108 
108  j <- c(2:4, 4:3)  j <- c(2:4, 4:3)
109  assert.EQ.mat(mC[i,], mm[i,])  assert.EQ.mat(mC[i,], mm[i,])
110  assert.EQ.mat(mC[,j], mm[,j])  assert.EQ.mat(mC[,j], mm[,j])
111    ## FIXME? assert.EQ.mat(mC[,NA], mm[,NA]) -- mC[,NA] is all 0 "instead" of all NA
112    ## MM currently thinks we should  NOT  allow  <sparse>[ <NA> ]
113  assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])  assert.EQ.mat(mC[i, 2:1], mm[i, 2:1])
114  assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])  assert.EQ.mat(mC[c(4,1,2:1), j], mm[c(4,1,2:1), j])
115  assert.EQ.mat(mC[i,j], mm[i,j])  assert.EQ.mat(mC[i,j], mm[i,j])
# Line 106  Line 124 
124  m. <- mC; m.[, c(2, 7:12)] <- 0  m. <- mC; m.[, c(2, 7:12)] <- 0
125  validObject(S <- crossprod(add.simpleDimnames(m.) %% 100))  validObject(S <- crossprod(add.simpleDimnames(m.) %% 100))
126  ss <- as(S, "matrix")  ss <- as(S, "matrix")
127    ds <- as(S, "denseMatrix")
128    ## NA-indexing of *dense* Matrices: should work as traditionally
129    assert.EQ.mat(ds[NA,NA], ss[NA,NA])
130    assert.EQ.mat(ds[NA,  ], ss[NA,])
131    assert.EQ.mat(ds[  ,NA], ss[,NA])
132  T <- as(S, "TsparseMatrix")  T <- as(S, "TsparseMatrix")
133    stopifnot(identical(ds[2 ,NA], ss[2,NA]),
134              identical(ds[NA, 1], ss[NA, 1]),
135              identical(S, as(T, "CsparseMatrix")) )
136    
137  ## non-repeated indices:  ## non-repeated indices:
138  i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])  i <- c(7:5, 2:4);assert.EQ.mat(T[i,i], ss[i,i])
139    ## NA in indices  -- check that we get a helpful error message:
140    i[2] <- NA
141    er <- tryCatch(T[i,i], error = function(e)e)
142    stopifnot(as.logical(grep("indices.*sparse Matrices", er$message)))
143    
144  N <- nrow(T)  N <- nrow(T)
145  set.seed(11)  set.seed(11)
146  for(n in 1:50) {  for(n in 1:50) {
# Line 146  Line 178 
178    
179    
180  stopifnot(all.equal(mC[,3], mm[,3]),  stopifnot(all.equal(mC[,3], mm[,3]),
181            identical(mC[ij], mm[ij]))            identical(mC[ij], mm[ij]),
182              identical(mC[iN], mm[iN]))
183    
184  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
185    identical    (mC[7,   drop=FALSE], mm[7,   drop=FALSE]) # *vector* indexing
186    
187  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
188            dim(mC[, integer(0)]) == c(40,0),            dim(mC[, integer(0)]) == c(40,0),
189            identical(mC[, integer(0)], mC[, FALSE]),            identical(mC[, integer(0)], mC[, FALSE]))
           identical(mC[7,  drop = FALSE],  
                     mC[7,, drop = FALSE]))  
190  validObject(print(mT[,c(2,4)]))  validObject(print(mT[,c(2,4)]))
191  stopifnot(all.equal(mT[2,], mm[2,]),  stopifnot(all.equal(mT[2,], mm[2,]),
192            ## row or column indexing in combination with t() :            ## row or column indexing in combination with t() :
# Line 219  Line 252 
252  stopifnot(identical(m0[2,], m0[,2]),  stopifnot(identical(m0[2,], m0[,2]),
253            identical(m0[,1], c(1,0,0,0,0)))            identical(m0[,1], c(1,0,0,0,0)))
254  ### Diagonal -- Sparse:  ### Diagonal -- Sparse:
255  (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix  (m1 <- as(m0, "TsparseMatrix"))  # dtTMatrix
256  (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)  (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix
257  m1g <- as(m1, "generalMatrix")  m1g <- as(m1, "generalMatrix")
258  stopifnot(is(m1g, "dgTMatrix"))  stopifnot(is(m1g, "dgTMatrix"))
259  assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])  assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])
# Line 241  Line 274 
274  M <- m0; M[1:3, 3] <- 0 ;M  M <- m0; M[1:3, 3] <- 0 ;M
275  T <- m0; T[1:3, 3] <- 10  T <- m0; T[1:3, 3] <- 10
276  stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),  stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
277            is(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))            isValid(T, "triangularMatrix"), identical(T[,3], c(10,10,10,0,0)))
278    
279  M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)  M <- m1; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
280  M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)  M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
# Line 249  Line 282 
282  M <- m1; M[1:3, 3] <- 0 ;M  M <- m1; M[1:3, 3] <- 0 ;M
283  assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)  assert.EQ.mat(M, diag(c(1,1, 0, 1,1)), tol=0)
284  T <- m1; T[1:3, 3] <- 10; checkMatrix(T)  T <- m1; T[1:3, 3] <- 10; checkMatrix(T)
285  stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))  stopifnot(isValid(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))
286    
287  M <- m2; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)  M <- m2; M[1,] <- 0 ; M ; assert.EQ.mat(M, diag(c(0,rep(1,4))), tol=0)
288  M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)  M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
# Line 261  Line 294 
294    
295    
296  ## "Vector indices" -------------------  ## "Vector indices" -------------------
297    .iniDiag.example <- expression({
298  D <- Diagonal(6)  D <- Diagonal(6)
299  M <- as(D,"dgeMatrix")  M <- as(D,"dgeMatrix")
300  m <- as(D,"matrix")  m <- as(D,"matrix")
301  s <- as(D,"TsparseMatrix")  s <- as(D,"TsparseMatrix")
302  S <- as(s,"CsparseMatrix")  S <- as(s,"CsparseMatrix")
303    })
304    eval(.iniDiag.example)
305  i <- c(3,1,6); v <- c(10,15,20)  i <- c(3,1,6); v <- c(10,15,20)
306  ## (logical,value) which both are recycled:  ## (logical,value) which both are recycled:
307  L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)  L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)
# Line 277  Line 313 
313  D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT  D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT
314  s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT  s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT
315  S[i] <- v; assert.EQ.mat(S,m); S # dtC -> dtT -> dgT -> dgC  S[i] <- v; assert.EQ.mat(S,m); S # dtC -> dtT -> dgT -> dgC
316    stopifnot(identical(s,D))
317  ## logical  ## logical
318    eval(.iniDiag.example)
319  m[L] <- z  m[L] <- z
320  M[L] <- z; assert.EQ.mat(M,m)  M[L] <- z; assert.EQ.mat(M,m)
321  D[L] <- z; assert.EQ.mat(D,m)  D[L] <- z; assert.EQ.mat(D,m)
# Line 285  Line 323 
323  S[L] <- z; assert.EQ.mat(S,m) ; S  S[L] <- z; assert.EQ.mat(S,m) ; S
324    
325  ## indexing [i]  vs  [i,] --- now ok  ## indexing [i]  vs  [i,] --- now ok
326  stopifnot(identical4(m[i], M[i], D[i], s[i]), identical(s[i],S[i]))  eval(.iniDiag.example)
327  stopifnot(identical4(m[L], M[L], D[L], s[L]), identical(s[L],S[L]))  stopifnot(identical5(m[i], M[i], D[i], s[i], S[i]))
328    stopifnot(identical5(m[L], M[L], D[L], s[L], S[L]))
329    ## bordercase ' drop = .' *vector* indexing {failed till 2009-04-..)
330    stopifnot(identical5(m[i,drop=FALSE], M[i,drop=FALSE], D[i,drop=FALSE],
331                         s[i,drop=FALSE], S[i,drop=FALSE]))
332    stopifnot(identical5(m[L,drop=FALSE], M[L,drop=FALSE], D[L,drop=FALSE],
333                         s[L,drop=FALSE], S[L,drop=FALSE]))
334    ## using L for row-indexing should give an error
335    assertError(m[L,]); assertError(m[L,, drop=FALSE])
336    ## these did not signal an error, upto (including) 0.999375-30:
337    assertError(s[L,]); assertError(s[L,, drop=FALSE])
338    assertError(S[L,]); assertError(S[L,, drop=FALSE])
339    
340    ## row indexing:
341  assert.EQ.mat(D[i,], m[i,])  assert.EQ.mat(D[i,], m[i,])
342  assert.EQ.mat(M[i,], m[i,])  assert.EQ.mat(M[i,], m[i,])
343  assert.EQ.mat(s[i,], m[i,])  assert.EQ.mat(s[i,], m[i,])
344  assert.EQ.mat(S[i,], m[i,])  assert.EQ.mat(S[i,], m[i,])
345    ## column indexing:
346  assert.EQ.mat(D[,i], m[,i])  assert.EQ.mat(D[,i], m[,i])
347  assert.EQ.mat(M[,i], m[,i])  assert.EQ.mat(M[,i], m[,i])
348  assert.EQ.mat(s[,i], m[,i])  assert.EQ.mat(s[,i], m[,i])
# Line 384  Line 435 
435  mc[4,] <- 0  mc[4,] <- 0
436  mc  mc
437    
438  S <- as(Diagonal(5),"sparseMatrix")  S <- as(Diagonal(5),"TsparseMatrix")
439  H <- Hilbert(9)  H <- Hilbert(9)
440  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
441  (trH <- tril(Hc[1:5, 1:5]))  (trH <- tril(Hc[1:5, 1:5]))
# Line 491  Line 542 
542            identical(m. < mc, as(!ge, "sparseMatrix")),            identical(m. < mc, as(!ge, "sparseMatrix")),
543            identical(ne., drop0(ne1)))            identical(ne., drop0(ne1)))
544    
545    d6 <- Diagonal(6)
546    ii <- c(1:2, 4:5)
547    d6[cbind(ii,ii)] <- 7*ii
548    stopifnot(is(d6, "ddiMatrix"), identical(d6, Diagonal(x=c(7*1:2,1,7*4:5,1))))
549    
550    for(j in 3:6) { ## even and odd j used to behave differently
551        M <- Matrix(0, j,j); m <- matrix(0, j,j)
552        T  <- as(M, "TsparseMatrix")
553        TG <- as(T, "generalMatrix")
554        G <-  as(M, "generalMatrix")
555        id <- cbind(1:j,1:j)
556        i2 <- cbind(1:j,j:1)
557        m[id] <- 1:j
558        M[id] <- 1:j ; stopifnot(is(M,"symmetricMatrix"))
559        T[id] <- 1:j ; stopifnot(is(T,"symmetricMatrix"))
560        G[id] <- 1:j
561        TG[id]<- 1:j
562        m[i2] <- 10
563        M[i2] <- 10 ; stopifnot(is(M,"symmetricMatrix"))
564        T[i2] <- 10 ; stopifnot(is(T,"symmetricMatrix"))
565        G[i2] <- 10
566        TG[i2]<- 10
567        ##
568        assert.EQ.mat(M, m)
569        assert.EQ.mat(T, m)
570        assert.EQ.mat(G, m)
571        assert.EQ.mat(TG,m)
572    }
573    
574    
575    ## drop, triangular, ...
576  (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail  (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
577  T3 <- as(M3, "TsparseMatrix")  T3 <- as(M3, "TsparseMatrix")
578  stopifnot(identical(drop(M3), M3),  stopifnot(identical(drop(M3), M3),
# Line 523  Line 605 
605            identical(R [2:3,TRUE], R [2:3,]),            identical(R [2:3,TRUE], R [2:3,]),
606            dim(R[FALSE, FALSE]) == c(0,0))            dim(R[FALSE, FALSE]) == c(0,0))
607    
608    n <- 50000L
609    Lrg <- new("dgTMatrix", Dim = c(n,n))
610    diag(Lrg) <- 1:n
611    dLrg <- as(Lrg, "diagonalMatrix")
612    stopifnot(identical(Diagonal(x = 1:n), dLrg))
613    diag(dLrg) <- 1 + diag(dLrg)
614    Clrg <- as(Lrg,"CsparseMatrix")
615    Ctrg <- as(Clrg, "triangularMatrix")
616    diag(Ctrg) <- 1 + diag(Ctrg)
617    stopifnot(identical(Diagonal(x = 1+ 1:n), dLrg),
618              identical(Ctrg, as(dLrg,"CsparseMatrix")))
619    
620    cc <- capture.output(show(dLrg))# show(<diag>) used to error for large n
621    
622    ## Large Matrix indexing / subassignment
623    ## ------------------------------------- (from ex. by Imran Rashid)
624    n <- 7000000
625    m <-  100000
626    nnz <- 20000
627    
628    set.seed(12)
629    f <- sparseMatrix(i = sample(n, size=nnz, replace=TRUE),
630                      j = sample(m, size=nnz, replace=TRUE))
631    str(f)
632    dim(f) # 6999863 x 99992
633    prod(dim(f)) # 699930301096 == 699'930'301'096  (~ 700'000 millions)
634    str(thisCol <-  f[,5000])# logi [~ 7 mio....]
635    sv <- as(thisCol, "sparseVector")
636    str(sv) ## "empty" !
637    validObject(spCol <- f[,5000, drop=FALSE])
638    ## ^^ FIXME slow Tsparse_to_Csparse from memory-hog
639    ## cholmod_sparse *CHOLMOD(triplet_to_sparse)
640    ## which has  "workspace: Iwork (max (nrow,ncol))"
641    ## in ../src/CHOLMOD/Core/cholmod_triplet.c  *and*
642    ## in ../src/CHOLMOD/Core/t_cholmod_triplet.c
643    ##
644    ## *not* identical(): as(spCol, "sparseVector")@length is "double"prec:
645    stopifnot(all.equal(as(spCol, "sparseVector"),
646                        as(sv,   "nsparseVector"), tol=0))
647    f[,5762] <- thisCol # now "fine" <<<<<<<<<< FIXME uses LARGE objects
648    ## is using  replCmat() in ../R/Csparse.R, then
649    ##           replTmat() in ../R/Tsparse.R
650    
651    fx <- sparseMatrix(i = sample(n, size=nnz, replace=TRUE),
652                       j = sample(m, size=nnz, replace=TRUE),
653                       x = round(10*rnorm(nnz)))
654    class(fx)## dgCMatrix
655    fx[,6000] <- (tC <- rep(thisCol, length=nrow(fx)))
656    thCol <- fx[,2000]
657    fx[,5762] <- thCol
658    stopifnot(is(f, "ngCMatrix"), is(fx, "dgCMatrix"),
659              identical(thisCol, f[,5762]),# perfect
660              identical(as.logical(fx[,6000]), tC),
661              identical(thCol,  fx[,5762]))
662    
663  cat('Time elapsed: ', (.pt <- proc.time()),'\n') # "stats"  cat('Time elapsed: ', (.pt <- proc.time()),'\n') # "stats"
664  ##  ##
665  cat("checkMatrix() of all: \n---------\n")  cat("checkMatrix() of all: \n---------\n")

Legend:
Removed from v.2186  
changed lines
  Added in v.2505

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