SCM

SCM Repository

[matrix] Annotation of /pkg/R/Matrix.R
ViewVC logotype

Annotation of /pkg/R/Matrix.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 956 - (view) (download)

1 : maechler 607 #### Toplevel ``virtual'' class "Matrix"
2 :    
3 : maechler 956 ## ## probably not needed eventually:
4 :     ## setAs(from = "ddenseMatrix", to = "matrix",
5 :     ## function(from) {
6 :     ## if(length(d <- dim(from)) != 2) stop("dim(.) has not length 2")
7 :     ## array(from@x, dim = d, dimnames = dimnames(from))
8 :     ## })
9 : maechler 607
10 : maechler 676 ## should propagate to all subclasses:
11 : maechler 658 setMethod("as.matrix", signature(x = "Matrix"), function(x) as(x, "matrix"))
12 : maechler 956 ## for 'Matrix' objects, as.array() should be equivalent:
13 :     setMethod("as.array", signature(x = "Matrix"), function(x) as(x, "matrix"))
14 : maechler 658
15 : maechler 956 ## slow "fall back" method {subclasses should have faster ones}:
16 :     setMethod("as.vector", signature(x = "Matrix", mode = "missing"),
17 :     function(x) as.vector(as(x, "matrix")))
18 :    
19 :    
20 : maechler 886 ## Note that isSymmetric is *not* exported
21 :     setMethod("isSymmetric", signature(object = "symmetricMatrix"),
22 :     function(object) TRUE)
23 :     setMethod("isSymmetric", signature(object = "triangularMatrix"),
24 :     ## FIXME: 'TRUE' if *diagonal*, i.e. return(isDiagonal(object))
25 :     function(object) FALSE)
26 : maechler 925 setMethod("isDiagonal", signature(object = "sparseMatrix"),
27 :     function(object) {
28 :     gT <- as(object, "TsparseMatrix")
29 :     all(gT@i == gT@j)
30 :     })
31 : maechler 886
32 : maechler 579 setMethod("dim", signature(x = "Matrix"),
33 : maechler 698 function(x) x@Dim, valueClass = "integer")
34 : maechler 579 setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)
35 : maechler 607 ## not exported but used more than once for "dimnames<-" method :
36 :     ## -- or do only once for all "Matrix" classes ??
37 :     dimnamesGets <- function (x, value) {
38 :     d <- dim(x)
39 :     if (!is.list(value) || length(value) != 2 ||
40 : maechler 698 !(is.null(v1 <- value[[1]]) || length(v1) == d[1]) ||
41 :     !(is.null(v2 <- value[[2]]) || length(v2) == d[2]))
42 :     stop(sprintf("invalid dimnames given for '%s' object", class(x)))
43 : maechler 607 x@Dimnames <- list(if(!is.null(v1)) as.character(v1),
44 : maechler 698 if(!is.null(v2)) as.character(v2))
45 : maechler 607 x
46 :     }
47 :     setMethod("dimnames<-", signature(x = "Matrix", value = "list"),
48 : maechler 698 dimnamesGets)
49 : maechler 579
50 : maechler 658 setMethod("unname", signature("Matrix", force="missing"),
51 : maechler 698 function(obj) { obj@Dimnames <- list(NULL,NULL); obj})
52 : maechler 607
53 : bates 10 Matrix <-
54 : maechler 954 function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
55 :     sparse = NULL)
56 : bates 10 {
57 : maechler 954 sparseDefault <- function(m)
58 :     prod(dim(m)) > 2*sum(as(m, "matrix") != 0)
59 :    
60 :     i.M <- is(data, "Matrix")
61 :     if(is.null(sparse) && (i.M || is(data, "matrix")))
62 :     sparse <- sparseDefault(data)
63 :    
64 :     if (i.M) {
65 :     sM <- is(data,"sparseMatrix")
66 :     if((sparse && sM) || (!sparse && !sM))
67 :     return(data)
68 :     ## else : convert dense <-> sparse -> at end
69 :     }
70 :     else if (!is.matrix(data)) { ## cut & paste from "base::matrix" :
71 : maechler 698 if (missing(nrow))
72 :     nrow <- ceiling(length(data)/ncol)
73 :     else if (missing(ncol))
74 :     ncol <- ceiling(length(data)/nrow)
75 : maechler 954 data <- .Internal(matrix(data, nrow, ncol, byrow))
76 :     if(is.null(sparse))
77 :     sparse <- sparseDefault(data)
78 :     dimnames(data) <- dimnames
79 : bates 10 }
80 : maechler 954
81 :     ## 'data' is now a "matrix" or "Matrix"
82 :     ## FIXME: consider it's type (logical,....)
83 :     ## ctype <- substr(class(data), 1,1) # "d", "l", ...
84 :     ## FIXME(2): check for symmetric / triangular / ...
85 :     if(sparse)
86 :     as(data, "dgCMatrix")
87 :     else
88 :     as(data, "dgeMatrix")
89 : bates 10 }
90 :    
91 : bates 683 ## Methods for operations where one argument is numeric
92 : maechler 512
93 : maechler 698 ## Using as.matrix() and rbind()
94 :     ## in order to get dimnames from names {at least potentially}:
95 :    
96 : bates 683 setMethod("%*%", signature(x = "Matrix", y = "numeric"),
97 : maechler 698 function(x, y) callGeneric(x, as.matrix(y)))
98 : bates 683
99 :     setMethod("%*%", signature(x = "numeric", y = "Matrix"),
100 : maechler 698 function(x, y) callGeneric(rbind(x), y))
101 : bates 683
102 :     setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
103 : maechler 698 function(x, y = NULL) callGeneric(x, as.matrix(y)))
104 : bates 683
105 :     setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
106 : maechler 698 function(x, y = NULL) callGeneric(rbind(x), y))
107 : bates 683
108 :     setMethod("solve", signature(a = "Matrix", b = "numeric"),
109 : maechler 698 function(a, b, ...) callGeneric(a, as.matrix(b)))
110 : maechler 848
111 : maechler 925 ## bail-out methods in order to get better error messages
112 :     setMethod("%*%", signature(x = "Matrix", y = "Matrix"),
113 :     function (x, y)
114 :     stop(gettextf('not-yet-implemented method for <%s> %%*%% <%s>',
115 :     class(x), class(y))))
116 : maechler 946
117 : maechler 925 setMethod("crossprod", signature(x = "Matrix", y = "ANY"),
118 : maechler 946 function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
119 : maechler 956 setMethod("crossprod", signature(x = "ANY", y = "Matrix"),
120 :     function (x, y = NULL) .bail.out.2(.Generic, class(x), class(y)))
121 : maechler 925
122 : maechler 956 ## There are special sparse methods; this is a "fall back":
123 :     setMethod("kronecker", signature(X = "Matrix", Y = "ANY",
124 :     FUN = "ANY", make.dimnames = "ANY"),
125 :     function(X, Y, FUN, make.dimnames, ...) {
126 :     X <- as(X, "matrix") ; Matrix(callGeneric()) })
127 :     setMethod("kronecker", signature(X = "ANY", Y = "Matrix",
128 :     FUN = "ANY", make.dimnames = "ANY"),
129 :     function(X, Y, FUN, make.dimnames, ...) {
130 :     Y <- as(Y, "matrix") ; Matrix(callGeneric()) })
131 :    
132 :    
133 : maechler 946 setMethod("t", signature(x = "Matrix"),
134 :     function(x) .bail.out.1(.Generic, class(x)))
135 : maechler 925
136 : maechler 956 ## Group Methods
137 :     setMethod("+", signature(e1 = "Matrix", e2 = "missing"), function(e1) e1)
138 :     ## "fallback":
139 :     setMethod("-", signature(e1 = "Matrix", e2 = "missing"),
140 :     function(e1) {
141 :     warning("inefficient method used for \"- e1\"")
142 :     0-e1
143 :     })
144 :    
145 :     ## bail-outs:
146 : maechler 946 setMethod("Compare", signature(e1 = "Matrix", e2 = "Matrix"),
147 : maechler 954 function(e1, e2) {
148 :     d <- dimCheck(e1,e2)
149 :     .bail.out.2(.Generic, class(e1), class(e2))
150 :     })
151 : maechler 946 setMethod("Compare", signature(e1 = "Matrix", e2 = "ANY"),
152 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
153 :     setMethod("Compare", signature(e1 = "ANY", e2 = "Matrix"),
154 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
155 :    
156 :    
157 :    
158 : maechler 871 ### --------------------------------------------------------------------------
159 :     ###
160 :     ### Subsetting "[" and
161 :     ### SubAssign "[<-" : The "missing" cases can be dealt with here, "at the top":
162 : maechler 868
163 : maechler 886 ## Using "index" for indices should allow
164 : maechler 875 ## integer (numeric), logical, or character (names!) indices :
165 :    
166 : maechler 868 ## "x[]":
167 :     setMethod("[", signature(x = "Matrix",
168 :     i = "missing", j = "missing", drop = "ANY"),
169 :     function (x, i, j, drop) x)
170 :     ## missing 'drop' --> 'drop = TRUE'
171 :     ## -----------
172 :     ## select rows
173 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "missing",
174 : maechler 868 drop = "missing"),
175 :     function(x,i,j, drop) callGeneric(x, i=i, drop= TRUE))
176 :     ## select columns
177 : maechler 886 setMethod("[", signature(x = "Matrix", i = "missing", j = "index",
178 : maechler 868 drop = "missing"),
179 :     function(x,i,j, drop) callGeneric(x, j=j, drop= TRUE))
180 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "index",
181 : maechler 868 drop = "missing"),
182 :     function(x,i,j, drop) callGeneric(x, i=i, j=j, drop= TRUE))
183 :    
184 : maechler 875 ## bail out if any of (i,j,drop) is "non-sense"
185 :     setMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY", drop = "ANY"),
186 :     function(x,i,j, drop)
187 :     stop("invalid or not-yet-implemented 'Matrix' subsetting"))
188 :    
189 : maechler 868 ## "FIXME:"
190 :     ## How can we get at A[ ij ] where ij is (i,j) 2-column matrix?
191 :     ## and A[ LL ] where LL is a logical *vector*
192 : maechler 886 ## -> [.data.frame uses nargs() - can we do this in the *generic* ?
193 : maechler 868
194 :    
195 : maechler 871 ### "[<-" : -----------------
196 : maechler 868
197 : maechler 871 ## x[] <- value :
198 :     setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",
199 : maechler 886 value = "index"),## double/logical/...
200 : maechler 871 function (x, value) { x@x <- value ; validObject(x); x })
201 :    
202 : maechler 886 ## Otherwise (value is not "index"): bail out
203 : maechler 871 setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
204 :     value = "ANY"),
205 : maechler 875 function (x, i, j, value)
206 : maechler 886 if(!is(value,"index"))
207 :     stop("RHS 'value' must be of class \"index\"")
208 : maechler 925 else stop("not-yet-implemented 'Matrix[<-' method"))
209 : maechler 871
210 :    
211 :    
212 : maechler 908 ## NOTE: the following only works for R 2.2.x (and later) ---
213 :     ## ---- *and* 'Matrix' must have been *installed* by R >= 2.2.x
214 : maechler 875
215 : maechler 908 if(paste(R.version$major, R.version$minor, sep=".") >= "2.2") {
216 :    
217 :     ## The trivial methods :
218 :     setMethod("cbind2", signature(x = "Matrix", y = "NULL"),
219 :     function(x, y) x)
220 :     setMethod("cbind2", signature(x = "Matrix", y = "missing"),
221 :     function(x, y) x)
222 :     setMethod("cbind2", signature(x = "NULL", y="Matrix"),
223 :     function(x, y) x)
224 :    
225 : maechler 925 setMethod("rbind2", signature(x = "Matrix", y = "NULL"),
226 :     function(x, y) x)
227 :     setMethod("rbind2", signature(x = "Matrix", y = "missing"),
228 :     function(x, y) x)
229 :     setMethod("rbind2", signature(x = "NULL", y="Matrix"),
230 :     function(x, y) x)
231 :    
232 : maechler 908 ## Makes sure one gets x decent error message for the unimplemented cases:
233 :     setMethod("cbind2", signature(x = "Matrix", y = "Matrix"),
234 :     function(x, y) {
235 :     rowCheck(x,y)
236 :     stop(gettextf("cbind2() method for (%s,%s) not-yet defined",
237 :     class(x), class(y)))
238 :     })
239 :    
240 : maechler 956 ## Use a working fall back {particularly useful for sparse}:
241 :     ## FIXME: implement rbind2 via "cholmod" for C* and Tsparse ones
242 : maechler 908 setMethod("rbind2", signature(x = "Matrix", y = "Matrix"),
243 :     function(x, y) {
244 :     colCheck(x,y)
245 : maechler 956 t(cbind2(t(x), t(y)))
246 : maechler 908 })
247 : maechler 956
248 : maechler 908 }## R-2.2.x and newer

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