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 1751, Tue Jan 30 17:41:02 2007 UTC
# Line 27  Line 27 
27  setMethod("head", signature(x = "Matrix"), utils::head.matrix)  setMethod("head", signature(x = "Matrix"), utils::head.matrix)
28  setMethod("tail", signature(x = "Matrix"), utils::tail.matrix)  setMethod("tail", signature(x = "Matrix"), utils::tail.matrix)
29    
30    setMethod("drop", signature(x = "Matrix"),
31              function(x) if(all(dim(x) != 1)) x else drop(as(x, "matrix")))
32    
33  ## slow "fall back" method {subclasses should have faster ones}:  ## slow "fall back" method {subclasses should have faster ones}:
34  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),  setMethod("as.vector", signature(x = "Matrix", mode = "missing"),
35            function(x) as.vector(as(x, "matrix")))            function(x) as.vector(as(x, "matrix")))
# Line 56  Line 59 
59    
60  setMethod("dim", signature(x = "Matrix"),  setMethod("dim", signature(x = "Matrix"),
61            function(x) x@Dim, valueClass = "integer")            function(x) x@Dim, valueClass = "integer")
62    
63    setMethod("length", "Matrix", function(x) prod(dim(x)))
64    
65  setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)  setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)
66    
67    
68  ## not exported but used more than once for "dimnames<-" method :  ## not exported but used more than once for "dimnames<-" method :
69  ## -- or do only once for all "Matrix" classes ??  ## -- or do only once for all "Matrix" classes ??
70  dimnamesGets <- function (x, value) {  dimnamesGets <- function (x, value) {
# Line 75  Line 83 
83  setMethod("unname", signature("Matrix", force="missing"),  setMethod("unname", signature("Matrix", force="missing"),
84            function(obj) { obj@Dimnames <- list(NULL,NULL); obj})            function(obj) { obj@Dimnames <- list(NULL,NULL); obj})
85    
86    setMethod("all", signature(x = "Matrix"),
87              function(x, ..., na.rm) { x <- as(x, "lMatrix"); callGeneric()})
88    setMethod("any", signature(x = "Matrix"),
89              function(x, ..., na.rm) { x <- as(x, "lMatrix"); callGeneric()})
90    
91    setMethod("!", "Matrix", function(e1) !as(e1, "lMatrix"))
92    
93    
94    
95  Matrix <-  Matrix <-
96      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
97                sparse = NULL, forceCheck = FALSE)                sparse = NULL, forceCheck = FALSE)
# Line 88  Line 105 
105    
106      doDN <- TRUE      doDN <- TRUE
107      if (i.M) {      if (i.M) {
108            if(!missing(nrow) || !missing(ncol)|| !missing(byrow))
109                warning("'nrow', 'ncol', etc, are disregarded when 'data' is \"Matrix\" already")
110          sM <- is(data,"sparseMatrix")          sM <- is(data,"sparseMatrix")
111          if(!forceCheck && ((sparse && sM) || (!sparse && !sM)))          if(!forceCheck && ((sparse && sM) || (!sparse && !sM)))
112              return(data)              return(data)
# Line 115  Line 134 
134              dimnames(data) <- dimnames              dimnames(data) <- dimnames
135          }          }
136          doDN <- FALSE          doDN <- FALSE
137      }      } else if(!missing(nrow) || !missing(ncol)|| !missing(byrow))
138            warning("'nrow', 'ncol', etc, are disregarded for matrix 'data'")
139    
140      ## 'data' is now a "matrix" or "Matrix"      ## 'data' is now a "matrix" or "Matrix"
141      if (doDN && !is.null(dimnames))      if (doDN && !is.null(dimnames))
142          dimnames(data) <- dimnames          dimnames(data) <- dimnames
# Line 194  Line 215 
215  ## when no sub-class method is found, bail out  ## when no sub-class method is found, bail out
216  setMethod("solve", signature(a = "Matrix", b = "matrix"),  setMethod("solve", signature(a = "Matrix", b = "matrix"),
217            function(a, b, ...) .bail.out.2("solve", class(a), "matrix"))            function(a, b, ...) .bail.out.2("solve", class(a), "matrix"))
218    setMethod("solve", signature(a = "Matrix", b = "Matrix"),
219              function(a, b, ...) .bail.out.2("solve", class(a), class(b)))
220    
221  ## bail-out methods in order to get better error messages  ## bail-out methods in order to get better error messages
222  setMethod("%*%", signature(x = "Matrix", y = "Matrix"),  setMethod("%*%", signature(x = "Matrix", y = "Matrix"),
# Line 239  Line 262 
262            function(x) .bail.out.1(.Generic, class(x)))            function(x) .bail.out.1(.Generic, class(x)))
263    
264  ## 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)))  
   
