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 1174 - (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 1174 ## Note that isSymmetric is *not* exported
21 :     ## but that "base" has an isSymmetric() S3-generic since R 2.3.0
22 : maechler 1108 setMethod("isSymmetric", signature(object = "symmetricMatrix"),
23 : maechler 973 function(object,tol) TRUE)
24 : maechler 1108 setMethod("isSymmetric", signature(object = "triangularMatrix"),
25 : maechler 1174 ## TRUE iff diagonal:
26 :     function(object,tol) isDiagonal(object))
27 :     if(paste(R.version$major, R.version$minor, sep=".") < "2.3")
28 :     ## need a "matrix" method as in R 2.3 and later
29 :     setMethod("isSymmetric", signature(object = "matrix"),
30 :     function(object, tol = 100*.Machine$double.eps, ...)
31 :     {
32 :     ## pretest: is it square?
33 :     d <- dim(object)
34 :     if(d[1] != d[2]) return(FALSE)
35 :     test <-
36 :     if(is.complex(object))
37 :     all.equal.numeric(object, Conj(t(object)), tol = tol, ...)
38 :     else # numeric, character, ..
39 :     all.equal(object, t(object), tol = tol, ...)
40 :     isTRUE(test)
41 :     })
42 : maechler 973
43 : maechler 1174
44 : maechler 1165 setMethod("isTriangular", signature(object = "triangularMatrix"),
45 :     function(object,tol) TRUE)
46 : maechler 1174 setMethod("isTriangular", signature(object = "matrix"),
47 :     .is.triangular)
48 : maechler 1165
49 : maechler 1174 setMethod("isDiagonal", signature(object = "matrix"), .is.diagonal)
50 : maechler 886
51 : maechler 1174
52 :    
53 : maechler 579 setMethod("dim", signature(x = "Matrix"),
54 : maechler 698 function(x) x@Dim, valueClass = "integer")
55 : maechler 579 setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)
56 : maechler 607 ## not exported but used more than once for "dimnames<-" method :
57 :     ## -- or do only once for all "Matrix" classes ??
58 :     dimnamesGets <- function (x, value) {
59 :     d <- dim(x)
60 :     if (!is.list(value) || length(value) != 2 ||
61 : maechler 698 !(is.null(v1 <- value[[1]]) || length(v1) == d[1]) ||
62 :     !(is.null(v2 <- value[[2]]) || length(v2) == d[2]))
63 :     stop(sprintf("invalid dimnames given for '%s' object", class(x)))
64 : maechler 607 x@Dimnames <- list(if(!is.null(v1)) as.character(v1),
65 : maechler 698 if(!is.null(v2)) as.character(v2))
66 : maechler 607 x
67 :     }
68 :     setMethod("dimnames<-", signature(x = "Matrix", value = "list"),
69 : maechler 698 dimnamesGets)
70 : maechler 579
71 : maechler 658 setMethod("unname", signature("Matrix", force="missing"),
72 : maechler 698 function(obj) { obj@Dimnames <- list(NULL,NULL); obj})
73 : maechler 607
74 : bates 10 Matrix <-
75 : maechler 954 function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
76 : maechler 1174 sparse = NULL)
77 : bates 10 {
78 : maechler 954 sparseDefault <- function(m)
79 : maechler 1174 prod(dim(m)) > 2*sum(as(m, "matrix") != 0)
80 : maechler 954
81 :     i.M <- is(data, "Matrix")
82 :     if(is.null(sparse) && (i.M || is(data, "matrix")))
83 : maechler 1174 sparse <- sparseDefault(data)
84 : maechler 954
85 :     if (i.M) {
86 : maechler 1174 sM <- is(data,"sparseMatrix")
87 :     if((sparse && sM) || (!sparse && !sM))
88 :     return(data)
89 :     ## else : convert dense <-> sparse -> at end
90 : maechler 954 }
91 :     else if (!is.matrix(data)) { ## cut & paste from "base::matrix" :
92 : maechler 698 if (missing(nrow))
93 :     nrow <- ceiling(length(data)/ncol)
94 :     else if (missing(ncol))
95 :     ncol <- ceiling(length(data)/nrow)
96 : maechler 954 data <- .Internal(matrix(data, nrow, ncol, byrow))
97 : maechler 1174 if(is.null(sparse))
98 :     sparse <- sparseDefault(data)
99 : maechler 954 dimnames(data) <- dimnames
100 : bates 10 }
101 : maechler 954
102 :     ## 'data' is now a "matrix" or "Matrix"
103 : maechler 1174
104 :     ## check for symmetric / triangular / diagonal :
105 :     isSym <- isSymmetric(data)
106 :     if((isTri <- !isSym))
107 :     isTri <- isTriangular(data)
108 :     isDiag <- isSym # cannot be diagonal if it isn't symmetric
109 :     if(isDiag)
110 :     isDiag <- isDiagonal(data)
111 :    
112 : maechler 965 ### TODO: Compare with as.Matrix() and its tests in ./dgeMatrix.R
113 : maechler 1174
114 :     ## Find proper matrix class 'cl'
115 :     cl <-
116 :     if(isDiag)
117 :     "diagonalMatrix" # -> will automatically check for type
118 :     else {
119 :     ## consider it's type
120 :     ctype <-
121 :     if(is(data,"Matrix")) class(data)
122 :     else {
123 :     if("complex" == (ctype <- typeof(data)))
124 :     "z" else ctype
125 :     }
126 :     ctype <- substr(ctype, 1,1) # "d", "l", "i" or "z"
127 :     if(ctype == "z")
128 :     stop("complex matrices not yet implemented in Matrix package")
129 :     if(ctype == "i") {
130 :     warning("integer matrices not yet implemented in 'Matrix'; ",
131 :     "using 'double' ones'")
132 :     ctype <- "d"
133 :     }
134 :     paste(ctype,
135 :     if(sparse) {
136 :     if(isSym) "sCMatrix" else
137 :     if(isTri) "tCMatrix" else "gCMatrix"
138 :     } else { ## dense
139 :     if(isSym) "syMatrix" else
140 :     if(isTri) "trMatrix" else "geMatrix"
141 :     }, sep="")
142 :     }
143 :    
144 :     ## Now coerce and return
145 :     as(data, cl)
146 : bates 10 }
147 :    
148 : bates 683 ## Methods for operations where one argument is numeric
149 : maechler 512
150 : maechler 698 ## Using as.matrix() and rbind()
151 :     ## in order to get dimnames from names {at least potentially}:
152 :    
153 : bates 683 setMethod("%*%", signature(x = "Matrix", y = "numeric"),
154 : maechler 698 function(x, y) callGeneric(x, as.matrix(y)))
155 : bates 683
156 :     setMethod("%*%", signature(x = "numeric", y = "Matrix"),
157 : maechler 698 function(x, y) callGeneric(rbind(x), y))
158 : bates 683
159 :     setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
160 : maechler 698 function(x, y = NULL) callGeneric(x, as.matrix(y)))
161 : bates 683
162 :     setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
163 : maechler 698 function(x, y = NULL) callGeneric(rbind(x), y))
164 : bates 683
165 :     setMethod("solve", signature(a = "Matrix", b = "numeric"),
166 : maechler 698 function(a, b, ...) callGeneric(a, as.matrix(b)))
167 : maechler 848
168 : maechler 925 ## bail-out methods in order to get better error messages
169 :     setMethod("%*%", signature(x = "Matrix", y = "Matrix"),
170 :     function (x, y)
171 :     stop(gettextf('not-yet-implemented method for <%s> %%*%% <%s>',
172 :     class(x), class(y))))
173 : maechler 946
174 : maechler 925 setMethod("crossprod", signature(x = "Matrix", y = "ANY"),
175 : maechler 946 function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
176 : maechler 956 setMethod("crossprod", signature(x = "ANY", y = "Matrix"),
177 :     function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
178 : maechler 925
179 : maechler 956 ## There are special sparse methods; this is a "fall back":
180 :     setMethod("kronecker", signature(X = "Matrix", Y = "ANY",
181 :     FUN = "ANY", make.dimnames = "ANY"),
182 :     function(X, Y, FUN, make.dimnames, ...) {
183 :     X <- as(X, "matrix") ; Matrix(callGeneric()) })
184 :     setMethod("kronecker", signature(X = "ANY", Y = "Matrix",
185 :     FUN = "ANY", make.dimnames = "ANY"),
186 :     function(X, Y, FUN, make.dimnames, ...) {
187 :     Y <- as(Y, "matrix") ; Matrix(callGeneric()) })
188 :    
189 :    
190 : maechler 946 setMethod("t", signature(x = "Matrix"),
191 :     function(x) .bail.out.1(.Generic, class(x)))
192 : maechler 925
193 : maechler 956 ## Group Methods
194 :     setMethod("+", signature(e1 = "Matrix", e2 = "missing"), function(e1) e1)
195 :     ## "fallback":
196 :     setMethod("-", signature(e1 = "Matrix", e2 = "missing"),
197 :     function(e1) {
198 :     warning("inefficient method used for \"- e1\"")
199 :     0-e1
200 :     })
201 :    
202 :     ## bail-outs:
203 : maechler 946 setMethod("Compare", signature(e1 = "Matrix", e2 = "Matrix"),
204 : maechler 954 function(e1, e2) {
205 :     d <- dimCheck(e1,e2)
206 :     .bail.out.2(.Generic, class(e1), class(e2))
207 :     })
208 : maechler 946 setMethod("Compare", signature(e1 = "Matrix", e2 = "ANY"),
209 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
210 :     setMethod("Compare", signature(e1 = "ANY", e2 = "Matrix"),
211 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
212 :    
213 :    
214 :    
215 : maechler 871 ### --------------------------------------------------------------------------
216 :     ###
217 :     ### Subsetting "[" and
218 :     ### SubAssign "[<-" : The "missing" cases can be dealt with here, "at the top":
219 : maechler 868
220 : maechler 886 ## Using "index" for indices should allow
221 : maechler 875 ## integer (numeric), logical, or character (names!) indices :
222 :    
223 : maechler 868 ## "x[]":
224 :     setMethod("[", signature(x = "Matrix",
225 :     i = "missing", j = "missing", drop = "ANY"),
226 :     function (x, i, j, drop) x)
227 :     ## missing 'drop' --> 'drop = TRUE'
228 :     ## -----------
229 :     ## select rows
230 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "missing",
231 : maechler 868 drop = "missing"),
232 :     function(x,i,j, drop) callGeneric(x, i=i, drop= TRUE))
233 :     ## select columns
234 : maechler 886 setMethod("[", signature(x = "Matrix", i = "missing", j = "index",
235 : maechler 868 drop = "missing"),
236 :     function(x,i,j, drop) callGeneric(x, j=j, drop= TRUE))
237 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "index",
238 : maechler 868 drop = "missing"),
239 :     function(x,i,j, drop) callGeneric(x, i=i, j=j, drop= TRUE))
240 :    
241 : maechler 875 ## bail out if any of (i,j,drop) is "non-sense"
242 :     setMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY", drop = "ANY"),
243 :     function(x,i,j, drop)
244 :     stop("invalid or not-yet-implemented 'Matrix' subsetting"))
245 :    
246 : maechler 868 ## "FIXME:"
247 :     ## How can we get at A[ ij ] where ij is (i,j) 2-column matrix?
248 :     ## and A[ LL ] where LL is a logical *vector*
249 : maechler 886 ## -> [.data.frame uses nargs() - can we do this in the *generic* ?
250 : maechler 868
251 :    
252 : maechler 871 ### "[<-" : -----------------
253 : maechler 868
254 : maechler 871 ## x[] <- value :
255 :     setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",
256 : maechler 886 value = "index"),## double/logical/...
257 : maechler 871 function (x, value) { x@x <- value ; validObject(x); x })
258 :    
259 : maechler 886 ## Otherwise (value is not "index"): bail out
260 : maechler 871 setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
261 :     value = "ANY"),
262 : maechler 875 function (x, i, j, value)
263 : maechler 886 if(!is(value,"index"))
264 :     stop("RHS 'value' must be of class \"index\"")
265 : maechler 925 else stop("not-yet-implemented 'Matrix[<-' method"))
266 : maechler 871
267 :    
268 :    
269 : maechler 908 ## NOTE: the following only works for R 2.2.x (and later) ---
270 :     ## ---- *and* 'Matrix' must have been *installed* by R >= 2.2.x
271 : maechler 875
272 : maechler 908 if(paste(R.version$major, R.version$minor, sep=".") >= "2.2") {
273 :    
274 :     ## The trivial methods :
275 :     setMethod("cbind2", signature(x = "Matrix", y = "NULL"),
276 :     function(x, y) x)
277 :     setMethod("cbind2", signature(x = "Matrix", y = "missing"),
278 :     function(x, y) x)
279 :     setMethod("cbind2", signature(x = "NULL", y="Matrix"),
280 :     function(x, y) x)
281 :    
282 : maechler 925 setMethod("rbind2", signature(x = "Matrix", y = "NULL"),
283 :     function(x, y) x)
284 :     setMethod("rbind2", signature(x = "Matrix", y = "missing"),
285 :     function(x, y) x)
286 :     setMethod("rbind2", signature(x = "NULL", y="Matrix"),
287 :     function(x, y) x)
288 :    
289 : maechler 908 ## Makes sure one gets x decent error message for the unimplemented cases:
290 :     setMethod("cbind2", signature(x = "Matrix", y = "Matrix"),
291 :     function(x, y) {
292 :     rowCheck(x,y)
293 :     stop(gettextf("cbind2() method for (%s,%s) not-yet defined",
294 :     class(x), class(y)))
295 :     })
296 :    
297 : maechler 956 ## Use a working fall back {particularly useful for sparse}:
298 :     ## FIXME: implement rbind2 via "cholmod" for C* and Tsparse ones
299 : maechler 908 setMethod("rbind2", signature(x = "Matrix", y = "Matrix"),
300 :     function(x, y) {
301 :     colCheck(x,y)
302 : maechler 956 t(cbind2(t(x), t(y)))
303 : maechler 908 })
304 : maechler 956
305 : 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