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 658 - (view) (download)

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