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 2154 - (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 2115 function(from) .Call(dense_to_Csparse, from))
35 :     ## function(from) {
36 :     ## if(is.numeric(from))
37 :     ## .Call(dense_to_Csparse, .Call(dup_mMatrix_as_dgeMatrix, from))
38 :     ## else if(is.logical(from)) ## FIXME: this works, but maybe wastefully
39 :     ## as(Matrix(from, sparse=TRUE), "CsparseMatrix")
40 :     ## else stop('not-yet-implemented coercion to "CsparseMatrix"')
41 :     ## })
42 : bates 1460
43 : maechler 1618
44 : bates 1460 ## special case needed in the Matrix function
45 :     setAs("matrix", "dgCMatrix",
46 :     function(from) {
47 :     storage.mode(from) <- "double"
48 :     .Call(dense_to_Csparse, from)
49 :     })
50 :    
51 :     setAs("numeric", "CsparseMatrix",
52 :     function(from)
53 :     .Call(dense_to_Csparse, .Call(dup_mMatrix_as_dgeMatrix, from)))
54 :    
55 : maechler 1391 setMethod("as.numeric", signature(x = "ddenseMatrix"),
56 :     function(x, ...) as(x, "dgeMatrix")@x)
57 :    
58 : maechler 792 ## -- see also ./Matrix.R e.g., for a show() method
59 :    
60 : bates 686 ## These methods are the 'fallback' methods for all dense numeric
61 : maechler 792 ## matrices in that they simply coerce the ddenseMatrix to a
62 :     ## dgeMatrix. Methods for special forms override these.
63 : bates 686
64 :     setMethod("norm", signature(x = "ddenseMatrix", type = "missing"),
65 : maechler 2123 function(x, type, ...) norm(as(x, "dgeMatrix")))
66 : bates 686
67 :     setMethod("norm", signature(x = "ddenseMatrix", type = "character"),
68 : maechler 2123 function(x, type, ...) norm(as(x, "dgeMatrix"), type))
69 : bates 686
70 :     setMethod("rcond", signature(x = "ddenseMatrix", type = "missing"),
71 : maechler 2123 function(x, type, ...) rcond(as(x, "dgeMatrix")))
72 : bates 686
73 :     setMethod("rcond", signature(x = "ddenseMatrix", type = "character"),
74 : maechler 2123 function(x, type, ...) rcond(as(x, "dgeMatrix"), type))
75 : bates 686
76 : maechler 946 ## Not really useful; now require *identical* class for result:
77 :     ## setMethod("t", signature(x = "ddenseMatrix"),
78 :     ## function(x) callGeneric(as(x, "dgeMatrix")))
79 : bates 686
80 : maechler 1087 setMethod("tcrossprod", signature(x = "ddenseMatrix", y = "missing"),
81 : maechler 2123 function(x, y = NULL) tcrossprod(as(x, "dgeMatrix")))
82 : bates 686
83 :     setMethod("crossprod", signature(x = "ddenseMatrix", y = "missing"),
84 : maechler 2123 function(x, y = NULL) crossprod(as(x, "dgeMatrix")))
85 : bates 686
86 :     setMethod("diag", signature(x = "ddenseMatrix"),
87 : maechler 2123 function(x, nrow, ncol) diag(as(x, "dgeMatrix")))
88 : bates 686
89 : maechler 1945 setMethod("solve", signature(a = "ddenseMatrix", b = "missing"),
90 : maechler 2123 function(a, b, ...) solve(as(a, "dgeMatrix")))
91 : bates 686
92 : maechler 1945 setMethod("solve", signature(a = "ddenseMatrix", b = "ANY"),
93 : maechler 2123 function(a, b, ...) solve(as(a, "dgeMatrix"), b))
94 : bates 686
95 : bates 1463 ## General method for dense matrix multiplication in case specific methods
96 :     ## have not been defined.
97 :     setMethod("%*%", signature(x = "ddenseMatrix", y = "ddenseMatrix"),
98 :     function(x, y) .Call(dgeMatrix_matrix_mm,
99 :     .Call(dup_mMatrix_as_dgeMatrix, x), y, FALSE),
100 :     valueClass = "dgeMatrix")
101 :    
102 : bates 686 setMethod("lu", signature(x = "ddenseMatrix"),
103 : maechler 2123 function(x, ...) lu(as(x, "dgeMatrix")))
104 : bates 686
105 : maechler 2154 setMethod("chol", signature(x = "ddenseMatrix"), cholMat)
106 : maechler 1654
107 : maechler 856 setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "missing"),
108 : maechler 2123 function(x, logarithm, ...) determinant(as(x, "dgeMatrix")))
109 : bates 686
110 : maechler 856 setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "logical"),
111 : maechler 2123 function(x, logarithm, ...)
112 :     determinant(as(x, "dgeMatrix"), logarithm))
113 : bates 686
114 : maechler 954 ## now done for "dMatrix":
115 :     ## setMethod("expm", signature(x = "ddenseMatrix"),
116 :     ## function(x) callGeneric(as(x, "dgeMatrix")))
117 : bates 686
118 : maechler 1331 setMethod("Math",
119 :     signature(x = "ddenseMatrix"),
120 :     function(x) callGeneric(as(x, "dgeMatrix")))
121 :    
122 :    
123 : maechler 1575
124 : maechler 2112 .trilDense <- function(x, k = 0, ...) {
125 :     k <- as.integer(k[1])
126 : maechler 2115 d <- dim(x)
127 :     stopifnot(-d[1] <= k, k <= d[1]) # had k <= 0
128 : maechler 2112 ## returns "lower triangular" if k <= 0 && sqr
129 : maechler 2115 .Call(dense_band, x, -d[1], k)
130 : maechler 2112 }
131 : bates 1453 ## NB: have extra tril(), triu() methods for symmetric ["dsy" and "dsp"] and
132 :     ## for triangular ["dtr" and "dtp"]
133 : maechler 2115 setMethod("tril", "denseMatrix", .trilDense)
134 :     setMethod("tril", "matrix", .trilDense)
135 : bates 1453
136 : maechler 2112 .triuDense <- function(x, k = 0, ...) {
137 :     k <- as.integer(k[1])
138 : maechler 2115 d <- dim(x)
139 :     stopifnot(-d[1] <= k, k <= d[1]) # had k >= 0
140 : maechler 2112 ## returns "upper triangular" if k >= 0
141 : maechler 2115 .Call(dense_band, x, k, d[2])
142 : maechler 2112 }
143 : maechler 2115 setMethod("triu", "denseMatrix", .triuDense)
144 :     setMethod("triu", "matrix", .triuDense)
145 : bates 1453
146 : maechler 2112 .bandDense <- function(x, k1, k2, ...) {
147 :     k1 <- as.integer(k1[1])
148 :     k2 <- as.integer(k2[1])
149 :     dd <- dim(x); sqr <- dd[1] == dd[2]
150 :     stopifnot(-dd[1] <= k1, k1 <= k2, k2 <= dd[1])
151 : maechler 2115 r <- .Call(dense_band, x, k1, k2)
152 :     if (sqr && k1 < 0 && k1 == -k2 && isSymmetric(x)) ## symmetric
153 :     forceSymmetric(r)
154 : maechler 2112 else
155 :     r
156 :     }
157 : maechler 2115 setMethod("band", "denseMatrix", .bandDense)
158 :     setMethod("band", "matrix", .bandDense)
159 : maechler 2112
160 :    
161 :     setMethod("symmpart", signature(x = "ddenseMatrix"),
162 :     function(x) .Call(ddense_symmpart, x))
163 :     setMethod("skewpart", signature(x = "ddenseMatrix"),
164 :     function(x) .Call(ddense_skewpart, x))
165 :    

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