SCM

SCM Repository

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

Diff of /pkg/R/Matrix.R

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1290, Thu Jun 8 09:30:21 2006 UTC revision 1389, Fri Aug 18 13:53:33 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 12  Line 18 
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    
21    ## head and tail apply to all Matrix objects for which subscripting is allowed:
22    if(paste(R.version$major, R.version$minor, sep=".") < "2.4") {
23        setMethod("head", signature(x = "Matrix"), utils:::head.matrix)
24        setMethod("tail", signature(x = "Matrix"), utils:::tail.matrix)
25    } else { # R 2.4.0 and newer
26        setMethod("head", signature(x = "Matrix"), utils::head.matrix)
27        setMethod("tail", signature(x = "Matrix"), utils::tail.matrix)
28    }
29  ## slow "fall back" method {subclasses should have faster ones}:  ## slow "fall back" method {subclasses should have faster ones}:
30  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),
31            function(x) as.vector(as(x, "matrix")))            function(x) as.vector(as(x, "matrix")))
32    
33    ## mainly need these for "dMatrix" or "lMatrix" respectively, but why not general:
34    setMethod("as.numeric", signature(x = "Matrix"),
35              function(x, ...) as.numeric(as.vector(x)))
36    setMethod("as.logical", signature(x = "Matrix"),
37              function(x, ...) as.logical(as.vector(x)))
38    
39    
40  ## Note that isSymmetric is *not* exported  ## Note that isSymmetric is *not* exported
41  ## but that "base" has an isSymmetric() S3-generic since R 2.3.0  ## but that "base" has an isSymmetric() S3-generic since R 2.3.0
# 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)) list(NULL,NULL)
128                            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.1389

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