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 2128 - (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 1654 setMethod("chol", signature(x = "ddiMatrix"),# pivot = "ANY"
331 :     function(x, pivot) {
332 :     if(any(x@x < 0)) stop("chol() is undefined for diagonal matrix with negative entries")
333 :     x@x <- sqrt(x@x)
334 :     x
335 :     })
336 :     ## chol(L) is L for logical diagonal:
337 :     setMethod("chol", signature(x = "ldiMatrix"), function(x, pivot) x)
338 :    
339 : maechler 1109 ## Basic Matrix Multiplication {many more to add}
340 : maechler 1654 ## ---------------------
341 :     ## Note that "ldi" logical are treated as numeric
342 : maechler 1109 diagdiagprod <- function(x, y) {
343 :     if(any(dim(x) != dim(y))) stop("non-matching dimensions")
344 :     if(x@diag != "U") {
345 : maechler 1654 if(y@diag != "U") {
346 :     nx <- x@x * y@x
347 :     if(is.numeric(nx) && !is.numeric(x@x))
348 :     x <- as(x, "dMatrix")
349 :     x@x <- as.numeric(nx)
350 :     }
351 :     return(x)
352 : maechler 1109 } else ## x is unit diagonal
353 :     return(y)
354 :     }
355 :    
356 : maechler 1654 setMethod("%*%", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
357 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
358 :    
359 : maechler 1654 formals(diagdiagprod) <- alist(x=, y=x)
360 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
361 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
362 : maechler 1654 setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
363 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
364 : maechler 1654 setMethod("crossprod", signature(x = "diagonalMatrix", y = "missing"),
365 :     diagdiagprod, valueClass = "ddiMatrix")
366 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "missing"),
367 :     diagdiagprod, valueClass = "ddiMatrix")
368 : maechler 1109
369 :    
370 :     diagmatprod <- function(x, y) {
371 :     dx <- dim(x)
372 :     dy <- dim(y)
373 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
374 :     n <- dx[1]
375 :     as(if(x@diag == "U") y else x@x * y, "Matrix")
376 :     }
377 :    
378 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "matrix"),
379 : maechler 1654 diagmatprod)
380 : maechler 1109 formals(diagmatprod) <- alist(x=, y=NULL)
381 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "matrix"),
382 : maechler 1654 diagmatprod)
383 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "matrix"),
384 :     diagmatprod)
385 : maechler 1109
386 :     diagdgeprod <- function(x, y) {
387 :     dx <- dim(x)
388 :     dy <- dim(y)
389 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
390 :     if(x@diag != "U")
391 :     y@x <- x@x * y@x
392 :     y
393 :     }
394 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "dgeMatrix"),
395 :     diagdgeprod, valueClass = "dgeMatrix")
396 :     formals(diagdgeprod) <- alist(x=, y=NULL)
397 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "dgeMatrix"),
398 :     diagdgeprod, valueClass = "dgeMatrix")
399 :    
400 :     setMethod("%*%", signature(x = "matrix", y = "diagonalMatrix"),
401 :     function(x, y) {
402 : maechler 1635 dx <- dim(x)
403 :     dy <- dim(y)
404 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
405 :     as(if(y@diag == "U") x else x * rep(y@x, each = dx[1]), "Matrix")
406 :     })
407 : maechler 1109
408 :     setMethod("%*%", signature(x = "dgeMatrix", y = "diagonalMatrix"),
409 :     function(x, y) {
410 : maechler 1635 dx <- dim(x)
411 :     dy <- dim(y)
412 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
413 :     if(y@diag == "N")
414 :     x@x <- x@x * rep(y@x, each = dx[1])
415 :     x
416 :     })
417 : maechler 1109
418 : maechler 1295 ## crossprod {more of these}
419 : maechler 1109
420 : maechler 1295 ## tcrossprod --- all are not yet there: do the dense ones here:
421 : maechler 1109
422 : maechler 1295 ## FIXME:
423 :     ## setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "denseMatrix"),
424 :     ## function(x, y = NULL) {
425 :     ## })
426 : maechler 1109
427 : maechler 1295 ## setMethod("tcrossprod", signature(x = "denseMatrix", y = "diagonalMatrix"),
428 :     ## function(x, y = NULL) {
429 :     ## })
430 : maechler 1109
431 : maechler 1799 setMethod("crossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
432 : maechler 2123 function(x, y = NULL) crossprod(as(x, "sparseMatrix"), y))
433 : maechler 1295
434 : maechler 1799 setMethod("crossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
435 : maechler 2123 function(x, y = NULL) crossprod(x, as(y, "sparseMatrix")))
436 : maechler 1799
437 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
438 : maechler 2123 function(x, y = NULL) tcrossprod(as(x, "sparseMatrix"), y))
439 : maechler 1799
440 :     setMethod("tcrossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
441 : maechler 2123 function(x, y = NULL) tcrossprod(x, as(y, "sparseMatrix")))
442 : maechler 1799
443 :    
444 :     ## FIXME?: In theory, this can be done *FASTER*, in some cases, via tapply1()
445 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "sparseMatrix"),
446 :     function(x, y) as(x, "sparseMatrix") %*% y)
447 :     ## NB: The previous is *not* triggering for "ddi" o "dgC" (= distance 3)
448 :     ## since there's a "ddense" o "Csparse" at dist. 2 => triggers first.
449 :     ## ==> do this:
450 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "CsparseMatrix"),
451 :     function(x, y) as(x, "CsparseMatrix") %*% y)
452 :     setMethod("%*%", signature(x = "CsparseMatrix", y = "diagonalMatrix"),
453 :     function(x, y) x %*% as(y, "CsparseMatrix"))
454 :     ## NB: this is *not* needed for Tsparse & Rsparse
455 :     ## TODO: Write tests in ./tests/ which ensure that many "ops" with diagonal*
456 :     ## do indeed work by going through sparse (and *not* ddense)!
457 :    
458 :     setMethod("%*%", signature(x = "sparseMatrix", y = "diagonalMatrix"),
459 :     function(x, y) x %*% as(y, "sparseMatrix"))
460 :    
461 :    
462 :     setMethod("solve", signature(a = "diagonalMatrix", b = "missing"),
463 :     function(a, b, ...) {
464 :     a@x <- 1/ a@x
465 :     a@Dimnames <- a@Dimnames[2:1]
466 :     a
467 :     })
468 :    
469 :     solveDiag <- function(a, b, ...) {
470 :     if((n <- a@Dim[1]) != nrow(b))
471 :     stop("incompatible matrix dimensions")
472 :     ## trivially invert a 'in place' and multiply:
473 :     a@x <- 1/ a@x
474 :     a@Dimnames <- a@Dimnames[2:1]
475 :     a %*% b
476 :     }
477 :     setMethod("solve", signature(a = "diagonalMatrix", b = "matrix"),
478 :     solveDiag)
479 :     setMethod("solve", signature(a = "diagonalMatrix", b = "Matrix"),
480 :     solveDiag)
481 :    
482 : maechler 2106 ## Schur() ---> ./eigen.R
483 : maechler 1799
484 :    
485 :    
486 : maechler 1654 ### ---------------- diagonal o sparse -----------------------------
487 : maechler 1295
488 : maechler 1655
489 :     ## Use function for several signatures, in order to evade
490 :     ## ambiguous dispatch for "ddi", since there's also Arith(ddense., ddense.)
491 :     diagOdiag <- function(e1,e2) { # result should also be diagonal
492 :     r <- callGeneric(.diag.x(e1), .diag.x(e2)) # error if not "compatible"
493 :     if(is.numeric(r)) {
494 :     if(is.numeric(e2@x)) {
495 :     e2@x <- r; return(.diag.2N(e2)) }
496 :     if(!is.numeric(e1@x))
497 :     ## e.g. e1, e2 are logical;
498 :     e1 <- as(e1, "dMatrix")
499 :     }
500 :     else if(is.logical(r))
501 :     e1 <- as(e1, "lMatrix")
502 :     else stop("intermediate 'r' is of type", typeof(r))
503 :     e1@x <- r
504 :     .diag.2N(e1)
505 :     }
506 :    
507 :     setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "diagonalMatrix"),
508 :     diagOdiag)
509 :     ## These two are just for method disambiguation:
510 :     setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "diagonalMatrix"),
511 :     diagOdiag)
512 :     setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "ddiMatrix"),
513 :     diagOdiag)
514 :    
515 : maechler 1845 ## FIXME: diagonal o triangular |--> triangular
516 :     ## ----- diagonal o symmetric |--> symmetric
517 :     ## {also when other is sparse: do these "here" --
518 :     ## before conversion to sparse, since that loses "diagonality"}
519 :    
520 : maechler 1655 ## For almost everything else, diag* shall be treated "as sparse" :
521 : maechler 1295 ## These are cheap implementations via coercion
522 :    
523 : maechler 2128 ## for disambiguation --- define this for "sparseMatrix" , then for "ANY" :
524 : maechler 1654 setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "sparseMatrix"),
525 : maechler 2128 function(e1,e2) callGeneric(diag2tT.u(e1,e2), e2))
526 : maechler 1654 setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "diagonalMatrix"),
527 : maechler 2128 function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1)))
528 : maechler 1655 ## in general:
529 :     setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "ANY"),
530 : maechler 2128 function(e1,e2) callGeneric(diag2tT.u(e1,e2), e2))
531 : maechler 1655 setMethod("Ops", signature(e1 = "ANY", e2 = "diagonalMatrix"),
532 : maechler 2128 function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1)))
533 : maechler 1654
534 : maechler 1655
535 :    
536 : maechler 1109 ## similar to prTriang() in ./Auxiliaries.R :
537 :     prDiag <-
538 :     function(x, digits = getOption("digits"), justify = "none", right = TRUE)
539 :     {
540 :     cf <- array(".", dim = x@Dim, dimnames = x@Dimnames)
541 :     cf[row(cf) == col(cf)] <-
542 :     sapply(diag(x), format, digits = digits, justify = justify)
543 :     print(cf, quote = FALSE, right = right)
544 :     invisible(x)
545 :     }
546 :    
547 :     setMethod("show", signature(object = "diagonalMatrix"),
548 : maechler 1592 function(object) {
549 :     d <- dim(object)
550 :     cl <- class(object)
551 :     cat(sprintf('%d x %d diagonal matrix of class "%s"\n',
552 :     d[1], d[2], cl))
553 :     prDiag(object)
554 :     })

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