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

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