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