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 2323, Wed Jan 21 20:50:12 2009 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, warn = 1)      options(error = recover, warn = 1)
10  } else options(verbose = TRUE, warn = 1)  } 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 39  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 116  Line 129 
129  assert.EQ.mat(ds[NA,NA], ss[NA,NA])  assert.EQ.mat(ds[NA,NA], ss[NA,NA])
130  assert.EQ.mat(ds[NA,  ], ss[NA,])  assert.EQ.mat(ds[NA,  ], ss[NA,])
131  assert.EQ.mat(ds[  ,NA], ss[,NA])  assert.EQ.mat(ds[  ,NA], ss[,NA])
 stopifnot(identical(ds[2 ,NA], ss[2,NA]),  
           identical(ds[NA, 1], ss[NA, 1]))  
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:  ## NA in indices  -- check that we get a helpful error message:
# Line 167  Line 182 
182            identical(mC[iN], mm[iN]))            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 260  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 268  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 280  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 296  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 304  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 587  Line 619 
619    
620  cc <- capture.output(show(dLrg))# show(<diag>) used to error for large n  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.2323  
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