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 1453, Sun Aug 27 16:16:35 2006 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    setAs("ddenseMatrix", "dgeMatrix",
7          function(from) .Call(dup_mMatrix_as_dgeMatrix, from))
8    
9    ## d(ouble) to l(ogical):
10    setAs("dgeMatrix", "lgeMatrix", d2l_Matrix)
11    setAs("dtrMatrix", "ltrMatrix", d2l_Matrix)
12    setAs("dtpMatrix", "ltpMatrix", d2l_Matrix)
13    setAs("dsyMatrix", "lsyMatrix", d2l_Matrix)
14    setAs("dspMatrix", "lspMatrix", d2l_Matrix)
15    
16    setMethod("as.numeric", signature(x = "ddenseMatrix"),
17              function(x, ...) as(x, "dgeMatrix")@x)
18    
19  ## -- see also ./Matrix.R  e.g., for a show() method  ## -- see also ./Matrix.R  e.g., for a show() method
20    
21  ## These methods are the 'fallback' methods for all dense numeric  ## These methods are the 'fallback' methods for all dense numeric
# Line 18  Line 34 
34  setMethod("rcond", signature(x = "ddenseMatrix", type = "character"),  setMethod("rcond", signature(x = "ddenseMatrix", type = "character"),
35            function(x, type, ...) callGeneric(as(x, "dgeMatrix"), type))            function(x, type, ...) callGeneric(as(x, "dgeMatrix"), type))
36    
37  setMethod("t", signature(x = "ddenseMatrix"),  ## Not really useful; now require *identical* class for result:
38            function(x) callGeneric(as(x, "dgeMatrix")))  ## setMethod("t", signature(x = "ddenseMatrix"),
39    ##        function(x) callGeneric(as(x, "dgeMatrix")))
40    
41  setMethod("tcrossprod", signature(x = "ddenseMatrix"),  setMethod("tcrossprod", signature(x = "ddenseMatrix", y = "missing"),
42            function(x) callGeneric(as(x, "dgeMatrix")))            function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))
43    
44  setMethod("crossprod", signature(x = "ddenseMatrix", y = "missing"),  setMethod("crossprod", signature(x = "ddenseMatrix", y = "missing"),
45            function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))            function(x, y = NULL) callGeneric(as(x, "dgeMatrix")))
# Line 46  Line 63 
63            function(x, logarithm, ...)            function(x, logarithm, ...)
64            callGeneric(as(x, "dgeMatrix"), logarithm))            callGeneric(as(x, "dgeMatrix"), logarithm))
65    
66  setMethod("expm", signature(x = "ddenseMatrix"),  ## now done for "dMatrix":
67            function(x) callGeneric(as(x, "dgeMatrix")))  ## setMethod("expm", signature(x = "ddenseMatrix"),
68    ##           function(x) callGeneric(as(x, "dgeMatrix")))
69    
70  setMethod("Schur", signature(x = "ddenseMatrix", vectors = "missing"),  setMethod("Schur", signature(x = "ddenseMatrix", vectors = "missing"),
71            function(x, vectors, ...) callGeneric(as(x, "dgeMatrix")))            function(x, vectors, ...) callGeneric(as(x, "dgeMatrix")))
# Line 56  Line 74 
74            function(x, vectors, ...) callGeneric(as(x, "dgeMatrix"), vectors))            function(x, vectors, ...) callGeneric(as(x, "dgeMatrix"), vectors))
75    
76    
77    ## Cheap version: work via "dgeMatrix" and use the group methods there:
78    ## FIXME(?): try to preserve "symmetric", "triangular", ...
79    setMethod("Arith", ##  "+", "-", "*", "^", "%%", "%/%", "/"
80              signature(e1 = "ddenseMatrix", e2 = "ddenseMatrix"),
81              function(e1, e2) callGeneric(as(e1, "dgeMatrix"),
82                                           as(e2, "dgeMatrix")))
83    setMethod("Arith",
84              signature(e1 = "ddenseMatrix", e2 = "numeric"),
85              function(e1, e2) callGeneric(as(e1, "dgeMatrix"), e2))
86    setMethod("Arith",
87              signature(e1 = "numeric", e2 = "ddenseMatrix"),
88              function(e1, e2) callGeneric(e1, as(e2, "dgeMatrix")))
89    
90    setMethod("Math",
91              signature(x = "ddenseMatrix"),
92              function(x) callGeneric(as(x, "dgeMatrix")))
93    
94    
95    
96    ### for R 2.2.x (and later): -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
97    
98    ### cbind2
99    setMethod("cbind2", signature(x = "ddenseMatrix", y = "numeric"),
100              function(x, y) {
101                  d <- dim(x); nr <- d[1]; nc <- d[2]
102                  y <- rep(y, length.out = nr) # 'silent procrustes'
103                  ## beware of (packed) triangular, symmetric, ...
104                  x <- as(x, "dgeMatrix")
105                  x@x <- c(x@x, as.double(y))
106                  x@Dim[2] <- nc + 1:1
107                  if(is.character(dn <- x@Dimnames[[2]]))
108                      x@Dimnames[[2]] <- c(dn, "")
109                  x
110              })
111    ## the same, (x,y) <-> (y,x):
112    setMethod("cbind2", signature(x = "numeric", y = "ddenseMatrix"),
113              function(x, y) {
114                  d <- dim(y); nr <- d[1]; nc <- d[2]
115                  x <- rep(x, length.out = nr)
116                  y <- as(y, "dgeMatrix")
117                  y@x <- c(as.double(x), y@x)
118                  y@Dim[2] <- nc + 1:1
119                  if(is.character(dn <- y@Dimnames[[2]]))
120                      y@Dimnames[[2]] <- c("", dn)
121                  y
122              })
123    
124    setMethod("cbind2", signature(x = "ddenseMatrix", y = "matrix"),
125              function(x, y) callGeneric(x, as(y, "dgeMatrix")))
126    setMethod("cbind2", signature(x = "matrix", y = "ddenseMatrix"),
127              function(x, y) callGeneric(as(x, "dgeMatrix"), y))
128    
129    setMethod("cbind2", signature(x = "ddenseMatrix", y = "ddenseMatrix"),
130              function(x, y) {
131                  nr <- rowCheck(x,y)
132                  ncx <- x@Dim[2]
133                  ncy <- y@Dim[2]
134                  ## beware of (packed) triangular, symmetric, ...
135                  hasDN <- !is.null(dnx <- dimnames(x)) |
136                  !is.null(dny <- dimnames(y))
137                  x <- as(x, "dgeMatrix")
138                  y <- as(y, "dgeMatrix")
139                  x@x <- c(x@x, y@x)
140                  x@Dim[2] <- ncx + ncy
141                  if(hasDN) {
142                      ## R and S+ are different in which names they take
143                      ## if they differ -- but there's no warning in any case
144                      rn <- if(!is.null(dnx[[1]])) dnx[[1]] else dny[[1]]
145                      cx <- dnx[[2]] ; cy <- dny[[2]]
146                      cn <- if(is.null(cx) && is.null(cy)) NULL
147                      else c(if(!is.null(cx)) cx else rep.int("", ncx),
148                             if(!is.null(cy)) cy else rep.int("", ncy))
149                      x@Dimnames <- list(rn, cn)
150                  }
151                  x
152              })
153    
154    ### rbind2 -- analogous to cbind2 --- more to do for @x though:
155    
156    setMethod("rbind2", signature(x = "ddenseMatrix", y = "numeric"),
157              function(x, y) {
158                  if(is.character(dn <- x@Dimnames[[1]])) dn <- c(dn, "")
159                  new("dgeMatrix", Dim = x@Dim + 1:0,
160                      Dimnames = list(dn, x@Dimnames[[2]]),
161                      x = c(rbind2(as(x,"matrix"), y)))
162              })
163    ## the same, (x,y) <-> (y,x):
164    setMethod("rbind2", signature(x = "numeric", y = "ddenseMatrix"),
165              function(x, y) {
166                  if(is.character(dn <- y@Dimnames[[1]])) dn <- c("", dn)
167                  new("dgeMatrix", Dim = y@Dim + 1:0,
168                      Dimnames = list(dn, y@Dimnames[[2]]),
169                      x = c(rbind2(x, as(y,"matrix"))))
170              })
171    
172    setMethod("rbind2", signature(x = "ddenseMatrix", y = "matrix"),
173              function(x, y) callGeneric(x, as(y, "dgeMatrix")))
174    setMethod("rbind2", signature(x = "matrix", y = "ddenseMatrix"),
175              function(x, y) callGeneric(as(x, "dgeMatrix"), y))
176    
177    setMethod("rbind2", signature(x = "ddenseMatrix", y = "ddenseMatrix"),
178              function(x, y) {
179                  nc <- colCheck(x,y)
180                  nrx <- x@Dim[1]
181                  nry <- y@Dim[1]
182                  dn <-
183                      if(!is.null(dnx <- dimnames(x)) |
184                         !is.null(dny <- dimnames(y))) {
185                          ## R and S+ are different in which names they take
186                          ## if they differ -- but there's no warning in any case
187                          list(if(is.null(rx <- dnx[[1]]) && is.null(ry <- dny[[1]]))
188                               NULL else
189                               c(if(!is.null(rx)) rx else rep.int("", nrx),
190                                 if(!is.null(ry)) ry else rep.int("", nry)),
191                               if(!is.null(dnx[[2]])) dnx[[2]] else dny[[2]])
192    
193                      } else list(NULL, NULL)
194                  ## beware of (packed) triangular, symmetric, ...
195                  new("dgeMatrix", Dim = c(nrx + nry, nc), Dimnames = dn,
196                      x = c(rbind2(as(x,"matrix"), as(y,"matrix"))))
197              })
198    ## NB: have extra tril(), triu() methods for symmetric ["dsy" and "dsp"] and
199    ##     for triangular ["dtr" and "dtp"]
200    setMethod("tril", "ddenseMatrix",
201              function(x, k = 0, ...) {
202                  k <- as.integer(k[1])
203                  dd <- dim(x); sqr <- dd[1] == dd[2]
204                  stopifnot(-dd[1] <= k, k <= dd[1]) # had k <= 0
205                  ## returns "lower triangular" if k <= 0 && sqr
206                  .Call(ddense_band, x, -dd[1], k)
207              })
208    
209    setMethod("triu", "ddenseMatrix",
210              function(x, k = 0, ...) {
211                  k <- as.integer(k[1])
212                  dd <- dim(x); sqr <- dd[1] == dd[2]
213                  stopifnot(-dd[1] <= k, k <= dd[1]) # had k >= 0
214                  ## returns "upper triangular" if k >= 0
215                  .Call(ddense_band, x, k, dd[2])
216              })
217    
218    setMethod("band", "ddenseMatrix",
219              function(x, k1, k2, ...) {
220                  k1 <- as.integer(k1[1])
221                  k2 <- as.integer(k2[1])
222                  dd <- dim(x); sqr <- dd[1] == dd[2]
223                  stopifnot(-dd[1] <= k1, k1 <= k2, k2 <= dd[1])
224                  r <- .Call(ddense_band, x, k1, k2)
225                  if (k1 < 0  &&  k1 == -k2  && isSymmetric(x)) ## symmetric
226                      as(r, paste(.M.kind(x), "syMatrix", sep=''))
227                  else
228                      r
229              })

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

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