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 850 - (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 850 paste(slotNames(M), collapse=","), ")\n", sep='')
10 : maechler 516 stopifnot(validObject(M),
11 : maechler 850 dim(M) == c(nrow(M), ncol(M)),
12 :     identical(dim(m <- as(M, "matrix")), dim(M))
13 :     )
14 : maechler 516 }
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 : maechler 850 stop(d.expr, "\n\t did not give an error", call. = FALSE)
22 : maechler 538 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 : maechler 834 chk.matrix(m2 <- Matrix(1:7, ncol=3)) # a (desired) warning
36 : maechler 850 stopifnot(unique(is(m1)) == c("dgeMatrix", "ddenseMatrix",
37 :     "dMatrix", "denseMatrix", "Matrix"),
38 :     dim(t(m1)) == 2:3,
39 :     identical(m1, t(t(m1))))
40 : maechler 579 c.nam <- paste("C",1:2, sep='')
41 :     dimnames(m1) <- list(NULL, c.nam)
42 :     stopifnot(colnames(m1) == c.nam,
43 : maechler 850 identical(dimnames(t(m1)), list(c.nam, NULL)),
44 :     identical(m1, t(t(m1))))
45 : maechler 538
46 : maechler 620 ## an example of *named* dimnames
47 :     (t34N <- as(unclass(table(x = gl(3,4), y=gl(4,3))), "dgeMatrix"))
48 :     stopifnot(identical(dimnames(t34N),
49 : maechler 850 dimnames(as(t34N, "matrix"))))
50 : maechler 579
51 : maechler 516 ## "dpo"
52 :     chk.matrix(cm <- crossprod(m1))
53 :     chk.matrix(as(cm, "dsyMatrix"))
54 : maechler 768 chk.matrix(dcm <- as(cm, "dgeMatrix"))
55 :     chk.matrix(mcm <- as(cm, "dMatrix"))
56 : maechler 834 ##BUG - FIXME: stopifnot(identical(dcm, mcm))
57 : maechler 850 ##--- ------ mcm[2,1] is garbage
58 : maechler 577 try( chk.matrix(as(cm, "Matrix")) )# gives an error: "Matrix" has NULL 'dim()'
59 : maechler 516
60 :     ## Cholesky
61 :     chk.matrix(ch <- chol(cm))
62 : maechler 768 #if(FALSE)# fails for Doug in R-devel (2005-06-06) :
63 :     chk.matrix(ch2 <- chol(as(cm, "dsyMatrix")))
64 :     #not yet{FIXME}: chk.matrix(ch3 <- chol(as(cm, "dgeMatrix")))
65 :     #if(FALSE)# ...R-devel
66 :     stopifnot(all.equal(as(ch, "matrix"), as(ch2, "matrix")))
67 : maechler 538
68 : maechler 850 ### Very basic triangular matrix stuff
69 : maechler 538
70 :     assertError( new("dtrMatrix", Dim = c(2,2), x= 1:4) )# double 'Dim'
71 : maechler 657 if(paste(R.version$major, R.version$minor, sep=".") >= "2.0.1")
72 : maechler 538 assertError( new("dtrMatrix", Dim = as.integer(c(2,2)), x= 1:4) )# int 'x'
73 : maechler 577 ## This caused a segfault (before revision r1172 in ../src/dtrMatrix.c):
74 : maechler 538 assertError( new("dtrMatrix", Dim = 2:2, x=as.double(1:4)) )# length(Dim) !=2
75 :     assertError( new("dtrMatrix", Dim = as.integer(c(2,2)), x= as.double(1:5)))
76 :    
77 :     tr22 <- new("dtrMatrix", Dim = as.integer(c(2,2)), x=as.double(1:4))
78 : maechler 579 tt22 <- t(tr22)
79 :     (tPt <- tr22 + tt22)
80 :     stopifnot(identical(10 * tPt, tPt * 10),
81 : maechler 850 (t.22 <- (tr22 / .5)* .5)@x == c(1,0,3,4),
82 :     TRUE) ## not yet: class(t.22) == "dtrMatrix")
83 : maechler 538
84 : maechler 579
85 : maechler 850 ## non-square triagonal Matrices --- are forbidden ---
86 :     try(tru <- new("dtrMatrix", Dim = 2:3,
87 :     x=as.double(1:6), uplo="L", diag="U"))

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