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

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