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 1747 - (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 1447 ## d(ouble) to l(ogical):
15 : maechler 1747 setAs("dgeMatrix", "lgeMatrix", function(from) d2l_Matrix(from, "dgeMatrix"))
16 :     setAs("dsyMatrix", "lsyMatrix", function(from) d2l_Matrix(from, "dsyMatrix"))
17 :     setAs("dspMatrix", "lspMatrix", function(from) d2l_Matrix(from, "dspMatrix"))
18 :     setAs("dtrMatrix", "ltrMatrix", function(from) d2l_Matrix(from, "dtrMatrix"))
19 :     setAs("dtpMatrix", "ltpMatrix", function(from) d2l_Matrix(from, "dtpMatrix"))
20 : maechler 956
21 : bates 1460 setAs("ddenseMatrix", "CsparseMatrix",
22 :     function(from) {
23 :     if (class(from) != "dgeMatrix")
24 :     from <- .Call(dup_mMatrix_as_dgeMatrix, from)
25 :     .Call(dense_to_Csparse, from)
26 :     })
27 :    
28 :     ## special case
29 :     setAs("dgeMatrix", "dgCMatrix",
30 :     function(from) .Call(dense_to_Csparse, from))
31 :    
32 :     setAs("matrix", "CsparseMatrix",
33 : maechler 1618 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 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix")))
64 :    
65 :     setMethod("norm", signature(x = "ddenseMatrix", type = "character"),
66 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix"), type))
67 :    
68 :     setMethod("rcond", signature(x = "ddenseMatrix", type = "missing"),
69 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix")))
70 :    
71 :     setMethod("rcond", signature(x = "ddenseMatrix", type = "character"),
72 :     function(x, type, ...) callGeneric(as(x, "dgeMatrix"), type))
73 :    
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 :     function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))
80 : bates 686
81 :     setMethod("crossprod", signature(x = "ddenseMatrix", y = "missing"),
82 :     function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))
83 :    
84 :     setMethod("diag", signature(x = "ddenseMatrix"),
85 : maechler 1747 function(x, nrow, ncol = n) callGeneric(as(x, "dgeMatrix")))
86 : bates 686
87 : bates 1463 ## These methods cause an infinite loop in pre-2.4.0
88 :     ## setMethod("solve", signature(a = "ddenseMatrix", b = "missing"),
89 :     ## function(a, b, ...) callGeneric(as(a, "dgeMatrix")))
90 : bates 686
91 : bates 1463 ## 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 :     setMethod("Schur", signature(x = "ddenseMatrix", vectors = "missing"),
118 :     function(x, vectors, ...) callGeneric(as(x, "dgeMatrix")))
119 :    
120 :     setMethod("Schur", signature(x = "ddenseMatrix", vectors = "logical"),
121 :     function(x, vectors, ...) callGeneric(as(x, "dgeMatrix"), vectors))
122 :    
123 : maechler 792
124 : maechler 1331 setMethod("Math",
125 :     signature(x = "ddenseMatrix"),
126 :     function(x) callGeneric(as(x, "dgeMatrix")))
127 :    
128 :    
129 : maechler 1575 ### FIXME: band() et al should be extended from "ddense" to "dense" !
130 :     ### However, needs much work to generalize dup_mMatrix_as_dgeMatrix()
131 :    
132 : bates 1453 ## NB: have extra tril(), triu() methods for symmetric ["dsy" and "dsp"] and
133 :     ## for triangular ["dtr" and "dtp"]
134 :     setMethod("tril", "ddenseMatrix",
135 :     function(x, k = 0, ...) {
136 :     k <- as.integer(k[1])
137 :     dd <- dim(x); sqr <- dd[1] == dd[2]
138 :     stopifnot(-dd[1] <= k, k <= dd[1]) # had k <= 0
139 :     ## returns "lower triangular" if k <= 0 && sqr
140 :     .Call(ddense_band, x, -dd[1], k)
141 :     })
142 :    
143 :     setMethod("triu", "ddenseMatrix",
144 :     function(x, k = 0, ...) {
145 :     k <- as.integer(k[1])
146 :     dd <- dim(x); sqr <- dd[1] == dd[2]
147 :     stopifnot(-dd[1] <= k, k <= dd[1]) # had k >= 0
148 :     ## returns "upper triangular" if k >= 0
149 :     .Call(ddense_band, x, k, dd[2])
150 :     })
151 :    
152 :     setMethod("band", "ddenseMatrix",
153 :     function(x, k1, k2, ...) {
154 :     k1 <- as.integer(k1[1])
155 :     k2 <- as.integer(k2[1])
156 :     dd <- dim(x); sqr <- dd[1] == dd[2]
157 :     stopifnot(-dd[1] <= k1, k1 <= k2, k2 <= dd[1])
158 :     r <- .Call(ddense_band, x, k1, k2)
159 :     if (k1 < 0 && k1 == -k2 && isSymmetric(x)) ## symmetric
160 :     as(r, paste(.M.kind(x), "syMatrix", sep=''))
161 :     else
162 :     r
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