SCM

SCM Repository

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

Annotation of /pkg/Matrix/R/ddenseMatrix.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1453 - (view) (download)
Original Path: pkg/R/ddenseMatrix.R

1 : bates 686 ### Define Methods that can be inherited for all subclasses
2 :    
3 : maechler 1447 ## This replaces many "d..Matrix" -> "dgeMatrix" ones
4 :     ## >> but << needs all sub(sub(sub)) classes of "ddenseMatrix" listed
5 :     ## ----- in ../src/Mutils.c
6 :     setAs("ddenseMatrix", "dgeMatrix",
7 :     function(from) .Call(dup_mMatrix_as_dgeMatrix, from))
8 :    
9 :     ## d(ouble) to l(ogical):
10 : maechler 956 setAs("dgeMatrix", "lgeMatrix", d2l_Matrix)
11 :     setAs("dtrMatrix", "ltrMatrix", d2l_Matrix)
12 :     setAs("dtpMatrix", "ltpMatrix", d2l_Matrix)
13 :     setAs("dsyMatrix", "lsyMatrix", d2l_Matrix)
14 :     setAs("dspMatrix", "lspMatrix", d2l_Matrix)
15 :    
16 : maechler 1391 setMethod("as.numeric", signature(x = "ddenseMatrix"),
17 :     function(x, ...) as(x, "dgeMatrix")@x)
18 :    
19 : maechler 792 ## -- see also ./Matrix.R e.g., for a show() method
20 :    
21 : bates 686 ## These methods are the 'fallback' methods for all dense numeric
22 : maechler 792 ## matrices in that they simply coerce the ddenseMatrix to a
23 :     ## dgeMatrix. Methods for special forms override these.
24 : bates 686
25 :     setMethod("norm", signature(x = "ddenseMatrix", type = "missing"),
26 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix")))
27 :    
28 :     setMethod("norm", signature(x = "ddenseMatrix", type = "character"),
29 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix"), type))
30 :    
31 :     setMethod("rcond", signature(x = "ddenseMatrix", type = "missing"),
32 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix")))
33 :    
34 :     setMethod("rcond", signature(x = "ddenseMatrix", type = "character"),
35 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix"), type))
36 :    
37 : maechler 946 ## Not really useful; now require *identical* class for result:
38 :     ## setMethod("t", signature(x = "ddenseMatrix"),
39 :     ## function(x) callGeneric(as(x, "dgeMatrix")))
40 : bates 686
41 : maechler 1087 setMethod("tcrossprod", signature(x = "ddenseMatrix", y = "missing"),
42 :     function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))
43 : bates 686
44 :     setMethod("crossprod", signature(x = "ddenseMatrix", y = "missing"),
45 :     function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))
46 :    
47 :     setMethod("diag", signature(x = "ddenseMatrix"),
48 :     function(x = 1, nrow, ncol = n) callGeneric(as(x, "dgeMatrix")))
49 :    
50 :     setMethod("solve", signature(a = "ddenseMatrix", b = "missing"),
51 :     function(a, b, ...) callGeneric(as(a, "dgeMatrix")))
52 :    
53 :     setMethod("solve", signature(a = "ddenseMatrix", b = "ANY"),
54 :     function(a, b, ...) callGeneric(as(a, "dgeMatrix"), b))
55 :    
56 :     setMethod("lu", signature(x = "ddenseMatrix"),
57 :     function(x, ...) callGeneric(as(x, "dgeMatrix")))
58 :    
59 : maechler 856 setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "missing"),
60 : bates 686 function(x, logarithm, ...) callGeneric(as(x, "dgeMatrix")))
61 :    
62 : maechler 856 setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "logical"),
63 : bates 686 function(x, logarithm, ...)
64 :     callGeneric(as(x, "dgeMatrix"), logarithm))
65 :    
66 : maechler 954 ## now done for "dMatrix":
67 :     ## setMethod("expm", signature(x = "ddenseMatrix"),
68 :     ## function(x) callGeneric(as(x, "dgeMatrix")))
69 : bates 686
70 :     setMethod("Schur", signature(x = "ddenseMatrix", vectors = "missing"),
71 :     function(x, vectors, ...) callGeneric(as(x, "dgeMatrix")))
72 :    
73 :     setMethod("Schur", signature(x = "ddenseMatrix", vectors = "logical"),
74 :     function(x, vectors, ...) callGeneric(as(x, "dgeMatrix"), vectors))
75 :    
76 : maechler 792
77 : maechler 1331 ## Cheap version: work via "dgeMatrix" and use the group methods there:
78 :     ## FIXME(?): try to preserve "symmetric", "triangular", ...
79 :     setMethod("Arith", ## "+", "-", "*", "^", "%%", "%/%", "/"
80 :     signature(e1 = "ddenseMatrix", e2 = "ddenseMatrix"),
81 :     function(e1, e2) callGeneric(as(e1, "dgeMatrix"),
82 :     as(e2, "dgeMatrix")))
83 :     setMethod("Arith",
84 :     signature(e1 = "ddenseMatrix", e2 = "numeric"),
85 :     function(e1, e2) callGeneric(as(e1, "dgeMatrix"), e2))
86 :     setMethod("Arith",
87 :     signature(e1 = "numeric", e2 = "ddenseMatrix"),
88 :     function(e1, e2) callGeneric(e1, as(e2, "dgeMatrix")))
89 : maechler 908
90 : maechler 1331 setMethod("Math",
91 :     signature(x = "ddenseMatrix"),
92 :     function(x) callGeneric(as(x, "dgeMatrix")))
93 :    
94 :    
95 :    
96 :     ### for R 2.2.x (and later): -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
97 :    
98 : maechler 925 ### cbind2
99 : maechler 1331 setMethod("cbind2", signature(x = "ddenseMatrix", y = "numeric"),
100 :     function(x, y) {
101 :     d <- dim(x); nr <- d[1]; nc <- d[2]
102 :     y <- rep(y, length.out = nr) # 'silent procrustes'
103 :     ## beware of (packed) triangular, symmetric, ...
104 :     x <- as(x, "dgeMatrix")
105 :     x@x <- c(x@x, as.double(y))
106 :     x@Dim[2] <- nc + 1:1
107 :     if(is.character(dn <- x@Dimnames[[2]]))
108 :     x@Dimnames[[2]] <- c(dn, "")
109 :     x
110 :     })
111 :     ## the same, (x,y) <-> (y,x):
112 :     setMethod("cbind2", signature(x = "numeric", y = "ddenseMatrix"),
113 :     function(x, y) {
114 :     d <- dim(y); nr <- d[1]; nc <- d[2]
115 :     x <- rep(x, length.out = nr)
116 :     y <- as(y, "dgeMatrix")
117 :     y@x <- c(as.double(x), y@x)
118 :     y@Dim[2] <- nc + 1:1
119 :     if(is.character(dn <- y@Dimnames[[2]]))
120 :     y@Dimnames[[2]] <- c("", dn)
121 :     y
122 :     })
123 : maechler 908
124 : maechler 1331 setMethod("cbind2", signature(x = "ddenseMatrix", y = "matrix"),
125 :     function(x, y) callGeneric(x, as(y, "dgeMatrix")))
126 :     setMethod("cbind2", signature(x = "matrix", y = "ddenseMatrix"),
127 :     function(x, y) callGeneric(as(x, "dgeMatrix"), y))
128 : maechler 908
129 : maechler 1331 setMethod("cbind2", signature(x = "ddenseMatrix", y = "ddenseMatrix"),
130 :     function(x, y) {
131 :     nr <- rowCheck(x,y)
132 :     ncx <- x@Dim[2]
133 :     ncy <- y@Dim[2]
134 :     ## beware of (packed) triangular, symmetric, ...
135 :     hasDN <- !is.null(dnx <- dimnames(x)) |
136 :     !is.null(dny <- dimnames(y))
137 :     x <- as(x, "dgeMatrix")
138 :     y <- as(y, "dgeMatrix")
139 :     x@x <- c(x@x, y@x)
140 :     x@Dim[2] <- ncx + ncy
141 :     if(hasDN) {
142 :     ## R and S+ are different in which names they take
143 :     ## if they differ -- but there's no warning in any case
144 :     rn <- if(!is.null(dnx[[1]])) dnx[[1]] else dny[[1]]
145 :     cx <- dnx[[2]] ; cy <- dny[[2]]
146 :     cn <- if(is.null(cx) && is.null(cy)) NULL
147 :     else c(if(!is.null(cx)) cx else rep.int("", ncx),
148 :     if(!is.null(cy)) cy else rep.int("", ncy))
149 :     x@Dimnames <- list(rn, cn)
150 :     }
151 :     x
152 :     })
153 : maechler 908
154 : maechler 925 ### rbind2 -- analogous to cbind2 --- more to do for @x though:
155 : maechler 908
156 : maechler 1331 setMethod("rbind2", signature(x = "ddenseMatrix", y = "numeric"),
157 :     function(x, y) {
158 :     if(is.character(dn <- x@Dimnames[[1]])) dn <- c(dn, "")
159 :     new("dgeMatrix", Dim = x@Dim + 1:0,
160 :     Dimnames = list(dn, x@Dimnames[[2]]),
161 :     x = c(rbind2(as(x,"matrix"), y)))
162 :     })
163 :     ## the same, (x,y) <-> (y,x):
164 :     setMethod("rbind2", signature(x = "numeric", y = "ddenseMatrix"),
165 :     function(x, y) {
166 :     if(is.character(dn <- y@Dimnames[[1]])) dn <- c("", dn)
167 :     new("dgeMatrix", Dim = y@Dim + 1:0,
168 :     Dimnames = list(dn, y@Dimnames[[2]]),
169 :     x = c(rbind2(x, as(y,"matrix"))))
170 :     })
171 : maechler 925
172 : maechler 1331 setMethod("rbind2", signature(x = "ddenseMatrix", y = "matrix"),
173 :     function(x, y) callGeneric(x, as(y, "dgeMatrix")))
174 :     setMethod("rbind2", signature(x = "matrix", y = "ddenseMatrix"),
175 :     function(x, y) callGeneric(as(x, "dgeMatrix"), y))
176 : maechler 925
177 : maechler 1331 setMethod("rbind2", signature(x = "ddenseMatrix", y = "ddenseMatrix"),
178 :     function(x, y) {
179 :     nc <- colCheck(x,y)
180 :     nrx <- x@Dim[1]
181 :     nry <- y@Dim[1]
182 :     dn <-
183 :     if(!is.null(dnx <- dimnames(x)) |
184 :     !is.null(dny <- dimnames(y))) {
185 :     ## R and S+ are different in which names they take
186 :     ## if they differ -- but there's no warning in any case
187 :     list(if(is.null(rx <- dnx[[1]]) && is.null(ry <- dny[[1]]))
188 :     NULL else
189 :     c(if(!is.null(rx)) rx else rep.int("", nrx),
190 :     if(!is.null(ry)) ry else rep.int("", nry)),
191 :     if(!is.null(dnx[[2]])) dnx[[2]] else dny[[2]])
192 : maechler 925
193 : maechler 1331 } else list(NULL, NULL)
194 :     ## beware of (packed) triangular, symmetric, ...
195 :     new("dgeMatrix", Dim = c(nrx + nry, nc), Dimnames = dn,
196 :     x = c(rbind2(as(x,"matrix"), as(y,"matrix"))))
197 :     })
198 : bates 1453 ## NB: have extra tril(), triu() methods for symmetric ["dsy" and "dsp"] and
199 :     ## for triangular ["dtr" and "dtp"]
200 :     setMethod("tril", "ddenseMatrix",
201 :     function(x, k = 0, ...) {
202 :     k <- as.integer(k[1])
203 :     dd <- dim(x); sqr <- dd[1] == dd[2]
204 :     stopifnot(-dd[1] <= k, k <= dd[1]) # had k <= 0
205 :     ## returns "lower triangular" if k <= 0 && sqr
206 :     .Call(ddense_band, x, -dd[1], k)
207 :     })
208 :    
209 :     setMethod("triu", "ddenseMatrix",
210 :     function(x, k = 0, ...) {
211 :     k <- as.integer(k[1])
212 :     dd <- dim(x); sqr <- dd[1] == dd[2]
213 :     stopifnot(-dd[1] <= k, k <= dd[1]) # had k >= 0
214 :     ## returns "upper triangular" if k >= 0
215 :     .Call(ddense_band, x, k, dd[2])
216 :     })
217 :    
218 :     setMethod("band", "ddenseMatrix",
219 :     function(x, k1, k2, ...) {
220 :     k1 <- as.integer(k1[1])
221 :     k2 <- as.integer(k2[1])
222 :     dd <- dim(x); sqr <- dd[1] == dd[2]
223 :     stopifnot(-dd[1] <= k1, k1 <= k2, k2 <= dd[1])
224 :     r <- .Call(ddense_band, x, k1, k2)
225 :     if (k1 < 0 && k1 == -k2 && isSymmetric(x)) ## symmetric
226 :     as(r, paste(.M.kind(x), "syMatrix", sep=''))
227 :     else
228 :     r
229 :     })

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