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 1747, Mon Jan 29 20:17:33 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    setMethod("!", "Matrix", function(e1) !as(e1, "lMatrix"))
89    
90    
91    
92  Matrix <-  Matrix <-
93      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
94                sparse = NULL, forceCheck = FALSE)                sparse = NULL, forceCheck = FALSE)
# Line 115  Line 129 
129              dimnames(data) <- dimnames              dimnames(data) <- dimnames
130          }          }
131          doDN <- FALSE          doDN <- FALSE
132      }      } else if(!missing(nrow) || !missing(ncol))
133            warning("'nrow', 'ncol', etc, are disregarded for matrix 'data'")
134    
135      ## 'data' is now a "matrix" or "Matrix"      ## 'data' is now a "matrix" or "Matrix"
136      if (doDN && !is.null(dimnames))      if (doDN && !is.null(dimnames))
137          dimnames(data) <- dimnames          dimnames(data) <- dimnames
# Line 194  Line 210 
210  ## when no sub-class method is found, bail out  ## when no sub-class method is found, bail out
211  setMethod("solve", signature(a = "Matrix", b = "matrix"),  setMethod("solve", signature(a = "Matrix", b = "matrix"),
212            function(a, b, ...) .bail.out.2("solve", class(a), "matrix"))            function(a, b, ...) .bail.out.2("solve", class(a), "matrix"))
213    setMethod("solve", signature(a = "Matrix", b = "Matrix"),
214              function(a, b, ...) .bail.out.2("solve", class(a), class(b)))
215    
216  ## bail-out methods in order to get better error messages  ## bail-out methods in order to get better error messages
217  setMethod("%*%", signature(x = "Matrix", y = "Matrix"),  setMethod("%*%", signature(x = "Matrix", y = "Matrix"),
# Line 239  Line 257 
257            function(x) .bail.out.1(.Generic, class(x)))            function(x) .bail.out.1(.Generic, class(x)))
258    
259  ## 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)))  
   
