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 1315 - (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 : maechler 1189
28 : maechler 1174 if(paste(R.version$major, R.version$minor, sep=".") < "2.3")
29 :     ## need a "matrix" method as in R 2.3 and later
30 :     setMethod("isSymmetric", signature(object = "matrix"),
31 : maechler 1189 function(object, tol = 100*.Machine$double.eps, ...)
32 :     {
33 :     ## pretest: is it square?
34 :     d <- dim(object)
35 :     if(d[1] != d[2]) return(FALSE)
36 :     ## for `broken' all.equal in R <= 2.2.x:
37 :     dn <- dimnames(object)
38 :     if(!identical(dn[1], dn[2])) return(FALSE)
39 :     test <-
40 :     if(is.complex(object))
41 :     all.equal.numeric(object, Conj(t(object)), tol = tol, ...)
42 :     else # numeric, character, ..
43 :     all.equal(object, t(object), tol = tol, ...)
44 :     isTRUE(test)
45 :     })
46 : maechler 973
47 : maechler 1174
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 1174 sparse = NULL)
81 : bates 10 {
82 : maechler 954 sparseDefault <- function(m)
83 : maechler 1174 prod(dim(m)) > 2*sum(as(m, "matrix") != 0)
84 : maechler 954
85 :     i.M <- is(data, "Matrix")
86 :     if(is.null(sparse) && (i.M || is(data, "matrix")))
87 : maechler 1174 sparse <- sparseDefault(data)
88 : maechler 954
89 :     if (i.M) {
90 : maechler 1174 sM <- is(data,"sparseMatrix")
91 :     if((sparse && sM) || (!sparse && !sM))
92 :     return(data)
93 :     ## else : convert dense <-> sparse -> at end
94 : maechler 954 }
95 :     else if (!is.matrix(data)) { ## cut & paste from "base::matrix" :
96 : maechler 698 if (missing(nrow))
97 :     nrow <- ceiling(length(data)/ncol)
98 :     else if (missing(ncol))
99 :     ncol <- ceiling(length(data)/nrow)
100 : maechler 1315 if(length(data) == 1 && data == 0 && !identical(sparse,FALSE)) {
101 :     if(is.null(sparse)) sparse <- TRUE
102 :     ## will be sparse: do NOT construct full matrix!
103 :     data <- new(if(is.numeric(data)) "dgTMatrix" else
104 :     if(is.logical(data)) "lgTMatrix" else
105 :     stop("invalid 'data'"),
106 :     Dim = as.integer(c(nrow,ncol)),
107 :     Dimnames = if(is.null(dimnames))
108 :     list(NULL,NULL) else dimnames)
109 :     } else { ## normal case
110 :     data <- .Internal(matrix(data, nrow, ncol, byrow))
111 :     if(is.null(sparse))
112 :     sparse <- sparseDefault(data)
113 :     dimnames(data) <- dimnames
114 :     }
115 : maechler 1189 } else if (!is.null(dimnames))
116 :     dimnames(data) <- dimnames
117 : maechler 954
118 :     ## 'data' is now a "matrix" or "Matrix"
119 : maechler 1174
120 :     ## check for symmetric / triangular / diagonal :
121 :     isSym <- isSymmetric(data)
122 :     if((isTri <- !isSym))
123 :     isTri <- isTriangular(data)
124 :     isDiag <- isSym # cannot be diagonal if it isn't symmetric
125 :     if(isDiag)
126 :     isDiag <- isDiagonal(data)
127 :    
128 : maechler 965 ### TODO: Compare with as.Matrix() and its tests in ./dgeMatrix.R
129 : maechler 1174
130 :     ## Find proper matrix class 'cl'
131 :     cl <-
132 :     if(isDiag)
133 :     "diagonalMatrix" # -> will automatically check for type
134 :     else {
135 :     ## consider it's type
136 :     ctype <-
137 :     if(is(data,"Matrix")) class(data)
138 :     else {
139 :     if("complex" == (ctype <- typeof(data)))
140 :     "z" else ctype
141 :     }
142 :     ctype <- substr(ctype, 1,1) # "d", "l", "i" or "z"
143 :     if(ctype == "z")
144 :     stop("complex matrices not yet implemented in Matrix package")
145 :     if(ctype == "i") {
146 :     warning("integer matrices not yet implemented in 'Matrix'; ",
147 :     "using 'double' ones'")
148 :     ctype <- "d"
149 :     }
150 :     paste(ctype,
151 :     if(sparse) {
152 :     if(isSym) "sCMatrix" else
153 :     if(isTri) "tCMatrix" else "gCMatrix"
154 :     } else { ## dense
155 :     if(isSym) "syMatrix" else
156 :     if(isTri) "trMatrix" else "geMatrix"
157 :     }, sep="")
158 :     }
159 :    
160 :     ## Now coerce and return
161 :     as(data, cl)
162 : bates 10 }
163 :    
164 : bates 683 ## Methods for operations where one argument is numeric
165 : maechler 512
166 : maechler 698 ## Using as.matrix() and rbind()
167 :     ## in order to get dimnames from names {at least potentially}:
168 :    
169 : bates 683 setMethod("%*%", signature(x = "Matrix", y = "numeric"),
170 : maechler 698 function(x, y) callGeneric(x, as.matrix(y)))
171 : bates 683
172 :     setMethod("%*%", signature(x = "numeric", y = "Matrix"),
173 : maechler 698 function(x, y) callGeneric(rbind(x), y))
174 : bates 683
175 :     setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
176 : maechler 698 function(x, y = NULL) callGeneric(x, as.matrix(y)))
177 : bates 683
178 :     setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
179 : maechler 698 function(x, y = NULL) callGeneric(rbind(x), y))
180 : bates 683
181 :     setMethod("solve", signature(a = "Matrix", b = "numeric"),
182 : maechler 698 function(a, b, ...) callGeneric(a, as.matrix(b)))
183 : maechler 848
184 : maechler 925 ## bail-out methods in order to get better error messages
185 :     setMethod("%*%", signature(x = "Matrix", y = "Matrix"),
186 :     function (x, y)
187 :     stop(gettextf('not-yet-implemented method for <%s> %%*%% <%s>',
188 :     class(x), class(y))))
189 : maechler 946
190 : maechler 925 setMethod("crossprod", signature(x = "Matrix", y = "ANY"),
191 : maechler 946 function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
192 : maechler 956 setMethod("crossprod", signature(x = "ANY", y = "Matrix"),
193 :     function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
194 : maechler 925
195 : maechler 956 ## There are special sparse methods; this is a "fall back":
196 :     setMethod("kronecker", signature(X = "Matrix", Y = "ANY",
197 :     FUN = "ANY", make.dimnames = "ANY"),
198 :     function(X, Y, FUN, make.dimnames, ...) {
199 :     X <- as(X, "matrix") ; Matrix(callGeneric()) })
200 :     setMethod("kronecker", signature(X = "ANY", Y = "Matrix",
201 :     FUN = "ANY", make.dimnames = "ANY"),
202 :     function(X, Y, FUN, make.dimnames, ...) {
203 :     Y <- as(Y, "matrix") ; Matrix(callGeneric()) })
204 :    
205 :    
206 : maechler 1315 setMethod("diag", signature(x = "Matrix"),
207 :     function(x, nrow, ncol) .bail.out.1(.Generic, class(x)))
208 : maechler 946 setMethod("t", signature(x = "Matrix"),
209 :     function(x) .bail.out.1(.Generic, class(x)))
210 : maechler 925
211 : maechler 956 ## Group Methods
212 :     setMethod("+", signature(e1 = "Matrix", e2 = "missing"), function(e1) e1)
213 :     ## "fallback":
214 :     setMethod("-", signature(e1 = "Matrix", e2 = "missing"),
215 :     function(e1) {
216 :     warning("inefficient method used for \"- e1\"")
217 :     0-e1
218 :     })
219 :    
220 :     ## bail-outs:
221 : maechler 946 setMethod("Compare", signature(e1 = "Matrix", e2 = "Matrix"),
222 : maechler 954 function(e1, e2) {
223 :     d <- dimCheck(e1,e2)
224 :     .bail.out.2(.Generic, class(e1), class(e2))
225 :     })
226 : maechler 946 setMethod("Compare", signature(e1 = "Matrix", e2 = "ANY"),
227 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
228 :     setMethod("Compare", signature(e1 = "ANY", e2 = "Matrix"),
229 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
230 :    
231 :    
232 :    
233 : maechler 871 ### --------------------------------------------------------------------------
234 :     ###
235 :     ### Subsetting "[" and
236 :     ### SubAssign "[<-" : The "missing" cases can be dealt with here, "at the top":
237 : maechler 868
238 : maechler 886 ## Using "index" for indices should allow
239 : maechler 875 ## integer (numeric), logical, or character (names!) indices :
240 :    
241 : maechler 868 ## "x[]":
242 :     setMethod("[", signature(x = "Matrix",
243 :     i = "missing", j = "missing", drop = "ANY"),
244 :     function (x, i, j, drop) x)
245 : maechler 1225
246 : maechler 868 ## missing 'drop' --> 'drop = TRUE'
247 :     ## -----------
248 :     ## select rows
249 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "missing",
250 : maechler 868 drop = "missing"),
251 :     function(x,i,j, drop) callGeneric(x, i=i, drop= TRUE))
252 :     ## select columns
253 : maechler 886 setMethod("[", signature(x = "Matrix", i = "missing", j = "index",
254 : maechler 868 drop = "missing"),
255 :     function(x,i,j, drop) callGeneric(x, j=j, drop= TRUE))
256 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "index",
257 : maechler 868 drop = "missing"),
258 :     function(x,i,j, drop) callGeneric(x, i=i, j=j, drop= TRUE))
259 :    
260 : maechler 875 ## bail out if any of (i,j,drop) is "non-sense"
261 :     setMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY", drop = "ANY"),
262 :     function(x,i,j, drop)
263 :     stop("invalid or not-yet-implemented 'Matrix' subsetting"))
264 :    
265 : maechler 1225 ## "logical *vector* indexing, such as M [ M >= 10 ] :
266 :     setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",
267 :     drop = "ANY"),
268 :     function (x, i, j, drop) {
269 :     as(x, geClass(x))@x[as.vector(i)]
270 :     # -> error when lengths don't match
271 :     })
272 :    
273 :     setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",
274 :     drop = "ANY"),
275 :     function (x, i, j, drop) as(x, geClass(x))@x[i])
276 :    
277 :    
278 : maechler 868 ## "FIXME:"
279 :     ## How can we get at A[ ij ] where ij is (i,j) 2-column matrix?
280 :     ## and A[ LL ] where LL is a logical *vector*
281 : maechler 886 ## -> [.data.frame uses nargs() - can we do this in the *generic* ?
282 : maechler 868
283 :    
284 : maechler 871 ### "[<-" : -----------------
285 : maechler 868
286 : maechler 871 ## x[] <- value :
287 :     setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",
288 : maechler 1189 value = "ANY"),## double/logical/...
289 :     function (x, value) {
290 :     x@x <- value
291 :     validObject(x)# check if type and lengths above match
292 :     x
293 :     })
294 : maechler 871
295 : maechler 1189 ## Method for all 'Matrix' kinds (rather than incomprehensible error messages);
296 :     ## (ANY,ANY,ANY) is used when no `real method' is implemented :
297 : maechler 871 setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
298 :     value = "ANY"),
299 : maechler 1189 function (x, i, j, value) {
300 :     if(!is.atomic(value))
301 :     stop("RHS 'value' must match matrix class ", class(x))
302 :     else stop("not-yet-implemented 'Matrix[<-' method")
303 :     })
304 : maechler 871
305 :    
306 : maechler 1290 ## The trivial methods :
307 :     setMethod("cbind2", signature(x = "Matrix", y = "NULL"),
308 :     function(x, y) x)
309 :     setMethod("cbind2", signature(x = "Matrix", y = "missing"),
310 :     function(x, y) x)
311 :     setMethod("cbind2", signature(x = "NULL", y="Matrix"),
312 :     function(x, y) x)
313 : maechler 871
314 : maechler 1290 setMethod("rbind2", signature(x = "Matrix", y = "NULL"),
315 :     function(x, y) x)
316 :     setMethod("rbind2", signature(x = "Matrix", y = "missing"),
317 :     function(x, y) x)
318 :     setMethod("rbind2", signature(x = "NULL", y="Matrix"),
319 :     function(x, y) x)
320 : maechler 875
321 : maechler 1290 ## Makes sure one gets x decent error message for the unimplemented cases:
322 :     setMethod("cbind2", signature(x = "Matrix", y = "Matrix"),
323 :     function(x, y) {
324 :     rowCheck(x,y)
325 :     stop(gettextf("cbind2() method for (%s,%s) not-yet defined",
326 :     class(x), class(y)))
327 :     })
328 : maechler 908
329 : maechler 1290 ## Use a working fall back {particularly useful for sparse}:
330 :     ## FIXME: implement rbind2 via "cholmod" for C* and Tsparse ones
331 :     setMethod("rbind2", signature(x = "Matrix", y = "Matrix"),
332 :     function(x, y) {
333 :     colCheck(x,y)
334 :     t(cbind2(t(x), t(y)))
335 :     })

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