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 2341, Mon Mar 2 17:53:13 2009 UTC revision 2496, Sat Nov 14 17:24:42 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 167  Line 180 
180            identical(mC[iN], mm[iN]))            identical(mC[iN], mm[iN]))
181    
182  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])  assert.EQ.mat(mC[7, , drop=FALSE], mm[7, , drop=FALSE])
183    identical    (mC[7,   drop=FALSE], mm[7,   drop=FALSE]) # *vector* indexing
184    
185  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings  stopifnot(dim(mC[numeric(0), ]) == c(0,20), # used to give warnings
186            dim(mC[, integer(0)]) == c(40,0),            dim(mC[, integer(0)]) == c(40,0),
187            identical(mC[, integer(0)], mC[, FALSE]),            identical(mC[, integer(0)], mC[, FALSE]))
           identical(mC[7,  drop = FALSE],  
                     mC[7,, drop = FALSE]))  
188  validObject(print(mT[,c(2,4)]))  validObject(print(mT[,c(2,4)]))
189  stopifnot(all.equal(mT[2,], mm[2,]),  stopifnot(all.equal(mT[2,], mm[2,]),
190            ## row or column indexing in combination with t() :            ## row or column indexing in combination with t() :
# Line 280  Line 292 
292    
293    
294  ## "Vector indices" -------------------  ## "Vector indices" -------------------
295    .iniDiag.example <- expression({
296  D <- Diagonal(6)  D <- Diagonal(6)
297  M <- as(D,"dgeMatrix")  M <- as(D,"dgeMatrix")
298  m <- as(D,"matrix")  m <- as(D,"matrix")
299  s <- as(D,"TsparseMatrix")  s <- as(D,"TsparseMatrix")
300  S <- as(s,"CsparseMatrix")  S <- as(s,"CsparseMatrix")
301    })
302    eval(.iniDiag.example)
303  i <- c(3,1,6); v <- c(10,15,20)  i <- c(3,1,6); v <- c(10,15,20)
304  ## (logical,value) which both are recycled:  ## (logical,value) which both are recycled:
305  L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)  L <- c(TRUE, rep(FALSE,8)) ; z <- c(50,99)
# Line 296  Line 311 
311  D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT  D[i] <- v; assert.EQ.mat(D,m) # ddi -> dtT -> dgT
312  s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT  s[i] <- v; assert.EQ.mat(s,m) # dtT -> dgT
313  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
314    stopifnot(identical(s,D))
315  ## logical  ## logical
316    eval(.iniDiag.example)
317  m[L] <- z  m[L] <- z
318  M[L] <- z; assert.EQ.mat(M,m)  M[L] <- z; assert.EQ.mat(M,m)
319  D[L] <- z; assert.EQ.mat(D,m)  D[L] <- z; assert.EQ.mat(D,m)
# Line 304  Line 321 
321  S[L] <- z; assert.EQ.mat(S,m) ; S  S[L] <- z; assert.EQ.mat(S,m) ; S
322    
323  ## indexing [i]  vs  [i,] --- now ok  ## indexing [i]  vs  [i,] --- now ok
324  stopifnot(identical4(m[i], M[i], D[i], s[i]), identical(s[i],S[i]))  eval(.iniDiag.example)
325  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]))
326    stopifnot(identical5(m[L], M[L], D[L], s[L], S[L]))
327    ## bordercase ' drop = .' *vector* indexing {failed till 2009-04-..)
328    stopifnot(identical5(m[i,drop=FALSE], M[i,drop=FALSE], D[i,drop=FALSE],
329                         s[i,drop=FALSE], S[i,drop=FALSE]))
330    stopifnot(identical5(m[L,drop=FALSE], M[L,drop=FALSE], D[L,drop=FALSE],
331                         s[L,drop=FALSE], S[L,drop=FALSE]))
332    ## using L for row-indexing should give an error
333    assertError(m[L,]); assertError(m[L,, drop=FALSE])
334    ## these did not signal an error, upto (including) 0.999375-30:
335    assertError(s[L,]); assertError(s[L,, drop=FALSE])
336    assertError(S[L,]); assertError(S[L,, drop=FALSE])
337    
338    ## row indexing:
339  assert.EQ.mat(D[i,], m[i,])  assert.EQ.mat(D[i,], m[i,])
340  assert.EQ.mat(M[i,], m[i,])  assert.EQ.mat(M[i,], m[i,])
341  assert.EQ.mat(s[i,], m[i,])  assert.EQ.mat(s[i,], m[i,])
342  assert.EQ.mat(S[i,], m[i,])  assert.EQ.mat(S[i,], m[i,])
343    ## column indexing:
344  assert.EQ.mat(D[,i], m[,i])  assert.EQ.mat(D[,i], m[,i])
345  assert.EQ.mat(M[,i], m[,i])  assert.EQ.mat(M[,i], m[,i])
346  assert.EQ.mat(s[,i], m[,i])  assert.EQ.mat(s[,i], m[,i])
# Line 597  Line 627 
627  f <- sparseMatrix(i = sample(n, size=nnz, replace=TRUE),  f <- sparseMatrix(i = sample(n, size=nnz, replace=TRUE),
628                    j = sample(m, size=nnz, replace=TRUE))                    j = sample(m, size=nnz, replace=TRUE))
629  str(f)  str(f)
630  str(thisCol <-  f[,5000])# logi [....]  dim(f) # 6999863 x 99992
631  f[,5762] <- thisCol # now fine  prod(dim(f)) # 699930301096 == 699'930'301'096  (~ 700'000 millions)
632    str(thisCol <-  f[,5000])# logi [~ 7 mio....]
633    sv <- as(thisCol, "sparseVector")
634    str(sv) ## "empty" !
635    validObject(spCol <- f[,5000, drop=FALSE])
636    ## ^^ FIXME slow Tsparse_to_Csparse from memory-hog
637    ## cholmod_sparse *CHOLMOD(triplet_to_sparse)
638    ## which has  "workspace: Iwork (max (nrow,ncol))"
639    ## in ../src/CHOLMOD/Core/cholmod_triplet.c  *and*
640    ## in ../src/CHOLMOD/Core/t_cholmod_triplet.c
641    ##
642    ## *not* identical(): as(spCol, "sparseVector")@length is "double"prec:
643    stopifnot(all.equal(as(spCol, "sparseVector"),
644                        as(sv,   "nsparseVector"), tol=0))
645    f[,5762] <- thisCol # now "fine" <<<<<<<<<< FIXME uses LARGE objects
646    ## is using  replCmat() in ../R/Csparse.R, then
647    ##           replTmat() in ../R/Tsparse.R
648    
649  fx <- sparseMatrix(i = sample(n, size=nnz, replace=TRUE),  fx <- sparseMatrix(i = sample(n, size=nnz, replace=TRUE),
650                     j = sample(m, size=nnz, replace=TRUE),                     j = sample(m, size=nnz, replace=TRUE),

Legend:
Removed from v.2341  
changed lines
  Added in v.2496

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