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 2207 - (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 : mmaechler 2207 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing", j = "index",
358 :     value = "scarceMatrix"),
359 :     function (x, i, j, ..., value)
360 :     callGeneric(x=x, , j=j, value = as(value, "sparseVector")))
361 :     setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index", j = "missing",
362 :     value = "scarceMatrix"),
363 :     function (x, i, j, ..., value)
364 :     callGeneric(x=x, i=i, , value = as(value, "sparseVector")))
365 :     setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index", j = "index",
366 :     value = "scarceMatrix"),
367 :     function (x, i, j, ..., value)
368 :     callGeneric(x=x, i=i, j=j, value = as(value, "sparseVector")))
369 : maechler 1710
370 : mmaechler 2207 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing", j = "index",
371 :     value = "sparseVector"),
372 :     replDiag)
373 :     setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index", j = "missing",
374 :     value = "sparseVector"),
375 :     replDiag)
376 :     setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index", j = "index",
377 :     value = "sparseVector"),
378 :     replDiag)
379 :    
380 :    
381 : maechler 1109 setMethod("t", signature(x = "diagonalMatrix"),
382 :     function(x) { x@Dimnames <- x@Dimnames[2:1] ; x })
383 :    
384 : maechler 1331 setMethod("isDiagonal", signature(object = "diagonalMatrix"),
385 :     function(object) TRUE)
386 :     setMethod("isTriangular", signature(object = "diagonalMatrix"),
387 :     function(object) TRUE)
388 : maechler 1109 setMethod("isSymmetric", signature(object = "diagonalMatrix"),
389 : maechler 2112 function(object, ...) TRUE)
390 : maechler 1109
391 : maechler 2112 setMethod("symmpart", signature(x = "diagonalMatrix"), function(x) x)
392 :     setMethod("skewpart", signature(x = "diagonalMatrix"), setZero)
393 :    
394 : maechler 2133 setMethod("chol", signature(x = "ddiMatrix"),
395 :     function(x, pivot, ...) {
396 : mmaechler 2183 if(x@diag == "U") return(x)
397 :     ## else
398 : maechler 2133 if(any(x@x < 0))
399 :     stop("chol() is undefined for diagonal matrix with negative entries")
400 : maechler 1654 x@x <- sqrt(x@x)
401 :     x
402 :     })
403 :     ## chol(L) is L for logical diagonal:
404 : maechler 2133 setMethod("chol", signature(x = "ldiMatrix"), function(x, pivot, ...) x)
405 : maechler 1654
406 : mmaechler 2175 setMethod("determinant", signature(x = "diagonalMatrix", logarithm = "logical"),
407 : mmaechler 2185 function(x, logarithm, ...) mkDet(.diag.x(x), logarithm))
408 : mmaechler 2175
409 : mmaechler 2183 setMethod("norm", signature(x = "diagonalMatrix", type = "character"),
410 :     function(x, type, ...) {
411 :     if((n <- x@Dim[1]) == 0) return(0) # as for "sparseMatrix"
412 :     type <- toupper(substr(type[1], 1, 1))
413 :     isU <- (x@diag == "U") # unit-diagonal
414 :     if(type == "F") sqrt(if(isU) n else sum(x@x^2))
415 :     else { ## norm == "I","1","O","M" :
416 :     if(isU) 1 else max(abs(x@x))
417 :     }
418 :     })
419 :    
420 :    
421 :    
422 : maechler 1109 ## Basic Matrix Multiplication {many more to add}
423 : maechler 1654 ## ---------------------
424 :     ## Note that "ldi" logical are treated as numeric
425 : maechler 1109 diagdiagprod <- function(x, y) {
426 : mmaechler 2183 n <- dimCheck(x,y)[1]
427 : maechler 1109 if(x@diag != "U") {
428 : maechler 1654 if(y@diag != "U") {
429 :     nx <- x@x * y@x
430 :     if(is.numeric(nx) && !is.numeric(x@x))
431 :     x <- as(x, "dMatrix")
432 :     x@x <- as.numeric(nx)
433 :     }
434 :     return(x)
435 : maechler 1109 } else ## x is unit diagonal
436 :     return(y)
437 :     }
438 :    
439 : maechler 1654 setMethod("%*%", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
440 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
441 :    
442 : maechler 1654 formals(diagdiagprod) <- alist(x=, y=x)
443 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
444 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
445 : maechler 1654 setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"),
446 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix")
447 : maechler 1654 setMethod("crossprod", signature(x = "diagonalMatrix", y = "missing"),
448 :     diagdiagprod, valueClass = "ddiMatrix")
449 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "missing"),
450 :     diagdiagprod, valueClass = "ddiMatrix")
451 : maechler 1109
452 :    
453 :     diagmatprod <- function(x, y) {
454 : mmaechler 2183 ## x is diagonalMatrix
455 : maechler 1109 dx <- dim(x)
456 :     dy <- dim(y)
457 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
458 :     n <- dx[1]
459 :     as(if(x@diag == "U") y else x@x * y, "Matrix")
460 :     }
461 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "matrix"),
462 : maechler 1654 diagmatprod)
463 : mmaechler 2183 ## sneaky .. :
464 : maechler 1109 formals(diagmatprod) <- alist(x=, y=NULL)
465 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "matrix"),
466 : maechler 1654 diagmatprod)
467 : maechler 1109
468 : mmaechler 2183 diagGeprod <- function(x, y) {
469 : maechler 1109 dx <- dim(x)
470 :     dy <- dim(y)
471 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
472 :     if(x@diag != "U")
473 :     y@x <- x@x * y@x
474 :     y
475 :     }
476 : mmaechler 2183 setMethod("%*%", signature(x= "diagonalMatrix", y= "dgeMatrix"), diagGeprod)
477 :     setMethod("%*%", signature(x= "diagonalMatrix", y= "lgeMatrix"), diagGeprod)
478 :     formals(diagGeprod) <- alist(x=, y=NULL)
479 : maechler 1109 setMethod("crossprod", signature(x = "diagonalMatrix", y = "dgeMatrix"),
480 : mmaechler 2183 diagGeprod, valueClass = "dgeMatrix")
481 :     setMethod("crossprod", signature(x = "diagonalMatrix", y = "lgeMatrix"),
482 :     diagGeprod)
483 : maechler 1109
484 : mmaechler 2183 matdiagprod <- function(x, y) {
485 :     dx <- dim(x)
486 :     dy <- dim(y)
487 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
488 :     Matrix(if(y@diag == "U") x else x * rep(y@x, each = dx[1]))
489 :     }
490 : maechler 1109 setMethod("%*%", signature(x = "matrix", y = "diagonalMatrix"),
491 : mmaechler 2183 matdiagprod)
492 :     formals(matdiagprod) <- alist(x=, y=NULL)
493 :     setMethod("tcrossprod", signature(x = "matrix", y = "diagonalMatrix"),
494 :     matdiagprod)
495 : maechler 1109
496 : mmaechler 2183 gediagprod <- function(x, y) {
497 :     dx <- dim(x)
498 :     dy <- dim(y)
499 :     if(dx[2] != dy[1]) stop("non-matching dimensions")
500 :     if(y@diag == "N")
501 :     x@x <- x@x * rep(y@x, each = dx[1])
502 :     x
503 :     }
504 :     setMethod("%*%", signature(x= "dgeMatrix", y= "diagonalMatrix"), gediagprod)
505 :     setMethod("%*%", signature(x= "lgeMatrix", y= "diagonalMatrix"), gediagprod)
506 :     formals(gediagprod) <- alist(x=, y=NULL)
507 :     setMethod("tcrossprod", signature(x = "dgeMatrix", y = "diagonalMatrix"),
508 :     gediagprod)
509 :     setMethod("tcrossprod", signature(x = "lgeMatrix", y = "diagonalMatrix"),
510 :     gediagprod)
511 : maechler 1109
512 : maechler 1295 ## crossprod {more of these}
513 : maechler 1109
514 : maechler 1295 ## tcrossprod --- all are not yet there: do the dense ones here:
515 : maechler 1109
516 : mmaechler 2183 setMethod("%*%", signature(x = "diagonalMatrix", y = "denseMatrix"),
517 :     function(x, y) if(x@diag == "U") y else x %*% as(y, "generalMatrix"))
518 :     setMethod("%*%", signature(x = "denseMatrix", y = "diagonalMatrix"),
519 :     function(x, y) if(y@diag == "U") x else as(x, "generalMatrix") %*% y)
520 :    
521 :    
522 : maechler 1295 ## FIXME:
523 :     ## setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "denseMatrix"),
524 :     ## function(x, y = NULL) {
525 :     ## })
526 : maechler 1109
527 : maechler 1295 ## setMethod("tcrossprod", signature(x = "denseMatrix", y = "diagonalMatrix"),
528 :     ## function(x, y = NULL) {
529 :     ## })
530 : maechler 1109
531 : maechler 1799 setMethod("crossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
532 : maechler 2123 function(x, y = NULL) crossprod(as(x, "sparseMatrix"), y))
533 : maechler 1295
534 : maechler 1799 setMethod("crossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
535 : maechler 2123 function(x, y = NULL) crossprod(x, as(y, "sparseMatrix")))
536 : maechler 1799
537 :     setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
538 : maechler 2123 function(x, y = NULL) tcrossprod(as(x, "sparseMatrix"), y))
539 : maechler 1799
540 :     setMethod("tcrossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
541 : maechler 2123 function(x, y = NULL) tcrossprod(x, as(y, "sparseMatrix")))
542 : maechler 1799
543 :    
544 :     ## FIXME?: In theory, this can be done *FASTER*, in some cases, via tapply1()
545 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "sparseMatrix"),
546 :     function(x, y) as(x, "sparseMatrix") %*% y)
547 : mmaechler 2183 setMethod("%*%", signature(x = "sparseMatrix", y = "diagonalMatrix"),
548 :     function(x, y) x %*% as(y, "sparseMatrix"))
549 : maechler 1799 ## NB: The previous is *not* triggering for "ddi" o "dgC" (= distance 3)
550 :     ## since there's a "ddense" o "Csparse" at dist. 2 => triggers first.
551 :     ## ==> do this:
552 :     setMethod("%*%", signature(x = "diagonalMatrix", y = "CsparseMatrix"),
553 :     function(x, y) as(x, "CsparseMatrix") %*% y)
554 :     setMethod("%*%", signature(x = "CsparseMatrix", y = "diagonalMatrix"),
555 :     function(x, y) x %*% as(y, "CsparseMatrix"))
556 :     ## NB: this is *not* needed for Tsparse & Rsparse
557 :     ## TODO: Write tests in ./tests/ which ensure that many "ops" with diagonal*
558 :     ## do indeed work by going through sparse (and *not* ddense)!
559 :    
560 :    
561 :    
562 :     setMethod("solve", signature(a = "diagonalMatrix", b = "missing"),
563 :     function(a, b, ...) {
564 :     a@x <- 1/ a@x
565 :     a@Dimnames <- a@Dimnames[2:1]
566 :     a
567 :     })
568 :    
569 :     solveDiag <- function(a, b, ...) {
570 :     if((n <- a@Dim[1]) != nrow(b))
571 :     stop("incompatible matrix dimensions")
572 :     ## trivially invert a 'in place' and multiply:
573 :     a@x <- 1/ a@x
574 :     a@Dimnames <- a@Dimnames[2:1]
575 :     a %*% b
576 :     }
577 :     setMethod("solve", signature(a = "diagonalMatrix", b = "matrix"),
578 :     solveDiag)
579 :     setMethod("solve", signature(a = "diagonalMatrix", b = "Matrix"),
580 :     solveDiag)
581 :    
582 : maechler 2106 ## Schur() ---> ./eigen.R
583 : maechler 1799
584 :    
585 :    
586 : mmaechler 2175 ###---------------- <Ops> (<Arith>, <Logic>, <Compare> ) ----------------------
587 : maechler 1295
588 : maechler 1655 ## Use function for several signatures, in order to evade
589 :     ## ambiguous dispatch for "ddi", since there's also Arith(ddense., ddense.)
590 : mmaechler 2175 diagOdiag <- function(e1,e2) {
591 :     ## result should also be diagonal _ if possible _
592 : maechler 1655 r <- callGeneric(.diag.x(e1), .diag.x(e2)) # error if not "compatible"
593 : mmaechler 2175 ## Check what happens with non-diagonals, i.e. (0 o 0), (FALSE o 0), ...:
594 :     r00 <- callGeneric(if(is.numeric(e1@x)) 0 else FALSE,
595 :     if(is.numeric(e2@x)) 0 else FALSE)
596 :     if(is0(r00)) { ## r00 == 0 or FALSE --- result *is* diagonal
597 :     if(is.numeric(r)) {
598 :     if(is.numeric(e2@x)) {
599 :     e2@x <- r; return(.diag.2N(e2)) }
600 :     if(!is.numeric(e1@x))
601 :     ## e.g. e1, e2 are logical;
602 :     e1 <- as(e1, "dMatrix")
603 :     }
604 :     else if(is.logical(r))
605 :     e1 <- as(e1, "lMatrix")
606 :     else stop("intermediate 'r' is of type", typeof(r))
607 :     e1@x <- r
608 :     .diag.2N(e1)
609 : maechler 1655 }
610 : mmaechler 2175 else { ## result not diagonal, but at least symmetric:
611 :     isNum <- (is.numeric(r) || is.numeric(r00))
612 :     isLog <- (is.logical(r) || is.logical(r00))
613 :    
614 :     if(getOption("verbose"))
615 :     message("exploding <diag> o <diag> into dense matrix")
616 :     d <- e1@Dim
617 :     n <- d[1]
618 :     stopifnot(length(r) == n)
619 :     xx <- as.vector(matrix(rbind(r, matrix(r00,n,n)), n,n))
620 :     newcl <-
621 :     paste(if(isNum) "d" else if(isLog) {
622 :     if(!any(is.na(r)) && !any(is.na(r00))) "n" else "l"
623 :     } else stop("not yet implemented .. please report")
624 :     ,
625 :     "syMatrix", sep='')
626 :    
627 :     new(newcl, Dim = e1@Dim, Dimnames = e1@Dimnames, x = xx)
628 :     }
629 : maechler 1655 }
630 :    
631 : mmaechler 2175 ### This would be *the* way, but we get tons of "ambiguous method dispatch"
632 : mmaechler 2185 ## we use this hack instead of signature x = "diagonalMatrix" :
633 :     diCls <- names(getClass("diagonalMatrix")@subclasses)
634 : mmaechler 2175 if(FALSE) {
635 : maechler 1655 setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "diagonalMatrix"),
636 :     diagOdiag)
637 : mmaechler 2175 } else { ## These are just for method disambiguation:
638 :     for(c1 in diCls)
639 :     for(c2 in diCls)
640 :     setMethod("Ops", signature(e1 = c1, e2 = c2), diagOdiag)
641 :     }
642 : maechler 1655
643 : maechler 1845 ## FIXME: diagonal o triangular |--> triangular
644 :     ## ----- diagonal o symmetric |--> symmetric
645 :     ## {also when other is sparse: do these "here" --
646 :     ## before conversion to sparse, since that loses "diagonality"}
647 :    
648 : maechler 1655 ## For almost everything else, diag* shall be treated "as sparse" :
649 : maechler 1295 ## These are cheap implementations via coercion
650 :    
651 : maechler 2144 ## For disambiguation --- define this for "sparseMatrix" , then for "ANY";
652 :     ## and because we can save an .M.kind() call, we use this explicit
653 :     ## "hack" for all diagonalMatrix *subclasses* instead of just "diagonalMatrix" :
654 :     ##
655 :     ## ddi*:
656 :     setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "sparseMatrix"),
657 : mmaechler 2175 function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "d"), e2))
658 : maechler 2144 setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "ddiMatrix"),
659 : mmaechler 2175 function(e1,e2) callGeneric(e1, diag2Tsmart(e2,e1, "d")))
660 : maechler 2144 ## ldi*
661 :     setMethod("Ops", signature(e1 = "ldiMatrix", e2 = "sparseMatrix"),
662 : mmaechler 2175 function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "l"), e2))
663 : maechler 2144 setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "ldiMatrix"),
664 : mmaechler 2175 function(e1,e2) callGeneric(e1, diag2Tsmart(e2,e1, "l")))
665 : maechler 1654
666 : mmaechler 2175 ## Ops: Arith --> numeric : "dMatrix"
667 :     ## Compare --> logical
668 :     ## Logic --> logical: "lMatrix"
669 :    
670 : maechler 2144 ## other = "numeric" : stay diagonal if possible
671 : mmaechler 2175 ## ddi*: Arith: result numeric, potentially ddiMatrix
672 :     setMethod("Arith", signature(e1 = "ddiMatrix", e2 = "numeric"),
673 : maechler 2144 function(e1,e2) {
674 : mmaechler 2203 n <- e1@Dim[1]; nsq <- n^2
675 : maechler 2144 f0 <- callGeneric(0, e2)
676 :     if(all(is0(f0))) { # remain diagonal
677 : maechler 2157 L1 <- (le <- length(e2)) == 1L
678 :     if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
679 :     if(e1@diag == "U" && any((r <- callGeneric(1, e2)) != 1)) {
680 : maechler 2144 e1@diag <- "N"
681 : maechler 2157 if(L1) r <- rep.int(r, n)
682 :     } else
683 : maechler 2144 r <- callGeneric(e1@x, e2)
684 : mmaechler 2197 e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
685 : maechler 2144 return(e1)
686 :     }
687 :     callGeneric(diag2tT.u(e1,e2, "d"), e2)
688 :     })
689 : maechler 1655
690 : mmaechler 2175 setMethod("Arith", signature(e1 = "numeric", e2 = "ddiMatrix"),
691 : maechler 2144 function(e1,e2) {
692 : mmaechler 2203 n <- e2@Dim[1]; nsq <- n^2
693 : maechler 2144 f0 <- callGeneric(e1, 0)
694 :     if(all(is0(f0))) { # remain diagonal
695 : maechler 2157 L1 <- (le <- length(e1)) == 1L
696 :     if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
697 :     if(e2@diag == "U" && any((r <- callGeneric(e1, 1)) != 1)) {
698 : maechler 2144 e2@diag <- "N"
699 : maechler 2157 if(L1) r <- rep.int(r, n)
700 :     } else
701 : maechler 2144 r <- callGeneric(e1, e2@x)
702 : mmaechler 2197 e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
703 : maechler 2144 return(e2)
704 :     }
705 :     callGeneric(e1, diag2tT.u(e2,e1, "d"))
706 :     })
707 : mmaechler 2175
708 :     ## ldi* Arith --> result numeric, potentially ddiMatrix
709 :     setMethod("Arith", signature(e1 = "ldiMatrix", e2 = "numeric"),
710 :     function(e1,e2) {
711 : mmaechler 2203 n <- e1@Dim[1]; nsq <- n^2
712 : mmaechler 2175 f0 <- callGeneric(0, e2)
713 :     if(all(is0(f0))) { # remain diagonal
714 :     L1 <- (le <- length(e2)) == 1L
715 :     if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
716 :     if(e1@diag == "U" && any((r <- callGeneric(1, e2)) != 1)) {
717 :     e1@diag <- "N"
718 :     if(L1) r <- rep.int(r, n)
719 :     } else
720 :     r <- callGeneric(e1@x, e2)
721 :     e1 <- copyClass(e1, "ddiMatrix", c("diag", "Dim", "Dimnames"))
722 : mmaechler 2197 e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
723 : mmaechler 2175 return(e1)
724 :     }
725 :     callGeneric(diag2tT.u(e1,e2, "d"), e2)
726 :     })
727 :    
728 :     setMethod("Arith", signature(e1 = "numeric", e2 = "ldiMatrix"),
729 :     function(e1,e2) {
730 : mmaechler 2203 n <- e2@Dim[1]; nsq <- n^2
731 : mmaechler 2175 f0 <- callGeneric(e1, 0)
732 :     if(all(is0(f0))) { # remain diagonal
733 :     L1 <- (le <- length(e1)) == 1L
734 :     if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
735 :     if(e2@diag == "U" && any((r <- callGeneric(e1, 1)) != 1)) {
736 :     e2@diag <- "N"
737 :     if(L1) r <- rep.int(r, n)
738 :     } else
739 :     r <- callGeneric(e1, e2@x)
740 :     e2 <- copyClass(e2, "ddiMatrix", c("diag", "Dim", "Dimnames"))
741 : mmaechler 2197 e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
742 : mmaechler 2175 return(e2)
743 :     }
744 :     callGeneric(e1, diag2tT.u(e2,e1, "d"))
745 :     })
746 :    
747 :     ## ddi*: for "Ops" without Arith --> result logical, potentially ldi
748 :     setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "numeric"),
749 :     function(e1,e2) {
750 : mmaechler 2203 n <- e1@Dim[1]; nsq <- n^2
751 : mmaechler 2175 f0 <- callGeneric(0, e2)
752 :     if(all(is0(f0))) { # remain diagonal
753 :     L1 <- (le <- length(e2)) == 1L
754 :     if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
755 :     if(e1@diag == "U" && any((r <- callGeneric(1, e2)) != 1)) {
756 :     e1@diag <- "N"
757 :     if(L1) r <- rep.int(r, n)
758 :     } else
759 :     r <- callGeneric(e1@x, e2)
760 :     e1 <- copyClass(e1, "ldiMatrix", c("diag", "Dim", "Dimnames"))
761 : mmaechler 2197 e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
762 : mmaechler 2175 return(e1)
763 :     }
764 :     callGeneric(diag2tT.u(e1,e2, "l"), e2)
765 :     })
766 :    
767 :     setMethod("Ops", signature(e1 = "numeric", e2 = "ddiMatrix"),
768 :     function(e1,e2) {
769 : mmaechler 2203 n <- e2@Dim[1]; nsq <- n^2
770 : mmaechler 2175 f0 <- callGeneric(e1, 0)
771 :     if(all(is0(f0))) { # remain diagonal
772 :     L1 <- (le <- length(e1)) == 1L
773 :     if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
774 :     if(e2@diag == "U" && any((r <- callGeneric(e1, 1)) != 1)) {
775 :     e2@diag <- "N"
776 :     if(L1) r <- rep.int(r, n)
777 :     } else
778 :     r <- callGeneric(e1, e2@x)
779 :     e2 <- copyClass(e2, "ldiMatrix", c("diag", "Dim", "Dimnames"))
780 : mmaechler 2197 e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
781 : mmaechler 2175 return(e2)
782 :     }
783 :     callGeneric(e1, diag2tT.u(e2,e1, "l"))
784 :     })
785 :    
786 :     ## ldi*: for "Ops" without Arith --> result logical, potentially ldi
787 : maechler 2144 setMethod("Ops", signature(e1 = "ldiMatrix", e2 = "numeric"),
788 :     function(e1,e2) {
789 : mmaechler 2203 n <- e1@Dim[1]; nsq <- n^2
790 : maechler 2144 f0 <- callGeneric(FALSE, e2)
791 :     if(all(is0(f0))) { # remain diagonal
792 : maechler 2157 L1 <- (le <- length(e2)) == 1L
793 :     if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
794 :     if(e1@diag == "U" && any((r <- callGeneric(TRUE, e2)) != 1)) {
795 : maechler 2144 e1@diag <- "N"
796 : maechler 2157 if(L1) r <- rep.int(r, n)
797 :     } else
798 : maechler 2144 r <- callGeneric(e1@x, e2)
799 : mmaechler 2197 e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
800 : maechler 2144 return(e1)
801 :     }
802 :     callGeneric(diag2tT.u(e1,e2, "l"), e2)
803 :     })
804 : maechler 1655
805 : maechler 2144 setMethod("Ops", signature(e1 = "numeric", e2 = "ldiMatrix"),
806 :     function(e1,e2) {
807 : mmaechler 2203 n <- e2@Dim[1]; nsq <- n^2
808 : maechler 2144 f0 <- callGeneric(e1, FALSE)
809 :     if(all(is0(f0))) { # remain diagonal
810 : maechler 2157 L1 <- (le <- length(e1)) == 1L
811 :     if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
812 :     if(e2@diag == "U" && any((r <- callGeneric(e1, TRUE)) != 1)) {
813 : maechler 2144 e2@diag <- "N"
814 : maechler 2157 if(L1) r <- rep.int(r, n)
815 :     } else
816 : maechler 2144 r <- callGeneric(e1, e2@x)
817 : mmaechler 2197 e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
818 : maechler 2144 return(e2)
819 :     }
820 :     callGeneric(e1, diag2tT.u(e2,e1, "l"))
821 :     })
822 :    
823 :    
824 :    
825 :     ## Not {"sparseMatrix", "numeric} : {"denseMatrix", "matrix", ... }
826 : mmaechler 2196 for(other in c("ANY", "Matrix", "dMatrix")) {
827 :     ## ddi*:
828 :     setMethod("Ops", signature(e1 = "ddiMatrix", e2 = other),
829 :     function(e1,e2) callGeneric(diag2tT.u(e1,e2, "d"), e2))
830 :     setMethod("Ops", signature(e1 = other, e2 = "ddiMatrix"),
831 :     function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1, "d")))
832 :     ## ldi*:
833 :     setMethod("Ops", signature(e1 = "ldiMatrix", e2 = other),
834 :     function(e1,e2) callGeneric(diag2tT.u(e1,e2, "l"), e2))
835 :     setMethod("Ops", signature(e1 = other, e2 = "ldiMatrix"),
836 :     function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1, "l")))
837 :     }
838 : mmaechler 2175 ## This should *not* dispatch to <dense> methods (in ./Ops.R ), as
839 :     ## FALSE & <anything> |-> FALSE : hence result should be diagonal:
840 :     for(cl in diCls) {
841 :     setMethod("&", signature(e1 = cl, e2 = "ANY"),
842 :     function(e1,e2) e1 & as(e2,"Matrix"))
843 :     setMethod("&", signature(e1 = "ANY", e2 = cl),
844 :     function(e1,e2) as(e1,"Matrix") & e2)
845 :     for(c2 in c("denseMatrix", "Matrix")) {
846 :     setMethod("&", signature(e1 = cl, e2 = c2),
847 :     function(e1,e2) e1 & Diagonal(x = diag(e2)))
848 :     setMethod("&", signature(e1 = c2, e2 = cl),
849 :     function(e1,e2) Diagonal(x = diag(e1)) & e2)
850 :     }
851 :     }
852 : maechler 2144
853 :    
854 : mmaechler 2175 ### "Summary" : "max" "min" "range" "prod" "sum" "any" "all"
855 :     ### ---------- any, all: separately here
856 :     for(cl in diCls) {
857 :     setMethod("any", cl,
858 :     function (x, ..., na.rm) {
859 :     if(any(x@Dim == 0)) FALSE
860 :     else if(x@diag == "U") TRUE else any(x@x, ..., na.rm = na.rm)
861 :     })
862 :     setMethod("all", cl, function (x, ..., na.rm) any(x@Dim == 0))
863 :     setMethod("prod", cl, function (x, ..., na.rm) as.numeric(any(x@Dim == 0)))
864 :    
865 :     setMethod("sum", cl,
866 :     function(x, ..., na.rm) {
867 :     r <- sum(x@x, ..., na.rm = na.rm)# double or integer, correctly
868 :     if(x@diag == "U" && !is.na(r)) r + x@Dim[1] else r
869 :     })
870 :     }
871 :    
872 :     ## The remaining ones are max, min, range :
873 :    
874 :     setMethod("Summary", "ddiMatrix",
875 :     function(x, ..., na.rm) {
876 :     if(any(x@Dim == 0)) callGeneric(numeric(0), ..., na.rm=na.rm)
877 :     else if(x@diag == "U")
878 :     callGeneric(x@x, 0, 1, ..., na.rm=na.rm)
879 :     else callGeneric(x@x, 0, ..., na.rm=na.rm)
880 :     })
881 :     setMethod("Summary", "ldiMatrix",
882 :     function(x, ..., na.rm) {
883 :     if(any(x@Dim == 0)) callGeneric(logical(0), ..., na.rm=na.rm)
884 :     else if(x@diag == "U")
885 :     callGeneric(x@x, FALSE, TRUE, ..., na.rm=na.rm)
886 :     else callGeneric(x@x, FALSE, ..., na.rm=na.rm)
887 :     })
888 :    
889 :    
890 :    
891 : maechler 1109 ## similar to prTriang() in ./Auxiliaries.R :
892 :     prDiag <-
893 :     function(x, digits = getOption("digits"), justify = "none", right = TRUE)
894 :     {
895 :     cf <- array(".", dim = x@Dim, dimnames = x@Dimnames)
896 :     cf[row(cf) == col(cf)] <-
897 :     sapply(diag(x), format, digits = digits, justify = justify)
898 :     print(cf, quote = FALSE, right = right)
899 :     invisible(x)
900 :     }
901 :    
902 :     setMethod("show", signature(object = "diagonalMatrix"),
903 : maechler 1592 function(object) {
904 :     d <- dim(object)
905 :     cl <- class(object)
906 : mmaechler 2203 cat(sprintf('%d x %d diagonal matrix of class "%s"',
907 : maechler 1592 d[1], d[2], cl))
908 : mmaechler 2203 if(d[1] < 50) {
909 :     cat("\n")
910 :     prDiag(object)
911 :     } else {
912 :     cat(", with diagonal entries\n")
913 :     show(diag(object))
914 :     invisible(object)
915 :     }
916 : 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