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

1 : maechler 607 #### Toplevel ``virtual'' class "Matrix"
2 :    
3 : maechler 1331
4 :     ### Virtual coercions -- via smart "helpers" (-> ./Auxiliaries.R)
5 :    
6 :     setAs("Matrix", "sparseMatrix", function(from) as_Csparse(from))
7 :     setAs("Matrix", "denseMatrix", function(from) as_dense(from))
8 :    
9 : maechler 956 ## ## probably not needed eventually:
10 :     ## setAs(from = "ddenseMatrix", to = "matrix",
11 :     ## function(from) {
12 :     ## if(length(d <- dim(from)) != 2) stop("dim(.) has not length 2")
13 :     ## array(from@x, dim = d, dimnames = dimnames(from))
14 :     ## })
15 : maechler 607
16 : maechler 676 ## should propagate to all subclasses:
17 : maechler 658 setMethod("as.matrix", signature(x = "Matrix"), function(x) as(x, "matrix"))
18 : maechler 956 ## for 'Matrix' objects, as.array() should be equivalent:
19 :     setMethod("as.array", signature(x = "Matrix"), function(x) as(x, "matrix"))
20 : maechler 658
21 : maechler 1347 ## head and tail apply to all Matrix objects for which subscripting is allowed:
22 : maechler 1390 ## if(paste(R.version$major, R.version$minor, sep=".") < "2.4") {
23 : maechler 1389 setMethod("head", signature(x = "Matrix"), utils:::head.matrix)
24 :     setMethod("tail", signature(x = "Matrix"), utils:::tail.matrix)
25 : maechler 1390 ## } else { # R 2.4.0 and newer
26 :     ## setMethod("head", signature(x = "Matrix"), utils::head.matrix)
27 :     ## setMethod("tail", signature(x = "Matrix"), utils::tail.matrix)
28 :     ## }
29 :    
30 : maechler 956 ## slow "fall back" method {subclasses should have faster ones}:
31 :     setMethod("as.vector", signature(x = "Matrix", mode = "missing"),
32 : maechler 1331 function(x) as.vector(as(x, "matrix")))
33 : maechler 956
34 : maechler 1389 ## mainly need these for "dMatrix" or "lMatrix" respectively, but why not general:
35 :     setMethod("as.numeric", signature(x = "Matrix"),
36 :     function(x, ...) as.numeric(as.vector(x)))
37 :     setMethod("as.logical", signature(x = "Matrix"),
38 :     function(x, ...) as.logical(as.vector(x)))
39 : maechler 956
40 : maechler 1389
41 : maechler 1174 ## Note that isSymmetric is *not* exported
42 :     ## but that "base" has an isSymmetric() S3-generic since R 2.3.0
43 : maechler 1108 setMethod("isSymmetric", signature(object = "symmetricMatrix"),
44 : maechler 973 function(object,tol) TRUE)
45 : maechler 1108 setMethod("isSymmetric", signature(object = "triangularMatrix"),
46 : maechler 1174 ## TRUE iff diagonal:
47 :     function(object,tol) isDiagonal(object))
48 : maechler 1189
49 : maechler 1174 if(paste(R.version$major, R.version$minor, sep=".") < "2.3")
50 :     ## need a "matrix" method as in R 2.3 and later
51 :     setMethod("isSymmetric", signature(object = "matrix"),
52 : maechler 1189 function(object, tol = 100*.Machine$double.eps, ...)
53 :     {
54 :     ## pretest: is it square?
55 :     d <- dim(object)
56 :     if(d[1] != d[2]) return(FALSE)
57 :     ## for `broken' all.equal in R <= 2.2.x:
58 :     dn <- dimnames(object)
59 :     if(!identical(dn[1], dn[2])) return(FALSE)
60 :     test <-
61 :     if(is.complex(object))
62 :     all.equal.numeric(object, Conj(t(object)), tol = tol, ...)
63 :     else # numeric, character, ..
64 :     all.equal(object, t(object), tol = tol, ...)
65 :     isTRUE(test)
66 :     })
67 : maechler 973
68 : maechler 1174
69 : maechler 1165 setMethod("isTriangular", signature(object = "triangularMatrix"),
70 : maechler 1238 function(object, ...) TRUE)
71 : maechler 1165
72 : maechler 1238 setMethod("isTriangular", signature(object = "matrix"), isTriMat)
73 :    
74 : maechler 1174 setMethod("isDiagonal", signature(object = "matrix"), .is.diagonal)
75 : maechler 886
76 : maechler 1174
77 :    
78 : maechler 579 setMethod("dim", signature(x = "Matrix"),
79 : maechler 698 function(x) x@Dim, valueClass = "integer")
80 : maechler 579 setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)
81 : maechler 607 ## not exported but used more than once for "dimnames<-" method :
82 :     ## -- or do only once for all "Matrix" classes ??
83 :     dimnamesGets <- function (x, value) {
84 :     d <- dim(x)
85 :     if (!is.list(value) || length(value) != 2 ||
86 : maechler 698 !(is.null(v1 <- value[[1]]) || length(v1) == d[1]) ||
87 :     !(is.null(v2 <- value[[2]]) || length(v2) == d[2]))
88 :     stop(sprintf("invalid dimnames given for '%s' object", class(x)))
89 : maechler 607 x@Dimnames <- list(if(!is.null(v1)) as.character(v1),
90 : maechler 698 if(!is.null(v2)) as.character(v2))
91 : maechler 607 x
92 :     }
93 :     setMethod("dimnames<-", signature(x = "Matrix", value = "list"),
94 : maechler 698 dimnamesGets)
95 : maechler 579
96 : maechler 658 setMethod("unname", signature("Matrix", force="missing"),
97 : maechler 698 function(obj) { obj@Dimnames <- list(NULL,NULL); obj})
98 : maechler 607
99 : bates 10 Matrix <-
100 : maechler 954 function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
101 : maechler 1455 sparse = NULL, forceCheck = FALSE)
102 : bates 10 {
103 : maechler 954 sparseDefault <- function(m)
104 : maechler 1174 prod(dim(m)) > 2*sum(as(m, "matrix") != 0)
105 : maechler 954
106 :     i.M <- is(data, "Matrix")
107 :     if(is.null(sparse) && (i.M || is(data, "matrix")))
108 : maechler 1174 sparse <- sparseDefault(data)
109 : maechler 954
110 : maechler 1455 doDN <- TRUE
111 :     if (i.M && !forceCheck) {
112 : maechler 1174 sM <- is(data,"sparseMatrix")
113 :     if((sparse && sM) || (!sparse && !sM))
114 :     return(data)
115 :     ## else : convert dense <-> sparse -> at end
116 : maechler 954 }
117 :     else if (!is.matrix(data)) { ## cut & paste from "base::matrix" :
118 : maechler 698 if (missing(nrow))
119 :     nrow <- ceiling(length(data)/ncol)
120 :     else if (missing(ncol))
121 :     ncol <- ceiling(length(data)/nrow)
122 : maechler 1315 if(length(data) == 1 && data == 0 && !identical(sparse,FALSE)) {
123 :     if(is.null(sparse)) sparse <- TRUE
124 :     ## will be sparse: do NOT construct full matrix!
125 :     data <- new(if(is.numeric(data)) "dgTMatrix" else
126 :     if(is.logical(data)) "lgTMatrix" else
127 :     stop("invalid 'data'"),
128 :     Dim = as.integer(c(nrow,ncol)),
129 : maechler 1332 Dimnames = if(is.null(dimnames)) list(NULL,NULL)
130 :     else dimnames)
131 : maechler 1315 } else { ## normal case
132 :     data <- .Internal(matrix(data, nrow, ncol, byrow))
133 :     if(is.null(sparse))
134 :     sparse <- sparseDefault(data)
135 :     dimnames(data) <- dimnames
136 :     }
137 : maechler 1455 doDN <- FALSE
138 :     }
139 :     ## 'data' is now a "matrix" or "Matrix"
140 :     if (doDN && !is.null(dimnames))
141 : maechler 1189 dimnames(data) <- dimnames
142 : maechler 954
143 : maechler 1174 ## check for symmetric / triangular / diagonal :
144 :     isSym <- isSymmetric(data)
145 :     if((isTri <- !isSym))
146 :     isTri <- isTriangular(data)
147 :     isDiag <- isSym # cannot be diagonal if it isn't symmetric
148 :     if(isDiag)
149 :     isDiag <- isDiagonal(data)
150 :    
151 : maechler 965 ### TODO: Compare with as.Matrix() and its tests in ./dgeMatrix.R
152 : maechler 1174
153 :     ## Find proper matrix class 'cl'
154 :     cl <-
155 :     if(isDiag)
156 :     "diagonalMatrix" # -> will automatically check for type
157 :     else {
158 :     ## consider it's type
159 :     ctype <-
160 :     if(is(data,"Matrix")) class(data)
161 :     else {
162 :     if("complex" == (ctype <- typeof(data)))
163 :     "z" else ctype
164 :     }
165 :     ctype <- substr(ctype, 1,1) # "d", "l", "i" or "z"
166 :     if(ctype == "z")
167 :     stop("complex matrices not yet implemented in Matrix package")
168 :     if(ctype == "i") {
169 :     warning("integer matrices not yet implemented in 'Matrix'; ",
170 :     "using 'double' ones'")
171 :     ctype <- "d"
172 :     }
173 :     paste(ctype,
174 :     if(sparse) {
175 :     if(isSym) "sCMatrix" else
176 :     if(isTri) "tCMatrix" else "gCMatrix"
177 :     } else { ## dense
178 :     if(isSym) "syMatrix" else
179 :     if(isTri) "trMatrix" else "geMatrix"
180 :     }, sep="")
181 :     }
182 :    
183 :     ## Now coerce and return
184 :     as(data, cl)
185 : bates 10 }
186 :    
187 : bates 683 ## Methods for operations where one argument is numeric
188 : maechler 512
189 : maechler 698 ## Using as.matrix() and rbind()
190 :     ## in order to get dimnames from names {at least potentially}:
191 :    
192 : bates 683 setMethod("%*%", signature(x = "Matrix", y = "numeric"),
193 : maechler 698 function(x, y) callGeneric(x, as.matrix(y)))
194 : bates 683
195 :     setMethod("%*%", signature(x = "numeric", y = "Matrix"),
196 : maechler 698 function(x, y) callGeneric(rbind(x), y))
197 : bates 683
198 :     setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
199 : maechler 698 function(x, y = NULL) callGeneric(x, as.matrix(y)))
200 : bates 683 setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
201 : maechler 1331 function(x, y = NULL) callGeneric(as.matrix(x), y))
202 : bates 683
203 : maechler 1331 ## The as.matrix() promotion seems illogical to MM,
204 :     ## but is according to help(tcrossprod, package = "base") :
205 :     setMethod("tcrossprod", signature(x = "Matrix", y = "numeric"),
206 :     function(x, y = NULL) callGeneric(x, as.matrix(y)))
207 :     setMethod("tcrossprod", signature(x = "numeric", y = "Matrix"),
208 :     function(x, y = NULL) callGeneric(as.matrix(x), y))
209 :    
210 : bates 683 setMethod("solve", signature(a = "Matrix", b = "numeric"),
211 : maechler 698 function(a, b, ...) callGeneric(a, as.matrix(b)))
212 : maechler 848
213 : maechler 925 ## bail-out methods in order to get better error messages
214 :     setMethod("%*%", signature(x = "Matrix", y = "Matrix"),
215 :     function (x, y)
216 :     stop(gettextf('not-yet-implemented method for <%s> %%*%% <%s>',
217 :     class(x), class(y))))
218 : maechler 946
219 : maechler 925 setMethod("crossprod", signature(x = "Matrix", y = "ANY"),
220 : maechler 946 function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
221 : maechler 956 setMethod("crossprod", signature(x = "ANY", y = "Matrix"),
222 :     function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
223 : maechler 1331 setMethod("tcrossprod", signature(x = "Matrix", y = "ANY"),
224 :     function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
225 :     setMethod("tcrossprod", signature(x = "ANY", y = "Matrix"),
226 :     function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
227 : maechler 925
228 : maechler 1331 ## cheap fallbacks
229 :     setMethod("crossprod", signature(x = "Matrix", y = "Matrix"),
230 :     function(x, y = NULL) t(x) %*% y)
231 :     setMethod("tcrossprod", signature(x = "Matrix", y = "Matrix"),
232 :     function(x, y = NULL) x %*% t(y))
233 :    
234 : maechler 956 ## There are special sparse methods; this is a "fall back":
235 :     setMethod("kronecker", signature(X = "Matrix", Y = "ANY",
236 :     FUN = "ANY", make.dimnames = "ANY"),
237 :     function(X, Y, FUN, make.dimnames, ...) {
238 :     X <- as(X, "matrix") ; Matrix(callGeneric()) })
239 :     setMethod("kronecker", signature(X = "ANY", Y = "Matrix",
240 :     FUN = "ANY", make.dimnames = "ANY"),
241 :     function(X, Y, FUN, make.dimnames, ...) {
242 :     Y <- as(Y, "matrix") ; Matrix(callGeneric()) })
243 :    
244 :    
245 : maechler 1315 setMethod("diag", signature(x = "Matrix"),
246 :     function(x, nrow, ncol) .bail.out.1(.Generic, class(x)))
247 : maechler 946 setMethod("t", signature(x = "Matrix"),
248 :     function(x) .bail.out.1(.Generic, class(x)))
249 : maechler 925
250 : maechler 956 ## Group Methods
251 :     setMethod("+", signature(e1 = "Matrix", e2 = "missing"), function(e1) e1)
252 :     ## "fallback":
253 :     setMethod("-", signature(e1 = "Matrix", e2 = "missing"),
254 :     function(e1) {
255 :     warning("inefficient method used for \"- e1\"")
256 :     0-e1
257 :     })
258 :    
259 :     ## bail-outs:
260 : maechler 946 setMethod("Compare", signature(e1 = "Matrix", e2 = "Matrix"),
261 : maechler 954 function(e1, e2) {
262 :     d <- dimCheck(e1,e2)
263 :     .bail.out.2(.Generic, class(e1), class(e2))
264 :     })
265 : maechler 946 setMethod("Compare", signature(e1 = "Matrix", e2 = "ANY"),
266 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
267 :     setMethod("Compare", signature(e1 = "ANY", e2 = "Matrix"),
268 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
269 :    
270 :    
271 :    
272 : maechler 871 ### --------------------------------------------------------------------------
273 :     ###
274 :     ### Subsetting "[" and
275 :     ### SubAssign "[<-" : The "missing" cases can be dealt with here, "at the top":
276 : maechler 868
277 : maechler 886 ## Using "index" for indices should allow
278 : maechler 875 ## integer (numeric), logical, or character (names!) indices :
279 :    
280 : maechler 868 ## "x[]":
281 :     setMethod("[", signature(x = "Matrix",
282 :     i = "missing", j = "missing", drop = "ANY"),
283 :     function (x, i, j, drop) x)
284 : maechler 1225
285 : maechler 868 ## missing 'drop' --> 'drop = TRUE'
286 :     ## -----------
287 :     ## select rows
288 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "missing",
289 : maechler 868 drop = "missing"),
290 :     function(x,i,j, drop) callGeneric(x, i=i, drop= TRUE))
291 :     ## select columns
292 : maechler 886 setMethod("[", signature(x = "Matrix", i = "missing", j = "index",
293 : maechler 868 drop = "missing"),
294 :     function(x,i,j, drop) callGeneric(x, j=j, drop= TRUE))
295 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "index",
296 : maechler 868 drop = "missing"),
297 :     function(x,i,j, drop) callGeneric(x, i=i, j=j, drop= TRUE))
298 :    
299 : maechler 875 ## bail out if any of (i,j,drop) is "non-sense"
300 :     setMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY", drop = "ANY"),
301 :     function(x,i,j, drop)
302 :     stop("invalid or not-yet-implemented 'Matrix' subsetting"))
303 :    
304 : maechler 1225 ## "logical *vector* indexing, such as M [ M >= 10 ] :
305 :     setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",
306 :     drop = "ANY"),
307 :     function (x, i, j, drop) {
308 :     as(x, geClass(x))@x[as.vector(i)]
309 : maechler 1329 ## -> error when lengths don't match
310 : maechler 1225 })
311 :    
312 : maechler 1329 ## FIXME: The following is good for M [ <logical> ]
313 :     ## *BUT* it also triggers for M [ <logical> , ] where it is *WRONG*
314 :     ## using nargs() does not help: it gives '3' for both cases
315 :     if(FALSE)
316 : maechler 1225 setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",
317 :     drop = "ANY"),
318 : maechler 1329 function (x, i, j, drop) {
319 :     ## DEBUG
320 :     cat("[(Matrix,i,..): nargs=", nargs(),"\n")
321 :     as(x, geClass(x))@x[i] })
322 : maechler 1225
323 :    
324 : maechler 868 ## "FIXME:"
325 :     ## How can we get at A[ ij ] where ij is (i,j) 2-column matrix?
326 :     ## and A[ LL ] where LL is a logical *vector*
327 : maechler 886 ## -> [.data.frame uses nargs() - can we do this in the *generic* ?
328 : maechler 868
329 :    
330 : maechler 871 ### "[<-" : -----------------
331 : maechler 868
332 : maechler 871 ## x[] <- value :
333 :     setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",
334 : maechler 1189 value = "ANY"),## double/logical/...
335 :     function (x, value) {
336 :     x@x <- value
337 :     validObject(x)# check if type and lengths above match
338 :     x
339 :     })
340 : maechler 871
341 : maechler 1189 ## Method for all 'Matrix' kinds (rather than incomprehensible error messages);
342 :     ## (ANY,ANY,ANY) is used when no `real method' is implemented :
343 : maechler 871 setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
344 :     value = "ANY"),
345 : maechler 1189 function (x, i, j, value) {
346 :     if(!is.atomic(value))
347 :     stop("RHS 'value' must match matrix class ", class(x))
348 :     else stop("not-yet-implemented 'Matrix[<-' method")
349 :     })
350 : maechler 871
351 :    
352 : maechler 1290 ## The trivial methods :
353 :     setMethod("cbind2", signature(x = "Matrix", y = "NULL"),
354 :     function(x, y) x)
355 :     setMethod("cbind2", signature(x = "Matrix", y = "missing"),
356 :     function(x, y) x)
357 :     setMethod("cbind2", signature(x = "NULL", y="Matrix"),
358 :     function(x, y) x)
359 : maechler 871
360 : maechler 1290 setMethod("rbind2", signature(x = "Matrix", y = "NULL"),
361 :     function(x, y) x)
362 :     setMethod("rbind2", signature(x = "Matrix", y = "missing"),
363 :     function(x, y) x)
364 :     setMethod("rbind2", signature(x = "NULL", y="Matrix"),
365 :     function(x, y) x)
366 : maechler 875
367 : maechler 1290 ## Makes sure one gets x decent error message for the unimplemented cases:
368 :     setMethod("cbind2", signature(x = "Matrix", y = "Matrix"),
369 :     function(x, y) {
370 :     rowCheck(x,y)
371 :     stop(gettextf("cbind2() method for (%s,%s) not-yet defined",
372 :     class(x), class(y)))
373 :     })
374 : maechler 908
375 : maechler 1290 ## Use a working fall back {particularly useful for sparse}:
376 :     ## FIXME: implement rbind2 via "cholmod" for C* and Tsparse ones
377 :     setMethod("rbind2", signature(x = "Matrix", y = "Matrix"),
378 :     function(x, y) {
379 :     colCheck(x,y)
380 :     t(cbind2(t(x), t(y)))
381 :     })

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