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 954 - (view) (download)

1 : maechler 607 #### Toplevel ``virtual'' class "Matrix"
2 :    
3 :     ## probably not needed eventually:
4 : maechler 618 setAs(from = "ddenseMatrix", to = "matrix",
5 : maechler 607 function(from) {
6 : maechler 698 if(length(d <- dim(from)) != 2) stop("dim(.) has not length 2")
7 :     array(from@x, dim = d, dimnames = dimnames(from))
8 : maechler 607 })
9 :    
10 : maechler 925 setMethod("show", signature(object = "ddenseMatrix"),
11 :     function(object) prMatrix(object))
12 : bates 10
13 : maechler 698 ##- ## FIXME: The following is only for the "dMatrix" objects that are not
14 :     ##- ## "dense" nor "sparse" -- i.e. "packed" ones :
15 :     ##- ## But these could be printed better -- "." for structural zeros.
16 :     ##- setMethod("show", signature(object = "dMatrix"), prMatrix)
17 :     ##- ## and improve this as well:
18 :     ##- setMethod("show", signature(object = "pMatrix"), prMatrix)
19 :     ##- ## this should now be superfluous [keep for safety for the moment]:
20 : maechler 925 setMethod("show", signature(object = "Matrix"),
21 :     function(object) prMatrix(object))
22 : maechler 512
23 : maechler 676 ## should propagate to all subclasses:
24 : maechler 658 setMethod("as.matrix", signature(x = "Matrix"), function(x) as(x, "matrix"))
25 :    
26 : maechler 886 ## Note that isSymmetric is *not* exported
27 :     setMethod("isSymmetric", signature(object = "symmetricMatrix"),
28 :     function(object) TRUE)
29 :     setMethod("isSymmetric", signature(object = "triangularMatrix"),
30 :     ## FIXME: 'TRUE' if *diagonal*, i.e. return(isDiagonal(object))
31 :     function(object) FALSE)
32 : maechler 925 setMethod("isDiagonal", signature(object = "sparseMatrix"),
33 :     function(object) {
34 :     gT <- as(object, "TsparseMatrix")
35 :     all(gT@i == gT@j)
36 :     })
37 : maechler 886
38 : maechler 579 setMethod("dim", signature(x = "Matrix"),
39 : maechler 698 function(x) x@Dim, valueClass = "integer")
40 : maechler 579 setMethod("dimnames", signature(x = "Matrix"), function(x) x@Dimnames)
41 : maechler 607 ## not exported but used more than once for "dimnames<-" method :
42 :     ## -- or do only once for all "Matrix" classes ??
43 :     dimnamesGets <- function (x, value) {
44 :     d <- dim(x)
45 :     if (!is.list(value) || length(value) != 2 ||
46 : maechler 698 !(is.null(v1 <- value[[1]]) || length(v1) == d[1]) ||
47 :     !(is.null(v2 <- value[[2]]) || length(v2) == d[2]))
48 :     stop(sprintf("invalid dimnames given for '%s' object", class(x)))
49 : maechler 607 x@Dimnames <- list(if(!is.null(v1)) as.character(v1),
50 : maechler 698 if(!is.null(v2)) as.character(v2))
51 : maechler 607 x
52 :     }
53 :     setMethod("dimnames<-", signature(x = "Matrix", value = "list"),
54 : maechler 698 dimnamesGets)
55 : maechler 579
56 : maechler 658 setMethod("unname", signature("Matrix", force="missing"),
57 : maechler 698 function(obj) { obj@Dimnames <- list(NULL,NULL); obj})
58 : maechler 607
59 : bates 10 Matrix <-
60 : maechler 954 function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL,
61 :     sparse = NULL)
62 : bates 10 {
63 : maechler 954 sparseDefault <- function(m)
64 :     prod(dim(m)) > 2*sum(as(m, "matrix") != 0)
65 :    
66 :     i.M <- is(data, "Matrix")
67 :     if(is.null(sparse) && (i.M || is(data, "matrix")))
68 :     sparse <- sparseDefault(data)
69 :    
70 :     if (i.M) {
71 :     sM <- is(data,"sparseMatrix")
72 :     if((sparse && sM) || (!sparse && !sM))
73 :     return(data)
74 :     ## else : convert dense <-> sparse -> at end
75 :     }
76 :     else if (!is.matrix(data)) { ## cut & paste from "base::matrix" :
77 : maechler 698 if (missing(nrow))
78 :     nrow <- ceiling(length(data)/ncol)
79 :     else if (missing(ncol))
80 :     ncol <- ceiling(length(data)/nrow)
81 : maechler 954 data <- .Internal(matrix(data, nrow, ncol, byrow))
82 :     if(is.null(sparse))
83 :     sparse <- sparseDefault(data)
84 :     dimnames(data) <- dimnames
85 : bates 10 }
86 : maechler 954
87 :     ## 'data' is now a "matrix" or "Matrix"
88 :     ## FIXME: consider it's type (logical,....)
89 :     ## ctype <- substr(class(data), 1,1) # "d", "l", ...
90 :     ## FIXME(2): check for symmetric / triangular / ...
91 :     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 925
126 : maechler 946 setMethod("t", signature(x = "Matrix"),
127 :     function(x) .bail.out.1(.Generic, class(x)))
128 : maechler 925
129 : maechler 946 ## Group Methods (bail-out)
130 :     setMethod("Compare", signature(e1 = "Matrix", e2 = "Matrix"),
131 : maechler 954 function(e1, e2) {
132 :     d <- dimCheck(e1,e2)
133 :     .bail.out.2(.Generic, class(e1), class(e2))
134 :     })
135 : maechler 946 setMethod("Compare", signature(e1 = "Matrix", e2 = "ANY"),
136 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
137 :     setMethod("Compare", signature(e1 = "ANY", e2 = "Matrix"),
138 :     function(e1, e2) .bail.out.2(.Generic, class(e1), class(e2)))
139 :    
140 :    
141 :    
142 : maechler 871 ### --------------------------------------------------------------------------
143 :     ###
144 :     ### Subsetting "[" and
145 :     ### SubAssign "[<-" : The "missing" cases can be dealt with here, "at the top":
146 : maechler 868
147 : maechler 886 ## Using "index" for indices should allow
148 : maechler 875 ## integer (numeric), logical, or character (names!) indices :
149 :    
150 : maechler 868 ## "x[]":
151 :     setMethod("[", signature(x = "Matrix",
152 :     i = "missing", j = "missing", drop = "ANY"),
153 :     function (x, i, j, drop) x)
154 :     ## missing 'drop' --> 'drop = TRUE'
155 :     ## -----------
156 :     ## select rows
157 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "missing",
158 : maechler 868 drop = "missing"),
159 :     function(x,i,j, drop) callGeneric(x, i=i, drop= TRUE))
160 :     ## select columns
161 : maechler 886 setMethod("[", signature(x = "Matrix", i = "missing", j = "index",
162 : maechler 868 drop = "missing"),
163 :     function(x,i,j, drop) callGeneric(x, j=j, drop= TRUE))
164 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "index",
165 : maechler 868 drop = "missing"),
166 :     function(x,i,j, drop) callGeneric(x, i=i, j=j, drop= TRUE))
167 :    
168 : maechler 875 ## bail out if any of (i,j,drop) is "non-sense"
169 :     setMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY", drop = "ANY"),
170 :     function(x,i,j, drop)
171 :     stop("invalid or not-yet-implemented 'Matrix' subsetting"))
172 :    
173 : maechler 868 ## "FIXME:"
174 :     ## How can we get at A[ ij ] where ij is (i,j) 2-column matrix?
175 :     ## and A[ LL ] where LL is a logical *vector*
176 : maechler 886 ## -> [.data.frame uses nargs() - can we do this in the *generic* ?
177 : maechler 868
178 :    
179 : maechler 871 ### "[<-" : -----------------
180 : maechler 868
181 : maechler 871 ## x[] <- value :
182 :     setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",
183 : maechler 886 value = "index"),## double/logical/...
184 : maechler 871 function (x, value) { x@x <- value ; validObject(x); x })
185 :    
186 : maechler 886 ## Otherwise (value is not "index"): bail out
187 : maechler 871 setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
188 :     value = "ANY"),
189 : maechler 875 function (x, i, j, value)
190 : maechler 886 if(!is(value,"index"))
191 :     stop("RHS 'value' must be of class \"index\"")
192 : maechler 925 else stop("not-yet-implemented 'Matrix[<-' method"))
193 : maechler 871
194 :    
195 :    
196 : maechler 908 ## NOTE: the following only works for R 2.2.x (and later) ---
197 :     ## ---- *and* 'Matrix' must have been *installed* by R >= 2.2.x
198 : maechler 875
199 : maechler 908 if(paste(R.version$major, R.version$minor, sep=".") >= "2.2") {
200 :    
201 :     ## The trivial methods :
202 :     setMethod("cbind2", signature(x = "Matrix", y = "NULL"),
203 :     function(x, y) x)
204 :     setMethod("cbind2", signature(x = "Matrix", y = "missing"),
205 :     function(x, y) x)
206 :     setMethod("cbind2", signature(x = "NULL", y="Matrix"),
207 :     function(x, y) x)
208 :    
209 : maechler 925 setMethod("rbind2", signature(x = "Matrix", y = "NULL"),
210 :     function(x, y) x)
211 :     setMethod("rbind2", signature(x = "Matrix", y = "missing"),
212 :     function(x, y) x)
213 :     setMethod("rbind2", signature(x = "NULL", y="Matrix"),
214 :     function(x, y) x)
215 :    
216 : maechler 908 ## Makes sure one gets x decent error message for the unimplemented cases:
217 :     setMethod("cbind2", signature(x = "Matrix", y = "Matrix"),
218 :     function(x, y) {
219 :     rowCheck(x,y)
220 :     stop(gettextf("cbind2() method for (%s,%s) not-yet defined",
221 :     class(x), class(y)))
222 :     })
223 :    
224 :     setMethod("rbind2", signature(x = "Matrix", y = "Matrix"),
225 :     function(x, y) {
226 :     colCheck(x,y)
227 :     stop(gettextf("rbind2() method for (%s,%s) not-yet defined",
228 :     class(x), class(y)))
229 :     })
230 :     }## 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