SCM

SCM Repository

[matrix] Annotation of /pkg/Matrix/R/diagMatrix.R
ViewVC logotype

Annotation of /pkg/Matrix/R/diagMatrix.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2136 - (view) (download)
Original Path: pkg/R/diagMatrix.R

1 : maechler 1617 #### All methods for "diagonalMatrix" and its subclasses,
2 :     #### currently "ddiMatrix", "ldiMatrix"
3 :    
4 : maechler 1109 ## Purpose: Constructor of diagonal matrices -- ~= diag() ,
5 :     ## but *not* diag() extractor!
6 :     Diagonal <- function(n, x = NULL)
7 :     {
8 : maechler 1575 ## Allow Diagonal(4) and Diagonal(x=1:5)
9 : maechler 1109 if(missing(n))
10 : maechler 1575 n <- length(x)
11 : maechler 1109 else {
12 : maechler 1575 stopifnot(length(n) == 1, n == as.integer(n), n >= 0)
13 :     n <- as.integer(n)
14 : maechler 1109 }
15 :    
16 : maechler 1654 if(missing(x)) ## unit diagonal matrix
17 : maechler 1575 new("ddiMatrix", Dim = c(n,n), diag = "U")
18 : maechler 1109 else {
19 : maechler 2128 lx <- length(x)
20 :     stopifnot(lx == 1 || lx == n) # but keep 'x' short for now
21 : maechler 1575 if(is.logical(x))
22 :     cl <- "ldiMatrix"
23 :     else if(is.numeric(x)) {
24 :     cl <- "ddiMatrix"
25 :     x <- as.numeric(x)
26 :     }
27 :     else if(is.complex(x)) {
28 :     cl <- "zdiMatrix" # will not yet work
29 :     } else stop("'x' has invalid data type")
30 : maechler 2128 new(cl, Dim = c(n,n), diag = "N",
31 :     x = if(lx == 1) rep.int(x,n) else x)
32 : maechler 1109 }
33 :     }
34 :    
35 : maechler 1617 ### This is modified from a post of Bert Gunter to R-help on 1 Sep 2005.
36 :     ### Bert's code built on a post by Andy Liaw who most probably was influenced
37 :     ### by earlier posts, notably one by Scott Chasalow on S-news, 16 Jan 2002
38 :     ### who posted his bdiag() function written in December 1995.
39 :    
40 :     bdiag <- function(...) {
41 :     if(nargs() == 0) return(new("dgCMatrix"))
42 :     ## else :
43 :     mlist <- if (nargs() == 1) as.list(...) else list(...)
44 :     dims <- sapply(mlist, dim)
45 :     ## make sure we had all matrices:
46 :     if(!(is.matrix(dims) && nrow(dims) == 2))
47 :     stop("some arguments are not matrices")
48 : maechler 1845 csdim <- rbind(rep.int(0L, 2),
49 : maechler 1617 apply(sapply(mlist, dim), 1, cumsum))
50 :     ret <- new("dgTMatrix", Dim = as.integer(csdim[nrow(csdim),]))
51 :     add1 <- matrix(1:0, 2,2)
52 : maechler 1654 for(i in seq_along(mlist)) {
53 : maechler 1617 indx <- apply(csdim[i:(i+1),] + add1, 2, function(n) n[1]:n[2])
54 :     if(is.null(dim(indx))) ## non-square matrix
55 :     ret[indx[[1]],indx[[2]]] <- mlist[[i]]
56 :     else ## square matrix
57 :     ret[indx[,1],indx[,2]] <- mlist[[i]]
58 :     }
59 :     ## slightly debatable if we really should return Csparse.. :
60 :     as(ret, "CsparseMatrix")
61 :     }
62 :    
63 : maechler 2128
64 : maechler 2136 .diag2tT <- function(from, uplo = "U", kind = .M.kind(from)) {
65 :     ## to triangular Tsparse
66 : maechler 1845 i <- if(from@diag == "U") integer(0) else seq_len(from@Dim[1]) - 1L
67 : maechler 2136 new(paste(kind, "tTMatrix", sep=''),
68 : maechler 1654 diag = from@diag, Dim = from@Dim, Dimnames = from@Dimnames,
69 : maechler 2128 uplo = uplo,
70 : maechler 1654 x = from@x, # <- ok for diag = "U" and "N" (!)
71 :     i = i, j = i)
72 :     }
73 : maechler 1109
74 : maechler 2136 .diag2sT <- function(from, uplo = "U", kind = .M.kind(from)) {
75 :     ## to symmetric Tsparse
76 : maechler 2128 n <- from@Dim[1]
77 :     i <- seq_len(n) - 1L
78 :     new(paste(kind, "sTMatrix", sep=''),
79 : maechler 1845 Dim = from@Dim, Dimnames = from@Dimnames,
80 : maechler 2128 i = i, j = i, uplo = uplo,
81 :     x = if(from@diag == "N") from@x else ## "U"-diag
82 :     rep.int(switch(kind,
83 :     "d" = 1.,
84 :     "l" =,
85 :     "n" = TRUE,
86 :     ## otherwise
87 :     stop("'", kind,"' kind not yet implemented")), n))
88 : maechler 1845 }
89 :    
90 : maechler 2128 ## diagonal -> triangular, upper / lower depending on "partner":
91 :     diag2tT.u <- function(d, x)
92 :     .diag2tT(d, uplo = if(is(x,"triangularMatrix")) x@uplo else "U")
93 :    
94 : maechler 2136
95 :     ## In order to evade method dispatch ambiguity warnings,
96 :     ## and because we can save a .M.kind() call, we use this explicit
97 :     ## "hack" instead of signature x = "diagonalMatrix" :
98 :     ##
99 :     ## ddi*:
100 :     diag2tT <- function(from) .diag2tT(from, "U", "d")
101 :     setAs("ddiMatrix", "triangularMatrix", diag2tT)
102 :     setAs("ddiMatrix", "sparseMatrix", diag2tT)
103 : maechler 1805 ## needed too (otherwise <dense> -> Tsparse is taken):
104 : maechler 2136 setAs("ddiMatrix", "TsparseMatrix", diag2tT)
105 :     setAs("ddiMatrix", "CsparseMatrix",
106 :     function(from) as(.diag2tT(from, "U", "d"), "CsparseMatrix"))
107 :     setAs("ddiMatrix", "symmetricMatrix",
108 :     function(from) .diag2sT(from, "U", "d"))
109 :     ##
110 :     ## ldi*:
111 :     diag2tT <- function(from) .diag2tT(from, "U", "l")
112 :     setAs("ldiMatrix", "triangularMatrix", diag2tT)
113 :     setAs("ldiMatrix", "sparseMatrix", diag2tT)
114 :     ## needed too (otherwise <dense> -> Tsparse is taken):
115 :     setAs("ldiMatrix", "TsparseMatrix", diag2tT)
116 :     setAs("ldiMatrix", "CsparseMatrix",
117 :     function(from) as(.diag2tT(from, "U", "l"), "CsparseMatrix"))
118 :     setAs("ldiMatrix", "symmetricMatrix",
119 :     function(from) .diag2sT(from, "U", "l"))
120 : maechler 1654
121 : maechler 1845
122 : maechler 2128 setAs("diagonalMatrix", "nMatrix",
123 :     function(from) {
124 :     n <- from@Dim[1]
125 :     i <- if(from@diag == "U") integer(0) else which(isN0(from@x)) - 1L
126 :     new("ntTMatrix", i = i, j = i, diag = from@diag,
127 :     Dim = from@Dim, Dimnames = from@Dimnames)
128 :     })
129 :    
130 :    
131 : maechler 1109 setAs("diagonalMatrix", "matrix",
132 :     function(from) {
133 :     n <- from@Dim[1]
134 :     diag(x = if(from@diag == "U") { if(is.numeric(from@x)) 1. else TRUE
135 :     } else from@x,
136 :     nrow = n, ncol = n)
137 :     })
138 :    
139 : maechler 2098 setMethod("as.vector", signature(x = "diagonalMatrix", mode="missing"),
140 :     function(x, mode) {
141 :     n <- x@Dim[1]
142 :     mod <- mode(x@x)
143 :     r <- vector(mod, length = n^2)
144 :     if(n)
145 :     r[1 + 0:(n - 1) * (n + 1)] <-
146 :     if(x@diag == "U")
147 :     switch(mod, "integer"= 1L,
148 :     "numeric"= 1, "logical"= TRUE)
149 :     else x@x
150 :     r
151 :     })
152 :    
153 : maechler 1654 setAs("diagonalMatrix", "generalMatrix", # prefer sparse:
154 : maechler 2128 function(from) as(as(from, "CsparseMatrix"), "generalMatrix"))
155 : maechler 1174
156 : maechler 1655 .diag.x <- function(m) {
157 :     if(m@diag == "U")
158 : maechler 2136 rep.int(if(is.numeric(m@x)) 1. else TRUE, m@Dim[1])
159 : maechler 1655 else m@x
160 :     }
161 :    
162 :     .diag.2N <- function(m) {
163 :     if(m@diag == "U") m@diag <- "N"
164 :     m
165 :     }
166 :    
167 : maechler 2128 if(FALSE) {
168 : maechler 1654 ## given the above, the following 4 coercions should be all unneeded;
169 :     ## we prefer triangular to general:
170 : maechler 1295 setAs("ddiMatrix", "dgTMatrix",
171 :     function(from) {
172 : maechler 1654 .Deprecated("as(, \"sparseMatrix\")")
173 : maechler 1295 n <- from@Dim[1]
174 : maechler 1845 i <- seq_len(n) - 1L
175 : maechler 1655 new("dgTMatrix", i = i, j = i, x = .diag.x(from),
176 : maechler 1295 Dim = c(n,n), Dimnames = from@Dimnames) })
177 :    
178 :     setAs("ddiMatrix", "dgCMatrix",
179 : maechler 1655 function(from) as(as(from, "sparseMatrix"), "dgCMatrix"))
180 : maechler 1295
181 :     setAs("ldiMatrix", "lgTMatrix",
182 :     function(from) {
183 : maechler 1654 .Deprecated("as(, \"sparseMatrix\")")
184 : maechler 1295 n <- from@Dim[1]
185 : maechler 1575 if(from@diag == "U") { # unit-diagonal
186 :     x <- rep.int(TRUE, n)
187 : maechler 1845 i <- seq_len(n) - 1L
188 : maechler 1575 } else { # "normal"
189 :     nz <- nz.NA(from@x, na. = TRUE)
190 :     x <- from@x[nz]
191 : maechler 1845 i <- which(nz) - 1L
192 : maechler 1575 }
193 :     new("lgTMatrix", i = i, j = i, x = x,
194 : maechler 1295 Dim = c(n,n), Dimnames = from@Dimnames) })
195 :    
196 :     setAs("ldiMatrix", "lgCMatrix",
197 :     function(from) as(as(from, "lgTMatrix"), "lgCMatrix"))
198 : maechler 2128 }
199 : maechler 1295
200 :    
201 : maechler 1447 if(FALSE) # now have faster "ddense" -> "dge"
202 : maechler 1174 setAs("ddiMatrix", "dgeMatrix",
203 :     function(from) as(as(from, "matrix"), "dgeMatrix"))
204 :    
205 : maechler 1109 setAs("matrix", "diagonalMatrix",
206 :     function(from) {
207 : maechler 1295 d <- dim(from)
208 : maechler 1109 if(d[1] != (n <- d[2])) stop("non-square matrix")
209 :     if(any(from[row(from) != col(from)] != 0))
210 :     stop("has non-zero off-diagonal entries")
211 : maechler 1295 x <- diag(from)
212 :     if(is.logical(x)) {
213 :     cl <- "ldiMatrix"
214 :     uni <- all(x)
215 :     } else {
216 :     cl <- "ddiMatrix"
217 :     uni <- all(x == 1)
218 :     storage.mode(x) <- "double"
219 : maechler 1575 } ## TODO: complex
220 : maechler 1295 new(cl, Dim = c(n,n), diag = if(uni) "U" else "N",
221 :     x = if(uni) x[FALSE] else x)
222 : maechler 1109 })
223 :    
224 :     ## ``generic'' coercion to diagonalMatrix : build on isDiagonal() and diag()
225 :     setAs("Matrix", "diagonalMatrix",
226 :     function(from) {
227 :     d <- dim(from)
228 :     if(d[1] != (n <- d[2])) stop("non-square matrix")
229 :     if(!isDiagonal(from)) stop("matrix is not diagonal")
230 :     ## else:
231 :     x <- diag(from)
232 :     if(is.logical(x)) {
233 :     cl <- "ldiMatrix"
234 :     uni <- all(x)
235 :     } else {
236 :     cl <- "ddiMatrix"
237 :     uni <- all(x == 1)
238 :     storage.mode(x) <- "double"
239 :     }
240 :     new(cl, Dim = c(n,n), diag = if(uni) "U" else "N",
241 :     x = if(uni) x[FALSE] else x)
242 :     })
243 :    
244 : maechler 1708
245 : maechler 2136 ## In order to evade method dispatch ambiguity warnings,
246 :     ## we use this hack instead of signature x = "diagonalMatrix" :
247 :     diCls <- names(getClass("diagonalMatrix")@subclasses)
248 :     for(cls in diCls) {
249 :     setMethod("diag", signature(x = cls),
250 :     function(x = 1, nrow, ncol) .diag.x(x))
251 :     }
252 : maechler 1708
253 : maechler 1799
254 : maechler 2098 subDiag <- function(x, i, j, ..., drop) {
255 : maechler 1799 x <- as(x, "sparseMatrix")
256 :     x <- if(missing(i))
257 :     x[, j, drop=drop]
258 :     else if(missing(j))
259 :     x[i, , drop=drop]
260 :     else
261 :     x[i,j, drop=drop]
262 : maechler 2120 if(isS4(x) && isDiagonal(x)) as(x, "diagonalMatrix") else x
263 : maechler 1799 }
264 :    
265 :     setMethod("[", signature(x = "diagonalMatrix", i = "index",
266 :     j = "index", drop = "logical"), subDiag)
267 :     setMethod("[", signature(x = "diagonalMatrix", i = "index",
268 :     j = "missing", drop = "logical"),
269 : maechler 2098 function(x, i, j, ..., drop) subDiag(x, i=i, drop=drop))
270 : maechler 1799 setMethod("[", signature(x = "diagonalMatrix", i = "missing",
271 :     j = "index", drop = "logical"),
272 : maechler 2098 function(x, i, j, ..., drop) subDiag(x, j=j, drop=drop))
273 : maechler 1799
274 : maechler 1617 ## When you assign to a diagonalMatrix, the result should be
275 : maechler 1708 ## diagonal or sparse ---
276 :     ## FIXME: this now fails because the "denseMatrix" methods come first in dispatch
277 : maechler 2098 ## Only(?) current bug: x[i] <- value is wrong when i is *vector*
278 :     replDiag <- function(x, i, j, ..., value) {
279 : maechler 1710 x <- as(x, "sparseMatrix")
280 :     if(missing(i))
281 :     x[, j] <- value
282 : maechler 2098 else if(missing(j)) { ## x[i , ] <- v *OR* x[i] <- v
283 :     na <- nargs()
284 :     ## message("diagnosing replDiag() -- nargs()= ", na)
285 :     if(na == 4)
286 :     x[i, ] <- value
287 :     else if(na == 3)
288 :     x[i] <- value
289 :     else stop("Internal bug: nargs()=",na,"; please report")
290 :     } else
291 : maechler 1710 x[i,j] <- value
292 :     if(isDiagonal(x)) as(x, "diagonalMatrix") else x
293 :     }
294 : maechler 1617
295 : maechler 1710 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",
296 :     j = "index", value = "replValue"), replDiag)
297 : maechler 2098
298 : maechler 1710 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",
299 :     j = "missing", value = "replValue"),
300 : maechler 2098 function(x,i,j, ..., value) {
301 :     ## message("before replDiag() -- nargs()= ", nargs())
302 :     if(nargs() == 3)
303 :     replDiag(x, i=i, value=value)
304 :     else ## nargs() == 4 :
305 :     replDiag(x, i=i, , value=value)
306 :     })
307 :    
308 : maechler 2096 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "matrix", # 2-col.matrix
309 :     j = "missing", value = "replValue"),
310 : maechler 2098 function(x,i,j, ..., value) {
311 : maechler 2096 if(ncol(i) == 2) {
312 :     if(all((ii <- i[,1]) == i[,2])) { # replace in diagonal only
313 :     x@x[ii] <- value
314 :     x
315 :     } else { ## no longer diagonal, but remain sparse:
316 :     x <- as(x, "sparseMatrix")
317 :     x[i] <- value
318 :     x
319 :     }
320 :     }
321 :     else { # behave as "base R": use as if vector
322 :     x <- as(x, "matrix")
323 :     x[i] <- value
324 :     Matrix(x)
325 :     }
326 :     })
327 :    
328 : maechler 1710 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing",
329 :     j = "index", value = "replValue"),
330 : maechler 2098 function(x,i,j, ..., value) replDiag(x, j=j, value=value))
331 : maechler 1710
332 :    
333 : maechler 1109 setMethod("t", signature(x = "diagonalMatrix"),
334 :     function(x) { x@Dimnames <- x@Dimnames[2:1] ; x })
335 :    
336 : maechler 1331 setMethod("isDiagonal", signature(object = "diagonalMatrix"),
337 :     function(object) TRUE)
338 :     setMethod("isTriangular", signature(object = "diagonalMatrix"),
339 :     function(object) TRUE)
340 : maechler 1109 setMethod("isSymmetric", signature(object = "diagonalMatrix"),
341 : maechler 2112 function(object, ...) TRUE)
342 : maechler 1109
343 : maechler 2112 setMethod("symmpart", signature(x = "diagonalMatrix"), function(x) x)
344 :     setMethod("skewpart", signature(x = "diagonalMatrix"), setZero)
345 :    
346 : maechler 2133 setMethod("chol", signature(x = "ddiMatrix"),
347 :     function(x, pivot, ...) {
348 :     if(any(x@x < 0))
349 :     stop("chol() is undefined for diagonal matrix with negative entries")
350 : maechler 1654 x@x <- sqrt(x@x)
351 :     x
352 :     })
353 :     ## chol(L) is L for logical diagonal:
354 : maechler 2133 setMethod("chol", signature(x = "ldiMatrix"), function(x, pivot, ...) x)
355 : maechler 1654
356 : maechler 1109 ## Basic Matrix Multiplication {many more to add}
357 : maechler 1654 ## ---------------------
358 :     ## Note that "ldi" logical are treated as numeric
359 : maechler 1109 diagdiagprod <- function(x, y) {
360 :     if(any(dim(x) != dim(y))) stop("non-matching dimensions")
361 :     if(x@diag != "U") {
362 : maechler 1654 if(y@diag != "U") {
363 :     nx <- x@x * y@x
364 :     if(is.numeric(nx) && !is.numeric(x@x))
365 :     x <- as(x, "dMatrix")
366 :     x@x <- as.numeric(nx)
367 :     }
368 :     return(x)
369 : maechler 1109 } else ## x is unit diagonal
370 :     return(y)
371 :     }
372 :    
373 : maechler 1654 setMethod("%*%", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
374 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
375 :    
376 : maechler 1654 formals(diagdiagprod) <- alist(x=, y=x)
377 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
378 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
379 : maechler 1654 setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
380 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
381 : maechler 1654 setMethod("crossprod", signature(x = "diagonalMatrix", y = "missing"),
382 :     diagdiagprod, valueClass = "ddiMatrix")
383 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "missing"),
384 :     diagdiagprod, valueClass = "ddiMatrix")
385 : maechler 1109
386 :    
387 :     diagmatprod <- function(x, y) {
388 :     dx <- dim(x)
389 :     dy <- dim(y)
390 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
391 :     n <- dx[1]
392 :     as(if(x@diag == "U") y else x@x * y, "Matrix")
393 :     }
394 :    
395 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "matrix"),
396 : maechler 1654 diagmatprod)
397 : maechler 1109 formals(diagmatprod) <- alist(x=, y=NULL)
398 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "matrix"),
399 : maechler 1654 diagmatprod)
400 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "matrix"),
401 :     diagmatprod)
402 : maechler 1109
403 :     diagdgeprod <- function(x, y) {
404 :     dx <- dim(x)
405 :     dy <- dim(y)
406 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
407 :     if(x@diag != "U")
408 :     y@x <- x@x * y@x
409 :     y
410 :     }
411 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "dgeMatrix"),
412 :     diagdgeprod, valueClass = "dgeMatrix")
413 :     formals(diagdgeprod) <- alist(x=, y=NULL)
414 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "dgeMatrix"),
415 :     diagdgeprod, valueClass = "dgeMatrix")
416 :    
417 :     setMethod("%*%", signature(x = "matrix", y = "diagonalMatrix"),
418 :     function(x, y) {
419 : maechler 1635 dx <- dim(x)
420 :     dy <- dim(y)
421 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
422 :     as(if(y@diag == "U") x else x * rep(y@x, each = dx[1]), "Matrix")
423 :     })
424 : maechler 1109
425 :     setMethod("%*%", signature(x = "dgeMatrix", y = "diagonalMatrix"),
426 :     function(x, y) {
427 : maechler 1635 dx <- dim(x)
428 :     dy <- dim(y)
429 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
430 :     if(y@diag == "N")
431 :     x@x <- x@x * rep(y@x, each = dx[1])
432 :     x
433 :     })
434 : maechler 1109
435 : maechler 1295 ## crossprod {more of these}
436 : maechler 1109
437 : maechler 1295 ## tcrossprod --- all are not yet there: do the dense ones here:
438 : maechler 1109
439 : maechler 1295 ## FIXME:
440 :     ## setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "denseMatrix"),
441 :     ## function(x, y = NULL) {
442 :     ## })
443 : maechler 1109
444 : maechler 1295 ## setMethod("tcrossprod", signature(x = "denseMatrix", y = "diagonalMatrix"),
445 :     ## function(x, y = NULL) {
446 :     ## })
447 : maechler 1109
448 : maechler 1799 setMethod("crossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
449 : maechler 2123 function(x, y = NULL) crossprod(as(x, "sparseMatrix"), y))
450 : maechler 1295
451 : maechler 1799 setMethod("crossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
452 : maechler 2123 function(x, y = NULL) crossprod(x, as(y, "sparseMatrix")))
453 : maechler 1799
454 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
455 : maechler 2123 function(x, y = NULL) tcrossprod(as(x, "sparseMatrix"), y))
456 : maechler 1799
457 :     setMethod("tcrossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
458 : maechler 2123 function(x, y = NULL) tcrossprod(x, as(y, "sparseMatrix")))
459 : maechler 1799
460 :    
461 :     ## FIXME?: In theory, this can be done *FASTER*, in some cases, via tapply1()
462 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "sparseMatrix"),
463 :     function(x, y) as(x, "sparseMatrix") %*% y)
464 :     ## NB: The previous is *not* triggering for "ddi" o "dgC" (= distance 3)
465 :     ## since there's a "ddense" o "Csparse" at dist. 2 => triggers first.
466 :     ## ==> do this:
467 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "CsparseMatrix"),
468 :     function(x, y) as(x, "CsparseMatrix") %*% y)
469 :     setMethod("%*%", signature(x = "CsparseMatrix", y = "diagonalMatrix"),
470 :     function(x, y) x %*% as(y, "CsparseMatrix"))
471 :     ## NB: this is *not* needed for Tsparse & Rsparse
472 :     ## TODO: Write tests in ./tests/ which ensure that many "ops" with diagonal*
473 :     ## do indeed work by going through sparse (and *not* ddense)!
474 :    
475 :     setMethod("%*%", signature(x = "sparseMatrix", y = "diagonalMatrix"),
476 :     function(x, y) x %*% as(y, "sparseMatrix"))
477 :    
478 :    
479 :     setMethod("solve", signature(a = "diagonalMatrix", b = "missing"),
480 :     function(a, b, ...) {
481 :     a@x <- 1/ a@x
482 :     a@Dimnames <- a@Dimnames[2:1]
483 :     a
484 :     })
485 :    
486 :     solveDiag <- function(a, b, ...) {
487 :     if((n <- a@Dim[1]) != nrow(b))
488 :     stop("incompatible matrix dimensions")
489 :     ## trivially invert a 'in place' and multiply:
490 :     a@x <- 1/ a@x
491 :     a@Dimnames <- a@Dimnames[2:1]
492 :     a %*% b
493 :     }
494 :     setMethod("solve", signature(a = "diagonalMatrix", b = "matrix"),
495 :     solveDiag)
496 :     setMethod("solve", signature(a = "diagonalMatrix", b = "Matrix"),
497 :     solveDiag)
498 :    
499 : maechler 2106 ## Schur() ---> ./eigen.R
500 : maechler 1799
501 :    
502 :    
503 : maechler 1654 ### ---------------- diagonal o sparse -----------------------------
504 : maechler 1295
505 : maechler 1655
506 :     ## Use function for several signatures, in order to evade
507 :     ## ambiguous dispatch for "ddi", since there's also Arith(ddense., ddense.)
508 :     diagOdiag <- function(e1,e2) { # result should also be diagonal
509 :     r <- callGeneric(.diag.x(e1), .diag.x(e2)) # error if not "compatible"
510 :     if(is.numeric(r)) {
511 :     if(is.numeric(e2@x)) {
512 :     e2@x <- r; return(.diag.2N(e2)) }
513 :     if(!is.numeric(e1@x))
514 :     ## e.g. e1, e2 are logical;
515 :     e1 <- as(e1, "dMatrix")
516 :     }
517 :     else if(is.logical(r))
518 :     e1 <- as(e1, "lMatrix")
519 :     else stop("intermediate 'r' is of type", typeof(r))
520 :     e1@x <- r
521 :     .diag.2N(e1)
522 :     }
523 :    
524 :     setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "diagonalMatrix"),
525 :     diagOdiag)
526 :     ## These two are just for method disambiguation:
527 :     setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "diagonalMatrix"),
528 :     diagOdiag)
529 :     setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "ddiMatrix"),
530 :     diagOdiag)
531 :    
532 : maechler 1845 ## FIXME: diagonal o triangular |--> triangular
533 :     ## ----- diagonal o symmetric |--> symmetric
534 :     ## {also when other is sparse: do these "here" --
535 :     ## before conversion to sparse, since that loses "diagonality"}
536 :    
537 : maechler 1655 ## For almost everything else, diag* shall be treated "as sparse" :
538 : maechler 1295 ## These are cheap implementations via coercion
539 :    
540 : maechler 2128 ## for disambiguation --- define this for "sparseMatrix" , then for "ANY" :
541 : maechler 1654 setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "sparseMatrix"),
542 : maechler 2128 function(e1,e2) callGeneric(diag2tT.u(e1,e2), e2))
543 : maechler 1654 setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "diagonalMatrix"),
544 : maechler 2128 function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1)))
545 : maechler 1655 ## in general:
546 :     setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "ANY"),
547 : maechler 2128 function(e1,e2) callGeneric(diag2tT.u(e1,e2), e2))
548 : maechler 1655 setMethod("Ops", signature(e1 = "ANY", e2 = "diagonalMatrix"),
549 : maechler 2128 function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1)))
550 : maechler 1654
551 : maechler 1655
552 :    
553 : maechler 1109 ## similar to prTriang() in ./Auxiliaries.R :
554 :     prDiag <-
555 :     function(x, digits = getOption("digits"), justify = "none", right = TRUE)
556 :     {
557 :     cf <- array(".", dim = x@Dim, dimnames = x@Dimnames)
558 :     cf[row(cf) == col(cf)] <-
559 :     sapply(diag(x), format, digits = digits, justify = justify)
560 :     print(cf, quote = FALSE, right = right)
561 :     invisible(x)
562 :     }
563 :    
564 :     setMethod("show", signature(object = "diagonalMatrix"),
565 : maechler 1592 function(object) {
566 :     d <- dim(object)
567 :     cl <- class(object)
568 :     cat(sprintf('%d x %d diagonal matrix of class "%s"\n',
569 :     d[1], d[2], cl))
570 :     prDiag(object)
571 :     })

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