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 1455, Mon Aug 28 15:35:44 2006 UTC revision 1615, Thu Oct 5 16:30:46 2006 UTC
# Line 6  Line 6 
6  setAs("Matrix", "sparseMatrix", function(from) as_Csparse(from))  setAs("Matrix", "sparseMatrix", function(from) as_Csparse(from))
7  setAs("Matrix", "denseMatrix",  function(from) as_dense(from))  setAs("Matrix", "denseMatrix",  function(from) as_dense(from))
8    
9    setAs(from = "Matrix", to = "matrix", # do *not* call base::as.matrix() here:
10          function(from) .bail.out.2("coerce", class(from), class(to)))
11    
12  ## ## probably not needed eventually:  ## ## probably not needed eventually:
13  ## setAs(from = "ddenseMatrix", to = "matrix",  ## setAs(from = "ddenseMatrix", to = "matrix",
14  ##       function(from) {  ##       function(from) {
# Line 19  Line 22 
22  setMethod("as.array",  signature(x = "Matrix"), function(x) as(x, "matrix"))  setMethod("as.array",  signature(x = "Matrix"), function(x) as(x, "matrix"))
23    
24  ## head and tail apply to all Matrix objects for which subscripting is allowed:  ## head and tail apply to all Matrix objects for which subscripting is allowed:
25  ## if(paste(R.version$major, R.version$minor, sep=".") < "2.4") {  setMethod("head", signature(x = "Matrix"), utils::head.matrix)
26      setMethod("head", signature(x = "Matrix"), utils:::head.matrix)  setMethod("tail", signature(x = "Matrix"), utils::tail.matrix)
     setMethod("tail", signature(x = "Matrix"), utils:::tail.matrix)  
 ## } else { # R 2.4.0 and newer  
 ##     setMethod("head", signature(x = "Matrix"), utils::head.matrix)  
 ##     setMethod("tail", signature(x = "Matrix"), utils::tail.matrix)  
 ## }  
27    
28  ## slow "fall back" method {subclasses should have faster ones}:  ## slow "fall back" method {subclasses should have faster ones}:
29  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),
# Line 38  Line 36 
36            function(x, ...) as.logical(as.vector(x)))            function(x, ...) as.logical(as.vector(x)))
37    
38    
39  ## Note that isSymmetric is *not* exported  ## "base" has an isSymmetric() S3-generic since R 2.3.0
 ## but that "base" has an isSymmetric() S3-generic since R 2.3.0  
40  setMethod("isSymmetric", signature(object = "symmetricMatrix"),  setMethod("isSymmetric", signature(object = "symmetricMatrix"),
41            function(object,tol) TRUE)            function(object,tol) TRUE)
42  setMethod("isSymmetric", signature(object = "triangularMatrix"),  setMethod("isSymmetric", signature(object = "triangularMatrix"),
43            ## TRUE iff diagonal:            ## TRUE iff diagonal:
44            function(object,tol) isDiagonal(object))            function(object,tol) isDiagonal(object))
45    
 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)  
           })  
   
   
