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 1390, Fri Aug 18 15:41:36 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    
30  ## slow "fall back" method {subclasses should have faster ones}:  ## slow "fall back" method {subclasses should have faster ones}:
31  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),
32            function(x) as.vector(as(x, "matrix")))            function(x) as.vector(as(x, "matrix")))
33    
34    ## mainly need these for "dMatrix" or "lMatrix" respectively, but why not general:
35    setMethod("as.numeric", signature(x = "Matrix"),
36              function(x, ...) as.numeric(as.vector(x)))
37    setMethod("as.logical", signature(x = "Matrix"),
38              function(x, ...) as.logical(as.vector(x)))
39    
40    
41  ## Note that isSymmetric is *not* exported  ## Note that isSymmetric is *not* exported
42  ## 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 118 
118              nrow <- ceiling(length(data)/ncol)              nrow <- ceiling(length(data)/ncol)
119          else if (missing(ncol))          else if (missing(ncol))
120              ncol <- ceiling(length(data)/nrow)              ncol <- ceiling(length(data)/nrow)
121            if(length(data) == 1 && data == 0 && !identical(sparse,FALSE)) {
122                if(is.null(sparse)) sparse <- TRUE
123                ## will be sparse: do NOT construct full matrix!
124                data <- new(if(is.numeric(data)) "dgTMatrix" else
125                            if(is.logical(data)) "lgTMatrix" else
126                            stop("invalid 'data'"),
127                            Dim = as.integer(c(nrow,ncol)),
128                            Dimnames = if(is.null(dimnames)) list(NULL,NULL)
129                            else dimnames)
130            } else { ## normal case
131          data <- .Internal(matrix(data, nrow, ncol, byrow))          data <- .Internal(matrix(data, nrow, ncol, byrow))
132          if(is.null(sparse))          if(is.null(sparse))
133              sparse <- sparseDefault(data)              sparse <- sparseDefault(data)
134          dimnames(data) <- dimnames          dimnames(data) <- dimnames
135            }
136      } else if (!is.null(dimnames))      } else if (!is.null(dimnames))
137          dimnames(data) <- dimnames          dimnames(data) <- dimnames
138    
# Line 163  Line 195 
195    
196  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
197            function(x, y = NULL) callGeneric(x, as.matrix(y)))            function(x, y = NULL) callGeneric(x, as.matrix(y)))
   
198  setMethod("crossprod", signature(x = "numeric", y = "Matrix"),  setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
199            function(x, y = NULL)  callGeneric(rbind(x), y))            function(x, y = NULL)  callGeneric(as.matrix(x), y))
200    
201    ## The as.matrix() promotion seems illogical to MM,
202    ## but is according to help(tcrossprod, package = "base") :
203    setMethod("tcrossprod", signature(x = "Matrix", y = "numeric"),
204              function(x, y = NULL) callGeneric(x, as.matrix(y)))
205    setMethod("tcrossprod", signature(x = "numeric", y = "Matrix"),
206              function(x, y = NULL)  callGeneric(as.matrix(x), y))
207    
208  setMethod("solve", signature(a = "Matrix", b = "numeric"),  setMethod("solve", signature(a = "Matrix", b = "numeric"),
209            function(a, b, ...) callGeneric(a, as.matrix(b)))            function(a, b, ...) callGeneric(a, as.matrix(b)))
# Line 180  Line 218 
218            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
219  setMethod("crossprod", signature(x = "ANY", y = "Matrix"),  setMethod("crossprod", signature(x = "ANY", y = "Matrix"),
220            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
221    setMethod("tcrossprod", signature(x = "Matrix", y = "ANY"),
222              function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
223    setMethod("tcrossprod", signature(x = "ANY", y = "Matrix"),
224              function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
225    
226    ## cheap fallbacks
227    setMethod("crossprod", signature(x = "Matrix", y = "Matrix"),
228              function(x, y = NULL) t(x) %*% y)
229    setMethod("tcrossprod", signature(x = "Matrix", y = "Matrix"),
230              function(x, y = NULL) x %*% t(y))
231    
232  ## There are special sparse methods; this is a "fall back":  ## There are special sparse methods; this is a "fall back":
233  setMethod("kronecker", signature(X = "Matrix", Y = "ANY",  setMethod("kronecker", signature(X = "Matrix", Y = "ANY",
# Line 192  Line 240 
240                Y <- as(Y, "matrix") ; Matrix(callGeneric()) })                Y <- as(Y, "matrix") ; Matrix(callGeneric()) })
241    
242    
243    setMethod("diag", signature(x = "Matrix"),
244              function(x, nrow, ncol) .bail.out.1(.Generic, class(x)))
245  setMethod("t", signature(x = "Matrix"),  setMethod("t", signature(x = "Matrix"),
246            function(x) .bail.out.1(.Generic, class(x)))            function(x) .bail.out.1(.Generic, class(x)))
247    
# Line 254  Line 304 
304                           drop = "ANY"),                           drop = "ANY"),
305            function (x, i, j, drop) {            function (x, i, j, drop) {
306                as(x, geClass(x))@x[as.vector(i)]                as(x, geClass(x))@x[as.vector(i)]
307                                          # -> error when lengths don't match                ## -> error when lengths don't match
308            })            })
309    
310    ## FIXME: The following is good for    M [ <logical>   ]
311    ##        *BUT* it also triggers for   M [ <logical> , ] where it is *WRONG*
312    ##       using nargs() does not help: it gives '3' for both cases
313    if(FALSE)
314  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",
315                           drop = "ANY"),                           drop = "ANY"),
316            function (x, i, j, drop) as(x, geClass(x))@x[i])            function (x, i, j, drop) {
317                  ## DEBUG
318                  cat("[(Matrix,i,..): nargs=", nargs(),"\n")
319                  as(x, geClass(x))@x[i] })
320    
321    
322  ## "FIXME:"  ## "FIXME:"

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

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