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 1513, Tue Sep 5 13:16:23 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")))
28    
29    ## mainly need these for "dMatrix" or "lMatrix" respectively, but why not general:
30    setMethod("as.numeric", signature(x = "Matrix"),
31              function(x, ...) as.numeric(as.vector(x)))
32    setMethod("as.logical", signature(x = "Matrix"),
33              function(x, ...) as.logical(as.vector(x)))
34    
35  ## Note that isSymmetric is *not* exported  
36  ## but that "base" has an isSymmetric() S3-generic since R 2.3.0  ## "base" has an isSymmetric() S3-generic since R 2.3.0
37  setMethod("isSymmetric", signature(object = "symmetricMatrix"),  setMethod("isSymmetric", signature(object = "symmetricMatrix"),
38            function(object,tol) TRUE)            function(object,tol) TRUE)
39  setMethod("isSymmetric", signature(object = "triangularMatrix"),  setMethod("isSymmetric", signature(object = "triangularMatrix"),
40            ## TRUE iff diagonal:            ## TRUE iff diagonal:
41            function(object,tol) isDiagonal(object))            function(object,tol) isDiagonal(object))
42    
 if(paste(R.version$major, R.version$minor, sep=".") < "2.3")  
     ## need a "matrix" method as in R 2.3 and later  
     setMethod("isSymmetric", signature(object = "matrix"),  
               function(object, tol = 100*.Machine$double.eps, ...)  
           {  
               ## pretest: is it square?  
               d <- dim(object)  
               if(d[1] != d[2]) return(FALSE)  
               ## for `broken' all.equal in R <= 2.2.x:  
               dn <- dimnames(object)  
               if(!identical(dn[1], dn[2])) return(FALSE)  
               test <-  
                   if(is.complex(object))  
                       all.equal.numeric(object, Conj(t(object)), tol = tol, ...)  
                   else              # numeric, character, ..  
                       all.equal(object, t(object), tol = tol, ...)  
               isTRUE(test)  
           })  
   
   
43  setMethod("isTriangular", signature(object = "triangularMatrix"),  setMethod("isTriangular", signature(object = "triangularMatrix"),
44            function(object, ...) TRUE)            function(object, ...) TRUE)
45    
# Line 77  Line 72 
72    
73  Matrix <-  Matrix <-
74      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
75                sparse = NULL)                sparse = NULL, forceCheck = FALSE)
76  {  {
77      sparseDefault <- function(m)      sparseDefault <- function(m)
78          prod(dim(m)) > 2*sum(as(m, "matrix") != 0)          prod(dim(m)) > 2*sum(is.na(m <- as(m, "matrix")) | m != 0)
79    
80      i.M <- is(data, "Matrix")      i.M <- is(data, "Matrix")
81      if(is.null(sparse) && (i.M || is(data, "matrix")))      if(is.null(sparse) && (i.M || is(data, "matrix")))
82          sparse <- sparseDefault(data)          sparse <- sparseDefault(data)
83    
84      if (i.M) {      doDN <- TRUE
85        if (i.M && !forceCheck) {
86          sM <- is(data,"sparseMatrix")          sM <- is(data,"sparseMatrix")
87          if((sparse && sM) || (!sparse && !sM))          if((sparse && sM) || (!sparse && !sM))
88              return(data)              return(data)
# Line 97  Line 93 
93              nrow <- ceiling(length(data)/ncol)              nrow <- ceiling(length(data)/ncol)
94          else if (missing(ncol))          else if (missing(ncol))
95              ncol <- ceiling(length(data)/nrow)              ncol <- ceiling(length(data)/nrow)
96            if(length(data) == 1 && !is.na(data) && data == 0 &&
97               !identical(sparse, FALSE)) {
98    
99                if(is.null(sparse)) sparse <- TRUE
100                ## will be sparse: do NOT construct full matrix!
101                data <- new(if(is.numeric(data)) "dgTMatrix" else
102                            if(is.logical(data)) "lgTMatrix" else
103                            stop("invalid 'data'"),
104                            Dim = as.integer(c(nrow,ncol)),
105                            Dimnames = if(is.null(dimnames)) list(NULL,NULL)
106                            else dimnames)
107            } else { ## normal case
108          data <- .Internal(matrix(data, nrow, ncol, byrow))          data <- .Internal(matrix(data, nrow, ncol, byrow))
109          if(is.null(sparse))          if(is.null(sparse))
110              sparse <- sparseDefault(data)              sparse <- sparseDefault(data)
111          dimnames(data) <- dimnames          dimnames(data) <- dimnames
112      } else if (!is.null(dimnames))          }
113          dimnames(data) <- dimnames          doDN <- FALSE
114        }
115      ## 'data' is now a "matrix" or "Matrix"      ## 'data' is now a "matrix" or "Matrix"
116        if (doDN && !is.null(dimnames))
117            dimnames(data) <- dimnames
118    
119      ## check for symmetric / triangular / diagonal :      ## check for symmetric / triangular / diagonal :
120      isSym <- isSymmetric(data)      isSym <- isSymmetric(data)
# Line 163  Line 173 
173    
174  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
175            function(x, y = NULL) callGeneric(x, as.matrix(y)))            function(x, y = NULL) callGeneric(x, as.matrix(y)))
   
