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