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 925 - (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 :     function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
61 :     {
62 :     if (is(data, "Matrix")) return(data)
63 :     if (is.matrix(data)) { val <- data }
64 : maechler 538 else { ## cut & paste from "base::matrix" :
65 : maechler 698 if (missing(nrow))
66 :     nrow <- ceiling(length(data)/ncol)
67 :     else if (missing(ncol))
68 :     ncol <- ceiling(length(data)/nrow)
69 :     val <- .Internal(matrix(data, nrow, ncol, byrow))
70 :     dimnames(val) <- dimnames
71 : bates 10 }
72 : bates 477 as(val, "dgeMatrix")
73 : bates 10 }
74 :    
75 : bates 683 ## Methods for operations where one argument is numeric
76 : maechler 512
77 : maechler 698 ## Using as.matrix() and rbind()
78 :     ## in order to get dimnames from names {at least potentially}:
79 :    
80 : bates 683 setMethod("%*%", signature(x = "Matrix", y = "numeric"),
81 : maechler 698 function(x, y) callGeneric(x, as.matrix(y)))
82 : bates 683
83 :     setMethod("%*%", signature(x = "numeric", y = "Matrix"),
84 : maechler 698 function(x, y) callGeneric(rbind(x), y))
85 : bates 683
86 :     setMethod("crossprod", signature(x = "Matrix", y = "numeric"),
87 : maechler 698 function(x, y = NULL) callGeneric(x, as.matrix(y)))
88 : bates 683
89 :     setMethod("crossprod", signature(x = "numeric", y = "Matrix"),
90 : maechler 698 function(x, y = NULL) callGeneric(rbind(x), y))
91 : bates 683
92 :     setMethod("solve", signature(a = "Matrix", b = "numeric"),
93 : maechler 698 function(a, b, ...) callGeneric(a, as.matrix(b)))
94 : maechler 848
95 : maechler 925 ## bail-out methods in order to get better error messages
96 :     setMethod("%*%", signature(x = "Matrix", y = "Matrix"),
97 :     function (x, y)
98 :     stop(gettextf('not-yet-implemented method for <%s> %%*%% <%s>',
99 :     class(x), class(y))))
100 :     setMethod("crossprod", signature(x = "Matrix", y = "ANY"),
101 :     function (x, y = NULL)
102 :     stop(gettextf('not-yet-implemented method for crossprod(<%s>, <%s>)',
103 :     class(x), class(y))))
104 :    
105 :    
106 : maechler 871 ### --------------------------------------------------------------------------
107 :     ###
108 :     ### Subsetting "[" and
109 :     ### SubAssign "[<-" : The "missing" cases can be dealt with here, "at the top":
110 : maechler 868
111 : maechler 886 ## Using "index" for indices should allow
112 : maechler 875 ## integer (numeric), logical, or character (names!) indices :
113 :    
114 : maechler 868 ## "x[]":
115 :     setMethod("[", signature(x = "Matrix",
116 :     i = "missing", j = "missing", drop = "ANY"),
117 :     function (x, i, j, drop) x)
118 :     ## missing 'drop' --> 'drop = TRUE'
119 :     ## -----------
120 :     ## select rows
121 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "missing",
122 : maechler 868 drop = "missing"),
123 :     function(x,i,j, drop) callGeneric(x, i=i, drop= TRUE))
124 :     ## select columns
125 : maechler 886 setMethod("[", signature(x = "Matrix", i = "missing", j = "index",
126 : maechler 868 drop = "missing"),
127 :     function(x,i,j, drop) callGeneric(x, j=j, drop= TRUE))
128 : maechler 886 setMethod("[", signature(x = "Matrix", i = "index", j = "index",
129 : maechler 868 drop = "missing"),
130 :     function(x,i,j, drop) callGeneric(x, i=i, j=j, drop= TRUE))
131 :    
132 : maechler 875 ## bail out if any of (i,j,drop) is "non-sense"
133 :     setMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY", drop = "ANY"),
134 :     function(x,i,j, drop)
135 :     stop("invalid or not-yet-implemented 'Matrix' subsetting"))
136 :    
137 : maechler 868 ## "FIXME:"
138 :     ## How can we get at A[ ij ] where ij is (i,j) 2-column matrix?
139 :     ## and A[ LL ] where LL is a logical *vector*
140 : maechler 886 ## -> [.data.frame uses nargs() - can we do this in the *generic* ?
141 : maechler 868
142 :    
143 : maechler 871 ### "[<-" : -----------------
144 : maechler 868
145 : maechler 871 ## x[] <- value :
146 :     setReplaceMethod("[", signature(x = "Matrix", i = "missing", j = "missing",
147 : maechler 886 value = "index"),## double/logical/...
148 : maechler 871 function (x, value) { x@x <- value ; validObject(x); x })
149 :    
150 : maechler 886 ## Otherwise (value is not "index"): bail out
151 : maechler 871 setReplaceMethod("[", signature(x = "Matrix", i = "ANY", j = "ANY",
152 :     value = "ANY"),
153 : maechler 875 function (x, i, j, value)
154 : maechler 886 if(!is(value,"index"))
155 :     stop("RHS 'value' must be of class \"index\"")
156 : maechler 925 else stop("not-yet-implemented 'Matrix[<-' method"))
157 : maechler 871
158 :    
159 :    
160 : maechler 908 ## NOTE: the following only works for R 2.2.x (and later) ---
161 :     ## ---- *and* 'Matrix' must have been *installed* by R >= 2.2.x
162 : maechler 875
163 : maechler 908 if(paste(R.version$major, R.version$minor, sep=".") >= "2.2") {
164 :    
165 :     ## The trivial methods :
166 :     setMethod("cbind2", signature(x = "Matrix", y = "NULL"),
167 :     function(x, y) x)
168 :     setMethod("cbind2", signature(x = "Matrix", y = "missing"),
169 :     function(x, y) x)
170 :     setMethod("cbind2", signature(x = "NULL", y="Matrix"),
171 :     function(x, y) x)
172 :    
173 : maechler 925 setMethod("rbind2", signature(x = "Matrix", y = "NULL"),
174 :     function(x, y) x)
175 :     setMethod("rbind2", signature(x = "Matrix", y = "missing"),
176 :     function(x, y) x)
177 :     setMethod("rbind2", signature(x = "NULL", y="Matrix"),
178 :     function(x, y) x)
179 :    
180 : maechler 908 ## Makes sure one gets x decent error message for the unimplemented cases:
181 :     setMethod("cbind2", signature(x = "Matrix", y = "Matrix"),
182 :     function(x, y) {
183 :     rowCheck(x,y)
184 :     stop(gettextf("cbind2() method for (%s,%s) not-yet defined",
185 :     class(x), class(y)))
186 :     })
187 :    
188 :     setMethod("rbind2", signature(x = "Matrix", y = "Matrix"),
189 :     function(x, y) {
190 :     colCheck(x,y)
191 :     stop(gettextf("rbind2() method for (%s,%s) not-yet defined",
192 :     class(x), class(y)))
193 :     })
194 :     }## 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