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 2472 - (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 :     setMethod("diag", signature(x = "ddenseMatrix"),
79 : maechler 2123 function(x, nrow, ncol) diag(as(x, "dgeMatrix")))
80 : bates 686
81 : maechler 1945 setMethod("solve", signature(a = "ddenseMatrix", b = "missing"),
82 : maechler 2123 function(a, b, ...) solve(as(a, "dgeMatrix")))
83 : bates 686
84 : mmaechler 2472 for(.b in c("Matrix","ANY")) ## << against ambiguity notes
85 :     setMethod("solve", signature(a = "ddenseMatrix", b = .b),
86 :     function(a, b, ...) solve(as(a, "dgeMatrix"), b))
87 :     for(.b in c("matrix","numeric")) ## << against ambiguity notes
88 :     setMethod("solve", signature(a = "ddenseMatrix", b = .b),
89 :     function(a, b, ...) solve(as(a, "dgeMatrix"), Matrix(b)))
90 : bates 686
91 :     setMethod("lu", signature(x = "ddenseMatrix"),
92 : maechler 2123 function(x, ...) lu(as(x, "dgeMatrix")))
93 : bates 686
94 : maechler 2154 setMethod("chol", signature(x = "ddenseMatrix"), cholMat)
95 : maechler 1654
96 : maechler 856 setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "missing"),
97 : maechler 2123 function(x, logarithm, ...) determinant(as(x, "dgeMatrix")))
98 : bates 686
99 : maechler 856 setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "logical"),
100 : maechler 2123 function(x, logarithm, ...)
101 :     determinant(as(x, "dgeMatrix"), logarithm))
102 : bates 686
103 : maechler 954 ## now done for "dMatrix":
104 :     ## setMethod("expm", signature(x = "ddenseMatrix"),
105 :     ## function(x) callGeneric(as(x, "dgeMatrix")))
106 : bates 686
107 : maechler 1331 setMethod("Math",
108 :     signature(x = "ddenseMatrix"),
109 :     function(x) callGeneric(as(x, "dgeMatrix")))
110 :    
111 :    
112 : maechler 1575
113 : maechler 2112 .trilDense <- function(x, k = 0, ...) {
114 :     k <- as.integer(k[1])
115 : maechler 2115 d <- dim(x)
116 :     stopifnot(-d[1] <= k, k <= d[1]) # had k <= 0
117 : maechler 2112 ## returns "lower triangular" if k <= 0 && sqr
118 : maechler 2115 .Call(dense_band, x, -d[1], k)
119 : maechler 2112 }
120 : bates 1453 ## NB: have extra tril(), triu() methods for symmetric ["dsy" and "dsp"] and
121 :     ## for triangular ["dtr" and "dtp"]
122 : maechler 2115 setMethod("tril", "denseMatrix", .trilDense)
123 :     setMethod("tril", "matrix", .trilDense)
124 : bates 1453
125 : maechler 2112 .triuDense <- function(x, k = 0, ...) {
126 :     k <- as.integer(k[1])
127 : maechler 2115 d <- dim(x)
128 :     stopifnot(-d[1] <= k, k <= d[1]) # had k >= 0
129 : maechler 2112 ## returns "upper triangular" if k >= 0
130 : maechler 2115 .Call(dense_band, x, k, d[2])
131 : maechler 2112 }
132 : maechler 2115 setMethod("triu", "denseMatrix", .triuDense)
133 :     setMethod("triu", "matrix", .triuDense)
134 : bates 1453
135 : maechler 2112 .bandDense <- function(x, k1, k2, ...) {
136 :     k1 <- as.integer(k1[1])
137 :     k2 <- as.integer(k2[1])
138 : mmaechler 2346 dd <- dim(x)
139 :     sqr <- dd[1] == dd[2]
140 :     stopifnot(-dd[1] <= k1, k1 <= k2, k2 <= dd[2])
141 : maechler 2115 r <- .Call(dense_band, x, k1, k2)
142 :     if (sqr && k1 < 0 && k1 == -k2 && isSymmetric(x)) ## symmetric
143 :     forceSymmetric(r)
144 : maechler 2112 else
145 :     r
146 :     }
147 : maechler 2115 setMethod("band", "denseMatrix", .bandDense)
148 :     setMethod("band", "matrix", .bandDense)
149 : maechler 2112
150 :    
151 :     setMethod("symmpart", signature(x = "ddenseMatrix"),
152 :     function(x) .Call(ddense_symmpart, x))
153 :     setMethod("skewpart", signature(x = "ddenseMatrix"),
154 :     function(x) .Call(ddense_skewpart, x))
155 :    

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