SCM

SCM Repository

[matrix] Annotation of /pkg/R/Matrix.R
ViewVC logotype

Annotation of /pkg/R/Matrix.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 607 - (view) (download)

1 : maechler 607 #### Toplevel ``virtual'' class "Matrix"
2 :    
3 :     ## probably not needed eventually:
4 :     setAs(from = "Matrix", to = "matrix",
5 :     function(from) {
6 :     if(length(d <- dim(from)) != 2) stop("dim(.) has not length 2")
7 :     array(as.numeric(NA), dim = d, dimnames = dimnames(from))
8 :     })
9 :    
10 : maechler 512 prMatrix <-
11 :     ## private function to be used as show() method possibly more than once
12 :     function(object) {
13 :     d <- dim(object)
14 :     cat(paste(d, collapse= " x "), " Matrix of class ",
15 :     sQuote(class(object)),"\n", sep='')
16 :     m <- as(object, "matrix")
17 :     maxp <- getOption("max.print")
18 :     if(prod(d) <= maxp) print(m)
19 :     else { ## d[1] > maxp / d[2] >= nr :
20 :     nr <- maxp %/% d[2]
21 :     n2 <- ceiling(nr / 2)
22 :     print(head(m, max(1, n2)))
23 :     cat("\n ..........\n\n")
24 :     print(tail(m, max(1, nr - n2)))
25 :     }
26 :     ## DEBUG: cat("str(.):\n") ; str(object)
27 :     invisible()
28 :     }
29 : bates 10
30 : maechler 607 setMethod("show", signature(object = "ddenseMatrix"), prMatrix)
31 :     ## this may go away {since sparse matrices need something better!} :
32 : maechler 512 setMethod("show", signature(object = "Matrix"), prMatrix)
33 :    
34 : maechler 579 setMethod("dim", signature(x = "Matrix"),
35 :     function(x) x@Dim, valueClass = "integer")
36 :     setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)
37 : maechler 607 ## not exported but used more than once for "dimnames<-" method :
38 :     ## -- or do only once for all "Matrix" classes ??
39 :     dimnamesGets <- function (x, value) {
40 :     d <- dim(x)
41 :     if (!is.list(value) || length(value) != 2 ||
42 :     !(is.null(v1 <- value[[1]]) || length(v1) == d[1]) ||
43 :     !(is.null(v2 <- value[[2]]) || length(v2) == d[2]))
44 :     stop(sprintf("invalid dimnames given for '%s' object", class(x)))
45 :     x@Dimnames <- list(if(!is.null(v1)) as.character(v1),
46 :     if(!is.null(v2)) as.character(v2))
47 :     x
48 :     }
49 :     setMethod("dimnames<-", signature(x = "Matrix", value = "list"),
50 :     dimnamesGets)
51 : maechler 579
52 : maechler 607
53 : bates 10 Matrix <-
54 :     function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
55 :     {
56 :     if (is(data, "Matrix")) return(data)
57 :     if (is.matrix(data)) { val <- data }
58 : maechler 538 else { ## cut & paste from "base::matrix" :
59 : bates 10 if (missing(nrow))
60 :     nrow <- ceiling(length(data)/ncol)
61 :     else if (missing(ncol))
62 :     ncol <- ceiling(length(data)/nrow)
63 :     val <- .Internal(matrix(data, nrow, ncol, byrow))
64 :     dimnames(val) <- dimnames
65 :     }
66 : bates 477 as(val, "dgeMatrix")
67 : bates 10 }
68 :    
69 : maechler 512
70 : maechler 579 if(FALSE) { ##--- not-yet used -- {almost same code also in ./dgeMatrix.R }
71 : maechler 512
72 : maechler 579 ## utility for as.Matrix() {which is currently invalid }
73 : bates 10 Matrix.class <- function(x, tol = 0, symmetry = TRUE, unit.diagonal = TRUE,
74 :     triangularity = c(TRUE, TRUE),
75 : maechler 499 orthogonality = c(TRUE, TRUE),
76 :     normality = c(TRUE, TRUE))
77 : bates 10 {
78 :     val <- "Matrix"
79 :     x <- as.matrix(x)
80 :     if (symmetry) {
81 :     if (is.Hermitian(x, tol)) val <- c("Hermitian", val)
82 :     }
83 :     if (triangularity[1]) {
84 :     if (is.LowerTriangular(x, tol)) {
85 :     val <- c("LowerTriangular", val)
86 :     if (unit.diagonal)
87 :     if (max(Mod(diag(x) - 1)) <= tol)
88 :     val <- c("UnitLowerTriangular", val)
89 :     }
90 :     }
91 :     if (triangularity[2]) {
92 :     if (is.UpperTriangular(x, tol)) {
93 :     val <- c("UpperTriangular", val)
94 :     if (unit.diagonal)
95 :     if (max(Mod(diag(x) - 1)) <= tol)
96 :     val <- c("UnitUpperTriangular", val)
97 :     }
98 :     }
99 :     if (orthogonality[1]) {
100 :     if (is.ColOrthonormal(x, tol)) {
101 :     val <- c("ColOrthoNormal", "ColOrthogonal", val)
102 :     } else {
103 :     if (Orthogonal.test(x, normal = FALSE) <= tol)
104 :     val <- c("ColOrthogonal", val)
105 :     }
106 :     }
107 :     if (orthogonality[2]) {
108 :     if (normality[2] && is.RowOrthonormal(x, tol)) {
109 :     val <- c("RowOrthoNormal", "RowOrthogonal", val)
110 :     } else {
111 :     if (Orthogonal.test(x, byrow = TRUE, normal = FALSE) <= tol)
112 :     val <- c("RowOrthogonal", val)
113 :     }
114 :     }
115 :     val
116 :     }
117 :    
118 :     as.Matrix <- function(x, tol = .Machine$double.eps)
119 :     {
120 : maechler 499 asObject(if (inherits(x, "Matrix")) x else as.matrix(x),
121 :     Matrix.class(x, tol = tol))
122 : bates 10 }
123 : maechler 512
124 : maechler 579 }## not-yet used

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