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

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