# SCM Repository

[matrix] Diff of /pkg/R/Matrix.R
 [matrix] / pkg / R / Matrix.R

# Diff of /pkg/R/Matrix.R

revision 1290, Thu Jun 8 09:30:21 2006 UTC revision 1331, Sat Jul 22 17:59:53 2006 UTC
# Line 1  Line 1
1  #### Toplevel ``virtual'' class "Matrix"  #### Toplevel ``virtual'' class "Matrix"
2
3
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  ## ## probably not needed eventually:  ## ## probably not needed eventually:
10  ## setAs(from = "ddenseMatrix", to = "matrix",  ## setAs(from = "ddenseMatrix", to = "matrix",
11  ##       function(from) {  ##       function(from) {
# Line 11  Line 17
17  setMethod("as.matrix", signature(x = "Matrix"), function(x) as(x, "matrix"))  setMethod("as.matrix", signature(x = "Matrix"), function(x) as(x, "matrix"))
18  ## for 'Matrix' objects, as.array() should be equivalent:  ## for 'Matrix' objects, as.array() should be equivalent:
19  setMethod("as.array",  signature(x = "Matrix"), function(x) as(x, "matrix"))  setMethod("as.array",  signature(x = "Matrix"), function(x) as(x, "matrix"))
20    ## head and tail apply to all Matrix objects for which subscripting is allowed
22              function(x, n = 6, ...)
23              x[seq(len = min(n, nrow(x))), , drop = FALSE])
24    setMethod("tail", signature(x = "Matrix"),
25              function (x, n = 6, addrownums = TRUE, ...)
26          {
27              nrx <- nrow(x)
28              sel <- seq(to = nrx, length = min(n, nrx))
29              ans <- x[sel, , drop = FALSE]
31                  rownames(ans) <- paste("[", sel, ",]", sep = "")
32              ans
33          })
34
35  ## slow "fall back" method {subclasses should have faster ones}:  ## slow "fall back" method {subclasses should have faster ones}:
36  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),
# Line 97  Line 117
117              nrow <- ceiling(length(data)/ncol)              nrow <- ceiling(length(data)/ncol)
118          else if (missing(ncol))          else if (missing(ncol))
119              ncol <- ceiling(length(data)/nrow)              ncol <- ceiling(length(data)/nrow)
120            if(length(data) == 1 && data == 0 && !identical(sparse,FALSE)) {
121                if(is.null(sparse)) sparse <- TRUE
122                ## will be sparse: do NOT construct full matrix!
123                data <- new(if(is.numeric(data)) "dgTMatrix" else
124                            if(is.logical(data)) "lgTMatrix" else
125                            stop("invalid 'data'"),
126                            Dim = as.integer(c(nrow,ncol)),
127                            Dimnames = if(is.null(dimnames))
128                            list(NULL,NULL) else dimnames)
129            } else { ## normal case
130          data <- .Internal(matrix(data, nrow, ncol, byrow))          data <- .Internal(matrix(data, nrow, ncol, byrow))
131          if(is.null(sparse))          if(is.null(sparse))
132              sparse <- sparseDefault(data)              sparse <- sparseDefault(data)
133          dimnames(data) <- dimnames          dimnames(data) <- dimnames
134            }
135      } else if (!is.null(dimnames))      } else if (!is.null(dimnames))
136          dimnames(data) <- dimnames          dimnames(data) <- dimnames
137
# Line 163  Line 194
194
195  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
196            function(x, y = NULL) callGeneric(x, as.matrix(y)))            function(x, y = NULL) callGeneric(x, as.matrix(y)))

197  setMethod("crossprod", signature(x = "numeric", y = "Matrix"),  setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
198            function(x, y = NULL)  callGeneric(rbind(x), y))            function(x, y = NULL)  callGeneric(as.matrix(x), y))
199
200    ## The as.matrix() promotion seems illogical to MM,
201    ## but is according to help(tcrossprod, package = "base") :
202    setMethod("tcrossprod", signature(x = "Matrix", y = "numeric"),
203              function(x, y = NULL) callGeneric(x, as.matrix(y)))
204    setMethod("tcrossprod", signature(x = "numeric", y = "Matrix"),
205              function(x, y = NULL)  callGeneric(as.matrix(x), y))
206
207  setMethod("solve", signature(a = "Matrix", b = "numeric"),  setMethod("solve", signature(a = "Matrix", b = "numeric"),
208            function(a, b, ...) callGeneric(a, as.matrix(b)))            function(a, b, ...) callGeneric(a, as.matrix(b)))
# Line 180  Line 217
217            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
218  setMethod("crossprod", signature(x = "ANY", y = "Matrix"),  setMethod("crossprod", signature(x = "ANY", y = "Matrix"),
219            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
220    setMethod("tcrossprod", signature(x = "Matrix", y = "ANY"),
221              function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
222    setMethod("tcrossprod", signature(x = "ANY", y = "Matrix"),
223              function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
224
225    ## cheap fallbacks
226    setMethod("crossprod", signature(x = "Matrix", y = "Matrix"),
227              function(x, y = NULL) t(x) %*% y)
228    setMethod("tcrossprod", signature(x = "Matrix", y = "Matrix"),
229              function(x, y = NULL) x %*% t(y))
230
231  ## There are special sparse methods; this is a "fall back":  ## There are special sparse methods; this is a "fall back":
232  setMethod("kronecker", signature(X = "Matrix", Y = "ANY",  setMethod("kronecker", signature(X = "Matrix", Y = "ANY",
# Line 192  Line 239
239                Y <- as(Y, "matrix") ; Matrix(callGeneric()) })                Y <- as(Y, "matrix") ; Matrix(callGeneric()) })
240
241
242    setMethod("diag", signature(x = "Matrix"),
243              function(x, nrow, ncol) .bail.out.1(.Generic, class(x)))
244  setMethod("t", signature(x = "Matrix"),  setMethod("t", signature(x = "Matrix"),
245            function(x) .bail.out.1(.Generic, class(x)))            function(x) .bail.out.1(.Generic, class(x)))
246
# Line 254  Line 303
303                           drop = "ANY"),                           drop = "ANY"),
304            function (x, i, j, drop) {            function (x, i, j, drop) {
305                as(x, geClass(x))@x[as.vector(i)]                as(x, geClass(x))@x[as.vector(i)]
306                                          # -> error when lengths don't match                ## -> error when lengths don't match
307            })            })
308
309    ## FIXME: The following is good for    M [ <logical>   ]
310    ##        *BUT* it also triggers for   M [ <logical> , ] where it is *WRONG*
311    ##       using nargs() does not help: it gives '3' for both cases
312    if(FALSE)
313  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",
314                           drop = "ANY"),                           drop = "ANY"),
315            function (x, i, j, drop) as(x, geClass(x))@x[i])            function (x, i, j, drop) {
316                  ## DEBUG
317                  cat("[(Matrix,i,..): nargs=", nargs(),"\n")
318                  as(x, geClass(x))@x[i] })
319
320
321  ## "FIXME:"  ## "FIXME:"

Legend:
 Removed from v.1290 changed lines Added in v.1331