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

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