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