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

1 : maechler 607 #### Toplevel ``virtual'' class "Matrix"
2 :    
3 : maechler 956 ## ## probably not needed eventually:
4 :     ## setAs(from = "ddenseMatrix", to = "matrix",
5 :     ## function(from) {
6 :     ## if(length(d <- dim(from)) != 2) stop("dim(.) has not length 2")
7 :     ## array(from@x, dim = d, dimnames = dimnames(from))
8 :     ## })
9 : maechler 607
10 : maechler 676 ## should propagate to all subclasses:
11 : maechler 658 setMethod("as.matrix", signature(x = "Matrix"), function(x) as(x, "matrix"))
12 : maechler 956 ## for 'Matrix' objects, as.array() should be equivalent:
13 :     setMethod("as.array", signature(x = "Matrix"), function(x) as(x, "matrix"))
14 : maechler 658
15 : maechler 956 ## slow "fall back" method {subclasses should have faster ones}:
16 :     setMethod("as.vector", signature(x = "Matrix", mode = "missing"),
17 :     function(x) as.vector(as(x, "matrix")))
18 :    
19 :    
20 : maechler 886 ## Note that isSymmetric is *not* exported
21 :     setMethod("isSymmetric", signature(object = "symmetricMatrix"),
22 :     function(object) TRUE)
23 :     setMethod("isSymmetric", signature(object = "triangularMatrix"),
24 :     ## FIXME: 'TRUE' if *diagonal*, i.e. return(isDiagonal(object))
25 :     function(object) FALSE)
26 : maechler 925 setMethod("isDiagonal", signature(object = "sparseMatrix"),
27 :     function(object) {
28 :     gT <- as(object, "TsparseMatrix")
29 :     all(gT@i == gT@j)
30 :     })
31 : maechler 886
32 : maechler 579 setMethod("dim", signature(x = "Matrix"),
33 : maechler 698 function(x) x@Dim, valueClass = "integer")
34 : maechler 579 setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)
35 : maechler 607 ## not exported but used more than once for "dimnames<-" method :
36 :     ## -- or do only once for all "Matrix" classes ??
37 :     dimnamesGets <- function (x, value) {
38 :     d <- dim(x)
39 :     if (!is.list(value) || length(value) != 2 ||
40 : maechler 698 !(is.null(v1 <- value[[1]]) || length(v1) == d[1]) ||
41 :     !(is.null(v2 <- value[[2]]) || length(v2) == d[2]))
42 :     stop(sprintf("invalid dimnames given for '%s' object", class(x)))
43 : maechler 607 x@Dimnames <- list(if(!is.null(v1)) as.character(v1),
44 : maechler 698 if(!is.null(v2)) as.character(v2))
45 : maechler 607 x
46 :     }
47 :     setMethod("dimnames<-", signature(x = "Matrix", value = "list"),
48 : maechler 698 dimnamesGets)
49 : maechler 579
50 : maechler 658 setMethod("unname", signature("Matrix", force="missing"),
51 : maechler 698 function(obj) { obj@Dimnames <- list(NULL,NULL); obj})
52 : maechler 607
53 : bates 10 Matrix <-
54 : maechler 954 function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
55 :     sparse = NULL)
56 : bates 10 {
57 : maechler 954 sparseDefault <- function(m)
58 :     prod(dim(m)) > 2*sum(as(m, "matrix") != 0)
59 :    
60 :     i.M <- is(data, "Matrix")
61 :     if(is.null(sparse) && (i.M || is(data, "matrix")))
62 :     sparse <- sparseDefault(data)
63 :    
64 :     if (i.M) {
65 :     sM <- is(data,"sparseMatrix")
66 :     if((sparse && sM) || (!sparse && !sM))
67 :     return(data)
68 :     ## else : convert dense <-> sparse -> at end
69 :     }
70 :     else if (!is.matrix(data)) { ## cut & paste from "base::matrix" :
71 : maechler 698 if (missing(nrow))
72 :     nrow <- ceiling(length(data)/ncol)
73 :     else if (missing(ncol))
74 :     ncol <- ceiling(length(data)/nrow)
75 : maechler 954 data <- .Internal(matrix(data, nrow, ncol, byrow))
76 :     if(is.null(sparse))
77 :     sparse <- sparseDefault(data)
78 :     dimnames(data) <- dimnames
79 : bates 10 }
80 : maechler 954
81 :     ## 'data' is now a "matrix" or "Matrix"
82 :     ## FIXME: consider it's type (logical,....)
83 :     ## ctype <- substr(class(data), 1,1) # "d", "l", ...
84 :     ## FIXME(2): check for symmetric / triangular / ...
85 : maechler 965 ### TODO: Compare with as.Matrix() and its tests in ./dgeMatrix.R
86 : maechler 954 if(sparse)
87 :     as(data, "dgCMatrix")
88 :     else
89 :     as(data, "dgeMatrix")
90 : bates 10 }
91 :    
92 : bates 683 ## Methods for operations where one argument is numeric
93 : maechler 512
94 : maechler 698 ## Using as.matrix() and rbind()
95 :     ## in order to get dimnames from names {at least potentially}:
96 :    
97 : bates 683 setMethod("%*%", signature(x = "Matrix", y = "numeric"),
98 : maechler 698 function(x, y) callGeneric(x, as.matrix(y)))
99 : bates 683
100 :     setMethod("%*%", signature(x = "numeric", y = "Matrix"),
101 : maechler 698 function(x, y) callGeneric(rbind(x), y))
102 : bates 683
103 :     setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
104 : maechler 698 function(x, y = NULL) callGeneric(x, as.matrix(y)))
105 : bates 683
106 :     setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
107 : maechler 698 function(x, y = NULL) callGeneric(rbind(x), y))
108 : bates 683
109 :     setMethod("solve", signature(a = "Matrix", b = "numeric"),
110 : maechler 698 function(a, b, ...) callGeneric(a, as.matrix(b)))
111 : maechler 848
112 : maechler 925 ## bail-out methods in order to get better error messages
113 :     setMethod("%*%", signature(x = "Matrix", y = "Matrix"),
114 :     function (x, y)
115 :     stop(gettextf('not-yet-implemented method for <%s> %%*%% <%s>',
116 :     class(x), class(y))))
117 : maechler 946
118 : maechler 925 setMethod("crossprod", signature(x = "Matrix", y = "ANY"),
119 : maechler 946 function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
120 : maechler 956 setMethod("crossprod", signature(x = "ANY", y = "Matrix"),
121 :     function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
122 : maechler 925
123 : maechler 956 ## There are special sparse methods; this is a "fall back":
124 :     setMethod("kronecker", signature(X = "Matrix", Y = "ANY",
125 :     FUN = "ANY", make.dimnames = "ANY"),
126 :     function(X, Y, FUN, make.dimnames, ...) {
127 :     X <- as(X, "matrix") ; Matrix(callGeneric()) })
128 :     setMethod("kronecker", signature(X = "ANY", Y = "Matrix",
129 :     FUN = "ANY", make.dimnames = "ANY"),
130 :     function(X, Y, FUN, make.dimnames, ...) {
131 :     Y <- as(Y, "matrix") ; Matrix(callGeneric()) })
132 :    
133 :    
134 : maechler 946 setMethod("t", signature(x = "Matrix"),
135 :     function(x) .bail.out.1(.Generic, class(x)))
136 : maechler 925
137 : maechler 956 ## Group Methods
138 :     setMethod("+", signature(e1 = "Matrix", e2 = "missing"), function(e1) e1)
139 :     ## "fallback":
140 :     setMethod("-", signature(e1 = "Matrix", e2 = "missing"),
141 :     function(e1) {
142 :     warning("inefficient method used for \"- e1\"")
143 :     0-e1
144 :     })
145 :    
146 :     ## bail-outs:
147 : maechler 946 setMethod("Compare", signature(e1 = "Matrix", e2 = "Matrix"),
148 : maechler 954 function(e1, e2) {
149 :     d <- dimCheck(e1,e2)
150 :     .bail.out.2(.Generic, class(e1), class(e2))
151 :     })
152 : maechler 946 setMethod("Compare", signature(e1 = "Matrix", e2 = "ANY"),
153 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
154 :     setMethod("Compare", signature(e1 = "ANY", e2 = "Matrix"),
155 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
156 :    
157 :    
158 :    
159 : maechler 871 ### --------------------------------------------------------------------------
160 :     ###
161 :     ### Subsetting "[" and
162 :     ### SubAssign "[<-" : The "missing" cases can be dealt with here, "at the top":
163 : maechler 868
164 : maechler 886 ## Using "index" for indices should allow
165 : maechler 875 ## integer (numeric), logical, or character (names!) indices :
166 :    
167 : maechler 868 ## "x[]":
168 :     setMethod("[", signature(x = "Matrix",
169 :     i = "missing", j = "missing", drop = "ANY"),
170 :     function (x, i, j, drop) x)
171 :     ## missing 'drop' --> 'drop = TRUE'
172 :     ## -----------
173 :     ## select rows
174 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "missing",
175 : maechler 868 drop = "missing"),
176 :     function(x,i,j, drop) callGeneric(x, i=i, drop= TRUE))
177 :     ## select columns
178 : maechler 886 setMethod("[", signature(x = "Matrix", i = "missing", j = "index",
179 : maechler 868 drop = "missing"),
180 :     function(x,i,j, drop) callGeneric(x, j=j, drop= TRUE))
181 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "index",
182 : maechler 868 drop = "missing"),
183 :     function(x,i,j, drop) callGeneric(x, i=i, j=j, drop= TRUE))
184 :    
185 : maechler 875 ## bail out if any of (i,j,drop) is "non-sense"
186 :     setMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY", drop = "ANY"),
187 :     function(x,i,j, drop)
188 :     stop("invalid or not-yet-implemented 'Matrix' subsetting"))
189 :    
190 : maechler 868 ## "FIXME:"
191 :     ## How can we get at A[ ij ] where ij is (i,j) 2-column matrix?
192 :     ## and A[ LL ] where LL is a logical *vector*
193 : maechler 886 ## -> [.data.frame uses nargs() - can we do this in the *generic* ?
194 : maechler 868
195 :    
196 : maechler 871 ### "[<-" : -----------------
197 : maechler 868
198 : maechler 871 ## x[] <- value :
199 :     setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",
200 : maechler 886 value = "index"),## double/logical/...
201 : maechler 871 function (x, value) { x@x <- value ; validObject(x); x })
202 :    
203 : maechler 886 ## Otherwise (value is not "index"): bail out
204 : maechler 871 setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
205 :     value = "ANY"),
206 : maechler 875 function (x, i, j, value)
207 : maechler 886 if(!is(value,"index"))
208 :     stop("RHS 'value' must be of class \"index\"")
209 : maechler 925 else stop("not-yet-implemented 'Matrix[<-' method"))
210 : maechler 871
211 :    
212 :    
213 : maechler 908 ## NOTE: the following only works for R 2.2.x (and later) ---
214 :     ## ---- *and* 'Matrix' must have been *installed* by R >= 2.2.x
215 : maechler 875
216 : maechler 908 if(paste(R.version$major, R.version$minor, sep=".") >= "2.2") {
217 :    
218 :     ## The trivial methods :
219 :     setMethod("cbind2", signature(x = "Matrix", y = "NULL"),
220 :     function(x, y) x)
221 :     setMethod("cbind2", signature(x = "Matrix", y = "missing"),
222 :     function(x, y) x)
223 :     setMethod("cbind2", signature(x = "NULL", y="Matrix"),
224 :     function(x, y) x)
225 :    
226 : maechler 925 setMethod("rbind2", signature(x = "Matrix", y = "NULL"),
227 :     function(x, y) x)
228 :     setMethod("rbind2", signature(x = "Matrix", y = "missing"),
229 :     function(x, y) x)
230 :     setMethod("rbind2", signature(x = "NULL", y="Matrix"),
231 :     function(x, y) x)
232 :    
233 : maechler 908 ## Makes sure one gets x decent error message for the unimplemented cases:
234 :     setMethod("cbind2", signature(x = "Matrix", y = "Matrix"),
235 :     function(x, y) {
236 :     rowCheck(x,y)
237 :     stop(gettextf("cbind2() method for (%s,%s) not-yet defined",
238 :     class(x), class(y)))
239 :     })
240 :    
241 : maechler 956 ## Use a working fall back {particularly useful for sparse}:
242 :     ## FIXME: implement rbind2 via "cholmod" for C* and Tsparse ones
243 : maechler 908 setMethod("rbind2", signature(x = "Matrix", y = "Matrix"),
244 :     function(x, y) {
245 :     colCheck(x,y)
246 : maechler 956 t(cbind2(t(x), t(y)))
247 : maechler 908 })
248 : maechler 956
249 : maechler 908 }## R-2.2.x and newer

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