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 1326, Wed Jul 19 14:58:26 2006 UTC revision 1455, Mon Aug 28 15:35:44 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  
21  setMethod("head", signature(x = "Matrix"),  ## head and tail apply to all Matrix objects for which subscripting is allowed:
22            function(x, n = 6, ...)  ## if(paste(R.version$major, R.version$minor, sep=".") < "2.4") {
23            x[seq(len = min(n, nrow(x))), , drop = FALSE])      setMethod("head", signature(x = "Matrix"), utils:::head.matrix)
24  setMethod("tail", signature(x = "Matrix"),      setMethod("tail", signature(x = "Matrix"), utils:::tail.matrix)
25            function (x, n = 6, addrownums = TRUE, ...)  ## } else { # R 2.4.0 and newer
26        {  ##     setMethod("head", signature(x = "Matrix"), utils::head.matrix)
27            nrx <- nrow(x)  ##     setMethod("tail", signature(x = "Matrix"), utils::tail.matrix)
28            sel <- seq(to = nrx, length = min(n, nrx))  ## }
           ans <- x[sel, , drop = FALSE]  
           if (addrownums && is.null(rownames(x)))  
               rownames(ans) <- paste("[", sel, ",]", sep = "")  
           ans  
       })  
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 91  Line 98 
98    
99  Matrix <-  Matrix <-
100      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
101                sparse = NULL)                sparse = NULL, forceCheck = FALSE)
102  {  {
103      sparseDefault <- function(m)      sparseDefault <- function(m)
104          prod(dim(m)) > 2*sum(as(m, "matrix") != 0)          prod(dim(m)) > 2*sum(as(m, "matrix") != 0)
# Line 100  Line 107 
107      if(is.null(sparse) && (i.M || is(data, "matrix")))      if(is.null(sparse) && (i.M || is(data, "matrix")))
108          sparse <- sparseDefault(data)          sparse <- sparseDefault(data)
109    
110      if (i.M) {      doDN <- TRUE
111        if (i.M && !forceCheck) {
112          sM <- is(data,"sparseMatrix")          sM <- is(data,"sparseMatrix")
113          if((sparse && sM) || (!sparse && !sM))          if((sparse && sM) || (!sparse && !sM))
114              return(data)              return(data)
# Line 118  Line 126 
126                          if(is.logical(data)) "lgTMatrix" else                          if(is.logical(data)) "lgTMatrix" else
127                          stop("invalid 'data'"),                          stop("invalid 'data'"),
128                          Dim = as.integer(c(nrow,ncol)),                          Dim = as.integer(c(nrow,ncol)),
129                          Dimnames = if(is.null(dimnames))                          Dimnames = if(is.null(dimnames)) list(NULL,NULL)
130                          list(NULL,NULL) else dimnames)                          else dimnames)
131          } else { ## normal case          } else { ## normal case
132              data <- .Internal(matrix(data, nrow, ncol, byrow))              data <- .Internal(matrix(data, nrow, ncol, byrow))
133              if(is.null(sparse))              if(is.null(sparse))
134                  sparse <- sparseDefault(data)                  sparse <- sparseDefault(data)
135              dimnames(data) <- dimnames              dimnames(data) <- dimnames
136          }          }
137      } else if (!is.null(dimnames))          doDN <- FALSE
138          dimnames(data) <- dimnames      }
   
139      ## 'data' is now a "matrix" or "Matrix"      ## 'data' is now a "matrix" or "Matrix"
140        if (doDN && !is.null(dimnames))
141            dimnames(data) <- dimnames
142    
143      ## check for symmetric / triangular / diagonal :      ## check for symmetric / triangular / diagonal :
144      isSym <- isSymmetric(data)      isSym <- isSymmetric(data)
# Line 188  Line 197 
197    
198  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
199            function(x, y = NULL) callGeneric(x, as.matrix(y)))            function(x, y = NULL) callGeneric(x, as.matrix(y)))
   
200  setMethod("crossprod", signature(x = "numeric", y = "Matrix"),  setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
201            function(x, y = NULL)  callGeneric(rbind(x), y))            function(x, y = NULL)  callGeneric(as.matrix(x), y))
202    
203    ## The as.matrix() promotion seems illogical to MM,
204    ## but is according to help(tcrossprod, package = "base") :
205    setMethod("tcrossprod", signature(x = "Matrix", y = "numeric"),
206              function(x, y = NULL) callGeneric(x, as.matrix(y)))
207    setMethod("tcrossprod", signature(x = "numeric", y = "Matrix"),
208              function(x, y = NULL)  callGeneric(as.matrix(x), y))
209    
210  setMethod("solve", signature(a = "Matrix", b = "numeric"),  setMethod("solve", signature(a = "Matrix", b = "numeric"),
211            function(a, b, ...) callGeneric(a, as.matrix(b)))            function(a, b, ...) callGeneric(a, as.matrix(b)))
# Line 205  Line 220 
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("crossprod", signature(x = "ANY", y = "Matrix"),  setMethod("crossprod", signature(x = "ANY", y = "Matrix"),
222            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
223    setMethod("tcrossprod", signature(x = "Matrix", y = "ANY"),
224              function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
225    setMethod("tcrossprod", signature(x = "ANY", y = "Matrix"),
226              function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
227    
228    ## cheap fallbacks
229    setMethod("crossprod", signature(x = "Matrix", y = "Matrix"),
230              function(x, y = NULL) t(x) %*% y)
231    setMethod("tcrossprod", signature(x = "Matrix", y = "Matrix"),
232              function(x, y = NULL) x %*% t(y))
233    
234  ## There are special sparse methods; this is a "fall back":  ## There are special sparse methods; this is a "fall back":
235  setMethod("kronecker", signature(X = "Matrix", Y = "ANY",  setMethod("kronecker", signature(X = "Matrix", Y = "ANY",
# Line 281  Line 306 
306                           drop = "ANY"),                           drop = "ANY"),
307            function (x, i, j, drop) {            function (x, i, j, drop) {
308                as(x, geClass(x))@x[as.vector(i)]                as(x, geClass(x))@x[as.vector(i)]
309                                          # -> error when lengths don't match                ## -> error when lengths don't match
310            })            })
311    
312    ## FIXME: The following is good for    M [ <logical>   ]
313    ##        *BUT* it also triggers for   M [ <logical> , ] where it is *WRONG*
314    ##       using nargs() does not help: it gives '3' for both cases
315    if(FALSE)
316  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",
317                           drop = "ANY"),                           drop = "ANY"),
318            function (x, i, j, drop) as(x, geClass(x))@x[i])            function (x, i, j, drop) {
319                  ## DEBUG
320                  cat("[(Matrix,i,..): nargs=", nargs(),"\n")
321                  as(x, geClass(x))@x[i] })
322    
323    
324  ## "FIXME:"  ## "FIXME:"

Legend:
Removed from v.1326  
changed lines
  Added in v.1455

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