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 1673, Mon Nov 6 20:54:26 2006 UTC revision 1724, Sat Jan 13 21:06:51 2007 UTC
# Line 56  Line 56 
56    
57  setMethod("dim", signature(x = "Matrix"),  setMethod("dim", signature(x = "Matrix"),
58            function(x) x@Dim, valueClass = "integer")            function(x) x@Dim, valueClass = "integer")
59    
60  setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)  setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)
61    
62    
63  ## not exported but used more than once for "dimnames<-" method :  ## not exported but used more than once for "dimnames<-" method :
64  ## -- or do only once for all "Matrix" classes ??  ## -- or do only once for all "Matrix" classes ??
65  dimnamesGets <- function (x, value) {  dimnamesGets <- function (x, value) {
# Line 75  Line 78 
78  setMethod("unname", signature("Matrix", force="missing"),  setMethod("unname", signature("Matrix", force="missing"),
79            function(obj) { obj@Dimnames <- list(NULL,NULL); obj})            function(obj) { obj@Dimnames <- list(NULL,NULL); obj})
80    
81    setMethod("all", signature(x = "Matrix"),
82              function(x, ..., na.rm) { x <- as(x, "lMatrix"); callNextMethod()})
83    setMethod("any", signature(x = "Matrix"),
84              function(x, ..., na.rm) { x <- as(x, "lMatrix"); callNextMethod()})
85    
86    
87    
88  Matrix <-  Matrix <-
89      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
90                sparse = NULL, forceCheck = FALSE)                sparse = NULL, forceCheck = FALSE)
# Line 239  Line 249 
249            function(x) .bail.out.1(.Generic, class(x)))            function(x) .bail.out.1(.Generic, class(x)))
250    
251  ## Group Methods  ## Group Methods
 setMethod("+", signature(e1 = "Matrix", e2 = "missing"), function(e1) e1)  
 ## "fallback":  
 setMethod("-", signature(e1 = "Matrix", e2 = "missing"),  
           function(e1) {  
               warning("inefficient method used for \"- e1\"")  
               0-e1  
           })  
   
 ## old-style matrices are made into new ones  
 setMethod("Ops", signature(e1 = "Matrix", e2 = "matrix"),  
           function(e1, e2) callGeneric(e1, Matrix(e2)))  
 ##          callGeneric(e1, Matrix(e2, sparse=is(e1,"sparseMatrix"))))  
 setMethod("Ops", signature(e1 = "matrix", e2 = "Matrix"),  
           function(e1, e2) callGeneric(Matrix(e1), e2))  
   
 ## bail-outs -- on highest possible level, hence "Ops", not "Compare"/"Arith" :  
 setMethod("Ops", signature(e1 = "Matrix", e2 = "Matrix"),  
           function(e1, e2) {  
               d <- dimCheck(e1,e2)  
               .bail.out.2(.Generic, class(e1), class(e2))  
           })  
 setMethod("Ops", signature(e1 = "Matrix", e2 = "ANY"),  
           function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))  
 setMethod("Ops", signature(e1 = "ANY", e2 = "Matrix"),  
           function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))  
   
