SCM

SCM Repository

[matrix] Annotation of /branches/Matrix-mer2/tests/validObj.R
ViewVC logotype

Annotation of /branches/Matrix-mer2/tests/validObj.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 768 - (view) (download)
Original Path: pkg/tests/validObj.R

1 : maechler 509 library(Matrix)
2 :    
3 : maechler 516 ### Do all kinds of object creation and coercion
4 : maechler 509
5 : maechler 516 chk.matrix <- function(M) {
6 : maechler 538 ## check object; including coercion to "matrix" :
7 : maechler 516 cl <- class(M)
8 : maechler 538 cat("class ", dQuote(cl), " [",nrow(M)," x ",ncol(M),"]; slots (",
9 : maechler 516 paste(slotNames(M), collapse=","), ")\n", sep='')
10 :     stopifnot(validObject(M),
11 : maechler 538 dim(M) == c(nrow(M), ncol(M)),
12 :     identical(dim(m <- as(M, "matrix")), dim(M))
13 : maechler 516 )
14 :     }
15 :    
16 : maechler 538 ## Make sure errors are signaled
17 :     assertError <- function(expr) {
18 :     d.expr <- deparse(substitute(expr))
19 :     t.res <- try(expr, silent = TRUE)
20 :     if(!inherits(t.res, "try-error"))
21 :     stop(d.expr, "\n\t did not give an error", call. = FALSE)
22 :     invisible(t.res)
23 :     }
24 :    
25 : maechler 612 ## the empty ones:
26 :     chk.matrix(new("dgeMatrix"))
27 : maechler 538
28 : maechler 516 ## "dge"
29 : maechler 538 assertError( new("dgeMatrix", Dim = c(2,2), x= 1:4) )# double 'Dim'
30 :     assertError( new("dgeMatrix", Dim = as.integer(c(2,2)), x= 1:4) )# int 'x'
31 :     assertError( new("dgeMatrix", Dim = 2:2, x=as.double(1:4)) )# length(Dim) !=2
32 :     assertError( new("dgeMatrix", Dim = as.integer(c(2,2)), x= as.double(1:5)))
33 :    
34 : maechler 516 chk.matrix(m1 <- Matrix(1:6, ncol=2))
35 :     chk.matrix(m2 <- Matrix(1:7, ncol=3)) # a warning
36 : maechler 579 stopifnot(is(m1) == c("dgeMatrix", "ddenseMatrix", "dMatrix", "Matrix"),
37 :     dim(t(m1)) == 2:3,
38 :     identical(m1, t(t(m1))))
39 :     c.nam <- paste("C",1:2, sep='')
40 :     dimnames(m1) <- list(NULL, c.nam)
41 :     stopifnot(colnames(m1) == c.nam,
42 :     identical(dimnames(t(m1)), list(c.nam, NULL)),
43 :     identical(m1, t(t(m1))))
44 : maechler 538
45 : maechler 620 ## an example of *named* dimnames
46 :     (t34N <- as(unclass(table(x = gl(3,4), y=gl(4,3))), "dgeMatrix"))
47 :     stopifnot(identical(dimnames(t34N),
48 :     dimnames(as(t34N, "matrix"))))
49 : maechler 579
50 : maechler 516 ## "dpo"
51 :     chk.matrix(cm <- crossprod(m1))
52 :     chk.matrix(as(cm, "dsyMatrix"))
53 : maechler 768 chk.matrix(dcm <- as(cm, "dgeMatrix"))
54 :     chk.matrix(mcm <- as(cm, "dMatrix"))
55 :     #BUG - FIXME: stopifnot(identical(dcm, mcm))
56 : maechler 577 try( chk.matrix(as(cm, "Matrix")) )# gives an error: "Matrix" has NULL 'dim()'
57 : maechler 516
58 :     ## Cholesky
59 :     chk.matrix(ch <- chol(cm))
60 : maechler 768 #if(FALSE)# fails for Doug in R-devel (2005-06-06) :
61 :     chk.matrix(ch2 <- chol(as(cm, "dsyMatrix")))
62 :     #not yet{FIXME}: chk.matrix(ch3 <- chol(as(cm, "dgeMatrix")))
63 :     #if(FALSE)# ...R-devel
64 :     stopifnot(all.equal(as(ch, "matrix"), as(ch2, "matrix")))
65 : maechler 538
66 :     ### Very basic triangular matrix stuff
67 :    
68 :     assertError( new("dtrMatrix", Dim = c(2,2), x= 1:4) )# double 'Dim'
69 : maechler 657 if(paste(R.version$major, R.version$minor, sep=".") >= "2.0.1")
70 : maechler 538 assertError( new("dtrMatrix", Dim = as.integer(c(2,2)), x= 1:4) )# int 'x'
71 : maechler 577 ## This caused a segfault (before revision r1172 in ../src/dtrMatrix.c):
72 : maechler 538 assertError( new("dtrMatrix", Dim = 2:2, x=as.double(1:4)) )# length(Dim) !=2
73 :     assertError( new("dtrMatrix", Dim = as.integer(c(2,2)), x= as.double(1:5)))
74 :    
75 :     tr22 <- new("dtrMatrix", Dim = as.integer(c(2,2)), x=as.double(1:4))
76 : maechler 579 tt22 <- t(tr22)
77 :     (tPt <- tr22 + tt22)
78 :     stopifnot(identical(10 * tPt, tPt * 10),
79 :     (t.22 <- (tr22 / .5)* .5)@x == c(1,0,3,4),
80 :     TRUE) ## not yet: class(t.22) == "dtrMatrix")
81 : maechler 538
82 : maechler 579
83 :     ## non-square triagonal Matrices --- should this be forbidden anyway? ---
84 : maechler 538 tru <- new("dtrMatrix", Dim = 2:3, x=as.double(1:6), uplo="L", diag="U")
85 :     trn <- new("dtrMatrix", Dim = 2:3, x=as.double(1:6), uplo="L", diag="N")
86 : maechler 577 tru + trn # a 'dgeMatrix'
87 : maechler 538
88 : maechler 579 as(t(tru),"dgeMatrix")
89 :     as(t(trn),"dgeMatrix")
90 :     as(t(t(tru)), "dgeMatrix")# pretty non sense

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