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 657 - (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 579 setMethod("dim", signature(x = "Matrix"),
43 :     function(x) x@Dim, valueClass = "integer")
44 :     setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)
45 : maechler 607 ## not exported but used more than once for "dimnames<-" method :
46 :     ## -- or do only once for all "Matrix" classes ??
47 :     dimnamesGets <- function (x, value) {
48 :     d <- dim(x)
49 :     if (!is.list(value) || length(value) != 2 ||
50 :     !(is.null(v1 <- value[[1]]) || length(v1) == d[1]) ||
51 :     !(is.null(v2 <- value[[2]]) || length(v2) == d[2]))
52 :     stop(sprintf("invalid dimnames given for '%s' object", class(x)))
53 :     x@Dimnames <- list(if(!is.null(v1)) as.character(v1),
54 :     if(!is.null(v2)) as.character(v2))
55 :     x
56 :     }
57 :     setMethod("dimnames<-", signature(x = "Matrix", value = "list"),
58 :     dimnamesGets)
59 : maechler 579
60 : maechler 607
61 : bates 10 Matrix <-
62 :     function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
63 :     {
64 :     if (is(data, "Matrix")) return(data)
65 :     if (is.matrix(data)) { val <- data }
66 : maechler 538 else { ## cut & paste from "base::matrix" :
67 : bates 10 if (missing(nrow))
68 :     nrow <- ceiling(length(data)/ncol)
69 :     else if (missing(ncol))
70 :     ncol <- ceiling(length(data)/nrow)
71 :     val <- .Internal(matrix(data, nrow, ncol, byrow))
72 :     dimnames(val) <- dimnames
73 :     }
74 : bates 477 as(val, "dgeMatrix")
75 : bates 10 }
76 :    
77 : maechler 512
78 : maechler 579 if(FALSE) { ##--- not-yet used -- {almost same code also in ./dgeMatrix.R }
79 : maechler 512
80 : maechler 579 ## utility for as.Matrix() {which is currently invalid }
81 : bates 10 Matrix.class <- function(x, tol = 0, symmetry = TRUE, unit.diagonal = TRUE,
82 :     triangularity = c(TRUE, TRUE),
83 : maechler 499 orthogonality = c(TRUE, TRUE),
84 :     normality = c(TRUE, TRUE))
85 : bates 10 {
86 :     val <- "Matrix"
87 :     x <- as.matrix(x)
88 :     if (symmetry) {
89 :     if (is.Hermitian(x, tol)) val <- c("Hermitian", val)
90 :     }
91 :     if (triangularity[1]) {
92 :     if (is.LowerTriangular(x, tol)) {
93 :     val <- c("LowerTriangular", val)
94 :     if (unit.diagonal)
95 :     if (max(Mod(diag(x) - 1)) <= tol)
96 :     val <- c("UnitLowerTriangular", val)
97 :     }
98 :     }
99 :     if (triangularity[2]) {
100 :     if (is.UpperTriangular(x, tol)) {
101 :     val <- c("UpperTriangular", val)
102 :     if (unit.diagonal)
103 :     if (max(Mod(diag(x) - 1)) <= tol)
104 :     val <- c("UnitUpperTriangular", val)
105 :     }
106 :     }
107 :     if (orthogonality[1]) {
108 :     if (is.ColOrthonormal(x, tol)) {
109 :     val <- c("ColOrthoNormal", "ColOrthogonal", val)
110 :     } else {
111 :     if (Orthogonal.test(x, normal = FALSE) <= tol)
112 :     val <- c("ColOrthogonal", val)
113 :     }
114 :     }
115 :     if (orthogonality[2]) {
116 :     if (normality[2] && is.RowOrthonormal(x, tol)) {
117 :     val <- c("RowOrthoNormal", "RowOrthogonal", val)
118 :     } else {
119 :     if (Orthogonal.test(x, byrow = TRUE, normal = FALSE) <= tol)
120 :     val <- c("RowOrthogonal", val)
121 :     }
122 :     }
123 :     val
124 :     }
125 :    
126 :     as.Matrix <- function(x, tol = .Machine$double.eps)
127 :     {
128 : maechler 499 asObject(if (inherits(x, "Matrix")) x else as.matrix(x),
129 :     Matrix.class(x, tol = tol))
130 : bates 10 }
131 : maechler 512
132 : 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