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 2206 - (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 2144 ## Pkg 'spdep' had (relatively slow) versions of this as_dsCMatrix_I()
36 :     .symDiagonal <- function(n, x = rep.int(1,n), uplo = "U") {
37 :     stopifnot(n == (n. <- as.integer(n)), (n <- n.) >= 0)
38 :     if((lx <- length(x)) == 1) x <- rep.int(x, n)
39 :     else if(lx != n) stop("length(x) must be 1 or n")
40 :     cls <-
41 :     if(is.double(x)) "dsCMatrix"
42 :     else if(is.logical(x)) "lsCMatrix"
43 :     else { ## for now
44 :     storage.mode(x) <- "double"
45 :     "dsCMatrix"
46 :     }
47 :     new(cls, Dim = c(n,n), x = x, uplo = uplo,
48 :     i = if(n) 0:(n - 1L) else integer(0), p = 0:n)
49 :     }
50 :    
51 : maechler 1617 ### This is modified from a post of Bert Gunter to R-help on 1 Sep 2005.
52 :     ### Bert's code built on a post by Andy Liaw who most probably was influenced
53 :     ### by earlier posts, notably one by Scott Chasalow on S-news, 16 Jan 2002
54 :     ### who posted his bdiag() function written in December 1995.
55 : mmaechler 2206 if(FALSE)##--- currently unused:
56 :     .bdiag <- function(lst) {
57 :     ### block-diagonal matrix [a dgTMatrix] from list of matrices
58 :     stopifnot(is.list(lst), length(lst) >= 1)
59 :     dims <- sapply(lst, dim, USE.NAMES=FALSE)
60 : maechler 1617 ## make sure we had all matrices:
61 :     if(!(is.matrix(dims) && nrow(dims) == 2))
62 :     stop("some arguments are not matrices")
63 : maechler 1845 csdim <- rbind(rep.int(0L, 2),
64 : mmaechler 2206 apply(dims, 1, cumsum))
65 :     r <- new("dgTMatrix")
66 :     r@Dim <- as.integer(csdim[nrow(csdim),])
67 : maechler 1617 add1 <- matrix(1:0, 2,2)
68 : mmaechler 2206 for(i in seq_along(lst)) {
69 : maechler 1617 indx <- apply(csdim[i:(i+1),] + add1, 2, function(n) n[1]:n[2])
70 :     if(is.null(dim(indx))) ## non-square matrix
71 : mmaechler 2206 r[indx[[1]],indx[[2]]] <- lst[[i]]
72 : maechler 1617 else ## square matrix
73 : mmaechler 2206 r[indx[,1], indx[,2]] <- lst[[i]]
74 : maechler 1617 }
75 : mmaechler 2206 r
76 : maechler 1617 }
77 : mmaechler 2206 ### Doug Bates needed something like bdiag() for lower-triangular
78 :     ### (Tsparse) Matrices and provided a much more efficient implementation:
79 :     .bdiag <- function(lst) {
80 :     ### block-diagonal matrix [a dgTMatrix] from list of matrices
81 :     stopifnot(is.list(lst), (nl <- length(lst)) >= 1)
82 : maechler 1617
83 : mmaechler 2206 Tlst <- lapply(lapply(lst, Matrix:::as_Csp2), # includes "diagU2N"
84 :     as, "TsparseMatrix")
85 : maechler 2128
86 : mmaechler 2206 if(nl == 1) return(Tlst[[1]])
87 :     ## else
88 :     i_off <- c(0L, cumsum(sapply(Tlst, nrow)))
89 :     j_off <- c(0L, cumsum(sapply(Tlst, ncol)))
90 :     new("dgTMatrix", Dim = c(i_off[nl+1], j_off[nl + 1]),
91 :     i = unlist(lapply(1:nl, function(k) Tlst[[k]]@i + i_off[k])),
92 :     j = unlist(lapply(1:nl, function(k) Tlst[[k]]@j + j_off[k])),
93 :     x = unlist(lapply(Tlst, slot, "x")))
94 :     }
95 :    
96 :     bdiag <- function(...) {
97 :     if((nA <- nargs()) == 0) return(new("dgCMatrix"))
98 :     if(nA == 1 && !is.list(...))
99 :     return(as(..., "CsparseMatrix"))
100 :     alis <- if(nA == 1 && is.list(..1)) ..1 else list(...)
101 :     if(length(alis) == 1)
102 :     return(as(alis[[1]], "CsparseMatrix"))
103 :    
104 :     ## else : two or more arguments
105 :     as(.bdiag(alis), "CsparseMatrix")
106 :     }
107 :    
108 :    
109 : maechler 2136 .diag2tT <- function(from, uplo = "U", kind = .M.kind(from)) {
110 :     ## to triangular Tsparse
111 : maechler 1845 i <- if(from@diag == "U") integer(0) else seq_len(from@Dim[1]) - 1L
112 : maechler 2136 new(paste(kind, "tTMatrix", sep=''),
113 : maechler 1654 diag = from@diag, Dim = from@Dim, Dimnames = from@Dimnames,
114 : maechler 2128 uplo = uplo,
115 : maechler 1654 x = from@x, # <- ok for diag = "U" and "N" (!)
116 :     i = i, j = i)
117 :     }
118 : maechler 1109
119 : maechler 2136 .diag2sT <- function(from, uplo = "U", kind = .M.kind(from)) {
120 :     ## to symmetric Tsparse
121 : maechler 2128 n <- from@Dim[1]
122 :     i <- seq_len(n) - 1L
123 :     new(paste(kind, "sTMatrix", sep=''),
124 : maechler 1845 Dim = from@Dim, Dimnames = from@Dimnames,
125 : maechler 2128 i = i, j = i, uplo = uplo,
126 :     x = if(from@diag == "N") from@x else ## "U"-diag
127 :     rep.int(switch(kind,
128 :     "d" = 1.,
129 :     "l" =,
130 :     "n" = TRUE,
131 :     ## otherwise
132 :     stop("'", kind,"' kind not yet implemented")), n))
133 : maechler 1845 }
134 :    
135 : maechler 2128 ## diagonal -> triangular, upper / lower depending on "partner":
136 : maechler 2144 diag2tT.u <- function(d, x, kind = .M.kind(d))
137 :     .diag2tT(d, uplo = if(is(x,"triangularMatrix")) x@uplo else "U", kind)
138 : maechler 2128
139 : mmaechler 2175 ## diagonal -> sparse {triangular OR symmetric} (upper / lower) depending on "partner":
140 :     diag2Tsmart <- function(d, x, kind = .M.kind(d)) {
141 :     clx <- getClassDef(class(x))
142 :     if(extends(clx, "symmetricMatrix"))
143 :     .diag2sT(d, uplo = x@uplo, kind)
144 :     else
145 :     .diag2tT(d, uplo = if(extends(clx,"triangularMatrix")) x@uplo else "U", kind)
146 :     }
147 : maechler 2136
148 : mmaechler 2175
149 : maechler 2136 ## In order to evade method dispatch ambiguity warnings,
150 :     ## and because we can save a .M.kind() call, we use this explicit
151 :     ## "hack" instead of signature x = "diagonalMatrix" :
152 :     ##
153 :     ## ddi*:
154 :     diag2tT <- function(from) .diag2tT(from, "U", "d")
155 :     setAs("ddiMatrix", "triangularMatrix", diag2tT)
156 :     setAs("ddiMatrix", "sparseMatrix", diag2tT)
157 : maechler 1805 ## needed too (otherwise <dense> -> Tsparse is taken):
158 : maechler 2136 setAs("ddiMatrix", "TsparseMatrix", diag2tT)
159 :     setAs("ddiMatrix", "CsparseMatrix",
160 :     function(from) as(.diag2tT(from, "U", "d"), "CsparseMatrix"))
161 :     setAs("ddiMatrix", "symmetricMatrix",
162 :     function(from) .diag2sT(from, "U", "d"))
163 :     ##
164 :     ## ldi*:
165 :     diag2tT <- function(from) .diag2tT(from, "U", "l")
166 :     setAs("ldiMatrix", "triangularMatrix", diag2tT)
167 :     setAs("ldiMatrix", "sparseMatrix", diag2tT)
168 :     ## needed too (otherwise <dense> -> Tsparse is taken):
169 :     setAs("ldiMatrix", "TsparseMatrix", diag2tT)
170 :     setAs("ldiMatrix", "CsparseMatrix",
171 :     function(from) as(.diag2tT(from, "U", "l"), "CsparseMatrix"))
172 :     setAs("ldiMatrix", "symmetricMatrix",
173 :     function(from) .diag2sT(from, "U", "l"))
174 : maechler 1654
175 : maechler 1845
176 : maechler 2128 setAs("diagonalMatrix", "nMatrix",
177 :     function(from) {
178 :     n <- from@Dim[1]
179 :     i <- if(from@diag == "U") integer(0) else which(isN0(from@x)) - 1L
180 :     new("ntTMatrix", i = i, j = i, diag = from@diag,
181 :     Dim = from@Dim, Dimnames = from@Dimnames)
182 :     })
183 :    
184 :    
185 : mmaechler 2175 ## Cheap fast substitute for diag() which *does* preserve the mode of x :
186 :     mkDiag <- function(x, n) {
187 :     y <- matrix(as0(mod=mode(x)), n,n)
188 :     if (n > 0) y[1L + 0:(n - 1L) * (n + 1L)] <- x
189 :     y
190 :     }
191 :    
192 : maechler 1109 setAs("diagonalMatrix", "matrix",
193 :     function(from) {
194 : mmaechler 2175 ## want "ldiMatrix" -> <logical> "matrix" :
195 :     mkDiag(if(from@diag == "U") as1(from@x) else from@x,
196 :     n = from@Dim[1])
197 : maechler 1109 })
198 :    
199 : maechler 2098 setMethod("as.vector", signature(x = "diagonalMatrix", mode="missing"),
200 :     function(x, mode) {
201 :     n <- x@Dim[1]
202 : mmaechler 2175 mod.x <- mode(x@x)
203 :     r <- vector(mod.x, length = n^2)
204 : maechler 2098 if(n)
205 :     r[1 + 0:(n - 1) * (n + 1)] <-
206 : mmaechler 2175 if(x@diag == "U") as1(mod=mod.x) else x@x
207 : maechler 2098 r
208 :     })
209 :    
210 : maechler 1654 setAs("diagonalMatrix", "generalMatrix", # prefer sparse:
211 : maechler 2128 function(from) as(as(from, "CsparseMatrix"), "generalMatrix"))
212 : maechler 1174
213 : mmaechler 2185 .diag.x <- function(m) if(m@diag == "U") rep.int(as1(m@x), m@Dim[1]) else m@x
214 : maechler 1655
215 :     .diag.2N <- function(m) {
216 :     if(m@diag == "U") m@diag <- "N"
217 :     m
218 :     }
219 :    
220 : maechler 1174 setAs("ddiMatrix", "dgeMatrix",
221 : mmaechler 2183 function(from) .Call(dup_mMatrix_as_dgeMatrix, from))
222 :     setAs("ddiMatrix", "ddenseMatrix",
223 :     function(from) as(as(from, "triangularMatrix"),"denseMatrix"))
224 :     setAs("ldiMatrix", "ldenseMatrix",
225 :     function(from) as(as(from, "triangularMatrix"),"denseMatrix"))
226 : maechler 1174
227 : mmaechler 2183
228 : maechler 1109 setAs("matrix", "diagonalMatrix",
229 :     function(from) {
230 : maechler 1295 d <- dim(from)
231 : maechler 1109 if(d[1] != (n <- d[2])) stop("non-square matrix")
232 :     if(any(from[row(from) != col(from)] != 0))
233 :     stop("has non-zero off-diagonal entries")
234 : maechler 1295 x <- diag(from)
235 :     if(is.logical(x)) {
236 :     cl <- "ldiMatrix"
237 :     uni <- all(x)
238 :     } else {
239 :     cl <- "ddiMatrix"
240 :     uni <- all(x == 1)
241 :     storage.mode(x) <- "double"
242 : maechler 1575 } ## TODO: complex
243 : maechler 1295 new(cl, Dim = c(n,n), diag = if(uni) "U" else "N",
244 :     x = if(uni) x[FALSE] else x)
245 : maechler 1109 })
246 :    
247 :     ## ``generic'' coercion to diagonalMatrix : build on isDiagonal() and diag()
248 :     setAs("Matrix", "diagonalMatrix",
249 :     function(from) {
250 :     d <- dim(from)
251 :     if(d[1] != (n <- d[2])) stop("non-square matrix")
252 :     if(!isDiagonal(from)) stop("matrix is not diagonal")
253 :     ## else:
254 :     x <- diag(from)
255 :     if(is.logical(x)) {
256 :     cl <- "ldiMatrix"
257 :     uni <- all(x)
258 :     } else {
259 :     cl <- "ddiMatrix"
260 :     uni <- all(x == 1)
261 :     storage.mode(x) <- "double"
262 :     }
263 :     new(cl, Dim = c(n,n), diag = if(uni) "U" else "N",
264 :     x = if(uni) x[FALSE] else x)
265 :     })
266 :    
267 : maechler 1708
268 : mmaechler 2185 setMethod("diag", signature(x = "diagonalMatrix"),
269 :     function(x = 1, nrow, ncol) .diag.x(x))
270 : maechler 1708
271 : maechler 2098 subDiag <- function(x, i, j, ..., drop) {
272 : maechler 1799 x <- as(x, "sparseMatrix")
273 :     x <- if(missing(i))
274 :     x[, j, drop=drop]
275 :     else if(missing(j))
276 :     x[i, , drop=drop]
277 :     else
278 :     x[i,j, drop=drop]
279 : maechler 2120 if(isS4(x) && isDiagonal(x)) as(x, "diagonalMatrix") else x
280 : maechler 1799 }
281 :    
282 :     setMethod("[", signature(x = "diagonalMatrix", i = "index",
283 :     j = "index", drop = "logical"), subDiag)
284 :     setMethod("[", signature(x = "diagonalMatrix", i = "index",
285 :     j = "missing", drop = "logical"),
286 : maechler 2098 function(x, i, j, ..., drop) subDiag(x, i=i, drop=drop))
287 : maechler 1799 setMethod("[", signature(x = "diagonalMatrix", i = "missing",
288 :     j = "index", drop = "logical"),
289 : maechler 2098 function(x, i, j, ..., drop) subDiag(x, j=j, drop=drop))
290 : maechler 1799
291 : maechler 1617 ## When you assign to a diagonalMatrix, the result should be
292 : maechler 1708 ## diagonal or sparse ---
293 :     ## FIXME: this now fails because the "denseMatrix" methods come first in dispatch
294 : maechler 2098 ## Only(?) current bug: x[i] <- value is wrong when i is *vector*
295 :     replDiag <- function(x, i, j, ..., value) {
296 : maechler 1710 x <- as(x, "sparseMatrix")
297 :     if(missing(i))
298 :     x[, j] <- value
299 : maechler 2098 else if(missing(j)) { ## x[i , ] <- v *OR* x[i] <- v
300 :     na <- nargs()
301 :     ## message("diagnosing replDiag() -- nargs()= ", na)
302 :     if(na == 4)
303 :     x[i, ] <- value
304 :     else if(na == 3)
305 :     x[i] <- value
306 :     else stop("Internal bug: nargs()=",na,"; please report")
307 :     } else
308 : maechler 1710 x[i,j] <- value
309 :     if(isDiagonal(x)) as(x, "diagonalMatrix") else x
310 :     }
311 : maechler 1617
312 : maechler 1710 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",
313 :     j = "index", value = "replValue"), replDiag)
314 : maechler 2098
315 : maechler 1710 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",
316 :     j = "missing", value = "replValue"),
317 : maechler 2098 function(x,i,j, ..., value) {
318 :     ## message("before replDiag() -- nargs()= ", nargs())
319 :     if(nargs() == 3)
320 :     replDiag(x, i=i, value=value)
321 :     else ## nargs() == 4 :
322 :     replDiag(x, i=i, , value=value)
323 :     })
324 :    
325 : mmaechler 2192 setReplaceMethod("[", signature(x = "diagonalMatrix",
326 :     i = "matrix", # 2-col.matrix
327 : maechler 2096 j = "missing", value = "replValue"),
328 : maechler 2098 function(x,i,j, ..., value) {
329 : maechler 2096 if(ncol(i) == 2) {
330 :     if(all((ii <- i[,1]) == i[,2])) { # replace in diagonal only
331 : mmaechler 2192 if(x@diag == "U") {
332 :     one <- as1(x@x)
333 :     if(any(value != one | is.na(value))) {
334 :     x@diag <- "N"
335 :     x@x <- rep.int(one, x@Dim[1])
336 :     }
337 :     }
338 : maechler 2096 x@x[ii] <- value
339 :     x
340 :     } else { ## no longer diagonal, but remain sparse:
341 :     x <- as(x, "sparseMatrix")
342 :     x[i] <- value
343 :     x
344 :     }
345 :     }
346 :     else { # behave as "base R": use as if vector
347 :     x <- as(x, "matrix")
348 :     x[i] <- value
349 :     Matrix(x)
350 :     }
351 :     })
352 :    
353 : maechler 1710 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing",
354 :     j = "index", value = "replValue"),
355 : maechler 2098 function(x,i,j, ..., value) replDiag(x, j=j, value=value))
356 : maechler 1710
357 :    
358 : maechler 1109 setMethod("t", signature(x = "diagonalMatrix"),
359 :     function(x) { x@Dimnames <- x@Dimnames[2:1] ; x })
360 :    
361 : maechler 1331 setMethod("isDiagonal", signature(object = "diagonalMatrix"),
362 :     function(object) TRUE)
363 :     setMethod("isTriangular", signature(object = "diagonalMatrix"),
364 :     function(object) TRUE)
365 : maechler 1109 setMethod("isSymmetric", signature(object = "diagonalMatrix"),
366 : maechler 2112 function(object, ...) TRUE)
367 : maechler 1109
368 : maechler 2112 setMethod("symmpart", signature(x = "diagonalMatrix"), function(x) x)
369 :     setMethod("skewpart", signature(x = "diagonalMatrix"), setZero)
370 :    
371 : maechler 2133 setMethod("chol", signature(x = "ddiMatrix"),
372 :     function(x, pivot, ...) {
373 : mmaechler 2183 if(x@diag == "U") return(x)
374 :     ## else
375 : maechler 2133 if(any(x@x < 0))
376 :     stop("chol() is undefined for diagonal matrix with negative entries")
377 : maechler 1654 x@x <- sqrt(x@x)
378 :     x
379 :     })
380 :     ## chol(L) is L for logical diagonal:
381 : maechler 2133 setMethod("chol", signature(x = "ldiMatrix"), function(x, pivot, ...) x)
382 : maechler 1654
383 : mmaechler 2175 setMethod("determinant", signature(x = "diagonalMatrix", logarithm = "logical"),
384 : mmaechler 2185 function(x, logarithm, ...) mkDet(.diag.x(x), logarithm))
385 : mmaechler 2175
386 : mmaechler 2183 setMethod("norm", signature(x = "diagonalMatrix", type = "character"),
387 :     function(x, type, ...) {
388 :     if((n <- x@Dim[1]) == 0) return(0) # as for "sparseMatrix"
389 :     type <- toupper(substr(type[1], 1, 1))
390 :     isU <- (x@diag == "U") # unit-diagonal
391 :     if(type == "F") sqrt(if(isU) n else sum(x@x^2))
392 :     else { ## norm == "I","1","O","M" :
393 :     if(isU) 1 else max(abs(x@x))
394 :     }
395 :     })
396 :    
397 :    
398 :    
399 : maechler 1109 ## Basic Matrix Multiplication {many more to add}
400 : maechler 1654 ## ---------------------
401 :     ## Note that "ldi" logical are treated as numeric
402 : maechler 1109 diagdiagprod <- function(x, y) {
403 : mmaechler 2183 n <- dimCheck(x,y)[1]
404 : maechler 1109 if(x@diag != "U") {
405 : maechler 1654 if(y@diag != "U") {
406 :     nx <- x@x * y@x
407 :     if(is.numeric(nx) && !is.numeric(x@x))
408 :     x <- as(x, "dMatrix")
409 :     x@x <- as.numeric(nx)
410 :     }
411 :     return(x)
412 : maechler 1109 } else ## x is unit diagonal
413 :     return(y)
414 :     }
415 :    
416 : maechler 1654 setMethod("%*%", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
417 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
418 :    
419 : maechler 1654 formals(diagdiagprod) <- alist(x=, y=x)
420 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
421 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
422 : maechler 1654 setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
423 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
424 : maechler 1654 setMethod("crossprod", signature(x = "diagonalMatrix", y = "missing"),
425 :     diagdiagprod, valueClass = "ddiMatrix")
426 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "missing"),
427 :     diagdiagprod, valueClass = "ddiMatrix")
428 : maechler 1109
429 :    
430 :     diagmatprod <- function(x, y) {
431 : mmaechler 2183 ## x is diagonalMatrix
432 : maechler 1109 dx <- dim(x)
433 :     dy <- dim(y)
434 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
435 :     n <- dx[1]
436 :     as(if(x@diag == "U") y else x@x * y, "Matrix")
437 :     }
438 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "matrix"),
439 : maechler 1654 diagmatprod)
440 : mmaechler 2183 ## sneaky .. :
441 : maechler 1109 formals(diagmatprod) <- alist(x=, y=NULL)
442 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "matrix"),
443 : maechler 1654 diagmatprod)
444 : maechler 1109
445 : mmaechler 2183 diagGeprod <- function(x, y) {
446 : maechler 1109 dx <- dim(x)
447 :     dy <- dim(y)
448 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
449 :     if(x@diag != "U")
450 :     y@x <- x@x * y@x
451 :     y
452 :     }
453 : mmaechler 2183 setMethod("%*%", signature(x= "diagonalMatrix", y= "dgeMatrix"), diagGeprod)
454 :     setMethod("%*%", signature(x= "diagonalMatrix", y= "lgeMatrix"), diagGeprod)
455 :     formals(diagGeprod) <- alist(x=, y=NULL)
456 : maechler 1109 setMethod("crossprod", signature(x = "diagonalMatrix", y = "dgeMatrix"),
457 : mmaechler 2183 diagGeprod, valueClass = "dgeMatrix")
458 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "lgeMatrix"),
459 :     diagGeprod)
460 : maechler 1109
461 : mmaechler 2183 matdiagprod <- function(x, y) {
462 :     dx <- dim(x)
463 :     dy <- dim(y)
464 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
465 :     Matrix(if(y@diag == "U") x else x * rep(y@x, each = dx[1]))
466 :     }
467 : maechler 1109 setMethod("%*%", signature(x = "matrix", y = "diagonalMatrix"),
468 : mmaechler 2183 matdiagprod)
469 :     formals(matdiagprod) <- alist(x=, y=NULL)
470 :     setMethod("tcrossprod", signature(x = "matrix", y = "diagonalMatrix"),
471 :     matdiagprod)
472 : maechler 1109
473 : mmaechler 2183 gediagprod <- function(x, y) {
474 :     dx <- dim(x)
475 :     dy <- dim(y)
476 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
477 :     if(y@diag == "N")
478 :     x@x <- x@x * rep(y@x, each = dx[1])
479 :     x
480 :     }
481 :     setMethod("%*%", signature(x= "dgeMatrix", y= "diagonalMatrix"), gediagprod)
482 :     setMethod("%*%", signature(x= "lgeMatrix", y= "diagonalMatrix"), gediagprod)
483 :     formals(gediagprod) <- alist(x=, y=NULL)
484 :     setMethod("tcrossprod", signature(x = "dgeMatrix", y = "diagonalMatrix"),
485 :     gediagprod)
486 :     setMethod("tcrossprod", signature(x = "lgeMatrix", y = "diagonalMatrix"),
487 :     gediagprod)
488 : maechler 1109
489 : maechler 1295 ## crossprod {more of these}
490 : maechler 1109
491 : maechler 1295 ## tcrossprod --- all are not yet there: do the dense ones here:
492 : maechler 1109
493 : mmaechler 2183 setMethod("%*%", signature(x = "diagonalMatrix", y = "denseMatrix"),
494 :     function(x, y) if(x@diag == "U") y else x %*% as(y, "generalMatrix"))
495 :     setMethod("%*%", signature(x = "denseMatrix", y = "diagonalMatrix"),
496 :     function(x, y) if(y@diag == "U") x else as(x, "generalMatrix") %*% y)
497 :    
498 :    
499 : maechler 1295 ## FIXME:
500 :     ## setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "denseMatrix"),
501 :     ## function(x, y = NULL) {
502 :     ## })
503 : maechler 1109
504 : maechler 1295 ## setMethod("tcrossprod", signature(x = "denseMatrix", y = "diagonalMatrix"),
505 :     ## function(x, y = NULL) {
506 :     ## })
507 : maechler 1109
508 : maechler 1799 setMethod("crossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
509 : maechler 2123 function(x, y = NULL) crossprod(as(x, "sparseMatrix"), y))
510 : maechler 1295
511 : maechler 1799 setMethod("crossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
512 : maechler 2123 function(x, y = NULL) crossprod(x, as(y, "sparseMatrix")))
513 : maechler 1799
514 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
515 : maechler 2123 function(x, y = NULL) tcrossprod(as(x, "sparseMatrix"), y))
516 : maechler 1799
517 :     setMethod("tcrossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
518 : maechler 2123 function(x, y = NULL) tcrossprod(x, as(y, "sparseMatrix")))
519 : maechler 1799
520 :    
521 :     ## FIXME?: In theory, this can be done *FASTER*, in some cases, via tapply1()
522 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "sparseMatrix"),
523 :     function(x, y) as(x, "sparseMatrix") %*% y)
524 : mmaechler 2183 setMethod("%*%", signature(x = "sparseMatrix", y = "diagonalMatrix"),
525 :     function(x, y) x %*% as(y, "sparseMatrix"))
526 : maechler 1799 ## NB: The previous is *not* triggering for "ddi" o "dgC" (= distance 3)
527 :     ## since there's a "ddense" o "Csparse" at dist. 2 => triggers first.
528 :     ## ==> do this:
529 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "CsparseMatrix"),
530 :     function(x, y) as(x, "CsparseMatrix") %*% y)
531 :     setMethod("%*%", signature(x = "CsparseMatrix", y = "diagonalMatrix"),
532 :     function(x, y) x %*% as(y, "CsparseMatrix"))
533 :     ## NB: this is *not* needed for Tsparse & Rsparse
534 :     ## TODO: Write tests in ./tests/ which ensure that many "ops" with diagonal*
535 :     ## do indeed work by going through sparse (and *not* ddense)!
536 :    
537 :    
538 :    
539 :     setMethod("solve", signature(a = "diagonalMatrix", b = "missing"),
540 :     function(a, b, ...) {
541 :     a@x <- 1/ a@x
542 :     a@Dimnames <- a@Dimnames[2:1]
543 :     a
544 :     })
545 :    
546 :     solveDiag <- function(a, b, ...) {
547 :     if((n <- a@Dim[1]) != nrow(b))
548 :     stop("incompatible matrix dimensions")
549 :     ## trivially invert a 'in place' and multiply:
550 :     a@x <- 1/ a@x
551 :     a@Dimnames <- a@Dimnames[2:1]
552 :     a %*% b
553 :     }
554 :     setMethod("solve", signature(a = "diagonalMatrix", b = "matrix"),
555 :     solveDiag)
556 :     setMethod("solve", signature(a = "diagonalMatrix", b = "Matrix"),
557 :     solveDiag)
558 :    
559 : maechler 2106 ## Schur() ---> ./eigen.R
560 : maechler 1799
561 :    
562 :    
563 : mmaechler 2175 ###---------------- <Ops> (<Arith>, <Logic>, <Compare> ) ----------------------
564 : maechler 1295
565 : maechler 1655 ## Use function for several signatures, in order to evade
566 :     ## ambiguous dispatch for "ddi", since there's also Arith(ddense., ddense.)
567 : mmaechler 2175 diagOdiag <- function(e1,e2) {
568 :     ## result should also be diagonal _ if possible _
569 : maechler 1655 r <- callGeneric(.diag.x(e1), .diag.x(e2)) # error if not "compatible"
570 : mmaechler 2175 ## Check what happens with non-diagonals, i.e. (0 o 0), (FALSE o 0), ...:
571 :     r00 <- callGeneric(if(is.numeric(e1@x)) 0 else FALSE,
572 :     if(is.numeric(e2@x)) 0 else FALSE)
573 :     if(is0(r00)) { ## r00 == 0 or FALSE --- result *is* diagonal
574 :     if(is.numeric(r)) {
575 :     if(is.numeric(e2@x)) {
576 :     e2@x <- r; return(.diag.2N(e2)) }
577 :     if(!is.numeric(e1@x))
578 :     ## e.g. e1, e2 are logical;
579 :     e1 <- as(e1, "dMatrix")
580 :     }
581 :     else if(is.logical(r))
582 :     e1 <- as(e1, "lMatrix")
583 :     else stop("intermediate 'r' is of type", typeof(r))
584 :     e1@x <- r
585 :     .diag.2N(e1)
586 : maechler 1655 }
587 : mmaechler 2175 else { ## result not diagonal, but at least symmetric:
588 :     isNum <- (is.numeric(r) || is.numeric(r00))
589 :     isLog <- (is.logical(r) || is.logical(r00))
590 :    
591 :     if(getOption("verbose"))
592 :     message("exploding <diag> o <diag> into dense matrix")
593 :     d <- e1@Dim
594 :     n <- d[1]
595 :     stopifnot(length(r) == n)
596 :     xx <- as.vector(matrix(rbind(r, matrix(r00,n,n)), n,n))
597 :     newcl <-
598 :     paste(if(isNum) "d" else if(isLog) {
599 :     if(!any(is.na(r)) && !any(is.na(r00))) "n" else "l"
600 :     } else stop("not yet implemented .. please report")
601 :     ,
602 :     "syMatrix", sep='')
603 :    
604 :     new(newcl, Dim = e1@Dim, Dimnames = e1@Dimnames, x = xx)
605 :     }
606 : maechler 1655 }
607 :    
608 : mmaechler 2175 ### This would be *the* way, but we get tons of "ambiguous method dispatch"
609 : mmaechler 2185 ## we use this hack instead of signature x = "diagonalMatrix" :
610 :     diCls <- names(getClass("diagonalMatrix")@subclasses)
611 : mmaechler 2175 if(FALSE) {
612 : maechler 1655 setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "diagonalMatrix"),
613 :     diagOdiag)
614 : mmaechler 2175 } else { ## These are just for method disambiguation:
615 :     for(c1 in diCls)
616 :     for(c2 in diCls)
617 :     setMethod("Ops", signature(e1 = c1, e2 = c2), diagOdiag)
618 :     }
619 : maechler 1655
620 : maechler 1845 ## FIXME: diagonal o triangular |--> triangular
621 :     ## ----- diagonal o symmetric |--> symmetric
622 :     ## {also when other is sparse: do these "here" --
623 :     ## before conversion to sparse, since that loses "diagonality"}
624 :    
625 : maechler 1655 ## For almost everything else, diag* shall be treated "as sparse" :
626 : maechler 1295 ## These are cheap implementations via coercion
627 :    
628 : maechler 2144 ## For disambiguation --- define this for "sparseMatrix" , then for "ANY";
629 :     ## and because we can save an .M.kind() call, we use this explicit
630 :     ## "hack" for all diagonalMatrix *subclasses* instead of just "diagonalMatrix" :
631 :     ##
632 :     ## ddi*:
633 :     setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "sparseMatrix"),
634 : mmaechler 2175 function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "d"), e2))
635 : maechler 2144 setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "ddiMatrix"),
636 : mmaechler 2175 function(e1,e2) callGeneric(e1, diag2Tsmart(e2,e1, "d")))
637 : maechler 2144 ## ldi*
638 :     setMethod("Ops", signature(e1 = "ldiMatrix", e2 = "sparseMatrix"),
639 : mmaechler 2175 function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "l"), e2))
640 : maechler 2144 setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "ldiMatrix"),
641 : mmaechler 2175 function(e1,e2) callGeneric(e1, diag2Tsmart(e2,e1, "l")))
642 : maechler 1654
643 : mmaechler 2175 ## Ops: Arith --> numeric : "dMatrix"
644 :     ## Compare --> logical
645 :     ## Logic --> logical: "lMatrix"
646 :    
647 : maechler 2144 ## other = "numeric" : stay diagonal if possible
648 : mmaechler 2175 ## ddi*: Arith: result numeric, potentially ddiMatrix
649 :     setMethod("Arith", signature(e1 = "ddiMatrix", e2 = "numeric"),
650 : maechler 2144 function(e1,e2) {
651 : mmaechler 2203 n <- e1@Dim[1]; nsq <- n^2
652 : maechler 2144 f0 <- callGeneric(0, e2)
653 :     if(all(is0(f0))) { # remain diagonal
654 : maechler 2157 L1 <- (le <- length(e2)) == 1L
655 :     if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
656 :     if(e1@diag == "U" && any((r <- callGeneric(1, e2)) != 1)) {
657 : maechler 2144 e1@diag <- "N"
658 : maechler 2157 if(L1) r <- rep.int(r, n)
659 :     } else
660 : maechler 2144 r <- callGeneric(e1@x, e2)
661 : mmaechler 2197 e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
662 : maechler 2144 return(e1)
663 :     }
664 :     callGeneric(diag2tT.u(e1,e2, "d"), e2)
665 :     })
666 : maechler 1655
667 : mmaechler 2175 setMethod("Arith", signature(e1 = "numeric", e2 = "ddiMatrix"),
668 : maechler 2144 function(e1,e2) {
669 : mmaechler 2203 n <- e2@Dim[1]; nsq <- n^2
670 : maechler 2144 f0 <- callGeneric(e1, 0)
671 :     if(all(is0(f0))) { # remain diagonal
672 : maechler 2157 L1 <- (le <- length(e1)) == 1L
673 :     if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
674 :     if(e2@diag == "U" && any((r <- callGeneric(e1, 1)) != 1)) {
675 : maechler 2144 e2@diag <- "N"
676 : maechler 2157 if(L1) r <- rep.int(r, n)
677 :     } else
678 : maechler 2144 r <- callGeneric(e1, e2@x)
679 : mmaechler 2197 e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
680 : maechler 2144 return(e2)
681 :     }
682 :     callGeneric(e1, diag2tT.u(e2,e1, "d"))
683 :     })
684 : mmaechler 2175
685 :     ## ldi* Arith --> result numeric, potentially ddiMatrix
686 :     setMethod("Arith", signature(e1 = "ldiMatrix", e2 = "numeric"),
687 :     function(e1,e2) {
688 : mmaechler 2203 n <- e1@Dim[1]; nsq <- n^2
689 : mmaechler 2175 f0 <- callGeneric(0, e2)
690 :     if(all(is0(f0))) { # remain diagonal
691 :     L1 <- (le <- length(e2)) == 1L
692 :     if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
693 :     if(e1@diag == "U" && any((r <- callGeneric(1, e2)) != 1)) {
694 :     e1@diag <- "N"
695 :     if(L1) r <- rep.int(r, n)
696 :     } else
697 :     r <- callGeneric(e1@x, e2)
698 :     e1 <- copyClass(e1, "ddiMatrix", c("diag", "Dim", "Dimnames"))
699 : mmaechler 2197 e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
700 : mmaechler 2175 return(e1)
701 :     }
702 :     callGeneric(diag2tT.u(e1,e2, "d"), e2)
703 :     })
704 :    
705 :     setMethod("Arith", signature(e1 = "numeric", e2 = "ldiMatrix"),
706 :     function(e1,e2) {
707 : mmaechler 2203 n <- e2@Dim[1]; nsq <- n^2
708 : mmaechler 2175 f0 <- callGeneric(e1, 0)
709 :     if(all(is0(f0))) { # remain diagonal
710 :     L1 <- (le <- length(e1)) == 1L
711 :     if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
712 :     if(e2@diag == "U" && any((r <- callGeneric(e1, 1)) != 1)) {
713 :     e2@diag <- "N"
714 :     if(L1) r <- rep.int(r, n)
715 :     } else
716 :     r <- callGeneric(e1, e2@x)
717 :     e2 <- copyClass(e2, "ddiMatrix", c("diag", "Dim", "Dimnames"))
718 : mmaechler 2197 e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
719 : mmaechler 2175 return(e2)
720 :     }
721 :     callGeneric(e1, diag2tT.u(e2,e1, "d"))
722 :     })
723 :    
724 :     ## ddi*: for "Ops" without Arith --> result logical, potentially ldi
725 :     setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "numeric"),
726 :     function(e1,e2) {
727 : mmaechler 2203 n <- e1@Dim[1]; nsq <- n^2
728 : mmaechler 2175 f0 <- callGeneric(0, e2)
729 :     if(all(is0(f0))) { # remain diagonal
730 :     L1 <- (le <- length(e2)) == 1L
731 :     if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
732 :     if(e1@diag == "U" && any((r <- callGeneric(1, e2)) != 1)) {
733 :     e1@diag <- "N"
734 :     if(L1) r <- rep.int(r, n)
735 :     } else
736 :     r <- callGeneric(e1@x, e2)
737 :     e1 <- copyClass(e1, "ldiMatrix", c("diag", "Dim", "Dimnames"))
738 : mmaechler 2197 e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
739 : mmaechler 2175 return(e1)
740 :     }
741 :     callGeneric(diag2tT.u(e1,e2, "l"), e2)
742 :     })
743 :    
744 :     setMethod("Ops", signature(e1 = "numeric", e2 = "ddiMatrix"),
745 :     function(e1,e2) {
746 : mmaechler 2203 n <- e2@Dim[1]; nsq <- n^2
747 : mmaechler 2175 f0 <- callGeneric(e1, 0)
748 :     if(all(is0(f0))) { # remain diagonal
749 :     L1 <- (le <- length(e1)) == 1L
750 :     if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
751 :     if(e2@diag == "U" && any((r <- callGeneric(e1, 1)) != 1)) {
752 :     e2@diag <- "N"
753 :     if(L1) r <- rep.int(r, n)
754 :     } else
755 :     r <- callGeneric(e1, e2@x)
756 :     e2 <- copyClass(e2, "ldiMatrix", c("diag", "Dim", "Dimnames"))
757 : mmaechler 2197 e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
758 : mmaechler 2175 return(e2)
759 :     }
760 :     callGeneric(e1, diag2tT.u(e2,e1, "l"))
761 :     })
762 :    
763 :     ## ldi*: for "Ops" without Arith --> result logical, potentially ldi
764 : maechler 2144 setMethod("Ops", signature(e1 = "ldiMatrix", e2 = "numeric"),
765 :     function(e1,e2) {
766 : mmaechler 2203 n <- e1@Dim[1]; nsq <- n^2
767 : maechler 2144 f0 <- callGeneric(FALSE, e2)
768 :     if(all(is0(f0))) { # remain diagonal
769 : maechler 2157 L1 <- (le <- length(e2)) == 1L
770 :     if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
771 :     if(e1@diag == "U" && any((r <- callGeneric(TRUE, e2)) != 1)) {
772 : maechler 2144 e1@diag <- "N"
773 : maechler 2157 if(L1) r <- rep.int(r, n)
774 :     } else
775 : maechler 2144 r <- callGeneric(e1@x, e2)
776 : mmaechler 2197 e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
777 : maechler 2144 return(e1)
778 :     }
779 :     callGeneric(diag2tT.u(e1,e2, "l"), e2)
780 :     })
781 : maechler 1655
782 : maechler 2144 setMethod("Ops", signature(e1 = "numeric", e2 = "ldiMatrix"),
783 :     function(e1,e2) {
784 : mmaechler 2203 n <- e2@Dim[1]; nsq <- n^2
785 : maechler 2144 f0 <- callGeneric(e1, FALSE)
786 :     if(all(is0(f0))) { # remain diagonal
787 : maechler 2157 L1 <- (le <- length(e1)) == 1L
788 :     if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
789 :     if(e2@diag == "U" && any((r <- callGeneric(e1, TRUE)) != 1)) {
790 : maechler 2144 e2@diag <- "N"
791 : maechler 2157 if(L1) r <- rep.int(r, n)
792 :     } else
793 : maechler 2144 r <- callGeneric(e1, e2@x)
794 : mmaechler 2197 e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
795 : maechler 2144 return(e2)
796 :     }
797 :     callGeneric(e1, diag2tT.u(e2,e1, "l"))
798 :     })
799 :    
800 :    
801 :    
802 :     ## Not {"sparseMatrix", "numeric} : {"denseMatrix", "matrix", ... }
803 : mmaechler 2196 for(other in c("ANY", "Matrix", "dMatrix")) {
804 :     ## ddi*:
805 :     setMethod("Ops", signature(e1 = "ddiMatrix", e2 = other),
806 :     function(e1,e2) callGeneric(diag2tT.u(e1,e2, "d"), e2))
807 :     setMethod("Ops", signature(e1 = other, e2 = "ddiMatrix"),
808 :     function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1, "d")))
809 :     ## ldi*:
810 :     setMethod("Ops", signature(e1 = "ldiMatrix", e2 = other),
811 :     function(e1,e2) callGeneric(diag2tT.u(e1,e2, "l"), e2))
812 :     setMethod("Ops", signature(e1 = other, e2 = "ldiMatrix"),
813 :     function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1, "l")))
814 :     }
815 : mmaechler 2175 ## This should *not* dispatch to <dense> methods (in ./Ops.R ), as
816 :     ## FALSE & <anything> |-> FALSE : hence result should be diagonal:
817 :     for(cl in diCls) {
818 :     setMethod("&", signature(e1 = cl, e2 = "ANY"),
819 :     function(e1,e2) e1 & as(e2,"Matrix"))
820 :     setMethod("&", signature(e1 = "ANY", e2 = cl),
821 :     function(e1,e2) as(e1,"Matrix") & e2)
822 :     for(c2 in c("denseMatrix", "Matrix")) {
823 :     setMethod("&", signature(e1 = cl, e2 = c2),
824 :     function(e1,e2) e1 & Diagonal(x = diag(e2)))
825 :     setMethod("&", signature(e1 = c2, e2 = cl),
826 :     function(e1,e2) Diagonal(x = diag(e1)) & e2)
827 :     }
828 :     }
829 : maechler 2144
830 :    
831 : mmaechler 2175 ### "Summary" : "max" "min" "range" "prod" "sum" "any" "all"
832 :     ### ---------- any, all: separately here
833 :     for(cl in diCls) {
834 :     setMethod("any", cl,
835 :     function (x, ..., na.rm) {
836 :     if(any(x@Dim == 0)) FALSE
837 :     else if(x@diag == "U") TRUE else any(x@x, ..., na.rm = na.rm)
838 :     })
839 :     setMethod("all", cl, function (x, ..., na.rm) any(x@Dim == 0))
840 :     setMethod("prod", cl, function (x, ..., na.rm) as.numeric(any(x@Dim == 0)))
841 :    
842 :     setMethod("sum", cl,
843 :     function(x, ..., na.rm) {
844 :     r <- sum(x@x, ..., na.rm = na.rm)# double or integer, correctly
845 :     if(x@diag == "U" && !is.na(r)) r + x@Dim[1] else r
846 :     })
847 :     }
848 :    
849 :     ## The remaining ones are max, min, range :
850 :    
851 :     setMethod("Summary", "ddiMatrix",
852 :     function(x, ..., na.rm) {
853 :     if(any(x@Dim == 0)) callGeneric(numeric(0), ..., na.rm=na.rm)
854 :     else if(x@diag == "U")
855 :     callGeneric(x@x, 0, 1, ..., na.rm=na.rm)
856 :     else callGeneric(x@x, 0, ..., na.rm=na.rm)
857 :     })
858 :     setMethod("Summary", "ldiMatrix",
859 :     function(x, ..., na.rm) {
860 :     if(any(x@Dim == 0)) callGeneric(logical(0), ..., na.rm=na.rm)
861 :     else if(x@diag == "U")
862 :     callGeneric(x@x, FALSE, TRUE, ..., na.rm=na.rm)
863 :     else callGeneric(x@x, FALSE, ..., na.rm=na.rm)
864 :     })
865 :    
866 :    
867 :    
868 : maechler 1109 ## similar to prTriang() in ./Auxiliaries.R :
869 :     prDiag <-
870 :     function(x, digits = getOption("digits"), justify = "none", right = TRUE)
871 :     {
872 :     cf <- array(".", dim = x@Dim, dimnames = x@Dimnames)
873 :     cf[row(cf) == col(cf)] <-
874 :     sapply(diag(x), format, digits = digits, justify = justify)
875 :     print(cf, quote = FALSE, right = right)
876 :     invisible(x)
877 :     }
878 :    
879 :     setMethod("show", signature(object = "diagonalMatrix"),
880 : maechler 1592 function(object) {
881 :     d <- dim(object)
882 :     cl <- class(object)
883 : mmaechler 2203 cat(sprintf('%d x %d diagonal matrix of class "%s"',
884 : maechler 1592 d[1], d[2], cl))
885 : mmaechler 2203 if(d[1] < 50) {
886 :     cat("\n")
887 :     prDiag(object)
888 :     } else {
889 :     cat(", with diagonal entries\n")
890 :     show(diag(object))
891 :     invisible(object)
892 :     }
893 : maechler 1592 })

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