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 2174, Wed Apr 23 11:21:37 2008 UTC revision 2175, Wed Apr 23 11:23:50 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)
9    } else options(verbose = TRUE)# to show message()s
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 234  Line 237 
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 242  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 341  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 376  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
# Line 423  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 445  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 478  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 501  Line 503 
503  M <- Diagonal(4); M[1,2] <- 2  M <- Diagonal(4); M[1,2] <- 2
504  M. <- as(M, "CsparseMatrix")  M. <- as(M, "CsparseMatrix")
505  (R <- as(M., "RsparseMatrix"))  (R <- as(M., "RsparseMatrix"))
506  stopifnot(is(M, "triangularMatrix"),  (Ms <- symmpart(M.))
507            is(M.,"triangularMatrix"),  Rs <- as(Ms, "RsparseMatrix")
508            is(R, "triangularMatrix"))  stopifnot(isValid(M, "triangularMatrix"),
509              isValid(M.,"triangularMatrix"),
510              isValid(Ms, "dsCMatrix"),
511              isValid(R,  "dtRMatrix"),
512              isValid(Rs, "dsRMatrix") )
513  stopifnot(dim(M[2:3, FALSE]) == c(2,0),  stopifnot(dim(M[2:3, FALSE]) == c(2,0),
514            dim(R[2:3, FALSE]) == c(2,0),            dim(R[2:3, FALSE]) == c(2,0),
515            identical(M [2:3,TRUE], M [2:3,]),            identical(M [2:3,TRUE], M [2:3,]),
# Line 511  Line 517 
517            identical(R [2:3,TRUE], R [2:3,]),            identical(R [2:3,TRUE], R [2:3,]),
518            dim(R[FALSE, FALSE]) == c(0,0))            dim(R[FALSE, FALSE]) == c(0,0))
519    
520  cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons''  cat('Time elapsed: ', (.pt <- proc.time()),'\n') # "stats"
521    ##
522    cat("checkMatrix() of all: \n---------\n")
523    Sys.setlocale("LC_COLLATE", "C")# to keep ls() reproducible
524    for(nm in ls()) if(is(.m <- get(nm), "Matrix")) {
525        cat(nm, "\n")
526        checkMatrix(.m, doCoerce2 = !is(.m, "RsparseMatrix"),
527                    verbose = FALSE)
528    }
529    cat('Time elapsed: ', proc.time() - .pt,'\n') # "stats"
530    
531    if(!interactive()) warnings()
532    

Legend:
Removed from v.2174  
changed lines
  Added in v.2175

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