46  setMethod("isTriangular", signature(object = "triangularMatrix"),  setMethod("isTriangular", signature(object = "triangularMatrix"),
47            function(object, ...) TRUE)            function(object, ...) TRUE)
48    
# Line 101  Line 78 
78                sparse = NULL, forceCheck = FALSE)                sparse = NULL, forceCheck = FALSE)
79  {  {
80      sparseDefault <- function(m)      sparseDefault <- function(m)
81          prod(dim(m)) > 2*sum(as(m, "matrix") != 0)          prod(dim(m)) > 2*sum(is.na(m <- as(m, "matrix")) | m != 0)
82    
83      i.M <- is(data, "Matrix")      i.M <- is(data, "Matrix")
84      if(is.null(sparse) && (i.M || is(data, "matrix")))      if(is.null(sparse) && (i.M || is(data, "matrix")))
85          sparse <- sparseDefault(data)          sparse <- sparseDefault(data)
86    
87      doDN <- TRUE      doDN <- TRUE
88      if (i.M && !forceCheck) {      if (i.M) {
89          sM <- is(data,"sparseMatrix")          sM <- is(data,"sparseMatrix")
90          if((sparse && sM) || (!sparse && !sM))          if(!forceCheck && ((sparse && sM) || (!sparse && !sM)))
91              return(data)              return(data)
92          ## else : convert  dense <-> sparse -> at end          ## else : convert  dense <-> sparse -> at end
93      }      }
# Line 119  Line 96 
96              nrow <- ceiling(length(data)/ncol)              nrow <- ceiling(length(data)/ncol)
97          else if (missing(ncol))          else if (missing(ncol))
98              ncol <- ceiling(length(data)/nrow)              ncol <- ceiling(length(data)/nrow)
99          if(length(data) == 1 && data == 0 && !identical(sparse,FALSE)) {          if(length(data) == 1 && !is.na(data) && data == 0 &&
100               !identical(sparse, FALSE)) {
101    
102              if(is.null(sparse)) sparse <- TRUE              if(is.null(sparse)) sparse <- TRUE
103              ## will be sparse: do NOT construct full matrix!              ## will be sparse: do NOT construct full matrix!
104              data <- new(if(is.numeric(data)) "dgTMatrix" else              data <- new(if(is.numeric(data)) "dgTMatrix" else
# Line 301  Line 280 
280            function(x,i,j, drop)            function(x,i,j, drop)
281            stop("invalid or not-yet-implemented 'Matrix' subsetting"))            stop("invalid or not-yet-implemented 'Matrix' subsetting"))
282    
283  ##  "logical *vector* indexing, such as  M [ M >= 10 ] :  ## logical indexing, such as M[ M >= 7 ] *BUT* also M[ M[,1] >= 3,],
284  setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",  ## The following is *both* for    M [ <logical>   ]
285                           drop = "ANY"),  ##                 and also for   M [ <logical> , ]
286            function (x, i, j, drop) {  .M.sub.i.logical <- function (x, i, j, drop)
287    {
288        nA <- nargs()
289        if(nA == 2) { ##  M [ M >= 7 ]
290                as(x, geClass(x))@x[as.vector(i)]                as(x, geClass(x))@x[as.vector(i)]
291                ## -> error when lengths don't match                ## -> error when lengths don't match
292            })      } else if(nA == 3) { ##  M [ M[,1, drop=FALSE] >= 7, ]
293            stop("not-yet-implemented 'Matrix' subsetting") ## FIXME
294    
295  ## FIXME: The following is good for    M [ <logical>   ]      } else stop("nargs() = ", nA,
296  ##        *BUT* it also triggers for   M [ <logical> , ] where it is *WRONG*                  " should never happen; please report.")
297  ##       using nargs() does not help: it gives '3' for both cases  }
298  if(FALSE)  setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",
299                             drop = "ANY"),
300              .M.sub.i.logical)
301  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",  setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",
302                           drop = "ANY"),                           drop = "ANY"),
303            function (x, i, j, drop) {            .M.sub.i.logical)
               ## DEBUG  
               cat("[(Matrix,i,..): nargs=", nargs(),"\n")  
               as(x, geClass(x))@x[i] })  
304    
305    
306  ## "FIXME:"  ## "FIXME:"
307  ## 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?
308  ##  and                A[ LL ]  where LL is a logical *vector*  
 ## -> [.data.frame uses nargs() - can we do this in the *generic* ?  
309    
310    
311  ### "[<-" : -----------------  ### "[<-" : -----------------
# Line 333  Line 314 
314  setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",  setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",
315                                  value = "ANY"),## double/logical/...                                  value = "ANY"),## double/logical/...
316            function (x, value) {            function (x, value) {
317                  ## Fails for 'nMatrix' ... FIXME : make sure have method there
318                x@x <- value                x@x <- value
319                validObject(x)# check if type and lengths above match                validObject(x)# check if type and lengths above match
320                x                x
321            })            })
322    
323  ## Method for all 'Matrix' kinds (rather than incomprehensible error messages);  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
324                                    value = "Matrix"),
325                     function (x, i, j, value)
326                     callGeneric(x=x, i=i, j=j, value = as.vector(value)))
327    setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
328                                    value = "matrix"),
329                     function (x, i, j, value)
330                     callGeneric(x=x, i=i, j=j, value = c(value)))
331    
332  ## (ANY,ANY,ANY) is used when no `real method' is implemented :  ## (ANY,ANY,ANY) is used when no `real method' is implemented :
333  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
334                                  value = "ANY"),                                  value = "ANY"),
335            function (x, i, j, value) {            function (x, i, j, value) {
336                if(!is.atomic(value))                if(!is.atomic(value))
337                    stop("RHS 'value' must match matrix class ", class(x))                    stop(sprintf("RHS 'value' (class %s) matches 'ANY', but must match matrix class %s",
338                                   class(value),class(x)))
339                else stop("not-yet-implemented 'Matrix[<-' method")                else stop("not-yet-implemented 'Matrix[<-' method")
340            })            })
341    

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

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