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 2112 - (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 : bates 1460
7 : maechler 1447 setAs("ddenseMatrix", "dgeMatrix",
8 : bates 1463 function(from) {
9 : maechler 1738 if (class(from) != "dgeMatrix")
10 :     .Call(dup_mMatrix_as_dgeMatrix, from)
11 :     else from
12 : bates 1463 })
13 : maechler 1575
14 : maechler 2112
15 : maechler 1447 ## d(ouble) to l(ogical):
16 : maechler 1747 setAs("dgeMatrix", "lgeMatrix", function(from) d2l_Matrix(from, "dgeMatrix"))
17 :     setAs("dsyMatrix", "lsyMatrix", function(from) d2l_Matrix(from, "dsyMatrix"))
18 :     setAs("dspMatrix", "lspMatrix", function(from) d2l_Matrix(from, "dspMatrix"))
19 :     setAs("dtrMatrix", "ltrMatrix", function(from) d2l_Matrix(from, "dtrMatrix"))
20 :     setAs("dtpMatrix", "ltpMatrix", function(from) d2l_Matrix(from, "dtpMatrix"))
21 : maechler 956
22 : bates 1460 setAs("ddenseMatrix", "CsparseMatrix",
23 :     function(from) {
24 : maechler 1799 if (class(from) != "dgeMatrix") # don't lose symmetry/triangularity/...
25 :     as_Csparse(from)
26 :     else .Call(dense_to_Csparse, from)
27 : bates 1460 })
28 :    
29 :     ## special case
30 :     setAs("dgeMatrix", "dgCMatrix",
31 :     function(from) .Call(dense_to_Csparse, from))
32 :    
33 :     setAs("matrix", "CsparseMatrix",
34 : maechler 1618 function(from) {
35 :     if(is.numeric(from))
36 :     .Call(dense_to_Csparse, .Call(dup_mMatrix_as_dgeMatrix, from))
37 :     else if(is.logical(from)) ## FIXME: this works, but maybe wastefully
38 :     as(Matrix(from, sparse=TRUE), "CsparseMatrix")
39 :     else stop('not-yet-implemented coercion to "CsparseMatrix"')
40 :     })
41 : bates 1460
42 : maechler 1618
43 : bates 1460 ## special case needed in the Matrix function
44 :     setAs("matrix", "dgCMatrix",
45 :     function(from) {
46 :     storage.mode(from) <- "double"
47 :     .Call(dense_to_Csparse, from)
48 :     })
49 :    
50 :     setAs("numeric", "CsparseMatrix",
51 :     function(from)
52 :     .Call(dense_to_Csparse, .Call(dup_mMatrix_as_dgeMatrix, from)))
53 :    
54 : maechler 1391 setMethod("as.numeric", signature(x = "ddenseMatrix"),
55 :     function(x, ...) as(x, "dgeMatrix")@x)
56 :    
57 : maechler 792 ## -- see also ./Matrix.R e.g., for a show() method
58 :    
59 : bates 686 ## These methods are the 'fallback' methods for all dense numeric
60 : maechler 792 ## matrices in that they simply coerce the ddenseMatrix to a
61 :     ## dgeMatrix. Methods for special forms override these.
62 : bates 686
63 :     setMethod("norm", signature(x = "ddenseMatrix", type = "missing"),
64 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix")))
65 :    
66 :     setMethod("norm", signature(x = "ddenseMatrix", type = "character"),
67 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix"), type))
68 :    
69 :     setMethod("rcond", signature(x = "ddenseMatrix", type = "missing"),
70 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix")))
71 :    
72 :     setMethod("rcond", signature(x = "ddenseMatrix", type = "character"),
73 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix"), type))
74 :    
75 : maechler 946 ## Not really useful; now require *identical* class for result:
76 :     ## setMethod("t", signature(x = "ddenseMatrix"),
77 :     ## function(x) callGeneric(as(x, "dgeMatrix")))
78 : bates 686
79 : maechler 1087 setMethod("tcrossprod", signature(x = "ddenseMatrix", y = "missing"),
80 :     function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))
81 : bates 686
82 :     setMethod("crossprod", signature(x = "ddenseMatrix", y = "missing"),
83 :     function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))
84 :    
85 :     setMethod("diag", signature(x = "ddenseMatrix"),
86 : maechler 2052 function(x, nrow, ncol) callGeneric(as(x, "dgeMatrix")))
87 : bates 686
88 : maechler 1945 setMethod("solve", signature(a = "ddenseMatrix", b = "missing"),
89 :     function(a, b, ...) callGeneric(as(a, "dgeMatrix")))
90 : bates 686
91 : maechler 1945 setMethod("solve", signature(a = "ddenseMatrix", b = "ANY"),
92 :     function(a, b, ...) callGeneric(as(a, "dgeMatrix"), b))
93 : bates 686
94 : bates 1463 ## General method for dense matrix multiplication in case specific methods
95 :     ## have not been defined.
96 :     setMethod("%*%", signature(x = "ddenseMatrix", y = "ddenseMatrix"),
97 :     function(x, y) .Call(dgeMatrix_matrix_mm,
98 :     .Call(dup_mMatrix_as_dgeMatrix, x), y, FALSE),
99 :     valueClass = "dgeMatrix")
100 :    
101 : bates 686 setMethod("lu", signature(x = "ddenseMatrix"),
102 :     function(x, ...) callGeneric(as(x, "dgeMatrix")))
103 :    
104 : maechler 1654 setMethod("chol", signature(x = "ddenseMatrix", pivot = "ANY"), cholMat)
105 :    
106 : maechler 856 setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "missing"),
107 : bates 686 function(x, logarithm, ...) callGeneric(as(x, "dgeMatrix")))
108 :    
109 : maechler 856 setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "logical"),
110 : bates 686 function(x, logarithm, ...)
111 :     callGeneric(as(x, "dgeMatrix"), logarithm))
112 :    
113 : maechler 954 ## now done for "dMatrix":
114 :     ## setMethod("expm", signature(x = "ddenseMatrix"),
115 :     ## function(x) callGeneric(as(x, "dgeMatrix")))
116 : bates 686
117 : maechler 1331 setMethod("Math",
118 :     signature(x = "ddenseMatrix"),
119 :     function(x) callGeneric(as(x, "dgeMatrix")))
120 :    
121 :    
122 : maechler 1575 ### FIXME: band() et al should be extended from "ddense" to "dense" !
123 :     ### However, needs much work to generalize dup_mMatrix_as_dgeMatrix()
124 : maechler 2112 ### --> use workaround below: go via "d"(ouble) and back
125 : maechler 1575
126 : maechler 2112 .trilDense <- function(x, k = 0, ...) {
127 :     k <- as.integer(k[1])
128 :     dd <- dim(x); sqr <- dd[1] == dd[2]
129 :     stopifnot(-dd[1] <= k, k <= dd[1]) # had k <= 0
130 :     ## returns "lower triangular" if k <= 0 && sqr
131 :     .Call(ddense_band, x, -dd[1], k)
132 :     }
133 : bates 1453 ## NB: have extra tril(), triu() methods for symmetric ["dsy" and "dsp"] and
134 :     ## for triangular ["dtr" and "dtp"]
135 : maechler 2112 setMethod("tril", "ddenseMatrix", .trilDense)
136 :     setMethod("tril", "matrix",
137 : bates 1453 function(x, k = 0, ...) {
138 : maechler 2112 if(is.double(x)) .trilDense(x, k)
139 :     else {
140 :     r <- .trilDense(x, k)
141 :     storage.mode(r) <- storage.mode(x)
142 :     r
143 :     }})
144 :     setMethod("tril", "denseMatrix",# all but ddense*
145 :     function(x, k = 0, ...)
146 :     as(.trilDense(as(x, "dMatrix"), k), class(x)))
147 : bates 1453
148 : maechler 2112 .triuDense <- function(x, k = 0, ...) {
149 :     k <- as.integer(k[1])
150 :     dd <- dim(x); sqr <- dd[1] == dd[2]
151 :     stopifnot(-dd[1] <= k, k <= dd[1]) # had k >= 0
152 :     ## returns "upper triangular" if k >= 0
153 :     .Call(ddense_band, x, k, dd[2])
154 :     }
155 :     setMethod("triu", "ddenseMatrix", .triuDense)
156 :     setMethod("triu", "matrix",
157 : bates 1453 function(x, k = 0, ...) {
158 : maechler 2112 if(is.double(x)) .triuDense(x, k)
159 :     else {
160 :     r <- .triuDense(x, k)
161 :     storage.mode(r) <- storage.mode(x)
162 :     r
163 :     }})
164 :     setMethod("triu", "denseMatrix",# all but ddense*
165 :     function(x, k = 0, ...)
166 :     as(.triuDense(as(x, "dMatrix"), k), class(x)))
167 : bates 1453
168 : maechler 2112 .bandDense <- function(x, k1, k2, ...) {
169 :     k1 <- as.integer(k1[1])
170 :     k2 <- as.integer(k2[1])
171 :     dd <- dim(x); sqr <- dd[1] == dd[2]
172 :     stopifnot(-dd[1] <= k1, k1 <= k2, k2 <= dd[1])
173 :     r <- .Call(ddense_band, x, k1, k2)
174 :     if (k1 < 0 && k1 == -k2 && isSymmetric(x)) ## symmetric
175 :     as(r, paste(.M.kind(x), "syMatrix", sep=''))
176 :     else
177 :     r
178 :     }
179 :    
180 :     setMethod("band", "ddenseMatrix", .bandDense)
181 :     setMethod("band", "matrix",
182 : bates 1453 function(x, k1, k2, ...) {
183 : maechler 2112 if(is.double(x)) .bandDense(x, k1, k2)
184 :     else {
185 :     r <- .bandDense(x, k1, k2)
186 :     storage.mode(r) <- storage.mode(x)
187 : bates 1453 r
188 : maechler 2112 }})
189 :     setMethod("band", "denseMatrix",# all but ddense*
190 :     function(x, k1, k2, ...)
191 :     as(.bandDense(as(x, "dMatrix"), k1, k2), class(x)))
192 :    
193 :     setMethod("symmpart", signature(x = "ddenseMatrix"),
194 :     function(x) .Call(ddense_symmpart, x))
195 :     setMethod("skewpart", signature(x = "ddenseMatrix"),
196 :     function(x) .Call(ddense_skewpart, x))
197 :    

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