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 2043, Sat Aug 11 03:18:26 2007 UTC revision 2110, Sat Jan 26 20:59:26 2008 UTC
# Line 44  Line 44 
44  setMethod("as.logical", signature(x = "Matrix"),  setMethod("as.logical", signature(x = "Matrix"),
45            function(x, ...) as.logical(as.vector(x)))            function(x, ...) as.logical(as.vector(x)))
46    
47    setMethod("cov2cor", signature(V = "Matrix"),
48              function(V) as(cov2cor(as(V, "matrix")), "dpoMatrix"))
49    
50  ## "base" has an isSymmetric() S3-generic since R 2.3.0  ## "base" has an isSymmetric() S3-generic since R 2.3.0
51  setMethod("isSymmetric", signature(object = "symmetricMatrix"),  setMethod("isSymmetric", signature(object = "symmetricMatrix"),
# Line 136  Line 138 
138                          Dim = as.integer(c(nrow,ncol)),                          Dim = as.integer(c(nrow,ncol)),
139                          Dimnames = if(is.null(dimnames)) list(NULL,NULL)                          Dimnames = if(is.null(dimnames)) list(NULL,NULL)
140                          else dimnames)                          else dimnames)
141          } else { ## normal case          } else { ## normal case - using .Internal() to avoid more copying
142                if(getRversion() >= "2.7.0")
143                    data <- .Internal(matrix(data, nrow, ncol, byrow, dimnames))
144                else {
145              data <- .Internal(matrix(data, nrow, ncol, byrow))              data <- .Internal(matrix(data, nrow, ncol, byrow))
146                    dimnames(data) <- dimnames
147                }
148              if(is.null(sparse))              if(is.null(sparse))
149                  sparse <- sparseDefault(data)                  sparse <- sparseDefault(data)
             dimnames(data) <- dimnames  
150          }          }
151          doDN <- FALSE          doDN <- FALSE
152      } else if(!missing(nrow) || !missing(ncol)|| !missing(byrow))      } else if(!missing(nrow) || !missing(ncol)|| !missing(byrow))
# Line 366  Line 372 
372  ## "x[]":  ## "x[]":
373  setMethod("[", signature(x = "Matrix",  setMethod("[", signature(x = "Matrix",
374                           i = "missing", j = "missing", drop = "ANY"),                           i = "missing", j = "missing", drop = "ANY"),
375            function (x, i, j, drop) x)            function (x, i, j, ..., drop) x)
376    
377  ## missing 'drop' --> 'drop = TRUE'  ## missing 'drop' --> 'drop = TRUE'
378  ##                     -----------  ##                     -----------
379  ## select rows  ## select rows
380  setMethod("[", signature(x = "Matrix", i = "index", j = "missing",  setMethod("[", signature(x = "Matrix", i = "index", j = "missing",
381                           drop = "missing"),                           drop = "missing"),
382            function(x,i,j, drop) {            function(x,i,j, ..., drop) {
383                if(nargs() == 1) { ## e.g. M[0] , M[TRUE],  M[1:2]                if(nargs() == 2) { ## e.g. M[0] , M[TRUE],  M[1:2]
384                    if(any(i)) as.vector(x)[i] else as.vector(x[1,1])[FALSE]                    if(any(i) || prod(dim(x)) == 0)
385                } else callGeneric(x, i=i, drop= TRUE)})                        as.vector(x)[i]
386                      else ## save memory
387                          as.vector(x[1,1])[FALSE]
388                  } else {
389                      callGeneric(x, i=i, , drop=TRUE)
390                      ##                  ^^
391                  }
392              })
393    
394  ## select columns  ## select columns
395  setMethod("[", signature(x = "Matrix", i = "missing", j = "index",  setMethod("[", signature(x = "Matrix", i = "missing", j = "index",
396                           drop = "missing"),                           drop = "missing"),
397            function(x,i,j, drop) callGeneric(x, j=j, drop= TRUE))            function(x,i,j, ..., drop) callGeneric(x, j=j, drop= TRUE))
398  setMethod("[", signature(x = "Matrix", i = "index", j = "index",  setMethod("[", signature(x = "Matrix", i = "index", j = "index",
399                           drop = "missing"),                           drop = "missing"),
400            function(x,i,j, drop) callGeneric(x, i=i, j=j, drop= TRUE))            function(x,i,j, ..., drop) callGeneric(x, i=i, j=j, drop= TRUE))
401    
402  ## bail out if any of (i,j,drop) is "non-sense"  ## bail out if any of (i,j,drop) is "non-sense"
403  setMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY", drop = "ANY"),  setMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY", drop = "ANY"),
404            function(x,i,j, drop)            function(x,i,j, ..., drop)
405            stop("invalid or not-yet-implemented 'Matrix' subsetting"))            stop("invalid or not-yet-implemented 'Matrix' subsetting"))
406    
407  ## logical indexing, such as M[ M >= 7 ] *BUT* also M[ M[,1] >= 3,],  ## logical indexing, such as M[ M >= 7 ] *BUT* also M[ M[,1] >= 3,],
408  ## The following is *both* for    M [ <logical>   ]  ## The following is *both* for    M [ <logical>   ]
409  ##                 and also for   M [ <logical> , ]  ##                 and also for   M [ <logical> , ]
410  .M.sub.i.logical <- function (x, i, j, drop)  .M.sub.i.logical <- function (x, i, j, ..., drop)
411  {  {
412      nA <- nargs()      nA <- nargs()
413      if(nA == 2) { ##  M [ M >= 7 ]      if(nA == 2) { ##  M [ M >= 7 ]
# Line 405  Line 418 
418          stop("not-yet-implemented 'Matrix' subsetting") ## FIXME          stop("not-yet-implemented 'Matrix' subsetting") ## FIXME
419    
420      } else stop("nargs() = ", nA,      } else stop("nargs() = ", nA,
421                  ".  Extraneous illegal arguments inside '[ .. ]' ?")                  ".  Extraneous illegal arguments inside '[ .. ]' (i.logical)?")
422  }  }
423  setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",  setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",
424                           drop = "ANY"),                           drop = "ANY"),
# Line 415  Line 428 
428            .M.sub.i.logical)            .M.sub.i.logical)
429    
430    
431  ## A[ ij ]  where ij is (i,j) 2-column matrix :  ## A[ ij ]  where ij is (i,j) 2-column matrix -- but also when that is logical mat!
432  .M.sub.i.2col <- function (x, i, j, drop)  .M.sub.i.2col <- function (x, i, j, ..., drop)
433  {  {
434      nA <- nargs()      nA <- nargs()
435      if(nA == 2) { ##  M [ cbind(ii,jj) ]      if(nA == 2) { ##  M [ cbind(ii,jj) ] or M [ <logical matrix> ]
436          if(!is.integer(nc <- ncol(i)))          if(!is.integer(nc <- ncol(i)))
437              stop("'i' has no integer column number",              stop(".M.sub.i.2col(): 'i' has no integer column number;\n",
438                   " should never happen; please report")                   " should never happen; please report")
439          if(is.logical(i))          if(is.logical(i))
440              return(.M.sub.i.logical(x,i,j,drop))              return(.M.sub.i.logical(x, i=i)) # call with 2 args!
441          else if(!is.numeric(i) || nc != 2)          else if(!is.numeric(i) || nc != 2)
442              stop("such indexing must be by logical or 2-column numeric matrix")              stop("such indexing must be by logical or 2-column numeric matrix")
443          m <- nrow(i)          m <- nrow(i)
# Line 436  Line 449 
449          unlist(lapply(seq_len(m), function(j) x[i1[j], i2[j]]))          unlist(lapply(seq_len(m), function(j) x[i1[j], i2[j]]))
450    
451      } else stop("nargs() = ", nA,      } else stop("nargs() = ", nA,
452                  ".  Extraneous illegal arguments inside '[ .. ]' ?")                  ".  Extraneous illegal arguments inside '[ .. ]' (i.2col)?")
453  }  }
454  setMethod("[", signature(x = "Matrix", i = "matrix", j = "missing"),# drop="ANY"  setMethod("[", signature(x = "Matrix", i = "matrix", j = "missing"),# drop="ANY"
455            .M.sub.i.2col)            .M.sub.i.2col)
# Line 455  Line 468 
468            })            })
469    
470  ## A[ ij ] <- value,  where ij is (i,j) 2-column matrix :  ## A[ ij ] <- value,  where ij is (i,j) 2-column matrix :
471  ## ----------------   The cheap general method --- FIXME: provide special ones  ## ----------------
472  .M.repl.i.2col <- function (x, i, j, value)  ## The cheap general method --- FIXME: provide special ones; done for Tsparse..
473    ## NOTE:  need '...' below such that setMethod() does
474    ##        not use .local() such that nargs() will work correctly:
475    .M.repl.i.2col <- function (x, i, j, ..., value)
476  {  {
477      nA <- nargs()      nA <- nargs()
478      if(nA == 3) { ##  M [ cbind(ii,jj) ] <- value      if(nA == 3) { ##  M [ cbind(ii,jj) ] <- value  or M [ Lmat ] <- value
479          if(!is.integer(nc <- ncol(i)))          if(!is.integer(nc <- ncol(i)))
480              stop("'i' has no integer column number",              stop(".M.repl.i.2col(): 'i' has no integer column number;\n",
481                   " should never happen; please report")                   " should never happen; please report")
482          else if(!is.numeric(i) || nc != 2)          else if(!is.numeric(i) || nc != 2)
483              stop("such indexing must be by logical or 2-column numeric matrix")              stop("such indexing must be by logical or 2-column numeric matrix")
484          if(is.logical(i)) {          if(is.logical(i)) {
485              message(".M.repl.i.2col(): drop 'matrix' case ...")              message(".M.repl.i.2col(): drop 'matrix' case ...")
486              i <- c(i) # drop "matrix"              ## c(i) : drop "matrix" to logical vector
487              return( callNextMethod() )              return( callGeneric(x, i=c(i), value=value) )
488          }          }
489          if(!is.integer(i)) storage.mode(i) <- "integer"          if(!is.integer(i)) storage.mode(i) <- "integer"
490          if(any(i < 0))          if(any(i < 0))
# Line 500  Line 516 
516            .M.repl.i.2col)            .M.repl.i.2col)
517    
518    
519  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "ANY",
520                                  value = "Matrix"),                                  value = "Matrix"),
521                   function (x, i, j, value) {                   function (x, i, j, ..., value)
522  ### *TEMPORARY* diagnostic output:                   callGeneric(x=x, , j=j, value = as.vector(value)))
523  ##                  cat("<Matrix1>[i,j] <- <Matrix1>:\n<Matrix1> = x :")  
524  ##                  str(x)  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "missing",
525  ##                  cat("<Matrix2> = value :")                                  value = "Matrix"),
526  ##                  str(value)                   function (x, i, j, ..., value)
527  ##                  cat("i :"); if(!missing(i)) str(i) else cat("<missing>\n")                   callGeneric(x=x, i=i, , value = as.vector(value)))
 ##                  cat("j :"); if(!missing(j)) str(j) else cat("<missing>\n")  
528    
                      callGeneric(x=x, i=i, j=j, value = as.vector(value))  
                  })  
529  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
530                                  value = "Matrix"),                                  value = "Matrix"),
531                   function (x, i, j, value)                   function (x, i, j, ..., value)
532                   callGeneric(x=x, i=i, j=j, value = as.vector(value)))                   callGeneric(x=x, i=i, j=j, value = as.vector(value)))
533    
534    setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "ANY",
535                                    value = "matrix"),
536                     function (x, i, j, ..., value)
537                     callGeneric(x=x, , j=j, value = c(value)))
538    
539    setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "missing",
540                                    value = "matrix"),
541                     function (x, i, j, ..., value)
542                     callGeneric(x=x, i=i, , value = c(value)))
543    
544  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
545                                  value = "matrix"),                                  value = "matrix"),
546                   function (x, i, j, value)                   function (x, i, j, value)

Legend:
Removed from v.2043  
changed lines
  Added in v.2110

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