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 1225, Mon Mar 13 14:06:17 2006 UTC revision 1347, Mon Aug 7 08:47:43 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    setMethod("head", signature(x = "Matrix"), utils:::head.matrix)
23    setMethod("tail", signature(x = "Matrix"), utils:::tail.matrix)
24    
25  ## slow "fall back" method {subclasses should have faster ones}:  ## slow "fall back" method {subclasses should have faster ones}:
26  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),
27            function(x) as.vector(as(x, "matrix")))            function(x) as.vector(as(x, "matrix")))
# Line 46  Line 56 
56    
57    
58  setMethod("isTriangular", signature(object = "triangularMatrix"),  setMethod("isTriangular", signature(object = "triangularMatrix"),
59            function(object,tol) TRUE)            function(object, ...) TRUE)
60  setMethod("isTriangular", signature(object = "matrix"),  
61            .is.triangular)  setMethod("isTriangular", signature(object = "matrix"), isTriMat)
62    
63  setMethod("isDiagonal", signature(object = "matrix"), .is.diagonal)  setMethod("isDiagonal", signature(object = "matrix"), .is.diagonal)
64    
# Line 97  Line 107 
107              nrow <- ceiling(length(data)/ncol)              nrow <- ceiling(length(data)/ncol)
108          else if (missing(ncol))          else if (missing(ncol))
109              ncol <- ceiling(length(data)/nrow)              ncol <- ceiling(length(data)/nrow)
110            if(length(data) == 1 && data == 0 && !identical(sparse,FALSE)) {
111                if(is.null(sparse)) sparse <- TRUE
112                ## will be sparse: do NOT construct full matrix!
113                data <- new(if(is.numeric(data)) "dgTMatrix" else
114                            if(is.logical(data)) "lgTMatrix" else
115                            stop("invalid 'data'"),
116                            Dim = as.integer(c(nrow,ncol)),
117                            Dimnames = if(is.null(dimnames)) list(NULL,NULL)
118                            else dimnames)
119            } else { ## normal case
120          data <- .Internal(matrix(data, nrow, ncol, byrow))          data <- .Internal(matrix(data, nrow, ncol, byrow))
121          if(is.null(sparse))          if(is.null(sparse))
122              sparse <- sparseDefault(data)              sparse <- sparseDefault(data)
123          dimnames(data) <- dimnames          dimnames(data) <- dimnames
124            }
125      } else if (!is.null(dimnames))      } else if (!is.null(dimnames))
126          dimnames(data) <- dimnames          dimnames(data) <- dimnames
127    
# Line 163  Line 184 
184    
185  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
186            function(x, y = NULL) callGeneric(x, as.matrix(y)))            function(x, y = NULL) callGeneric(x, as.matrix(y)))
   
187  setMethod("crossprod", signature(x = "numeric", y = "Matrix"),  setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
188            function(x, y = NULL)  callGeneric(rbind(x), y))            function(x, y = NULL)  callGeneric(as.matrix(x), y))
189    
190    ## The as.matrix() promotion seems illogical to MM,
191    ## but is according to help(tcrossprod, package = "base") :
192    setMethod("tcrossprod", signature(x = "Matrix", y = "numeric"),
193              function(x, y = NULL) callGeneric(x, as.matrix(y)))
194    setMethod("tcrossprod", signature(x = "numeric", y = "Matrix"),
195              function(x, y = NULL)  callGeneric(as.matrix(x), y))
196    
197  setMethod("solve", signature(a = "Matrix", b = "numeric"),  setMethod("solve", signature(a = "Matrix", b = "numeric"),
198            function(a, b, ...) callGeneric(a, as.matrix(b)))            function(a, b, ...) callGeneric(a, as.matrix(b)))
# Line 180  Line 207 
207            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
208  setMethod("crossprod", signature(x = "ANY", y = "Matrix"),  setMethod("crossprod", signature(x = "ANY", y = "Matrix"),
209            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
210    setMethod("tcrossprod", signature(x = "Matrix", y = "ANY"),
211              function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
212    setMethod("tcrossprod", signature(x = "ANY", y = "Matrix"),
213              function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
214    
215    ## cheap fallbacks
216    setMethod("crossprod", signature(x = "Matrix", y = "Matrix"),
217              function(x, y = NULL) t(x) %*% y)
218    setMethod("tcrossprod", signature(x = "Matrix", y = "Matrix"),
219              function(x, y = NULL) x %*% t(y))
220    
221  ## There are special sparse methods; this is a "fall back":  ## There are special sparse methods; this is a "fall back":
222  setMethod("kronecker", signature(X = "Matrix", Y = "ANY",  setMethod("kronecker", signature(X = "Matrix", Y = "ANY",
# Line 192  Line 229 
229                Y <- as(Y, "matrix") ; Matrix(callGeneric()) })                Y <- as(Y, "matrix") ; Matrix(callGeneric()) })
230    
231    
232    setMethod("diag", signature(x = "Matrix"),
233              function(x, nrow, ncol) .bail.out.1(.Generic, class(x)))
234  setMethod("t", signature(x = "Matrix"),  setMethod("t", signature(x = "Matrix"),
235            function(x) .bail.out.1(.Generic, class(x)))            function(x) .bail.out.1(.Generic, class(x)))
236    
# Line 254  Line 293 
293                           drop = "ANY"),                           drop = "ANY"),
294            function (x, i, j, drop) {            function (x, i, j, drop) {
295                as(x, geClass(x))@x[as.vector(i)]                as(x, geClass(x))@x[as.vector(i)]
296                                          # -> error when lengths don't match                ## -> error when lengths don't match
297            })            })
298    
299    ## FIXME: The following is good for    M [ <logical>   ]
300    ##        *BUT* it also triggers for   M [ <logical> , ] where it is *WRONG*
301    ##       using nargs() does not help: it gives '3' for both cases
302    if(FALSE)
303  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",
304                           drop = "ANY"),                           drop = "ANY"),
305            function (x, i, j, drop) as(x, geClass(x))@x[i])            function (x, i, j, drop) {
306                  ## DEBUG
307                  cat("[(Matrix,i,..): nargs=", nargs(),"\n")
308                  as(x, geClass(x))@x[i] })
309    
310    
311  ## "FIXME:"  ## "FIXME:"
# Line 290  Line 336 
336            })            })
337    
338    
   
 ## NOTE: the following only works for R 2.2.x (and later) ---  
 ## ----  *and* 'Matrix' must have been *installed* by R >= 2.2.x  
   
 if(paste(R.version$major, R.version$minor, sep=".") >= "2.2") {  
   
339      ## The trivial methods :      ## The trivial methods :
340      setMethod("cbind2", signature(x = "Matrix", y = "NULL"),      setMethod("cbind2", signature(x = "Matrix", y = "NULL"),
341                function(x, y) x)                function(x, y) x)
# Line 326  Line 366 
366                    colCheck(x,y)                    colCheck(x,y)
367                    t(cbind2(t(x), t(y)))                    t(cbind2(t(x), t(y)))
368                })                })
   
 }## R-2.2.x and newer  

Legend:
Removed from v.1225  
changed lines
  Added in v.1347

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