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 1738, Tue Jan 23 17:17:15 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("length", "Matrix", function(x) prod(dim(x)))
61    
62  setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)  setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)
63    
64    
65  ## not exported but used more than once for "dimnames<-" method :  ## not exported but used more than once for "dimnames<-" method :
66  ## -- or do only once for all "Matrix" classes ??  ## -- or do only once for all "Matrix" classes ??
67  dimnamesGets <- function (x, value) {  dimnamesGets <- function (x, value) {
# Line 75  Line 80 
80  setMethod("unname", signature("Matrix", force="missing"),  setMethod("unname", signature("Matrix", force="missing"),
81            function(obj) { obj@Dimnames <- list(NULL,NULL); obj})            function(obj) { obj@Dimnames <- list(NULL,NULL); obj})
82    
83    setMethod("all", signature(x = "Matrix"),
84              function(x, ..., na.rm) { x <- as(x, "lMatrix"); callGeneric()})
85    setMethod("any", signature(x = "Matrix"),
86              function(x, ..., na.rm) { x <- as(x, "lMatrix"); callGeneric()})
87    
88    
89    
90  Matrix <-  Matrix <-
91      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
92                sparse = NULL, forceCheck = FALSE)                sparse = NULL, forceCheck = FALSE)
# Line 239  Line 251 
251            function(x) .bail.out.1(.Generic, class(x)))            function(x) .bail.out.1(.Generic, class(x)))
252    
253  ## 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)))  
   
254    
255    ##-> see ./Ops.R
256    
257  ### --------------------------------------------------------------------------  ### --------------------------------------------------------------------------
258  ###  ###
# Line 362  Line 349 
349            })            })
350    
351  ## A[ ij ] <- value,  where ij is (i,j) 2-column matrix :  ## A[ ij ] <- value,  where ij is (i,j) 2-column matrix :
352    ## ----------------   The cheap general method --- FIXME: provide special ones
353  .M.repl.i.2col <- function (x, i, j, value)  .M.repl.i.2col <- function (x, i, j, value)
354  {  {
355      nA <- nargs()      nA <- nargs()
# Line 369  Line 357 
357          if(!is.integer(nc <- ncol(i)))          if(!is.integer(nc <- ncol(i)))
358              stop("'i' has no integer column number",              stop("'i' has no integer column number",
359                   " should never happen; please report")                   " should never happen; please report")
360            else if(!is.numeric(i) || nc != 2)
361                stop("such indexing must be by logical or 2-column numeric matrix")
362          if(is.logical(i)) {          if(is.logical(i)) {
363                message(".M.repl.i.2col(): drop 'matrix' case ...")
364              i <- c(i) # drop "matrix"              i <- c(i) # drop "matrix"
365              return( callNextMethod() )              return( callNextMethod() )
366          } else if(!is.numeric(i) || nc != 2)          }
367              stop("such indexing must be by logical or 2-column numeric matrix")          if(!is.integer(i)) storage.mode(i) <- "integer"
368            if(any(i < 0))
369                stop("negative values are not allowed in a matrix subscript")
370            if(any(is.na(i)))
371                stop("NAs are not allowed in subscripted assignments")
372            if(any(i0 <- (i == 0))) # remove them
373                i <- i[ - which(i0, arr.ind = TRUE)[,"row"], ]
374            ## now have integer i >= 1
375          m <- nrow(i)          m <- nrow(i)
376          mod.x <- .type.kind[.M.kind(x)]          ## mod.x <- .type.kind[.M.kind(x)]
377          if(length(value) > 0 && m %% length(value) != 0)          if(length(value) > 0 && m %% length(value) != 0)
378              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")
379          ## recycle:          ## recycle:
# Line 385  Line 383 
383          ## inefficient -- FIXME -- (also loses "symmetry" unnecessarily)          ## inefficient -- FIXME -- (also loses "symmetry" unnecessarily)
384          for(k in seq_len(m))          for(k in seq_len(m))
385              x[i1[k], i2[k]] <- value[k]              x[i1[k], i2[k]] <- value[k]
         x  
386    
387            x
388      } else stop("nargs() = ", nA, " should never happen; please report.")      } else stop("nargs() = ", nA, " should never happen; please report.")
389  }  }
390    
391  setReplaceMethod("[", signature(x = "Matrix", i = "matrix", j = "missing",  setReplaceMethod("[", signature(x = "Matrix", i = "matrix", j = "missing",
392                                  value = "replValue"),                                  value = "replValue"),
393            .M.repl.i.2col)            .M.repl.i.2col)
# Line 396  Line 395 
395    
396  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
397                                  value = "Matrix"),                                  value = "Matrix"),
398                     function (x, i, j, value) {
399    ### *TEMPORARY* diagnostic output:
400    ##                  cat("<Matrix1>[i,j] <- <Matrix1>:\n<Matrix1> = x :")
401    ##                  str(x)
402    ##                  cat("<Matrix2> = value :")
403    ##                  str(value)
404    ##                  cat("i :"); if(!missing(i)) str(i) else cat("<missing>\n")
405    ##                  cat("j :"); if(!missing(j)) str(j) else cat("<missing>\n")
406    
407                         callGeneric(x=x, i=i, j=j, value = as.vector(value))
408                     })
409    setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
410                                    value = "Matrix"),
411                   function (x, i, j, value)                   function (x, i, j, value)
412                   callGeneric(x=x, i=i, j=j, value = as.vector(value)))                   callGeneric(x=x, i=i, j=j, value = as.vector(value)))
413    
414  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
415                                  value = "matrix"),                                  value = "matrix"),
416                   function (x, i, j, value)                   function (x, i, j, value)
# Line 412  Line 425 
425                                 class(value),class(x)))                                 class(value),class(x)))
426                else stop("not-yet-implemented 'Matrix[<-' method")                else stop("not-yet-implemented 'Matrix[<-' method")
427            })            })
   
   
 ## 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.1738

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