176  setMethod("crossprod", signature(x = "numeric", y = "Matrix"),  setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
177            function(x, y = NULL)  callGeneric(rbind(x), y))            function(x, y = NULL)  callGeneric(as.matrix(x), y))
178    
179    ## The as.matrix() promotion seems illogical to MM,
180    ## but is according to help(tcrossprod, package = "base") :
181    setMethod("tcrossprod", signature(x = "Matrix", y = "numeric"),
182              function(x, y = NULL) callGeneric(x, as.matrix(y)))
183    setMethod("tcrossprod", signature(x = "numeric", y = "Matrix"),
184              function(x, y = NULL)  callGeneric(as.matrix(x), y))
185    
186  setMethod("solve", signature(a = "Matrix", b = "numeric"),  setMethod("solve", signature(a = "Matrix", b = "numeric"),
187            function(a, b, ...) callGeneric(a, as.matrix(b)))            function(a, b, ...) callGeneric(a, as.matrix(b)))
# Line 180  Line 196 
196            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
197  setMethod("crossprod", signature(x = "ANY", y = "Matrix"),  setMethod("crossprod", signature(x = "ANY", y = "Matrix"),
198            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))            function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
199    setMethod("tcrossprod", signature(x = "Matrix", y = "ANY"),
200              function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
201    setMethod("tcrossprod", signature(x = "ANY", y = "Matrix"),
202              function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
203    
204    ## cheap fallbacks
205    setMethod("crossprod", signature(x = "Matrix", y = "Matrix"),
206              function(x, y = NULL) t(x) %*% y)
207    setMethod("tcrossprod", signature(x = "Matrix", y = "Matrix"),
208              function(x, y = NULL) x %*% t(y))
209    
210  ## There are special sparse methods; this is a "fall back":  ## There are special sparse methods; this is a "fall back":
211  setMethod("kronecker", signature(X = "Matrix", Y = "ANY",  setMethod("kronecker", signature(X = "Matrix", Y = "ANY",
# Line 192  Line 218 
218                Y <- as(Y, "matrix") ; Matrix(callGeneric()) })                Y <- as(Y, "matrix") ; Matrix(callGeneric()) })
219    
220    
221    setMethod("diag", signature(x = "Matrix"),
222              function(x, nrow, ncol) .bail.out.1(.Generic, class(x)))
223  setMethod("t", signature(x = "Matrix"),  setMethod("t", signature(x = "Matrix"),
224            function(x) .bail.out.1(.Generic, class(x)))            function(x) .bail.out.1(.Generic, class(x)))
225    
# Line 249  Line 277 
277            function(x,i,j, drop)            function(x,i,j, drop)
278            stop("invalid or not-yet-implemented 'Matrix' subsetting"))            stop("invalid or not-yet-implemented 'Matrix' subsetting"))
279    
280  ##  "logical *vector* indexing, such as  M [ M >= 10 ] :  ## logical indexing, such as M[ M >= 7 ] *BUT* also M[ M[,1] >= 3,],
281  setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",  ## The following is *both* for    M [ <logical>   ]
282                           drop = "ANY"),  ##                 and also for   M [ <logical> , ]
283            function (x, i, j, drop) {  .M.sub.i.logical <- function (x, i, j, drop)
284    {
285        nA <- nargs()
286        if(nA == 2) { ##  M [ M >= 7 ]
287                as(x, geClass(x))@x[as.vector(i)]                as(x, geClass(x))@x[as.vector(i)]
288                                          # -> error when lengths don't match          ## -> error when lengths don't match
289            })      } else if(nA == 3) { ##  M [ M[,1, drop=FALSE] >= 7, ]
290            stop("not-yet-implemented 'Matrix' subsetting") ## FIXME
291    
292        } else stop("nargs() = ", nA,
293                    " should never happen; please report.")
294    }
295    setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",
296                             drop = "ANY"),
297              .M.sub.i.logical)
298  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",
299                           drop = "ANY"),                           drop = "ANY"),
300            function (x, i, j, drop) as(x, geClass(x))@x[i])            .M.sub.i.logical)
301    
302    
303  ## "FIXME:"  ## "FIXME:"
304  ## How can we get at   A[ ij ]  where ij is (i,j) 2-column matrix?  ## ------ get at  A[ ij ]  where ij is (i,j) 2-column matrix?
305  ##  and                A[ LL ]  where LL is a logical *vector*  
 ## -> [.data.frame uses nargs() - can we do this in the *generic* ?  
306    
307    
308  ### "[<-" : -----------------  ### "[<-" : -----------------

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

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