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

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