SCM

SCM Repository

[matrix] Diff of /pkg/R/ddenseMatrix.R
ViewVC logotype

Diff of /pkg/R/ddenseMatrix.R

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 856, Tue Aug 16 20:20:13 2005 UTC revision 1799, Sat Mar 24 14:54:00 2007 UTC
# Line 1  Line 1 
1  ### Define Methods that can be inherited for all subclasses  ### Define Methods that can be inherited for all subclasses
2    
3    ## This replaces many "d..Matrix" -> "dgeMatrix" ones
4    ## >> but << needs all sub(sub(sub)) classes of "ddenseMatrix" listed
5    ##   -----  in  ../src/Mutils.c
6    
7    setAs("ddenseMatrix", "dgeMatrix",
8          function(from) {
9              if (class(from) != "dgeMatrix")
10                  .Call(dup_mMatrix_as_dgeMatrix, from)
11              else from
12          })
13    
14    ## d(ouble) to l(ogical):
15    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    
21    setAs("ddenseMatrix", "CsparseMatrix",
22          function(from) {
23              if (class(from) != "dgeMatrix") # don't lose symmetry/triangularity/...
24                  as_Csparse(from)
25              else .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          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    
41    
42    ## 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    setMethod("as.numeric", signature(x = "ddenseMatrix"),
54              function(x, ...) as(x, "dgeMatrix")@x)
55    
56  ## -- see also ./Matrix.R  e.g., for a show() method  ## -- see also ./Matrix.R  e.g., for a show() method
57    
58  ## These methods are the 'fallback' methods for all dense numeric  ## These methods are the 'fallback' methods for all dense numeric
# Line 18  Line 71 
71  setMethod("rcond", signature(x = "ddenseMatrix", type = "character"),  setMethod("rcond", signature(x = "ddenseMatrix", type = "character"),
72            function(x, type, ...) callGeneric(as(x, "dgeMatrix"), type))            function(x, type, ...) callGeneric(as(x, "dgeMatrix"), type))
73    
74  setMethod("t", signature(x = "ddenseMatrix"),  ## Not really useful; now require *identical* class for result:
75            function(x) callGeneric(as(x, "dgeMatrix")))  ## setMethod("t", signature(x = "ddenseMatrix"),
76    ##        function(x) callGeneric(as(x, "dgeMatrix")))
77    
78  setMethod("tcrossprod", signature(x = "ddenseMatrix"),  setMethod("tcrossprod", signature(x = "ddenseMatrix", y = "missing"),
79            function(x) callGeneric(as(x, "dgeMatrix")))            function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))
80    
81  setMethod("crossprod", signature(x = "ddenseMatrix", y = "missing"),  setMethod("crossprod", signature(x = "ddenseMatrix", y = "missing"),
82            function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))            function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))
83    
84  setMethod("diag", signature(x = "ddenseMatrix"),  setMethod("diag", signature(x = "ddenseMatrix"),
85            function(x = 1, nrow, ncol = n) callGeneric(as(x, "dgeMatrix")))            function(x, nrow, ncol = n) callGeneric(as(x, "dgeMatrix")))
   
 setMethod("solve", signature(a = "ddenseMatrix", b = "missing"),  
           function(a, b, ...) callGeneric(as(a, "dgeMatrix")))  
86    
87  setMethod("solve", signature(a = "ddenseMatrix", b = "ANY"),  ## These methods cause an infinite loop in pre-2.4.0
88            function(a, b, ...) callGeneric(as(a, "dgeMatrix"), b))  ## setMethod("solve", signature(a = "ddenseMatrix", b = "missing"),
89    ##           function(a, b, ...) callGeneric(as(a, "dgeMatrix")))
90    
91    ## setMethod("solve", signature(a = "ddenseMatrix", b = "ANY"),
92    ##           function(a, b, ...) callGeneric(as(a, "dgeMatrix"), b))
93    
94    ## 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  setMethod("lu", signature(x = "ddenseMatrix"),  setMethod("lu", signature(x = "ddenseMatrix"),
102            function(x, ...) callGeneric(as(x, "dgeMatrix")))            function(x, ...) callGeneric(as(x, "dgeMatrix")))
103    
104    setMethod("chol", signature(x = "ddenseMatrix", pivot = "ANY"), cholMat)
105    
106  setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "missing"),  setMethod("determinant", signature(x = "ddenseMatrix", logarithm = "missing"),
107            function(x, logarithm, ...) callGeneric(as(x, "dgeMatrix")))            function(x, logarithm, ...) callGeneric(as(x, "dgeMatrix")))
108    
# Line 46  Line 110 
110            function(x, logarithm, ...)            function(x, logarithm, ...)
111            callGeneric(as(x, "dgeMatrix"), logarithm))            callGeneric(as(x, "dgeMatrix"), logarithm))
112    
113  setMethod("expm", signature(x = "ddenseMatrix"),  ## now done for "dMatrix":
114            function(x) callGeneric(as(x, "dgeMatrix")))  ## setMethod("expm", signature(x = "ddenseMatrix"),
115    ##           function(x) callGeneric(as(x, "dgeMatrix")))
116    
117  setMethod("Schur", signature(x = "ddenseMatrix", vectors = "missing"),  setMethod("Schur", signature(x = "ddenseMatrix", vectors = "missing"),
118            function(x, vectors, ...) callGeneric(as(x, "dgeMatrix")))            function(x, vectors, ...) callGeneric(as(x, "dgeMatrix")))
# Line 56  Line 121 
121            function(x, vectors, ...) callGeneric(as(x, "dgeMatrix"), vectors))            function(x, vectors, ...) callGeneric(as(x, "dgeMatrix"), vectors))
122    
123    
124    setMethod("Math",
125              signature(x = "ddenseMatrix"),
126              function(x) callGeneric(as(x, "dgeMatrix")))
127    
128    
129    ### FIXME: band() et al should be extended from "ddense" to "dense" !
130    ###        However, needs much work to generalize dup_mMatrix_as_dgeMatrix()
131    
132    ## 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              })

Legend:
Removed from v.856  
changed lines
  Added in v.1799

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