# SCM Repository

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

# Diff of /pkg/R/Matrix.R

revision 1165, Sat Jan 14 23:41:55 2006 UTC revision 1225, Mon Mar 13 14:06:17 2006 UTC
# Line 17  Line 17
17            function(x) as.vector(as(x, "matrix")))            function(x) as.vector(as(x, "matrix")))
18
19
20  ## Note that isSymmetric is *not* exported ---  ## Note that isSymmetric is *not* exported
21  ## but also note that "base" eigen now (R 2.3.0) has an isSymmetric()  ## but that "base" has an isSymmetric() S3-generic since R 2.3.0
22  setMethod("isSymmetric", signature(object = "symmetricMatrix"),  setMethod("isSymmetric", signature(object = "symmetricMatrix"),
23            function(object,tol) TRUE)            function(object,tol) TRUE)
24  setMethod("isSymmetric", signature(object = "triangularMatrix"),  setMethod("isSymmetric", signature(object = "triangularMatrix"),
25            ## FIXME: 'TRUE' if *diagonal*, i.e. return(isDiagonal(object))            ## TRUE iff diagonal:
26            function(object,tol) FALSE)            function(object,tol) isDiagonal(object))
27
28    if(paste(R.version\$major, R.version\$minor, sep=".") < "2.3")
29        ## need a "matrix" method as in R 2.3 and later
30        setMethod("isSymmetric", signature(object = "matrix"),
31                  function(object, tol = 100*.Machine\$double.eps, ...)
32              {
33                  ## pretest: is it square?
34                  d <- dim(object)
35                  if(d[1] != d[2]) return(FALSE)
36                  ## for `broken' all.equal in R <= 2.2.x:
37                  dn <- dimnames(object)
38                  if(!identical(dn[1], dn[2])) return(FALSE)
39                  test <-
40                      if(is.complex(object))
41                          all.equal.numeric(object, Conj(t(object)), tol = tol, ...)
42                      else              # numeric, character, ..
43                          all.equal(object, t(object), tol = tol, ...)
44                  isTRUE(test)
45              })
46
47
48  setMethod("isTriangular", signature(object = "triangularMatrix"),  setMethod("isTriangular", signature(object = "triangularMatrix"),
49            function(object,tol) TRUE)            function(object,tol) TRUE)
50    setMethod("isTriangular", signature(object = "matrix"),
51              .is.triangular)
52
53    setMethod("isDiagonal", signature(object = "matrix"), .is.diagonal)
54
55
setMethod("isDiagonal", signature(object = "sparseMatrix"),
function(object) {
gT <- as(object, "TsparseMatrix")
all(gT@i == gT@j)
})
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")
# Line 81  Line 101
101          if(is.null(sparse))          if(is.null(sparse))
102              sparse <- sparseDefault(data)              sparse <- sparseDefault(data)
103          dimnames(data) <- dimnames          dimnames(data) <- dimnames
104      }      } else if (!is.null(dimnames))
105            dimnames(data) <- dimnames
106
107      ## 'data' is now a "matrix" or "Matrix"      ## 'data' is now a "matrix" or "Matrix"
108      ## FIXME: consider it's type (logical,....)
109      ## ctype <- substr(class(data), 1,1) # "d", "l", ...      ## check for symmetric / triangular / diagonal :
110      ## FIXME(2): check for symmetric / triangular / ...      isSym <- isSymmetric(data)
111        if((isTri <- !isSym))
112            isTri <- isTriangular(data)
113        isDiag <- isSym # cannot be diagonal if it isn't symmetric
114        if(isDiag)
115            isDiag <- isDiagonal(data)
116
117  ### TODO: Compare with as.Matrix() and its tests in ./dgeMatrix.R  ### TODO: Compare with as.Matrix() and its tests in ./dgeMatrix.R
118      if(sparse)
119          as(data, "dgCMatrix")      ## Find proper matrix class 'cl'
120      else      cl <-
121          as(data, "dgeMatrix")          if(isDiag)
122                "diagonalMatrix" # -> will automatically check for type
123            else {
124                ## consider it's type
125                ctype <-
126                    if(is(data,"Matrix")) class(data)
127                    else {
128                        if("complex" == (ctype <- typeof(data)))
129                            "z" else ctype
130                    }
131                ctype <- substr(ctype, 1,1) # "d", "l", "i" or "z"
132                if(ctype == "z")
133                    stop("complex matrices not yet implemented in Matrix package")
134                if(ctype == "i") {
135                    warning("integer matrices not yet implemented in 'Matrix'; ",
136                            "using 'double' ones'")
137                    ctype <- "d"
138                }
139                paste(ctype,
140                      if(sparse) {
141                          if(isSym) "sCMatrix" else
142                          if(isTri) "tCMatrix" else "gCMatrix"
143                      } else { ## dense
144                          if(isSym) "syMatrix" else
145                          if(isTri) "trMatrix" else "geMatrix"
146                      }, sep="")
147            }
148
149        ## Now coerce and return
150        as(data, cl)
151  }  }
152
153  ## Methods for operations where one argument is numeric  ## Methods for operations where one argument is numeric
# Line 173  Line 229
229  setMethod("[", signature(x = "Matrix",  setMethod("[", signature(x = "Matrix",
230                           i = "missing", j = "missing", drop = "ANY"),                           i = "missing", j = "missing", drop = "ANY"),
231            function (x, i, j, drop) x)            function (x, i, j, drop) x)
232
233  ## missing 'drop' --> 'drop = TRUE'  ## missing 'drop' --> 'drop = TRUE'
234  ##                     -----------  ##                     -----------
235  ## select rows  ## select rows
# Line 192  Line 249
249            function(x,i,j, drop)            function(x,i,j, drop)
250            stop("invalid or not-yet-implemented 'Matrix' subsetting"))            stop("invalid or not-yet-implemented 'Matrix' subsetting"))
251
252    ##  "logical *vector* indexing, such as  M [ M >= 10 ] :
253    setMethod("[", signature(x = "Matrix", i = "lMatrix", j = "missing",
254                             drop = "ANY"),
255              function (x, i, j, drop) {
256                  as(x, geClass(x))@x[as.vector(i)]
257                                            # -> error when lengths don't match
258              })
259
260    setMethod("[", signature(x = "Matrix", i = "logical", j = "missing",
261                             drop = "ANY"),
262              function (x, i, j, drop) as(x, geClass(x))@x[i])
263
264
265  ## "FIXME:"  ## "FIXME:"
266  ## How can we get at   A[ ij ]  where ij is (i,j) 2-column matrix?  ## How can we get at   A[ ij ]  where ij is (i,j) 2-column matrix?
267  ##  and                A[ LL ]  where LL is a logical *vector*  ##  and                A[ LL ]  where LL is a logical *vector*
# Line 202  Line 272
272
273  ## x[] <- value :  ## x[] <- value :
274  setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",  setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",
275                                  value = "index"),##  double/logical/...                                  value = "ANY"),## double/logical/...
276            function (x, value) { x@x <- value ; validObject(x); x })            function (x, value) {
277                  x@x <- value
278                  validObject(x)# check if type and lengths above match
279                  x
280              })
281
282  ## Otherwise (value is not "index"): bail out  ## Method for all 'Matrix' kinds (rather than incomprehensible error messages);
283    ## (ANY,ANY,ANY) is used when no `real method' is implemented :
284  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",  setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
285                                  value = "ANY"),                                  value = "ANY"),
286            function (x, i, j, value)            function (x, i, j, value) {
287                   if(!is(value,"index"))                if(!is.atomic(value))
288                   stop("RHS 'value' must be of class \"index\"")                    stop("RHS 'value' must match matrix class ", class(x))
289                   else stop("not-yet-implemented 'Matrix[<-' method"))                else stop("not-yet-implemented 'Matrix[<-' method")
290              })
291
292
293

Legend:
 Removed from v.1165 changed lines Added in v.1225