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 2113, Mon Feb 18 08:27:41 2008 UTC revision 2239, Mon Jul 28 19:26:40 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  if(interactive()) {
8        options(error = recover, warn = 1)
9    } else options(verbose = TRUE, warn = 1)
10    
11  ### Dense Matrices  ### Dense Matrices
12    
# Line 34  Line 36 
36  mn <- m  mn <- m
37  dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),  dimnames(mn) <- list(paste("r",letters[1:nrow(mn)],sep=""),
38                       LETTERS[1:ncol(mn)])                       LETTERS[1:ncol(mn)])
39    checkMatrix(mn)
40  mn["rd", "D"]  mn["rd", "D"]
41  stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,  stopifnot(identical(mn["rc", "D"], mn[3,4]), mn[3,4] == 24,
42            identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,            identical(mn[, "A"], mn[,1]), mn[,1] == 1:7,
# Line 178  Line 181 
181  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0  lx.x <- as(x.x, "lsCMatrix") # FALSE only for "structural" 0
182  (l10 <- lx.x[1:10, 1:10])# "lsC"  (l10 <- lx.x[1:10, 1:10])# "lsC"
183  (l3 <-  lx.x[1:3, ])  (l3 <-  lx.x[1:3, ])
184  m.x <- as(x.x, "matrix")  m.x <- as.mat(x.x) # as.mat() *drops* (NULL,NULL) dimnames
185  stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !  stopifnot(class(l10) == "lsCMatrix", # symmetric indexing -> symmetric !
186            identical(as.mat(lx.x), m.x != 0),            identical(as.mat(lx.x), m.x != 0),
187            identical(as.logical(lx.x), as.logical(m.x)),            identical(as.logical(lx.x), as.logical(m.x)),
# Line 212  Line 215 
215            all(sm[,-(1:3)] == 0)            all(sm[,-(1:3)] == 0)
216            )            )
217    
 ### Diagonal -- Sparse:  
