SCM

SCM Repository

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

Annotation of /pkg/R/diagMatrix.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1805 - (view) (download)

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 1575 stopifnot(length(x) == n)
20 :     if(is.logical(x))
21 :     cl <- "ldiMatrix"
22 :     else if(is.numeric(x)) {
23 :     cl <- "ddiMatrix"
24 :     x <- as.numeric(x)
25 :     }
26 :     else if(is.complex(x)) {
27 :     cl <- "zdiMatrix" # will not yet work
28 :     } else stop("'x' has invalid data type")
29 :     new(cl, Dim = c(n,n), diag = "N", x = x)
30 : maechler 1109 }
31 :     }
32 :    
33 : maechler 1617 ### This is modified from a post of Bert Gunter to R-help on 1 Sep 2005.
34 :     ### Bert's code built on a post by Andy Liaw who most probably was influenced
35 :     ### by earlier posts, notably one by Scott Chasalow on S-news, 16 Jan 2002
36 :     ### who posted his bdiag() function written in December 1995.
37 :    
38 :     bdiag <- function(...) {
39 :     if(nargs() == 0) return(new("dgCMatrix"))
40 :     ## else :
41 :     mlist <- if (nargs() == 1) as.list(...) else list(...)
42 :     dims <- sapply(mlist, dim)
43 :     ## make sure we had all matrices:
44 :     if(!(is.matrix(dims) && nrow(dims) == 2))
45 :     stop("some arguments are not matrices")
46 :     csdim <- rbind(rep.int(0:0, 2),
47 :     apply(sapply(mlist, dim), 1, cumsum))
48 :     ret <- new("dgTMatrix", Dim = as.integer(csdim[nrow(csdim),]))
49 :     add1 <- matrix(1:0, 2,2)
50 : maechler 1654 for(i in seq_along(mlist)) {
51 : maechler 1617 indx <- apply(csdim[i:(i+1),] + add1, 2, function(n) n[1]:n[2])
52 :     if(is.null(dim(indx))) ## non-square matrix
53 :     ret[indx[[1]],indx[[2]]] <- mlist[[i]]
54 :     else ## square matrix
55 :     ret[indx[,1],indx[,2]] <- mlist[[i]]
56 :     }
57 :     ## slightly debatable if we really should return Csparse.. :
58 :     as(ret, "CsparseMatrix")
59 :     }
60 :    
61 : maechler 1654 diag2T <- function(from) {
62 :     i <- if(from@diag == "U") integer(0) else seq_len(from@Dim[1]) - 1:1
63 :     new(paste(.M.kind(from), "tTMatrix", sep=''),
64 :     diag = from@diag, Dim = from@Dim, Dimnames = from@Dimnames,
65 :     x = from@x, # <- ok for diag = "U" and "N" (!)
66 :     i = i, j = i)
67 :     }
68 : maechler 1109
69 : maechler 1654 setAs("diagonalMatrix", "triangularMatrix", diag2T)
70 :     setAs("diagonalMatrix", "sparseMatrix", diag2T)
71 : maechler 1805 ## needed too (otherwise <dense> -> Tsparse is taken):
72 :     setAs("diagonalMatrix", "TsparseMatrix", diag2T)
73 : maechler 1654 ## is better than this:
74 :     ## setAs("diagonalMatrix", "sparseMatrix",
75 :     ## function(from)
76 :     ## as(from, if(is(from, "dMatrix")) "dgCMatrix" else "lgCMatrix"))
77 :     setAs("diagonalMatrix", "CsparseMatrix",
78 :     function(from) as(diag2T(from), "CsparseMatrix"))
79 :    
80 : maechler 1109 setAs("diagonalMatrix", "matrix",
81 :     function(from) {
82 :     n <- from@Dim[1]
83 :     diag(x = if(from@diag == "U") { if(is.numeric(from@x)) 1. else TRUE
84 :     } else from@x,
85 :     nrow = n, ncol = n)
86 :     })
87 :    
88 : maechler 1654 setAs("diagonalMatrix", "generalMatrix", # prefer sparse:
89 :     function(from) as(from, paste(.M.kind(from), "gCMatrix", sep='')))
90 : maechler 1174
91 : maechler 1655 .diag.x <- function(m) {
92 :     if(m@diag == "U")
93 :     rep.int(if(is.numeric(m@x)) 1. else TRUE,
94 :     m@Dim[1])
95 :     else m@x
96 :     }
97 :    
98 :     .diag.2N <- function(m) {
99 :     if(m@diag == "U") m@diag <- "N"
100 :     m
101 :     }
102 :    
103 : maechler 1654 ## given the above, the following 4 coercions should be all unneeded;
104 :     ## we prefer triangular to general:
105 : maechler 1295 setAs("ddiMatrix", "dgTMatrix",
106 :     function(from) {
107 : maechler 1654 .Deprecated("as(, \"sparseMatrix\")")
108 : maechler 1295 n <- from@Dim[1]
109 : maechler 1654 i <- seq_len(n) - 1:1
110 : maechler 1655 new("dgTMatrix", i = i, j = i, x = .diag.x(from),
111 : maechler 1295 Dim = c(n,n), Dimnames = from@Dimnames) })
112 :    
113 :     setAs("ddiMatrix", "dgCMatrix",
114 : maechler 1655 function(from) as(as(from, "sparseMatrix"), "dgCMatrix"))
115 : maechler 1295
116 :     setAs("ldiMatrix", "lgTMatrix",
117 :     function(from) {
118 : maechler 1654 .Deprecated("as(, \"sparseMatrix\")")
119 : maechler 1295 n <- from@Dim[1]
120 : maechler 1575 if(from@diag == "U") { # unit-diagonal
121 :     x <- rep.int(TRUE, n)
122 : maechler 1654 i <- seq_len(n) - 1:1
123 : maechler 1575 } else { # "normal"
124 :     nz <- nz.NA(from@x, na. = TRUE)
125 :     x <- from@x[nz]
126 :     i <- which(nz) - 1:1
127 :     }
128 :     new("lgTMatrix", i = i, j = i, x = x,
129 : maechler 1295 Dim = c(n,n), Dimnames = from@Dimnames) })
130 :    
131 :     setAs("ldiMatrix", "lgCMatrix",
132 :     function(from) as(as(from, "lgTMatrix"), "lgCMatrix"))
133 :    
134 :    
135 : maechler 1447 if(FALSE) # now have faster "ddense" -> "dge"
136 : maechler 1174 setAs("ddiMatrix", "dgeMatrix",
137 :     function(from) as(as(from, "matrix"), "dgeMatrix"))
138 :    
139 : maechler 1109 setAs("matrix", "diagonalMatrix",
140 :     function(from) {
141 : maechler 1295 d <- dim(from)
142 : maechler 1109 if(d[1] != (n <- d[2])) stop("non-square matrix")
143 :     if(any(from[row(from) != col(from)] != 0))
144 :     stop("has non-zero off-diagonal entries")
145 : maechler 1295 x <- diag(from)
146 :     if(is.logical(x)) {
147 :     cl <- "ldiMatrix"
148 :     uni <- all(x)
149 :     } else {
150 :     cl <- "ddiMatrix"
151 :     uni <- all(x == 1)
152 :     storage.mode(x) <- "double"
153 : maechler 1575 } ## TODO: complex
154 : maechler 1295 new(cl, Dim = c(n,n), diag = if(uni) "U" else "N",
155 :     x = if(uni) x[FALSE] else x)
156 : maechler 1109 })
157 :    
158 :     ## ``generic'' coercion to diagonalMatrix : build on isDiagonal() and diag()
159 :     setAs("Matrix", "diagonalMatrix",
160 :     function(from) {
161 :     d <- dim(from)
162 :     if(d[1] != (n <- d[2])) stop("non-square matrix")
163 :     if(!isDiagonal(from)) stop("matrix is not diagonal")
164 :     ## else:
165 :     x <- diag(from)
166 :     if(is.logical(x)) {
167 :     cl <- "ldiMatrix"
168 :     uni <- all(x)
169 :     } else {
170 :     cl <- "ddiMatrix"
171 :     uni <- all(x == 1)
172 :     storage.mode(x) <- "double"
173 :     }
174 :     new(cl, Dim = c(n,n), diag = if(uni) "U" else "N",
175 :     x = if(uni) x[FALSE] else x)
176 :     })
177 :    
178 : maechler 1708
179 :     setMethod("diag", signature(x = "diagonalMatrix"),
180 :     function(x = 1, nrow, ncol = n) .diag.x(x))
181 :    
182 : maechler 1799
183 :     subDiag <- function(x, i, j, drop) {
184 :     x <- as(x, "sparseMatrix")
185 :     x <- if(missing(i))
186 :     x[, j, drop=drop]
187 :     else if(missing(j))
188 :     x[i, , drop=drop]
189 :     else
190 :     x[i,j, drop=drop]
191 :     if(isDiagonal(x)) as(x, "diagonalMatrix") else x
192 :     }
193 :    
194 :     setMethod("[", signature(x = "diagonalMatrix", i = "index",
195 :     j = "index", drop = "logical"), subDiag)
196 :     setMethod("[", signature(x = "diagonalMatrix", i = "index",
197 :     j = "missing", drop = "logical"),
198 :     function(x, i, drop) subDiag(x, i=i, drop=drop))
199 :     setMethod("[", signature(x = "diagonalMatrix", i = "missing",
200 :     j = "index", drop = "logical"),
201 :     function(x, j, drop) subDiag(x, j=j, drop=drop))
202 :    
203 : maechler 1617 ## When you assign to a diagonalMatrix, the result should be
204 : maechler 1708 ## diagonal or sparse ---
205 :     ## FIXME: this now fails because the "denseMatrix" methods come first in dispatch
206 : maechler 1710 replDiag <- function(x, i, j, value) {
207 :     x <- as(x, "sparseMatrix")
208 :     if(missing(i))
209 :     x[, j] <- value
210 :     else if(missing(j))
211 :     x[i, ] <- value
212 :     else
213 :     x[i,j] <- value
214 :     if(isDiagonal(x)) as(x, "diagonalMatrix") else x
215 :     }
216 : maechler 1617
217 : maechler 1710 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",
218 :     j = "index", value = "replValue"), replDiag)
219 :     setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",
220 :     j = "missing", value = "replValue"),
221 :     function(x, i, value) replDiag(x, i=i, value=value))
222 :     setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing",
223 :     j = "index", value = "replValue"),
224 :     function(x, j, value) replDiag(x, j=j, value=value))
225 :    
226 :    
227 : maechler 1109 setMethod("t", signature(x = "diagonalMatrix"),
228 :     function(x) { x@Dimnames <- x@Dimnames[2:1] ; x })
229 :    
230 : maechler 1331 setMethod("isDiagonal", signature(object = "diagonalMatrix"),
231 :     function(object) TRUE)
232 :     setMethod("isTriangular", signature(object = "diagonalMatrix"),
233 :     function(object) TRUE)
234 : maechler 1109 setMethod("isSymmetric", signature(object = "diagonalMatrix"),
235 :     function(object) TRUE)
236 :    
237 : maechler 1654 setMethod("chol", signature(x = "ddiMatrix"),# pivot = "ANY"
238 :     function(x, pivot) {
239 :     if(any(x@x < 0)) stop("chol() is undefined for diagonal matrix with negative entries")
240 :     x@x <- sqrt(x@x)
241 :     x
242 :     })
243 :     ## chol(L) is L for logical diagonal:
244 :     setMethod("chol", signature(x = "ldiMatrix"), function(x, pivot) x)
245 :    
246 : maechler 1109 setMethod("!", "ldiMatrix", function(e1) {
247 :     if(e1@diag == "N")
248 :     e1@x <- !e1@x
249 :     else { ## "U"
250 :     e1@diag <- "N"
251 :     e1@x <- rep.int(FALSE, e1@Dim[1])
252 :     }
253 : maechler 1725 e1
254 : maechler 1109 })
255 :    
256 :     ## Basic Matrix Multiplication {many more to add}
257 : maechler 1654 ## ---------------------
258 :     ## Note that "ldi" logical are treated as numeric
259 : maechler 1109 diagdiagprod <- function(x, y) {
260 :     if(any(dim(x) != dim(y))) stop("non-matching dimensions")
261 :     if(x@diag != "U") {
262 : maechler 1654 if(y@diag != "U") {
263 :     nx <- x@x * y@x
264 :     if(is.numeric(nx) && !is.numeric(x@x))
265 :     x <- as(x, "dMatrix")
266 :     x@x <- as.numeric(nx)
267 :     }
268 :     return(x)
269 : maechler 1109 } else ## x is unit diagonal
270 :     return(y)
271 :     }
272 :    
273 : maechler 1654 setMethod("%*%", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
274 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
275 :    
276 : maechler 1654 formals(diagdiagprod) <- alist(x=, y=x)
277 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
278 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
279 : maechler 1654 setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
280 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
281 : maechler 1654 setMethod("crossprod", signature(x = "diagonalMatrix", y = "missing"),
282 :     diagdiagprod, valueClass = "ddiMatrix")
283 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "missing"),
284 :     diagdiagprod, valueClass = "ddiMatrix")
285 : maechler 1109
286 :    
287 :     diagmatprod <- function(x, y) {
288 :     dx <- dim(x)
289 :     dy <- dim(y)
290 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
291 :     n <- dx[1]
292 :     as(if(x@diag == "U") y else x@x * y, "Matrix")
293 :     }
294 :    
295 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "matrix"),
296 : maechler 1654 diagmatprod)
297 : maechler 1109 formals(diagmatprod) <- alist(x=, y=NULL)
298 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "matrix"),
299 : maechler 1654 diagmatprod)
300 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "matrix"),
301 :     diagmatprod)
302 : maechler 1109
303 :     diagdgeprod <- function(x, y) {
304 :     dx <- dim(x)
305 :     dy <- dim(y)
306 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
307 :     if(x@diag != "U")
308 :     y@x <- x@x * y@x
309 :     y
310 :     }
311 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "dgeMatrix"),
312 :     diagdgeprod, valueClass = "dgeMatrix")
313 :     formals(diagdgeprod) <- alist(x=, y=NULL)
314 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "dgeMatrix"),
315 :     diagdgeprod, valueClass = "dgeMatrix")
316 :    
317 :     setMethod("%*%", signature(x = "matrix", y = "diagonalMatrix"),
318 :     function(x, y) {
319 : maechler 1635 dx <- dim(x)
320 :     dy <- dim(y)
321 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
322 :     as(if(y@diag == "U") x else x * rep(y@x, each = dx[1]), "Matrix")
323 :     })
324 : maechler 1109
325 :     setMethod("%*%", signature(x = "dgeMatrix", y = "diagonalMatrix"),
326 :     function(x, y) {
327 : maechler 1635 dx <- dim(x)
328 :     dy <- dim(y)
329 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
330 :     if(y@diag == "N")
331 :     x@x <- x@x * rep(y@x, each = dx[1])
332 :     x
333 :     })
334 : maechler 1109
335 : maechler 1295 ## crossprod {more of these}
336 : maechler 1109
337 : maechler 1295 ## tcrossprod --- all are not yet there: do the dense ones here:
338 : maechler 1109
339 : maechler 1295 ## FIXME:
340 :     ## setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "denseMatrix"),
341 :     ## function(x, y = NULL) {
342 :     ## })
343 : maechler 1109
344 : maechler 1295 ## setMethod("tcrossprod", signature(x = "denseMatrix", y = "diagonalMatrix"),
345 :     ## function(x, y = NULL) {
346 :     ## })
347 : maechler 1109
348 : maechler 1799 setMethod("crossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
349 :     function(x, y = NULL) { x <- as(x, "sparseMatrix"); callGeneric() })
350 : maechler 1295
351 : maechler 1799 setMethod("crossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
352 :     function(x, y = NULL) { y <- as(y, "sparseMatrix"); callGeneric() })
353 :    
354 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
355 :     function(x, y = NULL) { x <- as(x, "sparseMatrix"); callGeneric() })
356 :    
357 :     setMethod("tcrossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
358 :     function(x, y = NULL) { y <- as(y, "sparseMatrix"); callGeneric() })
359 :    
360 :    
361 :     ## FIXME?: In theory, this can be done *FASTER*, in some cases, via tapply1()
362 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "sparseMatrix"),
363 :     function(x, y) as(x, "sparseMatrix") %*% y)
364 :     ## NB: The previous is *not* triggering for "ddi" o "dgC" (= distance 3)
365 :     ## since there's a "ddense" o "Csparse" at dist. 2 => triggers first.
366 :     ## ==> do this:
367 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "CsparseMatrix"),
368 :     function(x, y) as(x, "CsparseMatrix") %*% y)
369 :     setMethod("%*%", signature(x = "CsparseMatrix", y = "diagonalMatrix"),
370 :     function(x, y) x %*% as(y, "CsparseMatrix"))
371 :     ## NB: this is *not* needed for Tsparse & Rsparse
372 :     ## TODO: Write tests in ./tests/ which ensure that many "ops" with diagonal*
373 :     ## do indeed work by going through sparse (and *not* ddense)!
374 :    
375 :     setMethod("%*%", signature(x = "sparseMatrix", y = "diagonalMatrix"),
376 :     function(x, y) x %*% as(y, "sparseMatrix"))
377 :    
378 :    
379 :     setMethod("solve", signature(a = "diagonalMatrix", b = "missing"),
380 :     function(a, b, ...) {
381 :     a@x <- 1/ a@x
382 :     a@Dimnames <- a@Dimnames[2:1]
383 :     a
384 :     })
385 :    
386 :     solveDiag <- function(a, b, ...) {
387 :     if((n <- a@Dim[1]) != nrow(b))
388 :     stop("incompatible matrix dimensions")
389 :     ## trivially invert a 'in place' and multiply:
390 :     a@x <- 1/ a@x
391 :     a@Dimnames <- a@Dimnames[2:1]
392 :     a %*% b
393 :     }
394 :     setMethod("solve", signature(a = "diagonalMatrix", b = "matrix"),
395 :     solveDiag)
396 :     setMethod("solve", signature(a = "diagonalMatrix", b = "Matrix"),
397 :     solveDiag)
398 :    
399 :    
400 :    
401 :    
402 : maechler 1654 ### ---------------- diagonal o sparse -----------------------------
403 : maechler 1295
404 : maechler 1655
405 :     ## Use function for several signatures, in order to evade
406 :     ## ambiguous dispatch for "ddi", since there's also Arith(ddense., ddense.)
407 :     diagOdiag <- function(e1,e2) { # result should also be diagonal
408 :     r <- callGeneric(.diag.x(e1), .diag.x(e2)) # error if not "compatible"
409 :     if(is.numeric(r)) {
410 :     if(is.numeric(e2@x)) {
411 :     e2@x <- r; return(.diag.2N(e2)) }
412 :     if(!is.numeric(e1@x))
413 :     ## e.g. e1, e2 are logical;
414 :     e1 <- as(e1, "dMatrix")
415 :     }
416 :     else if(is.logical(r))
417 :     e1 <- as(e1, "lMatrix")
418 :     else stop("intermediate 'r' is of type", typeof(r))
419 :     e1@x <- r
420 :     .diag.2N(e1)
421 :     }
422 :    
423 :     setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "diagonalMatrix"),
424 :     diagOdiag)
425 :     ## These two are just for method disambiguation:
426 :     setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "diagonalMatrix"),
427 :     diagOdiag)
428 :     setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "ddiMatrix"),
429 :     diagOdiag)
430 :    
431 :     ## For almost everything else, diag* shall be treated "as sparse" :
432 : maechler 1295 ## These are cheap implementations via coercion
433 :    
434 : maechler 1655 ## for disambiguation
435 : maechler 1654 setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "sparseMatrix"),
436 :     function(e1,e2) callGeneric(as(e1, "sparseMatrix"), e2))
437 :     setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "diagonalMatrix"),
438 :     function(e1,e2) callGeneric(e1, as(e2, "sparseMatrix")))
439 : maechler 1655 ## in general:
440 :     setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "ANY"),
441 :     function(e1,e2) callGeneric(as(e1,"sparseMatrix"), e2))
442 :     setMethod("Ops", signature(e1 = "ANY", e2 = "diagonalMatrix"),
443 :     function(e1,e2) callGeneric(e1, as(e2,"sparseMatrix")))
444 : maechler 1654
445 : maechler 1655
446 :    
447 : maechler 1109 ## similar to prTriang() in ./Auxiliaries.R :
448 :     prDiag <-
449 :     function(x, digits = getOption("digits"), justify = "none", right = TRUE)
450 :     {
451 :     cf <- array(".", dim = x@Dim, dimnames = x@Dimnames)
452 :     cf[row(cf) == col(cf)] <-
453 :     sapply(diag(x), format, digits = digits, justify = justify)
454 :     print(cf, quote = FALSE, right = right)
455 :     invisible(x)
456 :     }
457 :    
458 :     setMethod("show", signature(object = "diagonalMatrix"),
459 : maechler 1592 function(object) {
460 :     d <- dim(object)
461 :     cl <- class(object)
462 :     cat(sprintf('%d x %d diagonal matrix of class "%s"\n',
463 :     d[1], d[2], cl))
464 :     prDiag(object)
465 :     })

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