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 868 - (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 : maechler 698 if(length(d <- dim(from)) != 2) stop("dim(.) has not length 2")
7 :     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 698 m <- as(object, "matrix")
16 :     maxp <- getOption("max.print")
17 :     if(prod(d) <= maxp) print(m)
18 :     else { ## d[1] > maxp / d[2] >= nr :
19 :     nr <- maxp %/% d[2]
20 :     n2 <- ceiling(nr / 2)
21 :     print(head(m, max(1, n2)))
22 :     cat("\n ..........\n\n")
23 :     print(tail(m, max(1, nr - n2)))
24 :     }
25 :     ## DEBUG: cat("str(.):\n") ; str(object)
26 : maechler 676 invisible(object)# as print() S3 methods do
27 : maechler 618 }
28 : bates 10
29 : maechler 607 setMethod("show", signature(object = "ddenseMatrix"), prMatrix)
30 : maechler 676
31 : maechler 698 ##- ## FIXME: The following is only for the "dMatrix" objects that are not
32 :     ##- ## "dense" nor "sparse" -- i.e. "packed" ones :
33 :     ##- ## But these could be printed better -- "." for structural zeros.
34 :     ##- setMethod("show", signature(object = "dMatrix"), prMatrix)
35 :     ##- ## and improve this as well:
36 :     ##- setMethod("show", signature(object = "pMatrix"), prMatrix)
37 :     ##- ## this should now be superfluous [keep for safety for the moment]:
38 : maechler 512 setMethod("show", signature(object = "Matrix"), prMatrix)
39 :    
40 : maechler 676 ## should propagate to all subclasses:
41 : maechler 658 setMethod("as.matrix", signature(x = "Matrix"), function(x) as(x, "matrix"))
42 :    
43 : maechler 579 setMethod("dim", signature(x = "Matrix"),
44 : maechler 698 function(x) x@Dim, valueClass = "integer")
45 : maechler 579 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 : maechler 698 !(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 : maechler 607 x@Dimnames <- list(if(!is.null(v1)) as.character(v1),
55 : maechler 698 if(!is.null(v2)) as.character(v2))
56 : maechler 607 x
57 :     }
58 :     setMethod("dimnames<-", signature(x = "Matrix", value = "list"),
59 : maechler 698 dimnamesGets)
60 : maechler 579
61 : maechler 658 setMethod("unname", signature("Matrix", force="missing"),
62 : maechler 698 function(obj) { obj@Dimnames <- list(NULL,NULL); obj})
63 : maechler 607
64 : bates 10 Matrix <-
65 :     function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
66 :     {
67 :     if (is(data, "Matrix")) return(data)
68 :     if (is.matrix(data)) { val <- data }
69 : maechler 538 else { ## cut & paste from "base::matrix" :
70 : maechler 698 if (missing(nrow))
71 :     nrow <- ceiling(length(data)/ncol)
72 :     else if (missing(ncol))
73 :     ncol <- ceiling(length(data)/nrow)
74 :     val <- .Internal(matrix(data, nrow, ncol, byrow))
75 :     dimnames(val) <- dimnames
76 : bates 10 }
77 : bates 477 as(val, "dgeMatrix")
78 : bates 10 }
79 :    
80 : bates 683 ## Methods for operations where one argument is numeric
81 : maechler 512
82 : maechler 698 ## Using as.matrix() and rbind()
83 :     ## in order to get dimnames from names {at least potentially}:
84 :    
85 : bates 683 setMethod("%*%", signature(x = "Matrix", y = "numeric"),
86 : maechler 698 function(x, y) callGeneric(x, as.matrix(y)))
87 : bates 683
88 :     setMethod("%*%", signature(x = "numeric", y = "Matrix"),
89 : maechler 698 function(x, y) callGeneric(rbind(x), y))
90 : bates 683
91 :     setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
92 : maechler 698 function(x, y = NULL) callGeneric(x, as.matrix(y)))
93 : bates 683
94 :     setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
95 : maechler 698 function(x, y = NULL) callGeneric(rbind(x), y))
96 : bates 683
97 :     setMethod("solve", signature(a = "Matrix", b = "numeric"),
98 : maechler 698 function(a, b, ...) callGeneric(a, as.matrix(b)))
99 : maechler 848
100 : maechler 868 ## Subsetting : The "missing" cases can be dealt with here, "at the top":
101 :    
102 :     ## "x[]":
103 :     setMethod("[", signature(x = "Matrix",
104 :     i = "missing", j = "missing", drop = "ANY"),
105 :     function (x, i, j, drop) x)
106 :    
107 :     ## missing 'drop' --> 'drop = TRUE'
108 :     ## -----------
109 :     ## select rows
110 :     setMethod("[", signature(x = "Matrix", i = "numeric", j = "missing",
111 :     drop = "missing"),
112 :     function(x,i,j, drop) callGeneric(x, i=i, drop= TRUE))
113 :     ## select columns
114 :     setMethod("[", signature(x = "Matrix", i = "missing", j = "numeric",
115 :     drop = "missing"),
116 :     function(x,i,j, drop) callGeneric(x, j=j, drop= TRUE))
117 :     setMethod("[", signature(x = "Matrix", i = "numeric", j = "numeric",
118 :     drop = "missing"),
119 :     function(x,i,j, drop) callGeneric(x, i=i, j=j, drop= TRUE))
120 :    
121 :    
122 :     ## "FIXME:"
123 :     ## How can we get at A[ ij ] where ij is (i,j) 2-column matrix?
124 :     ## and A[ LL ] where LL is a logical *vector*
125 :    
126 :    
127 :    
128 :    
129 : maechler 848 if(FALSE) ## The following can't work as long as cbind is function(..., *)
130 :     setMethod("cbind", signature(a = "Matrix", b = "Matrix"),
131 :     function(a, b, ...) {
132 :     da <- Dim(a)
133 :     db <- Dim(b)
134 :     if(da[1] != db[1])
135 :     stop("Matrices must have same number of rows for cbind()ing")
136 :     })
137 : maechler 868
138 :    
139 :    

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