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

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