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 3258 - (view) (download)

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 : mmaechler 3092 setAs("ddenseMatrix", "dgeMatrix", ..2dge)
8 : maechler 1575
9 : mmaechler 2199 setAs("ddenseMatrix", "matrix",
10 : mmaechler 3092 function(from) as(..2dge(from), "matrix"))
11 : maechler 2112
12 : maechler 1447 ## d(ouble) to l(ogical):
13 : maechler 1747 setAs("dgeMatrix", "lgeMatrix", function(from) d2l_Matrix(from, "dgeMatrix"))
14 :     setAs("dsyMatrix", "lsyMatrix", function(from) d2l_Matrix(from, "dsyMatrix"))
15 :     setAs("dspMatrix", "lspMatrix", function(from) d2l_Matrix(from, "dspMatrix"))
16 :     setAs("dtrMatrix", "ltrMatrix", function(from) d2l_Matrix(from, "dtrMatrix"))
17 :     setAs("dtpMatrix", "ltpMatrix", function(from) d2l_Matrix(from, "dtpMatrix"))
18 : maechler 956
19 : mmaechler 2551 if(FALSE) ## FIXME, this fails for ("dtpMatrix" -> "CsparseMatrix") where .dense2C() works
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 : mmaechler 3079 setMethod("as.numeric", "ddenseMatrix", function(x, ...) ..2dge(x)@x)
32 : maechler 1391
33 : maechler 792 ## -- see also ./Matrix.R e.g., for a show() method
34 :    
35 : bates 686 ## These methods are the 'fallback' methods for all dense numeric
36 : maechler 792 ## matrices in that they simply coerce the ddenseMatrix to a
37 :     ## dgeMatrix. Methods for special forms override these.
38 : bates 686
39 :     setMethod("norm", signature(x = "ddenseMatrix", type = "missing"),
40 : mmaechler 3079 function(x, type, ...) norm(..2dge(x)))
41 : bates 686
42 :     setMethod("norm", signature(x = "ddenseMatrix", type = "character"),
43 : mmaechler 3079 function(x, type, ...) norm(..2dge(x), type))
44 : bates 686
45 : maechler 2158 setMethod("rcond", signature(x = "ddenseMatrix", norm = "missing"),
46 : mmaechler 3079 function(x, norm, ...) rcond(..2dge(x), ...))
47 : bates 686
48 : maechler 2158 setMethod("rcond", signature(x = "ddenseMatrix", norm = "character"),
49 : mmaechler 3079 function(x, norm, ...) rcond(..2dge(x), norm, ...))
50 : bates 686
51 : maechler 946 ## Not really useful; now require *identical* class for result:
52 :     ## setMethod("t", signature(x = "ddenseMatrix"),
53 : mmaechler 3079 ## function(x) callGeneric(..2dge(x)))
54 : bates 686
55 : mmaechler 2811 ## "diag" --> specific methods for dge, dtr,dtp, dsy,dsp
56 : bates 686
57 : maechler 1945 setMethod("solve", signature(a = "ddenseMatrix", b = "missing"),
58 : mmaechler 3079 function(a, b, ...) solve(..2dge(a)))
59 : bates 686
60 : mmaechler 2472 for(.b in c("Matrix","ANY")) ## << against ambiguity notes
61 :     setMethod("solve", signature(a = "ddenseMatrix", b = .b),
62 : mmaechler 3079 function(a, b, ...) solve(..2dge(a), b))
63 : mmaechler 2472 for(.b in c("matrix","numeric")) ## << against ambiguity notes
64 :     setMethod("solve", signature(a = "ddenseMatrix", b = .b),
65 : mmaechler 3079 function(a, b, ...) solve(..2dge(a), Matrix(b)))
66 : mmaechler 2898 rm(.b)
67 : bates 686
68 :     setMethod("lu", signature(x = "ddenseMatrix"),
69 : mmaechler 2898 function(x, ...)
70 : mmaechler 3079 .set.factors(x, "LU", lu(..2dge(x), ...)))
71 : bates 686
72 : maechler 2154 setMethod("chol", signature(x = "ddenseMatrix"), cholMat)
73 : maechler 1654
74 : maechler 856 setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "missing"),
75 : mmaechler 3079 function(x, logarithm, ...) determinant(..2dge(x)))
76 : bates 686
77 : maechler 856 setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "logical"),
78 : maechler 2123 function(x, logarithm, ...)
79 : mmaechler 3079 determinant(..2dge(x), logarithm))
80 : bates 686
81 : maechler 954 ## now done for "dMatrix":
82 :     ## setMethod("expm", signature(x = "ddenseMatrix"),
83 : mmaechler 3079 ## function(x) callGeneric(..2dge(x)))
84 : bates 686
85 : maechler 1331
86 : maechler 2112 .trilDense <- function(x, k = 0, ...) {
87 :     k <- as.integer(k[1])
88 : maechler 2115 d <- dim(x)
89 :     stopifnot(-d[1] <= k, k <= d[1]) # had k <= 0
90 : maechler 2112 ## returns "lower triangular" if k <= 0 && sqr
91 : maechler 2115 .Call(dense_band, x, -d[1], k)
92 : maechler 2112 }
93 : bates 1453 ## NB: have extra tril(), triu() methods for symmetric ["dsy" and "dsp"] and
94 :     ## for triangular ["dtr" and "dtp"]
95 : maechler 2115 setMethod("tril", "denseMatrix", .trilDense)
96 :     setMethod("tril", "matrix", .trilDense)
97 : bates 1453
98 : maechler 2112 .triuDense <- function(x, k = 0, ...) {
99 :     k <- as.integer(k[1])
100 : maechler 2115 d <- dim(x)
101 :     stopifnot(-d[1] <= k, k <= d[1]) # had k >= 0
102 : maechler 2112 ## returns "upper triangular" if k >= 0
103 : maechler 2115 .Call(dense_band, x, k, d[2])
104 : maechler 2112 }
105 : maechler 2115 setMethod("triu", "denseMatrix", .triuDense)
106 :     setMethod("triu", "matrix", .triuDense)
107 : bates 1453
108 : maechler 2112 .bandDense <- function(x, k1, k2, ...) {
109 :     k1 <- as.integer(k1[1])
110 :     k2 <- as.integer(k2[1])
111 : mmaechler 2346 dd <- dim(x)
112 :     sqr <- dd[1] == dd[2]
113 :     stopifnot(-dd[1] <= k1, k1 <= k2, k2 <= dd[2])
114 : maechler 2115 r <- .Call(dense_band, x, k1, k2)
115 :     if (sqr && k1 < 0 && k1 == -k2 && isSymmetric(x)) ## symmetric
116 :     forceSymmetric(r)
117 : maechler 2112 else
118 :     r
119 :     }
120 : maechler 2115 setMethod("band", "denseMatrix", .bandDense)
121 :     setMethod("band", "matrix", .bandDense)
122 : maechler 2112
123 :    
124 :     setMethod("symmpart", signature(x = "ddenseMatrix"),
125 :     function(x) .Call(ddense_symmpart, x))
126 :     setMethod("skewpart", signature(x = "ddenseMatrix"),
127 :     function(x) .Call(ddense_skewpart, x))
128 :    
129 : mmaechler 2814
130 :     setMethod("is.finite", signature(x = "dgeMatrix"),
131 :     function(x) {
132 :     if(all(ifin <- is.finite(x@x)))
133 :     allTrueMat(x)
134 :     else if(any(ifin)) {
135 :     r <- as(x, "lMatrix") #-> logical x-slot
136 :     r@x <- ifin
137 :     as(r, "nMatrix")
138 :     }
139 :     else is.na_nsp(x)
140 :     })
141 :    
142 :     ## TODO? -- rather methods for specific subclasses of ddenseMatrix
143 :     setMethod("is.finite", signature(x = "ddenseMatrix"),
144 :     function(x) {
145 :     if(all(ifin <- is.finite(x@x))) return(allTrueMat(x))
146 :     ## *NOT* dge, i.e., either triangular or symmetric
147 :     ## (possibly packed): has finite 0-triangle
148 :     cdx <- getClassDef(class(x))
149 :    
150 :     r <- new(if(extends(cdx,"symmetricMatrix"))"nsyMatrix" else "ngeMatrix")
151 :     r@Dim <- (d <- x@Dim)
152 :     r@Dimnames <- x@Dimnames
153 :     isPacked <- (le <- prod(d)) > length(ifin)
154 :     r@x <- rep.int(TRUE, le)
155 :     iTr <- indTri(d[1], upper= x@uplo == "U", diag= TRUE)
156 :     if(isPacked) { ## x@x is "usable"
157 :     r@x[iTr] <- ifin
158 :     } else {
159 :     r@x[iTr] <- ifin[iTr]
160 :     }
161 :     r
162 :     })
163 :    
164 :     setMethod("is.infinite", signature(x = "ddenseMatrix"),
165 :     function(x) {
166 :     if(any((isInf <- is.infinite(x@x)))) {
167 :     r <- as(x, "lMatrix")#-> logical x-slot; 0 |--> FALSE
168 :     r@x <- isInf
169 :     as(r, "nMatrix")# often sparse .. better way?
170 :     }
171 :     else is.na_nsp(x)
172 :     })

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