260    
261    ##-> see ./Ops.R
262    
263  ### --------------------------------------------------------------------------  ### --------------------------------------------------------------------------
264  ###  ###
# Line 313  Line 306 
306          stop("not-yet-implemented 'Matrix' subsetting") ## FIXME          stop("not-yet-implemented 'Matrix' subsetting") ## FIXME
307    
308      } else stop("nargs() = ", nA,      } else stop("nargs() = ", nA,
309                  " should never happen; please report.")                  ".  Extraneous illegal arguments inside '[ .. ]' ?")
310  }  }
311  setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",  setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",
312                           drop = "ANY"),                           drop = "ANY"),
# Line 343  Line 336 
336          ## potentially inefficient -- FIXME --          ## potentially inefficient -- FIXME --
337          unlist(lapply(seq_len(m), function(j) x[i1[j], i2[j]]))          unlist(lapply(seq_len(m), function(j) x[i1[j], i2[j]]))
338    
339      } else stop("nargs() = ", nA, " should never happen; please report.")      } else stop("nargs() = ", nA,
340                    ".  Extraneous illegal arguments inside '[ .. ]' ?")
341  }  }
342  setMethod("[", signature(x = "Matrix", i = "matrix", j = "missing"),# drop="ANY"  setMethod("[", signature(x = "Matrix", i = "matrix", j = "missing"),# drop="ANY"
343            .M.sub.i.2col)            .M.sub.i.2col)
# Line 356  Line 350 
350                                  value = "ANY"),## double/logical/...                                  value = "ANY"),## double/logical/...
351            function (x, value) {            function (x, value) {
352                ## Fails for 'nMatrix' ... FIXME : make sure have method there                ## Fails for 'nMatrix' ... FIXME : make sure have method there
353                x@x <- value                x@x <- rep(value, length = length(x@x))
354                validObject(x)# check if type and lengths above match                validObject(x)# check if type and lengths above match
355                x                x
356            })            })
357    
358  ## A[ ij ] <- value,  where ij is (i,j) 2-column matrix :  ## A[ ij ] <- value,  where ij is (i,j) 2-column matrix :
359    ## ----------------   The cheap general method --- FIXME: provide special ones
360  .M.repl.i.2col <- function (x, i, j, value)  .M.repl.i.2col <- function (x, i, j, value)
361  {  {
362      nA <- nargs()      nA <- nargs()
# Line 369  Line 364 
364          if(!is.integer(nc <- ncol(i)))          if(!is.integer(nc <- ncol(i)))
365              stop("'i' has no integer column number",              stop("'i' has no integer column number",
366                   " should never happen; please report")                   " should never happen; please report")
367            else if(!is.numeric(i) || nc != 2)
368                stop("such indexing must be by logical or 2-column numeric matrix")
369          if(is.logical(i)) {          if(is.logical(i)) {
370                message(".M.repl.i.2col(): drop 'matrix' case ...")
371              i <- c(i) # drop "matrix"              i <- c(i) # drop "matrix"
372              return( callNextMethod() )              return( callNextMethod() )
373          } else if(!is.numeric(i) || nc != 2)          }
374              stop("such indexing must be by logical or 2-column numeric matrix")          if(!is.integer(i)) storage.mode(i) <- "integer"
375            if(any(i < 0))
376                stop("negative values are not allowed in a matrix subscript")
377            if(any(is.na(i)))
378                stop("NAs are not allowed in subscripted assignments")
379            if(any(i0 <- (i == 0))) # remove them
380                i <- i[ - which(i0, arr.ind = TRUE)[,"row"], ]
381            ## now have integer i >= 1
382          m <- nrow(i)          m <- nrow(i)
383          mod.x <- .type.kind[.M.kind(x)]          ## mod.x <- .type.kind[.M.kind(x)]
384          if(length(value) > 0 && m %% length(value) != 0)          if(length(value) > 0 && m %% length(value) != 0)
385              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")
386          ## recycle:          ## recycle:
# Line 385  Line 390 
390          ## inefficient -- FIXME -- (also loses "symmetry" unnecessarily)          ## inefficient -- FIXME -- (also loses "symmetry" unnecessarily)
391          for(k in seq_len(m))          for(k in seq_len(m))
392              x[i1[k], i2[k]] <- value[k]              x[i1[k], i2[k]] <- value[k]
         x  
393    
394      } else stop("nargs() = ", nA, " should never happen; please report.")          x
395        } else stop("nargs() = ", nA,
396                    ".  Extraneous illegal arguments inside '[ .. ]' ?")
397  }  }
398    
399  setReplaceMethod("[", signature(x = "Matrix", i = "matrix", j = "missing",  setReplaceMethod("[", signature(x = "Matrix", i = "matrix", j = "missing",
400                                  value = "replValue"),                                  value = "replValue"),
401            .M.repl.i.2col)            .M.repl.i.2col)
# Line 396  Line 403 
403    
404  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
405                                  value = "Matrix"),                                  value = "Matrix"),
406                     function (x, i, j, value) {
407    ### *TEMPORARY* diagnostic output:
408    ##                  cat("<Matrix1>[i,j] <- <Matrix1>:\n<Matrix1> = x :")
409    ##                  str(x)
410    ##                  cat("<Matrix2> = value :")
411    ##                  str(value)
412    ##                  cat("i :"); if(!missing(i)) str(i) else cat("<missing>\n")
413    ##                  cat("j :"); if(!missing(j)) str(j) else cat("<missing>\n")
414    
415                         callGeneric(x=x, i=i, j=j, value = as.vector(value))
416                     })
417    setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
418                                    value = "Matrix"),
419                   function (x, i, j, value)                   function (x, i, j, value)
420                   callGeneric(x=x, i=i, j=j, value = as.vector(value)))                   callGeneric(x=x, i=i, j=j, value = as.vector(value)))
421    
422  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
423                                  value = "matrix"),                                  value = "matrix"),
424                   function (x, i, j, value)                   function (x, i, j, value)
# Line 412  Line 433 
433                                 class(value),class(x)))                                 class(value),class(x)))
434                else stop("not-yet-implemented 'Matrix[<-' method")                else stop("not-yet-implemented 'Matrix[<-' method")
435            })            })
   
   
 ## 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.1747

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