265    
266    ##-> see ./Ops.R
267    
268  ### --------------------------------------------------------------------------  ### --------------------------------------------------------------------------
269  ###  ###
# Line 313  Line 311 
311          stop("not-yet-implemented 'Matrix' subsetting") ## FIXME          stop("not-yet-implemented 'Matrix' subsetting") ## FIXME
312    
313      } else stop("nargs() = ", nA,      } else stop("nargs() = ", nA,
314                  " should never happen; please report.")                  ".  Extraneous illegal arguments inside '[ .. ]' ?")
315  }  }
316  setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",  setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",
317                           drop = "ANY"),                           drop = "ANY"),
# Line 343  Line 341 
341          ## potentially inefficient -- FIXME --          ## potentially inefficient -- FIXME --
342          unlist(lapply(seq_len(m), function(j) x[i1[j], i2[j]]))          unlist(lapply(seq_len(m), function(j) x[i1[j], i2[j]]))
343    
344      } else stop("nargs() = ", nA, " should never happen; please report.")      } else stop("nargs() = ", nA,
345                    ".  Extraneous illegal arguments inside '[ .. ]' ?")
346  }  }
347  setMethod("[", signature(x = "Matrix", i = "matrix", j = "missing"),# drop="ANY"  setMethod("[", signature(x = "Matrix", i = "matrix", j = "missing"),# drop="ANY"
348            .M.sub.i.2col)            .M.sub.i.2col)
# Line 356  Line 355 
355                                  value = "ANY"),## double/logical/...                                  value = "ANY"),## double/logical/...
356            function (x, value) {            function (x, value) {
357                ## Fails for 'nMatrix' ... FIXME : make sure have method there                ## Fails for 'nMatrix' ... FIXME : make sure have method there
358                x@x <- value                x@x <- rep(value, length = length(x@x))
359                validObject(x)# check if type and lengths above match                validObject(x)# check if type and lengths above match
360                x                x
361            })            })
362    
363  ## A[ ij ] <- value,  where ij is (i,j) 2-column matrix :  ## A[ ij ] <- value,  where ij is (i,j) 2-column matrix :
364    ## ----------------   The cheap general method --- FIXME: provide special ones
365  .M.repl.i.2col <- function (x, i, j, value)  .M.repl.i.2col <- function (x, i, j, value)
366  {  {
367      nA <- nargs()      nA <- nargs()
# Line 369  Line 369 
369          if(!is.integer(nc <- ncol(i)))          if(!is.integer(nc <- ncol(i)))
370              stop("'i' has no integer column number",              stop("'i' has no integer column number",
371                   " should never happen; please report")                   " should never happen; please report")
372            else if(!is.numeric(i) || nc != 2)
373                stop("such indexing must be by logical or 2-column numeric matrix")
374          if(is.logical(i)) {          if(is.logical(i)) {
375                message(".M.repl.i.2col(): drop 'matrix' case ...")
376              i <- c(i) # drop "matrix"              i <- c(i) # drop "matrix"
377              return( callNextMethod() )              return( callNextMethod() )
378          } else if(!is.numeric(i) || nc != 2)          }
379              stop("such indexing must be by logical or 2-column numeric matrix")          if(!is.integer(i)) storage.mode(i) <- "integer"
380            if(any(i < 0))
381                stop("negative values are not allowed in a matrix subscript")
382            if(any(is.na(i)))
383                stop("NAs are not allowed in subscripted assignments")
384            if(any(i0 <- (i == 0))) # remove them
385                i <- i[ - which(i0, arr.ind = TRUE)[,"row"], ]
386            ## now have integer i >= 1
387          m <- nrow(i)          m <- nrow(i)
388          mod.x <- .type.kind[.M.kind(x)]          ## mod.x <- .type.kind[.M.kind(x)]
389          if(length(value) > 0 && m %% length(value) != 0)          if(length(value) > 0 && m %% length(value) != 0)
390              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")
391          ## recycle:          ## recycle:
# Line 385  Line 395 
395          ## inefficient -- FIXME -- (also loses "symmetry" unnecessarily)          ## inefficient -- FIXME -- (also loses "symmetry" unnecessarily)
396          for(k in seq_len(m))          for(k in seq_len(m))
397              x[i1[k], i2[k]] <- value[k]              x[i1[k], i2[k]] <- value[k]
         x  
398    
399      } else stop("nargs() = ", nA, " should never happen; please report.")          x
400        } else stop("nargs() = ", nA,
401                    ".  Extraneous illegal arguments inside '[ .. ]' ?")
402  }  }
403    
404  setReplaceMethod("[", signature(x = "Matrix", i = "matrix", j = "missing",  setReplaceMethod("[", signature(x = "Matrix", i = "matrix", j = "missing",
405                                  value = "replValue"),                                  value = "replValue"),
406            .M.repl.i.2col)            .M.repl.i.2col)
# Line 396  Line 408 
408    
409  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
410                                  value = "Matrix"),                                  value = "Matrix"),
411                     function (x, i, j, value) {
412    ### *TEMPORARY* diagnostic output:
413    ##                  cat("<Matrix1>[i,j] <- <Matrix1>:\n<Matrix1> = x :")
414    ##                  str(x)
415    ##                  cat("<Matrix2> = value :")
416    ##                  str(value)
417    ##                  cat("i :"); if(!missing(i)) str(i) else cat("<missing>\n")
418    ##                  cat("j :"); if(!missing(j)) str(j) else cat("<missing>\n")
419    
420                         callGeneric(x=x, i=i, j=j, value = as.vector(value))
421                     })
422    setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
423                                    value = "Matrix"),
424                   function (x, i, j, value)                   function (x, i, j, value)
425                   callGeneric(x=x, i=i, j=j, value = as.vector(value)))                   callGeneric(x=x, i=i, j=j, value = as.vector(value)))
426    
427  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
428                                  value = "matrix"),                                  value = "matrix"),
429                   function (x, i, j, value)                   function (x, i, j, value)
# Line 412  Line 438 
438                                 class(value),class(x)))                                 class(value),class(x)))
439                else stop("not-yet-implemented 'Matrix[<-' method")                else stop("not-yet-implemented 'Matrix[<-' method")
440            })            })
   
   
 ## 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.1751

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