218  m0 <- Diagonal(5)  m0 <- Diagonal(5)
219  (m1 <- as(m0, "sparseMatrix"))  # dtTMatrix  stopifnot(identical(m0[2,], m0[,2]),
220  (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix (with an irrelevant warning)            identical(m0[,1], c(1,0,0,0,0)))
221    ### Diagonal -- Sparse:
222    (m1 <- as(m0, "TsparseMatrix"))  # dtTMatrix
223    (m2 <- as(m0, "CsparseMatrix")) # dtCMatrix
224  m1g <- as(m1, "generalMatrix")  m1g <- as(m1, "generalMatrix")
225  stopifnot(is(m1g, "dgTMatrix"))  stopifnot(is(m1g, "dgTMatrix"))
226  assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])  assert.EQ.mat(m2[1:3,],    diag(5)[1:3,])
# Line 225  Line 230 
230                      Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))                      Matrix:::uniqTsparse(as(m2[, c(4,2)], "TsparseMatrix")))
231            )## failed in 0.9975-11            )## failed in 0.9975-11
232    
233    (uTr <- new("dtTMatrix", Dim = c(3L,3L), diag="U"))
234    uTr[1,] <- 0
235    assert.EQ.mat(uTr, cbind(0, rbind(0,diag(2))))
236    
237  M <- m0; M[1,] <- 0  M <- m0; M[1,] <- 0
238  stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))  stopifnot(identical(M, Diagonal(x=c(0, rep(1,4)))))
239  M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3)  M <- m0; M[,3] <- 3 ; M ; stopifnot(is(M, "sparseMatrix"), M[,3] == 3)
240  validObject(M)  checkMatrix(M)
241  M <- m0; M[1:3, 3] <- 0 ;M  M <- m0; M[1:3, 3] <- 0 ;M
242  T <- m0; T[1:3, 3] <- 10  T <- m0; T[1:3, 3] <- 10
243  stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),  stopifnot(identical(M, Diagonal(x=c(1,1, 0, 1,1))),
# Line 236  Line 245 
245    
246  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)
247  M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)  M <- m1; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
248  validObject(M)  checkMatrix(M)
249  M <- m1; M[1:3, 3] <- 0 ;M  M <- m1; M[1:3, 3] <- 0 ;M
250  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)
251  T <- m1; T[1:3, 3] <- 10; validObject(T)  T <- m1; T[1:3, 3] <- 10; checkMatrix(T)
252  stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))  stopifnot(is(T, "dtTMatrix"), identical(T[,3], c(10,10,10,0,0)))
253    
254  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)
255  M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)  M <- m2; M[,3] <- 3 ; stopifnot(is(M,"sparseMatrix"), M[,3] == 3)
256  validObject(M)  checkMatrix(M)
257  M <- m2; M[1:3, 3] <- 0 ;M  M <- m2; M[1:3, 3] <- 0 ;M
258  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)
259  T <- m2; T[1:3, 3] <- 10; validObject(T)  T <- m2; T[1:3, 3] <- 10; checkMatrix(T)
260  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)))
261    
262    
# Line 335  Line 344 
344    
345  mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)  mt[c(2,4), ] <- 0; stopifnot(as(mt[c(2,4), ],"matrix") == 0)
346  mt[2:3, 4:7] <- 33  mt[2:3, 4:7] <- 33
347  validObject(mt)  checkMatrix(mt)
348  mt  mt
349    
350  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)  mc[1,4] <- -99 ; stopifnot(mc[1,4] == -99)
# Line 370  Line 379 
379  mc # no longer has non-structural zeros  mc # no longer has non-structural zeros
380  mc[ii, jj] <- 1:6  mc[ii, jj] <- 1:6
381  mc[c(2,5), c(3,5)] <- 3.2  mc[c(2,5), c(3,5)] <- 3.2
382  validObject(mc)  checkMatrix(mc)
383  m. <- mc  m. <- mc
384  mc[4,] <- 0  mc[4,] <- 0
385  mc  mc
386    
387  S <- as(Diagonal(5),"sparseMatrix")  S <- as(Diagonal(5),"TsparseMatrix")
388  H <- Hilbert(9)  H <- Hilbert(9)
389  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...  Hc <- as(round(H, 3), "dsCMatrix")# a sparse matrix with no 0 ...
390  (trH <- tril(Hc[1:5, 1:5]))  (trH <- tril(Hc[1:5, 1:5]))
# Line 417  Line 426 
426    
427  ## an example that failed for a long time  ## an example that failed for a long time
428  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))
429  validObject(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker  checkMatrix(dm <- kronecker(Diagonal(2), sy3))# now sparse with new kronecker
430  dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"  dm <- Matrix(as.matrix(dm))# -> "dsyMatrix"
431  (s2 <- as(dm, "sparseMatrix"))  (s2 <- as(dm, "sparseMatrix"))
432  validObject(st <- as(s2, "TsparseMatrix"))  checkMatrix(st <- as(s2, "TsparseMatrix"))
433  stopifnot(is(s2, "symmetricMatrix"),  stopifnot(is(s2, "symmetricMatrix"),
434            is(st, "symmetricMatrix"))            is(st, "symmetricMatrix"))
435  validObject(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix  checkMatrix(s.32  <- st[1:3,1:2]) ## 3 x 2 - and *not* dsTMatrix
436  validObject(s2.32 <- s2[1:3,1:2])  checkMatrix(s2.32 <- s2[1:3,1:2])
437  I <- c(1,4:3)  I <- c(1,4:3)
438  stopifnot(is(s2.32, "generalMatrix"),  stopifnot(is(s2.32, "generalMatrix"),
439            is(s.32,  "generalMatrix"),            is(s.32,  "generalMatrix"),
# Line 439  Line 448 
448    
449  ## now sub-assign  and check for consistency  ## now sub-assign  and check for consistency
450  ## symmetric subassign should keep symmetry  ## symmetric subassign should keep symmetry
451  st[I,I] <- 0; validObject(st); stopifnot(is(st,"symmetricMatrix"))  st[I,I] <- 0; checkMatrix(st); stopifnot(is(st,"symmetricMatrix"))
452  s2[I,I] <- 0; validObject(s2); stopifnot(is(s2,"symmetricMatrix"))  s2[I,I] <- 0; checkMatrix(s2); stopifnot(is(s2,"symmetricMatrix"))
453  ##  ##
454  m <- as.mat(st)  m <- as.mat(st)
455   m[2:1,2:1] <- 4:1   m[2:1,2:1] <- 4:1
# Line 472  Line 481 
481  m.[ cbind(3:5, 1:3) ] <- 1:3  m.[ cbind(3:5, 1:3) ] <- 1:3
482  stopifnot(m.[3,1] == 1, m.[4,2] == 2)  stopifnot(m.[3,1] == 1, m.[4,2] == 2)
483  x.x[ cbind(2:6, 2:6)] <- 12:16  x.x[ cbind(2:6, 2:6)] <- 12:16
484  validObject(x.x)  stopifnot(isValid(x.x, "dsCMatrix"),
 stopifnot(class(x.x) == "dsCMatrix",  
485            12:16 == as.mat(x.x)[cbind(2:6, 2:6)])            12:16 == as.mat(x.x)[cbind(2:6, 2:6)])
486  (ne1 <- (mc - m.) != 0)  (ne1 <- (mc - m.) != 0)
487  stopifnot(identical(ne1, 0 != abs(mc - m.)))  stopifnot(identical(ne1, 0 != abs(mc - m.)))
# Line 483  Line 491 
491            identical(m. < mc, as(!ge, "sparseMatrix")),            identical(m. < mc, as(!ge, "sparseMatrix")),
492            identical(ne., drop0(ne1)))            identical(ne., drop0(ne1)))
493    
494    d6 <- Diagonal(6)
495    ii <- c(1:2, 4:5)
496    d6[cbind(ii,ii)] <- 7*ii
497    stopifnot(is(d6, "ddiMatrix"), identical(d6, Diagonal(x=c(7*1:2,1,7*4:5,1))))
498    
499  (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail  (M3 <- Matrix(upper.tri(matrix(, 3, 3)))) # ltC; indexing used to fail
500  T3 <- as(M3, "TsparseMatrix")  T3 <- as(M3, "TsparseMatrix")
501  stopifnot(identical(drop(M3), M3),  stopifnot(identical(drop(M3), M3),
# Line 492  Line 505 
505            !is(T3[,2, drop=FALSE], "triangularMatrix")            !is(T3[,2, drop=FALSE], "triangularMatrix")
506            )            )
507    
508    (T6 <- as(as(kronecker(Matrix(c(0,0,1,0),2,2), t(T3)), "lMatrix"),
509              "triangularMatrix"))
510    T6[1:4, -(1:3)] # failed (trying to coerce back to ltTMatrix)
511    stopifnot(identical(T6[1:4, -(1:3)][2:3, -3],
512                        spMatrix(2,2, i=c(1,2,2), j=c(1,1,2), x=rep(TRUE,3))))
513    
514  M <- Diagonal(4); M[1,2] <- 2  M <- Diagonal(4); M[1,2] <- 2
515  M. <- as(M, "CsparseMatrix")  M. <- as(M, "CsparseMatrix")
516  (R <- as(M., "RsparseMatrix"))  (R <- as(M., "RsparseMatrix"))
517  stopifnot(is(M, "triangularMatrix"),  (Ms <- symmpart(M.))
518            is(M.,"triangularMatrix"),  Rs <- as(Ms, "RsparseMatrix")
519            is(R, "triangularMatrix"))  stopifnot(isValid(M, "triangularMatrix"),
520              isValid(M.,"triangularMatrix"),
521              isValid(Ms, "dsCMatrix"),
522              isValid(R,  "dtRMatrix"),
523              isValid(Rs, "dsRMatrix") )
524  stopifnot(dim(M[2:3, FALSE]) == c(2,0),  stopifnot(dim(M[2:3, FALSE]) == c(2,0),
525            dim(R[2:3, FALSE]) == c(2,0),            dim(R[2:3, FALSE]) == c(2,0),
526            identical(M [2:3,TRUE], M [2:3,]),            identical(M [2:3,TRUE], M [2:3,]),
# Line 505  Line 528 
528            identical(R [2:3,TRUE], R [2:3,]),            identical(R [2:3,TRUE], R [2:3,]),
529            dim(R[FALSE, FALSE]) == c(0,0))            dim(R[FALSE, FALSE]) == c(0,0))
530    
531  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  n <- 50000L
532    Lrg <- new("dgTMatrix", Dim = c(n,n))
533    diag(Lrg) <- 1:n
534    dLrg <- as(Lrg, "diagonalMatrix")
535    stopifnot(identical(Diagonal(x = 1:n), dLrg))
536    diag(dLrg) <- 1 + diag(dLrg)
537    Clrg <- as(Lrg,"CsparseMatrix")
538    Ctrg <- as(Clrg, "triangularMatrix")
539    diag(Ctrg) <- 1 + diag(Ctrg)
540    stopifnot(identical(Diagonal(x = 1+ 1:n), dLrg),
541              identical(Ctrg, as(dLrg,"CsparseMatrix")))
542    
543    cc <- capture.output(show(dLrg))# show(<diag>) used to error for large n
544    
545    cat('Time elapsed: ', (.pt <- proc.time()),'\n') # "stats"
546    ##
547    cat("checkMatrix() of all: \n---------\n")
548    Sys.setlocale("LC_COLLATE", "C")# to keep ls() reproducible
549    for(nm in ls()) if(is(.m <- get(nm), "Matrix")) {
550        cat(nm, "\n")
551        checkMatrix(.m, verbose = FALSE)
552    }
553    cat('Time elapsed: ', proc.time() - .pt,'\n') # "stats"
554    
555    if(!interactive()) warnings()
556    

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

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