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 1654, Fri Oct 27 16:58:15 2006 UTC revision 1673, Mon Nov 6 20:54:26 2006 UTC
# Line 79  Line 79 
79      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,      function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
80                sparse = NULL, forceCheck = FALSE)                sparse = NULL, forceCheck = FALSE)
81  {  {
82      sparseDefault <- function(m)      sparseDefault <- function(m) prod(dim(m)) > 2*sum(isN0(as(m, "matrix")))
         prod(dim(m)) > 2*sum(is.na(m <- as(m, "matrix")) | m != 0)  
83    
84      i.M <- is(data, "Matrix")      i.M <- is(data, "Matrix")
85    
# Line 172  Line 171 
171            function(x, y) callGeneric(x, as.matrix(y)))            function(x, y) callGeneric(x, as.matrix(y)))
172    
173  setMethod("%*%", signature(x = "numeric", y = "Matrix"),  setMethod("%*%", signature(x = "numeric", y = "Matrix"),
174            function(x, y) callGeneric(rbind(x), y))            function(x, y) callGeneric(matrix(x, nrow = 1, byrow=TRUE), y))
175    
176  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),  setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
177            function(x, y = NULL) callGeneric(x, as.matrix(y)))            function(x, y = NULL) callGeneric(x, as.matrix(y)))
# Line 186  Line 185 
185  setMethod("tcrossprod", signature(x = "numeric", y = "Matrix"),  setMethod("tcrossprod", signature(x = "numeric", y = "Matrix"),
186            function(x, y = NULL)  callGeneric(as.matrix(x), y))            function(x, y = NULL)  callGeneric(as.matrix(x), y))
187    
188    ## maybe not optimal
189    setMethod("solve", signature(a = "Matrix", b = "missing"),
190              function(a, b, ...) solve(a, Diagonal(nrow(a))))
191    
192  setMethod("solve", signature(a = "Matrix", b = "numeric"),  setMethod("solve", signature(a = "Matrix", b = "numeric"),
193            function(a, b, ...) callGeneric(a, as.matrix(b)))            function(a, b, ...) callGeneric(a, as.matrix(b)))
194    ## when no sub-class method is found, bail out
195    setMethod("solve", signature(a = "Matrix", b = "matrix"),
196              function(a, b, ...) .bail.out.2("solve", class(a), "matrix"))
197    
198  ## bail-out methods in order to get better error messages  ## bail-out methods in order to get better error messages
199  setMethod("%*%", signature(x = "Matrix", y = "Matrix"),  setMethod("%*%", signature(x = "Matrix", y = "Matrix"),
# Line 241  Line 247 
247                0-e1                0-e1
248            })            })
249    
250    ## old-style matrices are made into new ones
251    setMethod("Ops", signature(e1 = "Matrix", e2 = "matrix"),
252              function(e1, e2) callGeneric(e1, Matrix(e2)))
253    ##          callGeneric(e1, Matrix(e2, sparse=is(e1,"sparseMatrix"))))
254    setMethod("Ops", signature(e1 = "matrix", e2 = "Matrix"),
255              function(e1, e2) callGeneric(Matrix(e1), e2))
256    
257  ## bail-outs -- on highest possible level, hence "Ops", not "Compare"/"Arith" :  ## bail-outs -- on highest possible level, hence "Ops", not "Compare"/"Arith" :
258  setMethod("Ops", signature(e1 = "Matrix", e2 = "Matrix"),  setMethod("Ops", signature(e1 = "Matrix", e2 = "Matrix"),
259            function(e1, e2) {            function(e1, e2) {
# Line 293  Line 306 
306  {  {
307      nA <- nargs()      nA <- nargs()
308      if(nA == 2) { ##  M [ M >= 7 ]      if(nA == 2) { ##  M [ M >= 7 ]
309            ## FIXME: when both 'x' and 'i' are sparse, this can be very inefficient
310          as(x, geClass(x))@x[as.vector(i)]          as(x, geClass(x))@x[as.vector(i)]
311          ## -> error when lengths don't match          ## -> error when lengths don't match
312      } else if(nA == 3) { ##  M [ M[,1, drop=FALSE] >= 7, ]      } else if(nA == 3) { ##  M [ M[,1, drop=FALSE] >= 7, ]
# Line 309  Line 323 
323            .M.sub.i.logical)            .M.sub.i.logical)
324    
325    
326  ## "FIXME:"  ## A[ ij ]  where ij is (i,j) 2-column matrix :
327  ## ------ get at  A[ ij ]  where ij is (i,j) 2-column matrix?  .M.sub.i.2col <- function (x, i, j, drop)
328    {
329        nA <- nargs()
330        if(nA == 2) { ##  M [ cbind(ii,jj) ]
331            if(!is.integer(nc <- ncol(i)))
332                stop("'i' has no integer column number",
333                     " should never happen; please report")
334            if(is.logical(i))
335                return(.M.sub.i.logical(x,i,j,drop))
336            else if(!is.numeric(i) || nc != 2)
337                stop("such indexing must be by logical or 2-column numeric matrix")
338            m <- nrow(i)
339            if(m == 0) return(vector(mode = .type.kind[.M.kind(x)]))
340            ## else
341            i1 <- i[,1]
342            i2 <- i[,2]
343            ## potentially inefficient -- FIXME --
344            unlist(lapply(seq_len(m), function(j) x[i1[j], i2[j]]))
345    
346        } else stop("nargs() = ", nA, " should never happen; please report.")
347    }
348    setMethod("[", signature(x = "Matrix", i = "matrix", j = "missing"),# drop="ANY"
349              .M.sub.i.2col)
350    
351    
352  ### "[<-" : -----------------  ### "[<-" : -----------------
# Line 326  Line 361 
361                x                x
362            })            })
363    
364    ## A[ ij ] <- value,  where ij is (i,j) 2-column matrix :
365    .M.repl.i.2col <- function (x, i, j, value)
366    {
367        nA <- nargs()
368        if(nA == 3) { ##  M [ cbind(ii,jj) ] <- value
369            if(!is.integer(nc <- ncol(i)))
370                stop("'i' has no integer column number",
371                     " should never happen; please report")
372            if(is.logical(i)) {
373                i <- c(i) # drop "matrix"
374                return( callNextMethod() )
375            } else if(!is.numeric(i) || nc != 2)
376                stop("such indexing must be by logical or 2-column numeric matrix")
377            m <- nrow(i)
378            mod.x <- .type.kind[.M.kind(x)]
379            if(length(value) > 0 && m %% length(value) != 0)
380                warning("number of items to replace is not a multiple of replacement length")
381            ## recycle:
382            value <- rep(value, length = m)
383            i1 <- i[,1]
384            i2 <- i[,2]
385            ## inefficient -- FIXME -- (also loses "symmetry" unnecessarily)
386            for(k in seq_len(m))
387                x[i1[k], i2[k]] <- value[k]
388            x
389    
390        } else stop("nargs() = ", nA, " should never happen; please report.")
391    }
392    setReplaceMethod("[", signature(x = "Matrix", i = "matrix", j = "missing",
393                                    value = "replValue"),
394              .M.repl.i.2col)
395    
396    
397  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
398                                  value = "Matrix"),                                  value = "Matrix"),
399                   function (x, i, j, value)                   function (x, i, j, value)

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

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