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