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

1 : maechler 607 #### Toplevel ``virtual'' class "Matrix"
2 :    
3 : maechler 1331
4 :     ### Virtual coercions -- via smart "helpers" (-> ./Auxiliaries.R)
5 :    
6 : maechler 1799 setAs("Matrix", "sparseMatrix", function(from) as(from, "CsparseMatrix"))
7 :     setAs("Matrix", "CsparseMatrix", function(from) as_Csparse(from))
8 : maechler 1331 setAs("Matrix", "denseMatrix", function(from) as_dense(from))
9 :    
10 : maechler 1799 ## Maybe TODO:
11 :     ## setAs("Matrix", "nMatrix", function(from) ....)
12 :    
13 : maechler 1618 ## Most of these work; this is a last resort:
14 : maechler 1548 setAs(from = "Matrix", to = "matrix", # do *not* call base::as.matrix() here:
15 :     function(from) .bail.out.2("coerce", class(from), class(to)))
16 : maechler 1618 setAs(from = "matrix", to = "Matrix", function(from) Matrix(from))
17 : maechler 1548
18 : maechler 956 ## ## probably not needed eventually:
19 :     ## setAs(from = "ddenseMatrix", to = "matrix",
20 :     ## function(from) {
21 :     ## if(length(d <- dim(from)) != 2) stop("dim(.) has not length 2")
22 :     ## array(from@x, dim = d, dimnames = dimnames(from))
23 :     ## })
24 : maechler 607
25 : maechler 676 ## should propagate to all subclasses:
26 : maechler 658 setMethod("as.matrix", signature(x = "Matrix"), function(x) as(x, "matrix"))
27 : maechler 956 ## for 'Matrix' objects, as.array() should be equivalent:
28 :     setMethod("as.array", signature(x = "Matrix"), function(x) as(x, "matrix"))
29 : maechler 658
30 : maechler 1347 ## head and tail apply to all Matrix objects for which subscripting is allowed:
31 : maechler 1513 setMethod("head", signature(x = "Matrix"), utils::head.matrix)
32 :     setMethod("tail", signature(x = "Matrix"), utils::tail.matrix)
33 : maechler 1390
34 : maechler 1751 setMethod("drop", signature(x = "Matrix"),
35 :     function(x) if(all(dim(x) != 1)) x else drop(as(x, "matrix")))
36 :    
37 : maechler 956 ## slow "fall back" method {subclasses should have faster ones}:
38 :     setMethod("as.vector", signature(x = "Matrix", mode = "missing"),
39 : maechler 1331 function(x) as.vector(as(x, "matrix")))
40 : maechler 956
41 : maechler 1389 ## mainly need these for "dMatrix" or "lMatrix" respectively, but why not general:
42 :     setMethod("as.numeric", signature(x = "Matrix"),
43 :     function(x, ...) as.numeric(as.vector(x)))
44 :     setMethod("as.logical", signature(x = "Matrix"),
45 :     function(x, ...) as.logical(as.vector(x)))
46 : maechler 956
47 : maechler 2096 setMethod("cov2cor", signature(V = "Matrix"),
48 :     function(V) as(cov2cor(as(V, "matrix")), "dpoMatrix"))
49 : maechler 1389
50 : maechler 1467 ## "base" has an isSymmetric() S3-generic since R 2.3.0
51 : maechler 1108 setMethod("isSymmetric", signature(object = "symmetricMatrix"),
52 : maechler 973 function(object,tol) TRUE)
53 : maechler 1108 setMethod("isSymmetric", signature(object = "triangularMatrix"),
54 : maechler 1174 ## TRUE iff diagonal:
55 :     function(object,tol) isDiagonal(object))
56 : maechler 1189
57 : maechler 1165 setMethod("isTriangular", signature(object = "triangularMatrix"),
58 : maechler 1238 function(object, ...) TRUE)
59 : maechler 1165
60 : maechler 1238 setMethod("isTriangular", signature(object = "matrix"), isTriMat)
61 :    
62 : maechler 1174 setMethod("isDiagonal", signature(object = "matrix"), .is.diagonal)
63 : maechler 886
64 : maechler 1174
65 :    
66 : maechler 579 setMethod("dim", signature(x = "Matrix"),
67 : maechler 698 function(x) x@Dim, valueClass = "integer")
68 : maechler 1705
69 : maechler 1738 setMethod("length", "Matrix", function(x) prod(dim(x)))
70 :    
71 : maechler 579 setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)
72 : maechler 1705
73 :    
74 : maechler 607 ## not exported but used more than once for "dimnames<-" method :
75 :     ## -- or do only once for all "Matrix" classes ??
76 :     dimnamesGets <- function (x, value) {
77 :     d <- dim(x)
78 :     if (!is.list(value) || length(value) != 2 ||
79 : maechler 698 !(is.null(v1 <- value[[1]]) || length(v1) == d[1]) ||
80 :     !(is.null(v2 <- value[[2]]) || length(v2) == d[2]))
81 :     stop(sprintf("invalid dimnames given for '%s' object", class(x)))
82 : maechler 607 x@Dimnames <- list(if(!is.null(v1)) as.character(v1),
83 : maechler 698 if(!is.null(v2)) as.character(v2))
84 : maechler 607 x
85 :     }
86 :     setMethod("dimnames<-", signature(x = "Matrix", value = "list"),
87 : maechler 698 dimnamesGets)
88 : maechler 579
89 : maechler 658 setMethod("unname", signature("Matrix", force="missing"),
90 : maechler 698 function(obj) { obj@Dimnames <- list(NULL,NULL); obj})
91 : maechler 607
92 : maechler 1705 setMethod("all", signature(x = "Matrix"),
93 : maechler 2015 function(x, ..., na.rm)
94 :     callGeneric(as(x, "lMatrix"), ..., na.rm=na.rm))
95 :    
96 : maechler 1705 setMethod("any", signature(x = "Matrix"),
97 : maechler 2015 function(x, ..., na.rm)
98 :     callGeneric(as(x, "lMatrix"), ..., na.rm=na.rm))
99 : maechler 1705
100 : maechler 1832 ## NOTE: "&" and "|" are now in group "Logic" c "Ops" --> ./Ops.R
101 :     ## "!" is in ./not.R
102 : maechler 1705
103 :    
104 : bates 10 Matrix <-
105 : maechler 954 function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
106 : maechler 1455 sparse = NULL, forceCheck = FALSE)
107 : bates 10 {
108 : maechler 1665 sparseDefault <- function(m) prod(dim(m)) > 2*sum(isN0(as(m, "matrix")))
109 : maechler 954
110 :     i.M <- is(data, "Matrix")
111 : maechler 2043 if(!i.M && inherits(data, "table")) # special treatment
112 :     class(data) <- "matrix" # "matrix" first for S4 dispatch
113 : maechler 1618 if(is.null(sparse1 <- sparse) && (i.M || is(data, "matrix")))
114 : maechler 1174 sparse <- sparseDefault(data)
115 : maechler 954
116 : maechler 1455 doDN <- TRUE
117 : maechler 1551 if (i.M) {
118 : maechler 1751 if(!missing(nrow) || !missing(ncol)|| !missing(byrow))
119 :     warning("'nrow', 'ncol', etc, are disregarded when 'data' is \"Matrix\" already")
120 : maechler 1174 sM <- is(data,"sparseMatrix")
121 : maechler 1551 if(!forceCheck && ((sparse && sM) || (!sparse && !sM)))
122 : maechler 1174 return(data)
123 :     ## else : convert dense <-> sparse -> at end
124 : maechler 954 }
125 :     else if (!is.matrix(data)) { ## cut & paste from "base::matrix" :
126 : maechler 698 if (missing(nrow))
127 :     nrow <- ceiling(length(data)/ncol)
128 :     else if (missing(ncol))
129 :     ncol <- ceiling(length(data)/nrow)
130 : maechler 1618 if(length(data) == 1 && is0(data) && !identical(sparse, FALSE)) {
131 :     ## Matrix(0, ...) : always sparse unless "sparse = FALSE":
132 :     if(is.null(sparse)) sparse1 <- sparse <- TRUE
133 : maechler 1825 i.M <- sM <- TRUE
134 : maechler 1315 ## will be sparse: do NOT construct full matrix!
135 :     data <- new(if(is.numeric(data)) "dgTMatrix" else
136 :     if(is.logical(data)) "lgTMatrix" else
137 :     stop("invalid 'data'"),
138 :     Dim = as.integer(c(nrow,ncol)),
139 : maechler 1332 Dimnames = if(is.null(dimnames)) list(NULL,NULL)
140 :     else dimnames)
141 : maechler 2081 } else { ## normal case - using .Internal() to avoid more copying
142 :     if(getRversion() >= "2.7.0")
143 :     data <- .Internal(matrix(data, nrow, ncol, byrow, dimnames))
144 :     else {
145 :     data <- .Internal(matrix(data, nrow, ncol, byrow))
146 :     dimnames(data) <- dimnames
147 :     }
148 : maechler 1315 if(is.null(sparse))
149 :     sparse <- sparseDefault(data)
150 :     }
151 : maechler 1455 doDN <- FALSE
152 : maechler 1751 } else if(!missing(nrow) || !missing(ncol)|| !missing(byrow))
153 : maechler 1747 warning("'nrow', 'ncol', etc, are disregarded for matrix 'data'")
154 :    
155 : maechler 1455 ## 'data' is now a "matrix" or "Matrix"
156 :     if (doDN && !is.null(dimnames))
157 : maechler 1189 dimnames(data) <- dimnames
158 : maechler 954
159 : maechler 1174 ## check for symmetric / triangular / diagonal :
160 :     isSym <- isSymmetric(data)
161 :     if((isTri <- !isSym))
162 :     isTri <- isTriangular(data)
163 :     isDiag <- isSym # cannot be diagonal if it isn't symmetric
164 :     if(isDiag)
165 :     isDiag <- isDiagonal(data)
166 :    
167 :     ## Find proper matrix class 'cl'
168 :     cl <-
169 : maechler 1618 if(isDiag && !isTRUE(sparse1))
170 : maechler 1174 "diagonalMatrix" # -> will automatically check for type
171 :     else {
172 :     ## consider it's type
173 :     ctype <-
174 :     if(is(data,"Matrix")) class(data)
175 :     else {
176 :     if("complex" == (ctype <- typeof(data)))
177 :     "z" else ctype
178 :     }
179 :     ctype <- substr(ctype, 1,1) # "d", "l", "i" or "z"
180 :     if(ctype == "z")
181 :     stop("complex matrices not yet implemented in Matrix package")
182 :     if(ctype == "i") {
183 :     warning("integer matrices not yet implemented in 'Matrix'; ",
184 :     "using 'double' ones'")
185 :     ctype <- "d"
186 :     }
187 :     paste(ctype,
188 :     if(sparse) {
189 :     if(isSym) "sCMatrix" else
190 :     if(isTri) "tCMatrix" else "gCMatrix"
191 :     } else { ## dense
192 :     if(isSym) "syMatrix" else
193 :     if(isTri) "trMatrix" else "geMatrix"
194 :     }, sep="")
195 :     }
196 :    
197 : maechler 1825 ## Can we coerce and be done?
198 :     if(!canCoerce(data,cl)) { ## try to coerce ``via'' virtual classes
199 :     if(sparse && !sM)
200 :     data <- as(data, "sparseMatrix")
201 :     else if(!sparse && !is(data, "denseMatrix"))
202 :     data <- as(data, "denseMatrix")
203 :     if(isTri && !is(data, "triangularMatrix"))
204 :     data <- as(data, "triangularMatrix")
205 :     else if(isSym && !is(data, "symmetricMatrix"))
206 :     data <- as(data, "symmetricMatrix")
207 :     }
208 :     ## now coerce in any case .. maybe producing sensible error message:
209 : maechler 1174 as(data, cl)
210 : bates 10 }
211 :    
212 : bates 683 ## Methods for operations where one argument is numeric
213 : maechler 512
214 : maechler 698 ## Using as.matrix() and rbind()
215 :     ## in order to get dimnames from names {at least potentially}:
216 :    
217 : bates 683 setMethod("%*%", signature(x = "Matrix", y = "numeric"),
218 : maechler 698 function(x, y) callGeneric(x, as.matrix(y)))
219 : bates 683 setMethod("%*%", signature(x = "numeric", y = "Matrix"),
220 : maechler 1660 function(x, y) callGeneric(matrix(x, nrow = 1, byrow=TRUE), y))
221 : bates 683
222 : maechler 2036 setMethod("%*%", signature(x = "Matrix", y = "matrix"),
223 :     function(x, y) callGeneric(x, Matrix(y)))
224 :     setMethod("%*%", signature(x = "matrix", y = "Matrix"),
225 :     function(x, y) callGeneric(Matrix(x), y))
226 :    
227 :    
228 : bates 683 setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
229 : maechler 698 function(x, y = NULL) callGeneric(x, as.matrix(y)))
230 : bates 683 setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
231 : maechler 1331 function(x, y = NULL) callGeneric(as.matrix(x), y))
232 : bates 683
233 : maechler 2036 setMethod("crossprod", signature(x = "Matrix", y = "matrix"),
234 : maechler 2039 function(x, y = NULL) callGeneric(x, Matrix(y)))
235 : maechler 2036 setMethod("crossprod", signature(x = "matrix", y = "Matrix"),
236 : maechler 2039 function(x, y = NULL) callGeneric(Matrix(x), y))
237 : maechler 2036
238 : maechler 1331 ## The as.matrix() promotion seems illogical to MM,
239 :     ## but is according to help(tcrossprod, package = "base") :
240 :     setMethod("tcrossprod", signature(x = "Matrix", y = "numeric"),
241 :     function(x, y = NULL) callGeneric(x, as.matrix(y)))
242 :     setMethod("tcrossprod", signature(x = "numeric", y = "Matrix"),
243 :     function(x, y = NULL) callGeneric(as.matrix(x), y))
244 : maechler 2036 setMethod("tcrossprod", signature(x = "Matrix", y = "matrix"),
245 : maechler 2039 function(x, y = NULL) callGeneric(x, Matrix(y)))
246 : maechler 2036 setMethod("tcrossprod", signature(x = "matrix", y = "Matrix"),
247 : maechler 2039 function(x, y = NULL) callGeneric(Matrix(x), y))
248 : maechler 1331
249 : maechler 2043 ## maybe not 100% optimal, but elegant:
250 : maechler 1665 setMethod("solve", signature(a = "Matrix", b = "missing"),
251 :     function(a, b, ...) solve(a, Diagonal(nrow(a))))
252 :    
253 : bates 683 setMethod("solve", signature(a = "Matrix", b = "numeric"),
254 : maechler 2036 function(a, b, ...) callGeneric(a, Matrix(b)))
255 :     setMethod("solve", signature(a = "Matrix", b = "matrix"),
256 :     function(a, b, ...) callGeneric(a, Matrix(b)))
257 :     setMethod("solve", signature(a = "matrix", b = "Matrix"),
258 :     function(a, b, ...) callGeneric(Matrix(a), b))
259 :    
260 : maechler 1659 ## when no sub-class method is found, bail out
261 : maechler 1747 setMethod("solve", signature(a = "Matrix", b = "Matrix"),
262 :     function(a, b, ...) .bail.out.2("solve", class(a), class(b)))
263 : maechler 848
264 : maechler 925 ## bail-out methods in order to get better error messages
265 :     setMethod("%*%", signature(x = "Matrix", y = "Matrix"),
266 :     function (x, y)
267 :     stop(gettextf('not-yet-implemented method for <%s> %%*%% <%s>',
268 :     class(x), class(y))))
269 : maechler 946
270 : maechler 925 setMethod("crossprod", signature(x = "Matrix", y = "ANY"),
271 : maechler 946 function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
272 : maechler 956 setMethod("crossprod", signature(x = "ANY", y = "Matrix"),
273 :     function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
274 : maechler 1331 setMethod("tcrossprod", signature(x = "Matrix", y = "ANY"),
275 :     function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
276 :     setMethod("tcrossprod", signature(x = "ANY", y = "Matrix"),
277 :     function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
278 : maechler 925
279 : maechler 1331 ## cheap fallbacks
280 :     setMethod("crossprod", signature(x = "Matrix", y = "Matrix"),
281 :     function(x, y = NULL) t(x) %*% y)
282 :     setMethod("tcrossprod", signature(x = "Matrix", y = "Matrix"),
283 :     function(x, y = NULL) x %*% t(y))
284 :    
285 : maechler 956 ## There are special sparse methods; this is a "fall back":
286 :     setMethod("kronecker", signature(X = "Matrix", Y = "ANY",
287 : maechler 1805 FUN = "ANY", make.dimnames = "ANY"),
288 :     function(X, Y, FUN, make.dimnames, ...) {
289 : maechler 1825 if(is(X, "sparseMatrix"))
290 :     warning("using slow kronecker() method")
291 : maechler 1805 X <- as(X, "matrix") ; Matrix(callGeneric()) })
292 :    
293 : maechler 956 setMethod("kronecker", signature(X = "ANY", Y = "Matrix",
294 : maechler 1805 FUN = "ANY", make.dimnames = "ANY"),
295 :     function(X, Y, FUN, make.dimnames, ...) {
296 : maechler 1825 if(is(Y, "sparseMatrix"))
297 :     warning("using slow kronecker() method")
298 : maechler 1805 Y <- as(Y, "matrix") ; Matrix(callGeneric()) })
299 : maechler 956
300 :    
301 : maechler 1654 ## FIXME: All of these should never be called
302 :     setMethod("chol", signature(x = "Matrix"),
303 :     function(x, pivot = FALSE) .bail.out.1(.Generic, class(x)))
304 :     setMethod("determinant", signature(x = "Matrix"),
305 :     function(x, logarithm = TRUE) .bail.out.1(.Generic, class(x)))
306 :    
307 : maechler 1315 setMethod("diag", signature(x = "Matrix"),
308 :     function(x, nrow, ncol) .bail.out.1(.Generic, class(x)))
309 : maechler 946 setMethod("t", signature(x = "Matrix"),
310 :     function(x) .bail.out.1(.Generic, class(x)))
311 : maechler 925
312 : maechler 2005 setMethod("norm", signature(x = "Matrix", type = "character"),
313 :     function(x, type, ...) .bail.out.1(.Generic, class(x)))
314 :     setMethod("rcond", signature(x = "Matrix", type = "character"),
315 :     function(x, type, ...) .bail.out.1(.Generic, class(x)))
316 :    
317 :    
318 :     ## for all :
319 :     setMethod("norm", signature(x = "ANY", type = "missing"),
320 :     function(x, type, ...) norm(x, type = "O", ...))
321 :     setMethod("rcond", signature(x = "ANY", type = "missing"),
322 :     function(x, type, ...) rcond(x, type = "O", ...))
323 :    
324 :    
325 :    
326 :    
327 :    
328 : maechler 1799 ## MM: More or less "Cut & paste" from
329 :     ## --- diff.default() from R/src/library/base/R/diff.R :
330 :     setMethod("diff", signature(x = "Matrix"),
331 :     function(x, lag = 1, differences = 1, ...) {
332 :     if (length(lag) > 1 || length(differences) > 1 ||
333 :     lag < 1 || differences < 1)
334 :     stop("'lag' and 'differences' must be integers >= 1")
335 :     xlen <- nrow(x)
336 :     if (lag * differences >= xlen)
337 :     return(x[,FALSE][0]) # empty of proper mode
338 :    
339 :     i1 <- -1:-lag
340 :     for (i in 1:differences)
341 :     x <- x[i1, , drop = FALSE] -
342 :     x[-nrow(x):-(nrow(x)-lag+1), , drop = FALSE]
343 :     x
344 :     })
345 :    
346 : maechler 1829 setMethod("image", "Matrix",
347 :     function(x, ...) { # coercing to sparse is not inefficient,
348 :     ## since we need 'i' and 'j' for levelplot()
349 :     x <- as(as(x, "sparseMatrix"), "dMatrix")
350 :     callGeneric()
351 :     })
352 : maechler 1799
353 : maechler 1829
354 : maechler 956 ## Group Methods
355 :    
356 : maechler 1714 ##-> see ./Ops.R
357 : maechler 2005 ## ~~~~~
358 :     ## For all non-dMatrix objects, and note that "all" and "any" have their own
359 :     setMethod("Summary", signature(x = "Matrix", na.rm = "ANY"),
360 :     function(x, ..., na.rm)
361 :     callGeneric(as(x,"dMatrix"), ..., na.rm = na.rm))
362 : maechler 1655
363 : maechler 2005
364 : maechler 871 ### --------------------------------------------------------------------------
365 :     ###
366 :     ### Subsetting "[" and
367 :     ### SubAssign "[<-" : The "missing" cases can be dealt with here, "at the top":
368 : maechler 868
369 : maechler 886 ## Using "index" for indices should allow
370 : maechler 875 ## integer (numeric), logical, or character (names!) indices :
371 :    
372 : maechler 868 ## "x[]":
373 :     setMethod("[", signature(x = "Matrix",
374 :     i = "missing", j = "missing", drop = "ANY"),
375 : maechler 2056 function (x, i, j, ..., drop) x)
376 : maechler 1225
377 : maechler 868 ## missing 'drop' --> 'drop = TRUE'
378 :     ## -----------
379 : maechler 2111 ## select rows __ or __ vector indexing:
380 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "missing",
381 : maechler 868 drop = "missing"),
382 : maechler 2056 function(x,i,j, ..., drop) {
383 : maechler 2098 if(nargs() == 2) { ## e.g. M[0] , M[TRUE], M[1:2]
384 : maechler 2111 if(any(as.logical(i)) || prod(dim(x)) == 0)
385 :     ## FIXME: for *large sparse*, use sparseVector !
386 : maechler 2098 as.vector(x)[i]
387 : maechler 2111 else ## save memory (for large sparse M):
388 : maechler 2098 as.vector(x[1,1])[FALSE]
389 :     } else {
390 :     callGeneric(x, i=i, , drop=TRUE)
391 :     ## ^^
392 :     }
393 :     })
394 : maechler 1799
395 : maechler 868 ## select columns
396 : maechler 886 setMethod("[", signature(x = "Matrix", i = "missing", j = "index",
397 : maechler 868 drop = "missing"),
398 : maechler 2056 function(x,i,j, ..., drop) callGeneric(x, j=j, drop= TRUE))
399 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "index",
400 : maechler 868 drop = "missing"),
401 : maechler 2056 function(x,i,j, ..., drop) callGeneric(x, i=i, j=j, drop= TRUE))
402 : maechler 868
403 : maechler 875 ## bail out if any of (i,j,drop) is "non-sense"
404 :     setMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY", drop = "ANY"),
405 : maechler 2056 function(x,i,j, ..., drop)
406 : maechler 875 stop("invalid or not-yet-implemented 'Matrix' subsetting"))
407 :    
408 : maechler 1472 ## logical indexing, such as M[ M >= 7 ] *BUT* also M[ M[,1] >= 3,],
409 :     ## The following is *both* for M [ <logical> ]
410 :     ## and also for M [ <logical> , ]
411 : maechler 2056 .M.sub.i.logical <- function (x, i, j, ..., drop)
412 : maechler 1472 {
413 :     nA <- nargs()
414 :     if(nA == 2) { ## M [ M >= 7 ]
415 : maechler 1673 ## FIXME: when both 'x' and 'i' are sparse, this can be very inefficient
416 : maechler 1472 as(x, geClass(x))@x[as.vector(i)]
417 :     ## -> error when lengths don't match
418 :     } else if(nA == 3) { ## M [ M[,1, drop=FALSE] >= 7, ]
419 :     stop("not-yet-implemented 'Matrix' subsetting") ## FIXME
420 :    
421 :     } else stop("nargs() = ", nA,
422 : maechler 2056 ". Extraneous illegal arguments inside '[ .. ]' (i.logical)?")
423 : maechler 1472 }
424 : maechler 1225 setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",
425 :     drop = "ANY"),
426 : maechler 1472 .M.sub.i.logical)
427 : maechler 1225 setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",
428 :     drop = "ANY"),
429 : maechler 1472 .M.sub.i.logical)
430 : maechler 1225
431 :    
432 : maechler 2110 ## A[ ij ] where ij is (i,j) 2-column matrix -- but also when that is logical mat!
433 : maechler 2056 .M.sub.i.2col <- function (x, i, j, ..., drop)
434 : maechler 1673 {
435 :     nA <- nargs()
436 : maechler 2110 if(nA == 2) { ## M [ cbind(ii,jj) ] or M [ <logical matrix> ]
437 : maechler 1673 if(!is.integer(nc <- ncol(i)))
438 : maechler 2110 stop(".M.sub.i.2col(): 'i' has no integer column number;\n",
439 :     "should never happen; please report")
440 : maechler 1673 if(is.logical(i))
441 : maechler 2110 return(.M.sub.i.logical(x, i=i)) # call with 2 args!
442 : maechler 1673 else if(!is.numeric(i) || nc != 2)
443 :     stop("such indexing must be by logical or 2-column numeric matrix")
444 :     m <- nrow(i)
445 :     if(m == 0) return(vector(mode = .type.kind[.M.kind(x)]))
446 :     ## else
447 :     i1 <- i[,1]
448 :     i2 <- i[,2]
449 :     ## potentially inefficient -- FIXME --
450 :     unlist(lapply(seq_len(m), function(j) x[i1[j], i2[j]]))
451 : maechler 868
452 : maechler 1747 } else stop("nargs() = ", nA,
453 : maechler 2056 ". Extraneous illegal arguments inside '[ .. ]' (i.2col)?")
454 : maechler 1673 }
455 :     setMethod("[", signature(x = "Matrix", i = "matrix", j = "missing"),# drop="ANY"
456 :     .M.sub.i.2col)
457 : maechler 868
458 : maechler 1472
459 : maechler 871 ### "[<-" : -----------------
460 : maechler 868
461 : maechler 871 ## x[] <- value :
462 :     setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",
463 : maechler 1189 value = "ANY"),## double/logical/...
464 :     function (x, value) {
465 : maechler 1615 ## Fails for 'nMatrix' ... FIXME : make sure have method there
466 : maechler 1747 x@x <- rep(value, length = length(x@x))
467 : maechler 1615 validObject(x)# check if type and lengths above match
468 :     x
469 : maechler 1189 })
470 : maechler 871
471 : maechler 1673 ## A[ ij ] <- value, where ij is (i,j) 2-column matrix :
472 : maechler 2056 ## ----------------
473 :     ## The cheap general method --- FIXME: provide special ones; done for Tsparse..
474 :     ## NOTE: need '...' below such that setMethod() does
475 :     ## not use .local() such that nargs() will work correctly:
476 :     .M.repl.i.2col <- function (x, i, j, ..., value)
477 : maechler 1673 {
478 :     nA <- nargs()
479 : maechler 2110 if(nA == 3) { ## M [ cbind(ii,jj) ] <- value or M [ Lmat ] <- value
480 : maechler 1673 if(!is.integer(nc <- ncol(i)))
481 : maechler 2110 stop(".M.repl.i.2col(): 'i' has no integer column number;\n",
482 :     "should never happen; please report")
483 : maechler 1724 else if(!is.numeric(i) || nc != 2)
484 :     stop("such indexing must be by logical or 2-column numeric matrix")
485 : maechler 1673 if(is.logical(i)) {
486 : maechler 1738 message(".M.repl.i.2col(): drop 'matrix' case ...")
487 : maechler 2110 ## c(i) : drop "matrix" to logical vector
488 :     return( callGeneric(x, i=c(i), value=value) )
489 :     }
490 : maechler 1724 if(!is.integer(i)) storage.mode(i) <- "integer"
491 :     if(any(i < 0))
492 :     stop("negative values are not allowed in a matrix subscript")
493 :     if(any(is.na(i)))
494 :     stop("NAs are not allowed in subscripted assignments")
495 :     if(any(i0 <- (i == 0))) # remove them
496 :     i <- i[ - which(i0, arr.ind = TRUE)[,"row"], ]
497 :     ## now have integer i >= 1
498 : maechler 1673 m <- nrow(i)
499 : maechler 1724 ## mod.x <- .type.kind[.M.kind(x)]
500 : maechler 1673 if(length(value) > 0 && m %% length(value) != 0)
501 :     warning("number of items to replace is not a multiple of replacement length")
502 :     ## recycle:
503 :     value <- rep(value, length = m)
504 :     i1 <- i[,1]
505 :     i2 <- i[,2]
506 :     ## inefficient -- FIXME -- (also loses "symmetry" unnecessarily)
507 :     for(k in seq_len(m))
508 :     x[i1[k], i2[k]] <- value[k]
509 : maechler 1724
510 : maechler 1673 x
511 : maechler 1747 } else stop("nargs() = ", nA,
512 :     ". Extraneous illegal arguments inside '[ .. ]' ?")
513 : maechler 1673 }
514 : maechler 1724
515 : maechler 1673 setReplaceMethod("[", signature(x = "Matrix", i = "matrix", j = "missing",
516 :     value = "replValue"),
517 :     .M.repl.i.2col)
518 :    
519 :    
520 : maechler 2062 setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "ANY",
521 : maechler 1615 value = "Matrix"),
522 : maechler 2062 function (x, i, j, ..., value)
523 : maechler 2098 callGeneric(x=x, , j=j, value = as.vector(value)))
524 : maechler 1705
525 : maechler 2062 setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "missing",
526 :     value = "Matrix"),
527 :     function (x, i, j, ..., value)
528 : maechler 2098 callGeneric(x=x, i=i, , value = as.vector(value)))
529 : maechler 2056
530 : maechler 1705 setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
531 :     value = "Matrix"),
532 : maechler 2062 function (x, i, j, ..., value)
533 : maechler 1615 callGeneric(x=x, i=i, j=j, value = as.vector(value)))
534 : maechler 1705
535 : maechler 2062 setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "ANY",
536 :     value = "matrix"),
537 :     function (x, i, j, ..., value)
538 : maechler 2098 callGeneric(x=x, , j=j, value = c(value)))
539 : maechler 2062
540 :     setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "missing",
541 :     value = "matrix"),
542 :     function (x, i, j, ..., value)
543 : maechler 2098 callGeneric(x=x, i=i, , value = c(value)))
544 : maechler 2062
545 : maechler 1615 setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
546 :     value = "matrix"),
547 :     function (x, i, j, value)
548 :     callGeneric(x=x, i=i, j=j, value = c(value)))
549 :    
550 : maechler 1189 ## (ANY,ANY,ANY) is used when no `real method' is implemented :
551 : maechler 871 setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
552 :     value = "ANY"),
553 : maechler 1189 function (x, i, j, value) {
554 :     if(!is.atomic(value))
555 : maechler 1615 stop(sprintf("RHS 'value' (class %s) matches 'ANY', but must match matrix class %s",
556 :     class(value),class(x)))
557 : maechler 1189 else stop("not-yet-implemented 'Matrix[<-' method")
558 :     })

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