252    
253    ##-> see ./Ops.R
254    
255  ### --------------------------------------------------------------------------  ### --------------------------------------------------------------------------
256  ###  ###
# Line 362  Line 347 
347            })            })
348    
349  ## A[ ij ] <- value,  where ij is (i,j) 2-column matrix :  ## A[ ij ] <- value,  where ij is (i,j) 2-column matrix :
350    ## ----------------   The cheap general method --- FIXME: provide special ones
351  .M.repl.i.2col <- function (x, i, j, value)  .M.repl.i.2col <- function (x, i, j, value)
352  {  {
353      nA <- nargs()      nA <- nargs()
# Line 369  Line 355 
355          if(!is.integer(nc <- ncol(i)))          if(!is.integer(nc <- ncol(i)))
356              stop("'i' has no integer column number",              stop("'i' has no integer column number",
357                   " should never happen; please report")                   " should never happen; please report")
358            else if(!is.numeric(i) || nc != 2)
359                stop("such indexing must be by logical or 2-column numeric matrix")
360          if(is.logical(i)) {          if(is.logical(i)) {
361              i <- c(i) # drop "matrix"              i <- c(i) # drop "matrix"
362              return( callNextMethod() )              return( callNextMethod() )
363          } else if(!is.numeric(i) || nc != 2)          }
364              stop("such indexing must be by logical or 2-column numeric matrix")          if(!is.integer(i)) storage.mode(i) <- "integer"
365            if(any(i < 0))
366                stop("negative values are not allowed in a matrix subscript")
367            if(any(is.na(i)))
368                stop("NAs are not allowed in subscripted assignments")
369            if(any(i0 <- (i == 0))) # remove them
370                i <- i[ - which(i0, arr.ind = TRUE)[,"row"], ]
371            ## now have integer i >= 1
372          m <- nrow(i)          m <- nrow(i)
373          mod.x <- .type.kind[.M.kind(x)]          ## mod.x <- .type.kind[.M.kind(x)]
374          if(length(value) > 0 && m %% length(value) != 0)          if(length(value) > 0 && m %% length(value) != 0)
375              warning("number of items to replace is not a multiple of replacement length")              warning("number of items to replace is not a multiple of replacement length")
376          ## recycle:          ## recycle:
# Line 385  Line 380 
380          ## inefficient -- FIXME -- (also loses "symmetry" unnecessarily)          ## inefficient -- FIXME -- (also loses "symmetry" unnecessarily)
381          for(k in seq_len(m))          for(k in seq_len(m))
382              x[i1[k], i2[k]] <- value[k]              x[i1[k], i2[k]] <- value[k]
         x  
383    
384            x
385      } else stop("nargs() = ", nA, " should never happen; please report.")      } else stop("nargs() = ", nA, " should never happen; please report.")
386  }  }
387    
388  setReplaceMethod("[", signature(x = "Matrix", i = "matrix", j = "missing",  setReplaceMethod("[", signature(x = "Matrix", i = "matrix", j = "missing",
389                                  value = "replValue"),                                  value = "replValue"),
390            .M.repl.i.2col)            .M.repl.i.2col)
# Line 396  Line 392 
392    
393  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
394                                  value = "Matrix"),                                  value = "Matrix"),
395                     function (x, i, j, value) {
396    ### *TEMPORARY* diagnostic output:
397    ##                  cat("<Matrix1>[i,j] <- <Matrix1>:\n<Matrix1> = x :")
398    ##                  str(x)
399    ##                  cat("<Matrix2> = value :")
400    ##                  str(value)
401    ##                  cat("i :"); if(!missing(i)) str(i) else cat("<missing>\n")
402    ##                  cat("j :"); if(!missing(j)) str(j) else cat("<missing>\n")
403    
404                         callGeneric(x=x, i=i, j=j, value = as.vector(value))
405                     })
406    setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
407                                    value = "Matrix"),
408                   function (x, i, j, value)                   function (x, i, j, value)
409                   callGeneric(x=x, i=i, j=j, value = as.vector(value)))                   callGeneric(x=x, i=i, j=j, value = as.vector(value)))
410    
411  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
412                                  value = "matrix"),                                  value = "matrix"),
413                   function (x, i, j, value)                   function (x, i, j, value)
# Line 412  Line 422 
422                                 class(value),class(x)))                                 class(value),class(x)))
423                else stop("not-yet-implemented 'Matrix[<-' method")                else stop("not-yet-implemented 'Matrix[<-' method")
424            })            })
   
   
 ## The trivial methods :  
 setMethod("cbind2", signature(x = "Matrix", y = "NULL"),  
           function(x, y) x)  
 setMethod("cbind2", signature(x = "Matrix", y = "missing"),  
           function(x, y) x)  
 setMethod("cbind2", signature(x = "NULL", y="Matrix"),  
           function(x, y) x)  
   
 setMethod("rbind2", signature(x = "Matrix", y = "NULL"),  
           function(x, y) x)  
 setMethod("rbind2", signature(x = "Matrix", y = "missing"),  
           function(x, y) x)  
 setMethod("rbind2", signature(x = "NULL", y="Matrix"),  
           function(x, y) x)  
   
 ## Makes sure one gets x decent error message for the unimplemented cases:  
 setMethod("cbind2", signature(x = "Matrix", y = "Matrix"),  
           function(x, y) {  
               rowCheck(x,y)  
               stop(gettextf("cbind2() method for (%s,%s) not-yet defined",  
                             class(x), class(y)))  
           })  
   
 ## Use a working fall back {particularly useful for sparse}:  
 ## FIXME: implement rbind2 via "cholmod" for C* and Tsparse ones  
 setMethod("rbind2", signature(x = "Matrix", y = "Matrix"),  
           function(x, y) {  
               colCheck(x,y)  
               t(cbind2(t(x), t(y)))  
           })  

Legend:
Removed from v.1673  
changed lines
  Added in v.1724

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