SCM Repository

[matrix] Diff of /pkg/R/Matrix.R
 [matrix] / pkg / R / Matrix.R

Diff of /pkg/R/Matrix.R

revision 1660, Thu Nov 2 11:05:35 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 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  ## when no sub-class method is found, bail out
# Line 303  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 319  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 336  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.1660 changed lines Added in v.1673