# SCM Repository

[matrix] Annotation of /pkg/Matrix/R/diagMatrix.R
 [matrix] / pkg / Matrix / R / diagMatrix.R

# Annotation of /pkg/Matrix/R/diagMatrix.R

Revision 3303 - (view) (download)

 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 : mmaechler 2820 ## Allow Diagonal(4), Diagonal(x=1:5), and Diagonal(4, TRUE) 9 : mmaechler 2863 n <- if(missing(n)) length(x) else { 10 : maechler 1575 stopifnot(length(n) == 1, n == as.integer(n), n >= 0) 11 : mmaechler 2863 as.integer(n) 12 : maechler 1109 } 13 : 14 : maechler 1654 if(missing(x)) ## unit diagonal matrix 15 : maechler 1575 new("ddiMatrix", Dim = c(n,n), diag = "U") 16 : maechler 1109 else { 17 : maechler 2128 lx <- length(x) 18 : mmaechler 2820 lx.1 <- lx == 1L 19 : stopifnot(lx.1 || lx == n) # but keep 'x' short for now 20 : maechler 1575 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 : mmaechler 2820 if(lx.1 && !is.na(x) && x == 1) # cheap check for uni-diagonal.. 30 : new(cl, Dim = c(n,n), diag = "U") 31 : else 32 : new(cl, Dim = c(n,n), diag = "N", 33 : x = if(lx.1) rep.int(x,n) else x) 34 : maechler 1109 } 35 : } 36 : 37 : mmaechler 2840 .sparseDiagonal <- function(n, x = 1, uplo = "U", 38 : mmaechler 2508 shape = if(missing(cols)) "t" else "g", 39 : mmaechler 2840 unitri, kind, 40 : cols = if(n) 0:(n - 1L) else integer(0)) 41 : mmaechler 2508 { 42 : mmaechler 3264 n <- if (missing(n)) length(x) else { 43 : stopifnot(length(n) == 1, n == as.integer(n), n >= 0) 44 : as.integer(n) 45 : } 46 : mmaechler 2840 if(!(mcols <- missing(cols))) 47 : mmaechler 2508 stopifnot(0 <= (cols <- as.integer(cols)), cols < n) 48 : m <- length(cols) 49 : if(missing(kind)) 50 : kind <- 51 : if(is.double(x)) "d" 52 : else if(is.logical(x)) "l" 53 : else { ## for now 54 : storage.mode(x) <- "double" 55 : "d" 56 : } 57 : else stopifnot(any(kind == c("d","l","n"))) 58 : mmaechler 2226 stopifnot(is.character(shape), nchar(shape) == 1, 59 : any(shape == c("t","s","g"))) # triangular / symmetric / general 60 : mmaechler 2840 if((missing(unitri) || unitri) && shape == "t" && 61 : (mcols || cols == 0:(n-1L)) && 62 : ((any(kind == c("l", "n")) && allTrue(x)) || 63 : ( kind == "d" && allTrue(x == 1)))) { ## uni-triangular 64 : new(paste0(kind,"tCMatrix"), Dim = c(n,n), 65 : uplo = uplo, diag = "U", p = rep.int(0L, n+1L)) 66 : } 67 : else if(kind == "n") { 68 : mmaechler 2508 if(shape == "g") 69 : new("ngCMatrix", Dim = c(n,m), i = cols, p = 0:m) 70 : else new(paste0("n", shape, "CMatrix"), Dim = c(n,m), uplo = uplo, 71 : i = cols, p = 0:m) 72 : } 73 : mmaechler 2840 else { ## kind != "n" -- have x slot : 74 : if((lx <- length(x)) == 1) x <- rep.int(x, m) 75 : else if(lx != m) stop("length(x) must be either 1 or #{cols}") 76 : if(shape == "g") 77 : new(paste0(kind, "gCMatrix"), Dim = c(n,m), 78 : x = x, i = cols, p = 0:m) 79 : else new(paste0(kind, shape, "CMatrix"), Dim = c(n,m), uplo = uplo, 80 : x = x, i = cols, p = 0:m) 81 : } 82 : maechler 2144 } 83 : 84 : mmaechler 2226 ## Pkg 'spdep' had (relatively slow) versions of this as_dsCMatrix_I() 85 : mmaechler 3264 .symDiagonal <- function(n, x = rep.int(1,n), uplo = "U", kind) 86 : mmaechler 3222 .sparseDiagonal(n, x, uplo, shape = "s", kind = kind) 87 : mmaechler 2226 88 : mmaechler 3222 ## NOTA BENE: .triDiagonal() would be misleading (<=> banded tri-diagonal matrix !) 89 : mmaechler 2608 # instead of diagU2N(as(Diagonal(n), "CsparseMatrix")), diag = "N" in any case: 90 : mmaechler 3264 .trDiagonal <- function(n, x = 1, uplo = "U", unitri = TRUE, kind) 91 : mmaechler 3222 .sparseDiagonal(n, x, uplo, shape = "t", unitri=unitri, kind=kind) 92 : mmaechler 2226 93 : 94 : mmaechler 2764 ## This is modified from a post of Bert Gunter to R-help on 1 Sep 2005. 95 : ## Bert's code built on a post by Andy Liaw who most probably was influenced 96 : ## by earlier posts, notably one by Scott Chasalow on S-news, 16 Jan 2002 97 : ## who posted his bdiag() function written in December 1995. 98 : mmaechler 2209 if(FALSE)##--- no longer used: 99 : mmaechler 2206 .bdiag <- function(lst) { 100 : mmaechler 2764 ## block-diagonal matrix [a dgTMatrix] from list of matrices 101 : mmaechler 2206 stopifnot(is.list(lst), length(lst) >= 1) 102 : mmaechler 2813 dims <- vapply(lst, dim, 1L, USE.NAMES=FALSE) 103 : maechler 1617 ## make sure we had all matrices: 104 : if(!(is.matrix(dims) && nrow(dims) == 2)) 105 : stop("some arguments are not matrices") 106 : maechler 1845 csdim <- rbind(rep.int(0L, 2), 107 : mmaechler 2206 apply(dims, 1, cumsum)) 108 : r <- new("dgTMatrix") 109 : r@Dim <- as.integer(csdim[nrow(csdim),]) 110 : maechler 1617 add1 <- matrix(1:0, 2,2) 111 : mmaechler 2206 for(i in seq_along(lst)) { 112 : maechler 1617 indx <- apply(csdim[i:(i+1),] + add1, 2, function(n) n[1]:n[2]) 113 : if(is.null(dim(indx))) ## non-square matrix 114 : mmaechler 2206 r[indx[[1]],indx[[2]]] <- lst[[i]] 115 : maechler 1617 else ## square matrix 116 : mmaechler 2206 r[indx[,1], indx[,2]] <- lst[[i]] 117 : maechler 1617 } 118 : mmaechler 2206 r 119 : maechler 1617 } 120 : mmaechler 2209 ## expand() needed something like bdiag() for lower-triangular 121 : ## (Tsparse) Matrices; hence Doug Bates provided a much more efficient 122 : ## implementation for those; now extended and generalized: 123 : mmaechler 2206 .bdiag <- function(lst) { 124 : mmaechler 2209 ## block-diagonal matrix [a dgTMatrix] from list of matrices 125 : mmaechler 2206 stopifnot(is.list(lst), (nl <- length(lst)) >= 1) 126 : maechler 1617 127 : mmaechler 3192 ### FIXME: next line is *slow* when lst = list of 75'000 dense 3x3 matrices 128 : mmaechler 2891 Tlst <- lapply(lapply(lst, as_Csp2), # includes "diagU2N" 129 : mmaechler 2209 as, "TsparseMatrix") 130 : mmaechler 2206 if(nl == 1) return(Tlst[[1]]) 131 : ## else 132 : mmaechler 2813 i_off <- c(0L, cumsum(vapply(Tlst, nrow, 1L))) 133 : j_off <- c(0L, cumsum(vapply(Tlst, ncol, 1L))) 134 : mmaechler 2209 135 : mmaechler 2813 clss <- vapply(Tlst, class, "") 136 : mmaechler 3158 ## NB ("FIXME"): this requires the component classes to be *called* 137 : ## -- "dgTMatrix" | "dnTMatrix" etc (and not just *extend* those)! 138 : mmaechler 2741 typ <- substr(clss, 2, 2) 139 : knd <- substr(clss, 1, 1) 140 : sym <- typ == "s" # symmetric ones 141 : tri <- typ == "t" # triangular ones 142 : use.n <- any(is.n <- knd == "n") 143 : if(use.n && !(use.n <- all(is.n))) { 144 : mmaechler 2209 Tlst[is.n] <- lapply(Tlst[is.n], as, "lMatrix") 145 : mmaechler 2741 knd [is.n] <- "l" 146 : } 147 : use.l <- !use.n && all(knd == "l") 148 : mmaechler 2209 if(all(sym)) { ## result should be *symmetric* 149 : mmaechler 2813 uplos <- vapply(Tlst, slot, ".", "uplo") ## either "U" or "L" 150 : mmaechler 2209 tLU <- table(uplos)# of length 1 or 2 .. 151 : if(length(tLU) == 1) { ## all "U" or all "L" 152 : useU <- uplos[1] == "U" 153 : } else { ## length(tLU) == 2, counting "L" and "U" 154 : useU <- diff(tLU) >= 0 155 : if(useU && (hasL <- tLU[1] > 0)) 156 : Tlst[hasL] <- lapply(Tlst[hasL], t) 157 : else if(!useU && (hasU <- tLU[2] > 0)) 158 : Tlst[hasU] <- lapply(Tlst[hasU], t) 159 : } 160 : if(use.n) { ## return nsparseMatrix : 161 : r <- new("nsTMatrix") 162 : } else { 163 : mmaechler 2741 r <- new(paste0(if(use.l) "l" else "d", "sTMatrix")) 164 : mmaechler 2209 r@x <- unlist(lapply(Tlst, slot, "x")) 165 : } 166 : r@uplo <- if(useU) "U" else "L" 167 : } 168 : mmaechler 2813 else if(all(tri) && { ULs <- vapply(Tlst, slot, ".", "uplo")## "U" or "L" 169 : mmaechler 2209 all(ULs[1L] == ULs[-1L]) } ## all upper or all lower 170 : ){ ## *triangular* result 171 : 172 : if(use.n) { ## return nsparseMatrix : 173 : r <- new("ntTMatrix") 174 : } else { 175 : mmaechler 2741 r <- new(paste0(if(use.l) "l" else "d", "tTMatrix")) 176 : mmaechler 2209 r@x <- unlist(lapply(Tlst, slot, "x")) 177 : } 178 : r@uplo <- ULs[1L] 179 : } 180 : else { 181 : if(any(sym)) 182 : Tlst[sym] <- lapply(Tlst[sym], as, "generalMatrix") 183 : if(use.n) { ## return nsparseMatrix : 184 : r <- new("ngTMatrix") 185 : } else { 186 : mmaechler 2741 r <- new(paste0(if(use.l) "l" else "d", "gTMatrix")) 187 : mmaechler 2209 r@x <- unlist(lapply(Tlst, slot, "x")) 188 : } 189 : } 190 : r@Dim <- c(i_off[nl+1], j_off[nl + 1]) 191 : r@i <- unlist(lapply(1:nl, function(k) Tlst[[k]]@i + i_off[k])) 192 : r@j <- unlist(lapply(1:nl, function(k) Tlst[[k]]@j + j_off[k])) 193 : r 194 : mmaechler 2206 } 195 : 196 : bdiag <- function(...) { 197 : if((nA <- nargs()) == 0) return(new("dgCMatrix")) 198 : if(nA == 1 && !is.list(...)) 199 : return(as(..., "CsparseMatrix")) 200 : alis <- if(nA == 1 && is.list(..1)) ..1 else list(...) 201 : if(length(alis) == 1) 202 : return(as(alis[[1]], "CsparseMatrix")) 203 : 204 : ## else : two or more arguments 205 : as(.bdiag(alis), "CsparseMatrix") 206 : } 207 : 208 : mmaechler 3023 setMethod("tril", "diagonalMatrix", function(x, k = 0, ...) 209 : if(k >= 0) x else .setZero(x, paste0(.M.kind(x), "tCMatrix"))) 210 : mmaechler 2206 211 : mmaechler 3023 setMethod("triu", "diagonalMatrix", function(x, k = 0, ...) 212 : if(k <= 0) x else .setZero(x, paste0(.M.kind(x), "tCMatrix"))) 213 : 214 : 215 : 216 : maechler 2136 .diag2tT <- function(from, uplo = "U", kind = .M.kind(from)) { 217 : ## to triangular Tsparse 218 : maechler 1845 i <- if(from@diag == "U") integer(0) else seq_len(from@Dim[1]) - 1L 219 : mmaechler 2778 new(paste0(kind, "tTMatrix"), 220 : maechler 1654 diag = from@diag, Dim = from@Dim, Dimnames = from@Dimnames, 221 : maechler 2128 uplo = uplo, 222 : maechler 1654 x = from@x, # <- ok for diag = "U" and "N" (!) 223 : i = i, j = i) 224 : } 225 : maechler 1109 226 : maechler 2136 .diag2sT <- function(from, uplo = "U", kind = .M.kind(from)) { 227 : ## to symmetric Tsparse 228 : maechler 2128 n <- from@Dim[1] 229 : i <- seq_len(n) - 1L 230 : mmaechler 2778 new(paste0(kind, "sTMatrix"), 231 : maechler 1845 Dim = from@Dim, Dimnames = from@Dimnames, 232 : maechler 2128 i = i, j = i, uplo = uplo, 233 : x = if(from@diag == "N") from@x else ## "U"-diag 234 : rep.int(switch(kind, 235 : "d" = 1., 236 : "l" =, 237 : "n" = TRUE, 238 : ## otherwise 239 : mmaechler 2863 stop(gettextf("%s kind not yet implemented", 240 : sQuote(kind)), domain=NA)), 241 : n)) 242 : maechler 1845 } 243 : 244 : mmaechler 2998 ## diagonal -> triangular, upper / lower depending on "partner" 'x': 245 : maechler 2144 diag2tT.u <- function(d, x, kind = .M.kind(d)) 246 : .diag2tT(d, uplo = if(is(x,"triangularMatrix")) x@uplo else "U", kind) 247 : maechler 2128 248 : mmaechler 2175 ## diagonal -> sparse {triangular OR symmetric} (upper / lower) depending on "partner": 249 : diag2Tsmart <- function(d, x, kind = .M.kind(d)) { 250 : clx <- getClassDef(class(x)) 251 : if(extends(clx, "symmetricMatrix")) 252 : .diag2sT(d, uplo = x@uplo, kind) 253 : else 254 : .diag2tT(d, uplo = if(extends(clx,"triangularMatrix")) x@uplo else "U", kind) 255 : } 256 : maechler 2136 257 : mmaechler 2814 ## FIXME: should not be needed {when ddi* is dsparse* etc}: 258 : setMethod("is.finite", signature(x = "diagonalMatrix"), 259 : function(x) is.finite(.diag2tT(x))) 260 : setMethod("is.infinite", signature(x = "diagonalMatrix"), 261 : function(x) is.infinite(.diag2tT(x))) 262 : mmaechler 2175 263 : maechler 2136 ## In order to evade method dispatch ambiguity warnings, 264 : ## and because we can save a .M.kind() call, we use this explicit 265 : ## "hack" instead of signature x = "diagonalMatrix" : 266 : ## 267 : ## ddi*: 268 : mmaechler 2922 di2tT <- function(from) .diag2tT(from, "U", "d") 269 : setAs("ddiMatrix", "triangularMatrix", di2tT) 270 : ##_no_longer_ setAs("ddiMatrix", "sparseMatrix", di2tT) 271 : maechler 1805 ## needed too (otherwise -> Tsparse is taken): 272 : mmaechler 2922 setAs("ddiMatrix", "TsparseMatrix", di2tT) 273 : setAs("ddiMatrix", "dsparseMatrix", di2tT) 274 : maechler 2136 setAs("ddiMatrix", "CsparseMatrix", 275 : function(from) as(.diag2tT(from, "U", "d"), "CsparseMatrix")) 276 : mmaechler 2998 setAs("ddiMatrix", "symmetricMatrix", function(from) .diag2sT(from, "U", "d")) 277 : maechler 2136 ## 278 : ## ldi*: 279 : mmaechler 2922 di2tT <- function(from) .diag2tT(from, "U", "l") 280 : setAs("ldiMatrix", "triangularMatrix", di2tT) 281 : ##_no_longer_ setAs("ldiMatrix", "sparseMatrix", di2tT) 282 : maechler 2136 ## needed too (otherwise -> Tsparse is taken): 283 : mmaechler 2922 setAs("ldiMatrix", "TsparseMatrix", di2tT) 284 : setAs("ldiMatrix", "lsparseMatrix", di2tT) 285 : maechler 2136 setAs("ldiMatrix", "CsparseMatrix", 286 : function(from) as(.diag2tT(from, "U", "l"), "CsparseMatrix")) 287 : mmaechler 2998 setAs("ldiMatrix", "symmetricMatrix", function(from) .diag2sT(from, "U", "l")) 288 : mmaechler 2922 rm(di2tT) 289 : maechler 1654 290 : maechler 2128 setAs("diagonalMatrix", "nMatrix", 291 : mmaechler 3092 di2nMat <- function(from) { 292 : maechler 2128 i <- if(from@diag == "U") integer(0) else which(isN0(from@x)) - 1L 293 : new("ntTMatrix", i = i, j = i, diag = from@diag, 294 : Dim = from@Dim, Dimnames = from@Dimnames) 295 : }) 296 : 297 : mmaechler 2237 setAs("diagonalMatrix", "nsparseMatrix", function(from) as(from, "nMatrix")) 298 : maechler 2128 299 : mmaechler 2998 ##' A version of diag(x,n) which *does* preserve the mode of x, where diag() "fails" 300 : mmaechler 2175 mkDiag <- function(x, n) { 301 : y <- matrix(as0(mod=mode(x)), n,n) 302 : mmaechler 2820 if (n > 0) y[1L + 0:(n - 1L) * (n + 1)] <- x 303 : mmaechler 2175 y 304 : } 305 : mmaechler 2998 ## NB: diag(x,n) is really faster for n >= 20, and even more for large n 306 : ## --> using diag() where possible, ==> .ddi2mat() 307 : mmaechler 2175 308 : mmaechler 2998 .diag2mat <- function(from) 309 : ## want "ldiMatrix" -> "matrix" (but integer -> for now) 310 : mkDiag(if(from@diag == "U") as1(from@x) else from@x, n = from@Dim[1]) 311 : maechler 1109 312 : mmaechler 2998 .ddi2mat <- function(from) 313 : base::diag(if(from@diag == "U") as1(from@x) else from@x, nrow = from@Dim[1]) 314 : 315 : setAs("ddiMatrix", "matrix", .ddi2mat) 316 : ## the non-ddi diagonalMatrix -- only "ldiMatrix" currently: 317 : setAs("diagonalMatrix", "matrix", .diag2mat) 318 : 319 : mmaechler 3154 setMethod("as.vector", "diagonalMatrix", 320 : maechler 2098 function(x, mode) { 321 : n <- x@Dim[1] 322 : mmaechler 2175 mod.x <- mode(x@x) 323 : r <- vector(mod.x, length = n^2) 324 : maechler 2098 if(n) 325 : mmaechler 2820 r[1 + 0:(n - 1L) * (n + 1)] <- 326 : mmaechler 2175 if(x@diag == "U") as1(mod=mod.x) else x@x 327 : mmaechler 3154 as.vector(r, mode) 328 : maechler 2098 }) 329 : 330 : maechler 1654 setAs("diagonalMatrix", "generalMatrix", # prefer sparse: 331 : maechler 2128 function(from) as(as(from, "CsparseMatrix"), "generalMatrix")) 332 : maechler 1174 333 : mmaechler 2268 setAs("diagonalMatrix", "denseMatrix", 334 : function(from) as(as(from, "CsparseMatrix"), "denseMatrix")) 335 : 336 : mmaechler 2877 ..diag.x <- function(m) rep.int(as1(m@x), m@Dim[1]) 337 : .diag.x <- function(m) if(m@diag == "U") rep.int(as1(m@x), m@Dim[1]) else m@x 338 : maechler 1655 339 : .diag.2N <- function(m) { 340 : if(m@diag == "U") m@diag <- "N" 341 : m 342 : } 343 : 344 : mmaechler 3092 setAs("ddiMatrix", "dgeMatrix", ..2dge) 345 : 346 : setAs("ddiMatrix", "ddenseMatrix", #-> "dtr" 347 : mmaechler 2183 function(from) as(as(from, "triangularMatrix"),"denseMatrix")) 348 : mmaechler 3092 setAs("ldiMatrix", "ldenseMatrix", #-> "ltr" 349 : mmaechler 2183 function(from) as(as(from, "triangularMatrix"),"denseMatrix")) 350 : maechler 1174 351 : mmaechler 2183 352 : maechler 1109 setAs("matrix", "diagonalMatrix", 353 : function(from) { 354 : maechler 1295 d <- dim(from) 355 : maechler 1109 if(d[1] != (n <- d[2])) stop("non-square matrix") 356 : if(any(from[row(from) != col(from)] != 0)) 357 : mmaechler 2863 stop("matrix with non-zero off-diagonals cannot be coerced to \"diagonalMatrix\"") 358 : mmaechler 3204 x <- diag(from); names(x) <- NULL # don't want them in 'x' slot 359 : maechler 1295 if(is.logical(x)) { 360 : cl <- "ldiMatrix" 361 : mmaechler 2341 uni <- allTrue(x) ## uni := {is it unit-diagonal ?} 362 : maechler 1295 } else { 363 : cl <- "ddiMatrix" 364 : mmaechler 2341 uni <- allTrue(x == 1) 365 : maechler 1295 storage.mode(x) <- "double" 366 : maechler 1575 } ## TODO: complex 367 : maechler 1295 new(cl, Dim = c(n,n), diag = if(uni) "U" else "N", 368 : mmaechler 3120 x = if(uni) x[FALSE] else x, Dimnames = .M.DN(from)) 369 : maechler 1109 }) 370 : 371 : ## ``generic'' coercion to diagonalMatrix : build on isDiagonal() and diag() 372 : setAs("Matrix", "diagonalMatrix", 373 : function(from) { 374 : mmaechler 2341 d <- dim(from) 375 : maechler 1109 if(d[1] != (n <- d[2])) stop("non-square matrix") 376 : mmaechler 2341 if(!isDiagonal(from)) stop("matrix is not diagonal") 377 : ## else: 378 : mmaechler 3204 x <- diag(from); names(x) <- NULL # don't want them in 'x' slot 379 : mmaechler 2341 if(is.logical(x)) { 380 : cl <- "ldiMatrix" 381 : uni <- allTrue(x) 382 : } else { 383 : cl <- "ddiMatrix" 384 : uni <- allTrue(x == 1) 385 : storage.mode(x) <- "double" 386 : } ## TODO: complex 387 : new(cl, Dim = c(n,n), diag = if(uni) "U" else "N", 388 : mmaechler 3120 x = if(uni) x[FALSE] else x, Dimnames = from@Dimnames) 389 : maechler 1109 }) 390 : 391 : maechler 1708 392 : mmaechler 2185 setMethod("diag", signature(x = "diagonalMatrix"), 393 : function(x = 1, nrow, ncol) .diag.x(x)) 394 : maechler 1708 395 : maechler 2098 subDiag <- function(x, i, j, ..., drop) { 396 : mmaechler 2508 x <- as(x, "CsparseMatrix") ## << was "TsparseMatrix" (Csparse is faster now) 397 : maechler 1799 x <- if(missing(i)) 398 : x[, j, drop=drop] 399 : else if(missing(j)) 400 : mmaechler 2363 if(nargs() == 4) x[i, , drop=drop] else x[i, drop=drop] 401 : maechler 1799 else 402 : x[i,j, drop=drop] 403 : maechler 2120 if(isS4(x) && isDiagonal(x)) as(x, "diagonalMatrix") else x 404 : maechler 1799 } 405 : 406 : setMethod("[", signature(x = "diagonalMatrix", i = "index", 407 : j = "index", drop = "logical"), subDiag) 408 : setMethod("[", signature(x = "diagonalMatrix", i = "index", 409 : mmaechler 2363 j = "missing", drop = "logical"), 410 : function(x, i, j, ..., drop) { 411 : na <- nargs() 412 : Matrix.msg("diag[i,m,l] : nargs()=", na, .M.level = 2) 413 : if(na == 4) 414 : subDiag(x, i=i, , drop=drop) 415 : else subDiag(x, i=i, drop=drop) 416 : }) 417 : maechler 1799 setMethod("[", signature(x = "diagonalMatrix", i = "missing", 418 : j = "index", drop = "logical"), 419 : maechler 2098 function(x, i, j, ..., drop) subDiag(x, j=j, drop=drop)) 420 : maechler 1799 421 : maechler 1617 ## When you assign to a diagonalMatrix, the result should be 422 : maechler 1708 ## diagonal or sparse --- 423 : ## FIXME: this now fails because the "denseMatrix" methods come first in dispatch 424 : maechler 2098 ## Only(?) current bug: x[i] <- value is wrong when i is *vector* 425 : replDiag <- function(x, i, j, ..., value) { 426 : mmaechler 3303 ## FIXME: if (i == j) && isSymmetric(value) then -- want symmetricMatrix result! -- or diagMatrix 427 : mmaechler 2811 x <- as(x, "CsparseMatrix")# was "Tsparse.." till 2012-07 428 : maechler 1710 if(missing(i)) 429 : x[, j] <- value 430 : maechler 2098 else if(missing(j)) { ## x[i , ] <- v *OR* x[i] <- v 431 : na <- nargs() 432 : ## message("diagnosing replDiag() -- nargs()= ", na) 433 : if(na == 4) 434 : x[i, ] <- value 435 : else if(na == 3) 436 : x[i] <- value 437 : mmaechler 2863 else stop(gettextf("Internal bug: nargs()=%d; please report", 438 : na), domain=NA) 439 : maechler 2098 } else 440 : maechler 1710 x[i,j] <- value 441 : mmaechler 3303 if(isDiagonal(x)) as(x, "diagonalMatrix") else 442 : if(isTriangular(x)) as(x, "triangularMatrix") else 443 : if(isSymmetric(x)) as(x, "symmetricMatrix") else x 444 : maechler 1710 } 445 : maechler 1617 446 : maechler 1710 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index", 447 : j = "index", value = "replValue"), replDiag) 448 : maechler 2098 449 : maechler 1710 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index", 450 : j = "missing", value = "replValue"), 451 : maechler 2098 function(x,i,j, ..., value) { 452 : ## message("before replDiag() -- nargs()= ", nargs()) 453 : if(nargs() == 3) 454 : replDiag(x, i=i, value=value) 455 : else ## nargs() == 4 : 456 : replDiag(x, i=i, , value=value) 457 : }) 458 : 459 : mmaechler 2877 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing", 460 : j = "index", value = "replValue"), 461 : function(x,i,j, ..., value) replDiag(x, j=j, value=value)) 462 : 463 : ## x[] <- value : 464 : setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing", 465 : j = "missing", value = "ANY"), 466 : function(x,i,j, ..., value) 467 : { 468 : if(all0(value)) { # be faster 469 : r <- new(paste0(.M.kindC(getClassDef(class(x))),"tTMatrix"))# of all "0" 470 : r@Dim <- x@Dim 471 : r@Dimnames <- x@Dimnames 472 : r 473 : } else { ## typically non-sense: assigning to full sparseMatrix 474 : x[TRUE] <- value 475 : x 476 : } 477 : }) 478 : 479 : 480 : mmaechler 2192 setReplaceMethod("[", signature(x = "diagonalMatrix", 481 : i = "matrix", # 2-col.matrix 482 : maechler 2096 j = "missing", value = "replValue"), 483 : maechler 2098 function(x,i,j, ..., value) { 484 : maechler 2096 if(ncol(i) == 2) { 485 : if(all((ii <- i[,1]) == i[,2])) { # replace in diagonal only 486 : mmaechler 2192 if(x@diag == "U") { 487 : one <- as1(x@x) 488 : if(any(value != one | is.na(value))) { 489 : x@diag <- "N" 490 : x@x <- rep.int(one, x@Dim[1]) 491 : mmaechler 2811 } else return(x) 492 : mmaechler 2192 } 493 : maechler 2096 x@x[ii] <- value 494 : x 495 : } else { ## no longer diagonal, but remain sparse: 496 : mmaechler 3303 x <- as(x, "triangularMatrix") # was "TsparseMatrix" 497 : maechler 2096 x[i] <- value 498 : x 499 : } 500 : } 501 : else { # behave as "base R": use as if vector 502 : x <- as(x, "matrix") 503 : x[i] <- value 504 : Matrix(x) 505 : } 506 : }) 507 : 508 : maechler 1710 509 : mmaechler 2877 ## value = "sparseMatrix": 510 : mmaechler 2207 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing", j = "index", 511 : mmaechler 2239 value = "sparseMatrix"), 512 : mmaechler 2207 function (x, i, j, ..., value) 513 : callGeneric(x=x, , j=j, value = as(value, "sparseVector"))) 514 : setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index", j = "missing", 515 : mmaechler 2239 value = "sparseMatrix"), 516 : mmaechler 2207 function (x, i, j, ..., value) 517 : callGeneric(x=x, i=i, , value = as(value, "sparseVector"))) 518 : setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index", j = "index", 519 : mmaechler 2239 value = "sparseMatrix"), 520 : mmaechler 2207 function (x, i, j, ..., value) 521 : callGeneric(x=x, i=i, j=j, value = as(value, "sparseVector"))) 522 : maechler 1710 523 : mmaechler 2877 ## value = "sparseVector": 524 : mmaechler 2207 setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing", j = "index", 525 : value = "sparseVector"), 526 : replDiag) 527 : setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index", j = "missing", 528 : value = "sparseVector"), 529 : replDiag) 530 : setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index", j = "index", 531 : value = "sparseVector"), 532 : replDiag) 533 : 534 : 535 : maechler 1109 setMethod("t", signature(x = "diagonalMatrix"), 536 : function(x) { x@Dimnames <- x@Dimnames[2:1] ; x }) 537 : 538 : mmaechler 2904 setMethod("isDiagonal", "diagonalMatrix", function(object) TRUE) 539 : mmaechler 3015 setMethod("isTriangular", "diagonalMatrix", function(object, upper=NA, ...) TRUE) 540 : mmaechler 2904 setMethod("isSymmetric", "diagonalMatrix", function(object, ...) TRUE) 541 : maechler 1109 542 : maechler 2112 setMethod("symmpart", signature(x = "diagonalMatrix"), function(x) x) 543 : mmaechler 3023 setMethod("skewpart", signature(x = "diagonalMatrix"), function(x) .setZero(x)) 544 : maechler 2112 545 : maechler 2133 setMethod("chol", signature(x = "ddiMatrix"), 546 : function(x, pivot, ...) { 547 : mmaechler 2183 if(x@diag == "U") return(x) 548 : ## else 549 : maechler 2133 if(any(x@x < 0)) 550 : stop("chol() is undefined for diagonal matrix with negative entries") 551 : maechler 1654 x@x <- sqrt(x@x) 552 : x 553 : }) 554 : ## chol(L) is L for logical diagonal: 555 : maechler 2133 setMethod("chol", signature(x = "ldiMatrix"), function(x, pivot, ...) x) 556 : maechler 1654 557 : mmaechler 2175 setMethod("determinant", signature(x = "diagonalMatrix", logarithm = "logical"), 558 : mmaechler 2185 function(x, logarithm, ...) mkDet(.diag.x(x), logarithm)) 559 : mmaechler 2175 560 : mmaechler 2183 setMethod("norm", signature(x = "diagonalMatrix", type = "character"), 561 : function(x, type, ...) { 562 : if((n <- x@Dim[1]) == 0) return(0) # as for "sparseMatrix" 563 : type <- toupper(substr(type[1], 1, 1)) 564 : isU <- (x@diag == "U") # unit-diagonal 565 : if(type == "F") sqrt(if(isU) n else sum(x@x^2)) 566 : mmaechler 3206 else { ## norm == "I","1","O","M","2" : 567 : mmaechler 2183 if(isU) 1 else max(abs(x@x)) 568 : } 569 : }) 570 : 571 : 572 : 573 : maechler 1109 ## Basic Matrix Multiplication {many more to add} 574 : maechler 1654 ## --------------------- 575 : ## Note that "ldi" logical are treated as numeric 576 : maechler 1109 diagdiagprod <- function(x, y) { 577 : mmaechler 2633 dimCheck(x,y) 578 : maechler 1109 if(x@diag != "U") { 579 : maechler 1654 if(y@diag != "U") { 580 : nx <- x@x * y@x 581 : if(is.numeric(nx) && !is.numeric(x@x)) 582 : x <- as(x, "dMatrix") 583 : x@x <- as.numeric(nx) 584 : } 585 : mmaechler 3120 x 586 : maechler 1109 } else ## x is unit diagonal 587 : mmaechler 3120 y 588 : maechler 1109 } 589 : 590 : mmaechler 3069 ##' Boolean Algebra/Arithmetic Product of Diagonal Matrices 591 : ##' %&% 592 : diagdiagprodBool <- function(x, y) { 593 : dimCheck(x,y) 594 : if(x@diag != "U") { 595 : if(!is.logical(x@x)) x <- as(x, "lMatrix") 596 : if(y@diag != "U") { 597 : nx <- x@x & y@x 598 : x@x <- as.logical(nx) 599 : } 600 : x 601 : } else { ## x is unit diagonal: return y 602 : if(!is.logical(y@x)) y <- as(y, "lMatrix") 603 : y 604 : } 605 : } 606 : 607 : maechler 1654 setMethod("%*%", signature(x = "diagonalMatrix", y = "diagonalMatrix"), 608 : maechler 1109 diagdiagprod, valueClass = "ddiMatrix") 609 : 610 : mmaechler 3069 setMethod("%&%", signature(x = "diagonalMatrix", y = "diagonalMatrix"), 611 : diagdiagprodBool, valueClass = "ldiMatrix")# do *not* have "ndiMatrix" ! 612 : 613 : mmaechler 3072 ##' Both Numeric or Boolean Algebra/Arithmetic Product of Diagonal Matrices 614 : diagdiagprodFlexi <- function(x, y=NULL, boolArith = NA, ...) 615 : { 616 : dimCheck(x,y) 617 : bool <- isTRUE(boolArith) 618 : if(x@diag != "U") { 619 : if(bool && !is.logical(x@x)) x <- as(x, "lMatrix") 620 : if(y@diag != "U") { 621 : if(bool) { 622 : nx <- x@x & y@x 623 : x@x <- as.logical(nx) 624 : } else { ## boolArith is NA or FALSE: ==> numeric, as have *no* "diagMatrix" patter[n]: 625 : nx <- x@x * y@x 626 : if(is.numeric(nx) && !is.numeric(x@x)) 627 : x <- as(x, "dMatrix") 628 : x@x <- as.numeric(nx) 629 : } 630 : } 631 : x 632 : } else { ## x is unit diagonal: return y 633 : if(bool && !is.logical(y@x)) y <- as(y, "lMatrix") 634 : y 635 : } 636 : } 637 : maechler 1654 setMethod("crossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"), 638 : mmaechler 3072 diagdiagprodFlexi) 639 : maechler 1654 setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "diagonalMatrix"), 640 : mmaechler 3072 diagdiagprodFlexi) 641 : mmaechler 3069 642 : ##' crossprod(x) := x'x 643 : mmaechler 3072 diagprod <- function(x, y = NULL, boolArith = NA, ...) { 644 : bool <- isTRUE(boolArith) 645 : if(bool && !is.logical(x@x)) x <- as(x, "lMatrix") 646 : mmaechler 3069 if(x@diag != "U") { 647 : mmaechler 3072 if(bool) { 648 : nx <- x@x & y@x 649 : x@x <- as.logical(nx) 650 : } else { ## boolArith is NA or FALSE: ==> numeric, as have *no* "diagMatrix" patter[n]: 651 : nx <- x@x * x@x 652 : if(is.numeric(nx) && !is.numeric(x@x)) 653 : x <- as(x, "dMatrix") 654 : x@x <- as.numeric(nx) 655 : } 656 : mmaechler 3069 } 657 : x 658 : } 659 : mmaechler 3072 setMethod( "crossprod", signature(x = "diagonalMatrix", y = "missing"), diagprod) 660 : setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "missing"), diagprod) 661 : maechler 1109 662 : 663 : mmaechler 2998 ## analogous to matdiagprod() below: 664 : maechler 1109 diagmatprod <- function(x, y) { 665 : mmaechler 2183 ## x is diagonalMatrix 666 : mmaechler 3167 dy <- dim(y) 667 : if(x@Dim[2L] != dy[1L]) stop("non-matching dimensions") 668 : mmaechler 3174 if(prod(dy)) 669 : Matrix(if(x@diag == "U") y else x@x * y) 670 : else 671 : Matrix(if(x@diag == "U") y else x@x * y, nrow=dy[1L], ncol=dy[2L]) 672 : maechler 1109 } 673 : mmaechler 3072 setMethod("%*%", signature(x = "diagonalMatrix", y = "matrix"), diagmatprod) 674 : mmaechler 3069 675 : mmaechler 3120 ##formals(diagmatprod) <- alist(x=, y=NULL, boolArith = NA, ...=) ## FIXME boolArith 676 : mmaechler 3079 diagmatprod2 <- function(x, y=NULL, boolArith = NA, ...) { 677 : ## x is diagonalMatrix 678 : mmaechler 3167 dy <- dim(y) 679 : if(x@Dim[2L] != dy[1L]) stop("non-matching dimensions") 680 : mmaechler 3174 if(prod(dy)) 681 : Matrix(if(x@diag == "U") y else x@x * y) 682 : else 683 : Matrix(if(x@diag == "U") y else x@x * y, nrow=dy[1L], ncol=dy[2L]) 684 : mmaechler 3079 } 685 : setMethod("crossprod", signature(x = "diagonalMatrix", y = "matrix"), diagmatprod2) 686 : maechler 1109 687 : mmaechler 2183 diagGeprod <- function(x, y) { 688 : mmaechler 3167 if(x@Dim[2L] != y@Dim[1L]) stop("non-matching dimensions") 689 : if(x@diag != "U") { 690 : if(!is.numeric(y@x)) y <- as(y, "dMatrix") 691 : maechler 1109 y@x <- x@x * y@x 692 : mmaechler 3167 } 693 : maechler 1109 y 694 : } 695 : mmaechler 2183 setMethod("%*%", signature(x= "diagonalMatrix", y= "dgeMatrix"), diagGeprod) 696 : setMethod("%*%", signature(x= "diagonalMatrix", y= "lgeMatrix"), diagGeprod) 697 : maechler 1109 698 : mmaechler 3069 diagGeprodBool <- function(x, y) { 699 : mmaechler 3167 if(x@Dim[2L] != y@Dim[1L]) stop("non-matching dimensions") 700 : mmaechler 3069 if(!is.logical(y@x)) y <- as(y, "lMatrix") 701 : if(x@diag != "U") 702 : y@x <- x@x & y@x 703 : y 704 : } 705 : setMethod("%&%", signature(x= "diagonalMatrix", y= "geMatrix"), diagGeprodBool) 706 : 707 : mmaechler 3072 diagGeprod2 <- function(x, y=NULL, boolArith = NA, ...) { 708 : mmaechler 3167 if(x@Dim[2L] != y@Dim[1L]) stop("non-matching dimensions") 709 : mmaechler 3072 bool <- isTRUE(boolArith) 710 : if(bool && !is.logical(y@x)) y <- as(y, "lMatrix") 711 : mmaechler 3167 else if(!bool && !is.numeric(y@x)) y <- as(y, "dMatrix") 712 : mmaechler 3072 if(x@diag != "U") 713 : y@x <- if(bool) x@x & y@x else x@x * y@x 714 : y 715 : } 716 : setMethod("crossprod", signature(x = "diagonalMatrix", y = "dgeMatrix"), diagGeprod2) 717 : setMethod("crossprod", signature(x = "diagonalMatrix", y = "lgeMatrix"), diagGeprod2) 718 : mmaechler 3069 719 : mmaechler 3072 720 : mmaechler 2998 ## analogous to diagmatprod() above: 721 : mmaechler 2183 matdiagprod <- function(x, y) { 722 : dx <- dim(x) 723 : mmaechler 3167 if(dx[2L] != y@Dim[1L]) stop("non-matching dimensions") 724 : Matrix(if(y@diag == "U") x else x * rep(y@x, each = dx[1L])) 725 : mmaechler 2183 } 726 : mmaechler 2998 setMethod("%*%", signature(x = "matrix", y = "diagonalMatrix"), matdiagprod) 727 : maechler 1109 728 : mmaechler 2183 gediagprod <- function(x, y) { 729 : dx <- dim(x) 730 : mmaechler 3167 if(dx[2L] != y@Dim[1L]) stop("non-matching dimensions") 731 : if(y@diag == "N") { 732 : if(!is.numeric(x@x)) x <- as(x, "dMatrix") 733 : x@x <- x@x * rep(y@x, each = dx[1L]) 734 : } 735 : mmaechler 2183 x 736 : } 737 : setMethod("%*%", signature(x= "dgeMatrix", y= "diagonalMatrix"), gediagprod) 738 : setMethod("%*%", signature(x= "lgeMatrix", y= "diagonalMatrix"), gediagprod) 739 : maechler 1109 740 : mmaechler 3069 gediagprodBool <- function(x, y) { 741 : dx <- dim(x) 742 : mmaechler 3167 if(dx[2L] != y@Dim[1L]) stop("non-matching dimensions") 743 : mmaechler 3069 if(!is.logical(x@x)) x <- as(x, "lMatrix") 744 : if(y@diag == "N") 745 : mmaechler 3167 x@x <- x@x & rep(y@x, each = dx[1L]) 746 : mmaechler 3069 x 747 : } 748 : setMethod("%&%", signature(x= "geMatrix", y= "diagonalMatrix"), gediagprodBool) 749 : 750 : mmaechler 3072 setMethod("tcrossprod",signature(x = "matrix", y = "diagonalMatrix"), 751 : function(x, y=NULL, boolArith = NA, ...) { 752 : dx <- dim(x) 753 : mmaechler 3167 if(dx[2L] != y@Dim[1L]) stop("non-matching dimensions") 754 : mmaechler 3072 bool <- isTRUE(boolArith) 755 : if(bool && !is.logical(y@x)) y <- as(y, "lMatrix") 756 : Matrix(if(y@diag == "U") x else 757 : mmaechler 3167 if(bool) x & rep(y@x, each = dx[1L]) 758 : else x * rep(y@x, each = dx[1L])) 759 : mmaechler 3072 }) 760 : 761 : setMethod("crossprod", signature(x = "matrix", y = "diagonalMatrix"), 762 : function(x, y=NULL, boolArith = NA, ...) { 763 : dx <- dim(x) 764 : mmaechler 3167 if(dx[1L] != y@Dim[1L]) stop("non-matching dimensions") 765 : mmaechler 3072 bool <- isTRUE(boolArith) 766 : if(bool && !is.logical(y@x)) y <- as(y, "lMatrix") 767 : mmaechler 3120 Matrix(if(y@diag == "U") t(x) else 768 : mmaechler 3167 if(bool) t(rep.int(y@x, dx[2L]) & x) 769 : else t(rep.int(y@x, dx[2L]) * x)) 770 : mmaechler 3072 }) 771 : 772 : 773 : gediagprod2 <- function(x, y=NULL, boolArith = NA, ...) { 774 : dx <- dim(x) 775 : mmaechler 3167 if(dx[2L] != y@Dim[1L]) stop("non-matching dimensions") 776 : mmaechler 3072 bool <- isTRUE(boolArith) 777 : if(bool && !is.logical(x@x)) x <- as(x, "lMatrix") 778 : mmaechler 3167 else if(!bool && !is.numeric(x@x)) x <- as(x, "dMatrix") 779 : mmaechler 3072 if(y@diag == "N") 780 : mmaechler 3167 x@x <- if(bool) x@x & rep(y@x, each = dx[1L]) 781 : else x@x * rep(y@x, each = dx[1L]) 782 : mmaechler 3072 x 783 : } 784 : setMethod("tcrossprod", signature(x = "dgeMatrix", y = "diagonalMatrix"), gediagprod2) 785 : setMethod("tcrossprod", signature(x = "lgeMatrix", y = "diagonalMatrix"), gediagprod2) 786 : 787 : 788 : maechler 1295 ## crossprod {more of these} 789 : maechler 1109 790 : maechler 1295 ## tcrossprod --- all are not yet there: do the dense ones here: 791 : maechler 1109 792 : mmaechler 2183 setMethod("%*%", signature(x = "diagonalMatrix", y = "denseMatrix"), 793 : function(x, y) if(x@diag == "U") y else x %*% as(y, "generalMatrix")) 794 : setMethod("%*%", signature(x = "denseMatrix", y = "diagonalMatrix"), 795 : function(x, y) if(y@diag == "U") x else as(x, "generalMatrix") %*% y) 796 : 797 : 798 : maechler 1295 ## FIXME: 799 : ## setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "denseMatrix"), 800 : ## function(x, y = NULL) { 801 : ## }) 802 : maechler 1109 803 : mmaechler 2912 ##' @param x CsparseMatrix 804 : ##' @param y diagonalMatrix 805 : ##' @return x %*% y 806 : mmaechler 3072 Cspdiagprod <- function(x, y, boolArith = NA, ...) { 807 : mmaechler 3167 if((m <- ncol(x)) != y@Dim[1L]) stop("non-matching dimensions") 808 : mmaechler 2912 if(y@diag == "N") { ## otherwise: y == Diagonal(n) : multiplication is identity 809 : mmaechler 3069 x <- .Call(Csparse_diagU2N, x) 810 : cx <- getClass(class(x)) 811 : if(!all(y@x[1L] == y@x[-1L]) && extends(cx, "symmetricMatrix")) 812 : mmaechler 2674 x <- as(x, "generalMatrix") 813 : mmaechler 2998 ind <- rep.int(seq_len(m), x@p[-1] - x@p[-m-1L]) 814 : mmaechler 3069 if(isTRUE(boolArith)) { 815 : if(extends(cx, "nMatrix")) x <- as(x, "lMatrix") # so, has y@x 816 : mmaechler 3079 x@x <- r <- x@x & y@x[x@i + 1L] 817 : if(!anyNA(r) && !extends(cx, "diagonalMatrix")) x <- as(drop0(x), "nMatrix") 818 : mmaechler 3069 } else { 819 : if(!extends(cx, "dMatrix")) x <- as(x, "dMatrix") # <- FIXME if we have zMatrix 820 : x@x <- x@x * y@x[ind] 821 : } 822 : mmaechler 2978 if(.hasSlot(x, "factors") && length(x@factors)) {# drop cashed ones 823 : mmaechler 2912 ## instead of dropping all factors, be smart about some 824 : ## TODO ...... 825 : x@factors <- list() 826 : } 827 : mmaechler 3079 x 828 : } else { # y is unit-diagonal ==> "return x" 829 : cx <- getClass(class(x)) 830 : if(isTRUE(boolArith)) { 831 : is.l <- if(extends(cx, "dMatrix")) { ## <- FIXME: extend once we have iMatrix, zMatrix 832 : x <- as(x, "lMatrix"); TRUE } else extends(cx, "lMatrix") 833 : if(is.l && !anyNA(x@x)) as(drop0(x), "nMatrix") 834 : else if(is.l) x else # defensive: 835 : as(x, "lMatrix") 836 : } else { 837 : ## else boolArith is NA or FALSE {which are equivalent here, das diagonal = "numLike"} 838 : if(extends(cx, "nMatrix") || extends(cx, "lMatrix")) 839 : as(x, "dMatrix") else x 840 : } 841 : mmaechler 2674 } 842 : dmbates 2417 } 843 : maechler 1109 844 : mmaechler 2912 ##' @param x diagonalMatrix 845 : ##' @param y CsparseMatrix 846 : ##' @return x %*% y 847 : mmaechler 3072 diagCspprod <- function(x, y, boolArith = NA, ...) { 848 : mmaechler 3167 if(x@Dim[2L] != y@Dim[1L]) stop("non-matching dimensions") 849 : mmaechler 2984 if(x@diag == "N") { 850 : mmaechler 3069 y <- .Call(Csparse_diagU2N, y) 851 : cy <- getClass(class(y)) 852 : if(!all(x@x[1L] == x@x[-1L]) && extends(cy, "symmetricMatrix")) 853 : mmaechler 2674 y <- as(y, "generalMatrix") 854 : mmaechler 3069 if(isTRUE(boolArith)) { 855 : if(extends(cy, "nMatrix")) y <- as(y, "lMatrix") # so, has y@x 856 : mmaechler 3079 y@x <- r <- y@x & x@x[y@i + 1L] 857 : if(!anyNA(r) && !extends(cy, "diagonalMatrix")) y <- as(drop0(y), "nMatrix") 858 : mmaechler 3069 } else { 859 : if(!extends(cy, "dMatrix")) y <- as(y, "dMatrix") # <- FIXME if we have zMatrix 860 : y@x <- y@x * x@x[y@i + 1L] 861 : } 862 : mmaechler 3046 if(.hasSlot(y, "factors") && length(y@factors)) { 863 : ## if(.hasSlot(y, "factors") && length(yf <- y@factors)) { ## -- TODO? -- 864 : ## instead of dropping all factors, be smart about some 865 : ## keep <- character() 866 : ## if(any(names(yf) == "LU")) { ## <- not easy: y = P'LUQ, x y = xP'LUQ => LU ??? 867 : ## keep <- "LU" 868 : ## } 869 : ## y@factors <- yf[keep] 870 : y@factors <- list() 871 : mmaechler 2912 } 872 : mmaechler 3079 y 873 : } else { ## x @ diag == "U" 874 : cy <- getClass(class(y)) 875 : if(isTRUE(boolArith)) { 876 : is.l <- if(extends(cy, "dMatrix")) { ## <- FIXME: extend once we have iMatrix, zMatrix 877 : y <- as(y, "lMatrix"); TRUE } else extends(cy, "lMatrix") 878 : if(is.l && !anyNA(y@x)) as(drop0(y), "nMatrix") 879 : else if(is.l) y else # defensive: 880 : as(y, "lMatrix") 881 : } else { 882 : ## else boolArith is NA or FALSE {which are equivalent here, das diagonal = "numLike"} 883 : if(extends(cy, "nMatrix") || extends(cy, "lMatrix")) 884 : as(y, "dMatrix") else y 885 : } 886 : mmaechler 2674 } 887 : dmbates 2417 } 888 : 889 : mmaechler 3069 ## + 'boolArith' argument { ==> .local() is used in any case; keep formals simple :} 890 : dmbates 2417 setMethod("crossprod", signature(x = "diagonalMatrix", y = "CsparseMatrix"), 891 : mmaechler 3072 function(x, y=NULL, boolArith=NA, ...) diagCspprod(x, y, boolArith=boolArith)) 892 : dmbates 2417 893 : maechler 1799 setMethod("crossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"), 894 : mmaechler 3072 function(x, y=NULL, boolArith=NA, ...) 895 : mmaechler 3069 diagCspprod(x, as(y, "CsparseMatrix"), boolArith=boolArith)) 896 : maechler 1295 897 : dmbates 2417 ## Prefer calling diagCspprod to Cspdiagprod if going to transpose anyway 898 : ## x'y == (y'x)' 899 : setMethod("crossprod", signature(x = "CsparseMatrix", y = "diagonalMatrix"), 900 : mmaechler 3072 function(x, y=NULL, boolArith=NA, ...) t(diagCspprod(y, x, boolArith=boolArith))) 901 : dmbates 2417 902 : maechler 1799 setMethod("crossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"), 903 : mmaechler 3072 function(x, y=NULL, boolArith=NA, ...) t(diagCspprod(y, as(x, "Csparsematrix"), boolArith=boolArith))) 904 : maechler 1799 905 : dmbates 2417 setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "CsparseMatrix"), 906 : mmaechler 3072 function(x, y=NULL, boolArith=NA, ...) diagCspprod(x, t(y), boolArith=boolArith)) 907 : dmbates 2417 908 : maechler 1799 setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"), 909 : mmaechler 3072 function(x, y=NULL, boolArith=NA, ...) diagCspprod(x, t(as(y, "CsparseMatrix")), boolArith=boolArith)) 910 : maechler 1799 911 : dmbates 2417 setMethod("tcrossprod", signature(x = "CsparseMatrix", y = "diagonalMatrix"), 912 : mmaechler 3072 function(x, y=NULL, boolArith=NA, ...) Cspdiagprod(x, y, boolArith=boolArith)) 913 : dmbates 2417 914 : maechler 1799 setMethod("tcrossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"), 915 : mmaechler 3072 function(x, y=NULL, boolArith=NA, ...) Cspdiagprod(as(x, "CsparseMatrix"), y, boolArith=boolArith)) 916 : mmaechler 2472 917 : dmbates 2417 setMethod("%*%", signature(x = "diagonalMatrix", y = "CsparseMatrix"), 918 : mmaechler 3069 function(x, y) diagCspprod(x, y, boolArith=NA)) 919 : setMethod("%&%", signature(x = "diagonalMatrix", y = "CsparseMatrix"), 920 : function(x, y) diagCspprod(x, y, boolArith=TRUE)) 921 : maechler 1799 922 : mmaechler 2912 ## instead of "sparseMatrix", use: [RT]sparse.. ("closer" in method dispatch) 923 : for(cl in c("TsparseMatrix", "RsparseMatrix")) { 924 : 925 : maechler 1799 setMethod("%*%", signature(x = "diagonalMatrix", y = "sparseMatrix"), 926 : mmaechler 3069 function(x, y) diagCspprod(as(x, "CsparseMatrix"), y, boolArith=NA)) 927 : mmaechler 2472 928 : mmaechler 2183 setMethod("%*%", signature(x = "sparseMatrix", y = "diagonalMatrix"), 929 : mmaechler 3069 function(x, y) Cspdiagprod(as(x, "CsparseMatrix"), y, boolArith=NA)) 930 : 931 : setMethod("%&%", signature(x = "diagonalMatrix", y = "sparseMatrix"), 932 : function(x, y) diagCspprod(as(x, "CsparseMatrix"), y, boolArith=TRUE)) 933 : 934 : setMethod("%&%", signature(x = "sparseMatrix", y = "diagonalMatrix"), 935 : function(x, y) Cspdiagprod(as(x, "CsparseMatrix"), y, boolArith=TRUE)) 936 : 937 : mmaechler 2912 } 938 : dmbates 2417 939 : maechler 1799 setMethod("%*%", signature(x = "CsparseMatrix", y = "diagonalMatrix"), 940 : mmaechler 3069 function(x, y) Cspdiagprod(x, y, boolArith=NA)) 941 : setMethod("%&%", signature(x = "CsparseMatrix", y = "diagonalMatrix"), 942 : function(x, y) Cspdiagprod(x, y, boolArith=TRUE)) 943 : dmbates 2417 944 : maechler 1799 ## TODO: Write tests in ./tests/ which ensure that many "ops" with diagonal* 945 : ## do indeed work by going through sparse (and *not* ddense)! 946 : 947 : setMethod("solve", signature(a = "diagonalMatrix", b = "missing"), 948 : function(a, b, ...) { 949 : a@x <- 1/ a@x 950 : a@Dimnames <- a@Dimnames[2:1] 951 : a 952 : }) 953 : 954 : solveDiag <- function(a, b, ...) { 955 : mmaechler 3167 if(a@Dim[1L] != nrow(b)) 956 : maechler 1799 stop("incompatible matrix dimensions") 957 : ## trivially invert a 'in place' and multiply: 958 : a@x <- 1/ a@x 959 : a@Dimnames <- a@Dimnames[2:1] 960 : a %*% b 961 : } 962 : setMethod("solve", signature(a = "diagonalMatrix", b = "matrix"), 963 : solveDiag) 964 : setMethod("solve", signature(a = "diagonalMatrix", b = "Matrix"), 965 : solveDiag) 966 : 967 : maechler 2106 ## Schur() ---> ./eigen.R 968 : maechler 1799 969 : 970 : 971 : mmaechler 2175 ###---------------- (, , ) ---------------------- 972 : maechler 1295 973 : maechler 1655 ## Use function for several signatures, in order to evade 974 : mmaechler 2175 diagOdiag <- function(e1,e2) { 975 : ## result should also be diagonal _ if possible _ 976 : maechler 1655 r <- callGeneric(.diag.x(e1), .diag.x(e2)) # error if not "compatible" 977 : mmaechler 2175 ## Check what happens with non-diagonals, i.e. (0 o 0), (FALSE o 0), ...: 978 : r00 <- callGeneric(if(is.numeric(e1@x)) 0 else FALSE, 979 : if(is.numeric(e2@x)) 0 else FALSE) 980 : if(is0(r00)) { ## r00 == 0 or FALSE --- result *is* diagonal 981 : mmaechler 2814 if(is.numeric(r)) { # "double" *or* "integer" 982 : mmaechler 2175 if(is.numeric(e2@x)) { 983 : e2@x <- r; return(.diag.2N(e2)) } 984 : if(!is.numeric(e1@x)) 985 : ## e.g. e1, e2 are logical; 986 : e1 <- as(e1, "dMatrix") 987 : mmaechler 2814 if(!is.double(r)) r <- as.double(r) 988 : mmaechler 2175 } 989 : else if(is.logical(r)) 990 : e1 <- as(e1, "lMatrix") 991 : mmaechler 2863 else stop(gettextf("intermediate 'r' is of type %s", 992 : typeof(r)), domain=NA) 993 : mmaechler 2175 e1@x <- r 994 : .diag.2N(e1) 995 : maechler 1655 } 996 : mmaechler 2175 else { ## result not diagonal, but at least symmetric: 997 : mmaechler 2496 ## e.g., m == m 998 : mmaechler 2175 isNum <- (is.numeric(r) || is.numeric(r00)) 999 : isLog <- (is.logical(r) || is.logical(r00)) 1000 : mmaechler 2496 Matrix.msg("exploding o into dense matrix", .M.level = 2) 1001 : mmaechler 2175 d <- e1@Dim 1002 : mmaechler 3167 n <- d[1L] 1003 : mmaechler 2175 stopifnot(length(r) == n) 1004 : mmaechler 2814 if(isNum && !is.double(r)) r <- as.double(r) 1005 : mmaechler 2175 xx <- as.vector(matrix(rbind(r, matrix(r00,n,n)), n,n)) 1006 : newcl <- 1007 : mmaechler 2811 paste0(if(isNum) "d" else if(isLog) { 1008 : mmaechler 3092 if(!anyNA(r) && !anyNA(r00)) "n" else "l" 1009 : mmaechler 2811 } else stop("not yet implemented .. please report"), "syMatrix") 1010 : mmaechler 2175 1011 : new(newcl, Dim = e1@Dim, Dimnames = e1@Dimnames, x = xx) 1012 : } 1013 : maechler 1655 } 1014 : 1015 : mmaechler 2175 ### This would be *the* way, but we get tons of "ambiguous method dispatch" 1016 : mmaechler 2185 ## we use this hack instead of signature x = "diagonalMatrix" : 1017 : diCls <- names(getClass("diagonalMatrix")@subclasses) 1018 : mmaechler 2175 if(FALSE) { 1019 : maechler 1655 setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "diagonalMatrix"), 1020 : diagOdiag) 1021 : mmaechler 2175 } else { ## These are just for method disambiguation: 1022 : for(c1 in diCls) 1023 : for(c2 in diCls) 1024 : setMethod("Ops", signature(e1 = c1, e2 = c2), diagOdiag) 1025 : } 1026 : maechler 1655 1027 : mmaechler 2493 ## diagonal o triangular |--> triangular 1028 : ## diagonal o symmetric |--> symmetric 1029 : maechler 1845 ## {also when other is sparse: do these "here" -- 1030 : ## before conversion to sparse, since that loses "diagonality"} 1031 : mmaechler 2811 diagOtri <- function(e1,e2) { 1032 : ## result must be triangular 1033 : r <- callGeneric(d1 <- .diag.x(e1), diag(e2)) # error if not "compatible" 1034 : ## Check what happens with non-diagonals, i.e. (0 o 0), (FALSE o 0), ...: 1035 : mmaechler 3046 e1.0 <- if(is.numeric(d1)) 0 else FALSE 1036 : mmaechler 3167 r00 <- callGeneric(e1.0, if(.n2 <- is.numeric(e2[0L])) 0 else FALSE) 1037 : mmaechler 2811 if(is0(r00)) { ## r00 == 0 or FALSE --- result *is* triangular 1038 : diag(e2) <- r 1039 : ## check what happens "in the triangle" 1040 : e2.2 <- if(.n2) 2 else TRUE 1041 : if(!callGeneric(e1.0, e2.2) == e2.2) { # values "in triangle" can change: 1042 : n <- dim(e2)[1L] 1043 : it <- indTri(n, upper = (e2@uplo == "U")) 1044 : e2[it] <- callGeneric(e1.0, e2[it]) 1045 : } 1046 : e2 1047 : } 1048 : else { ## result not triangular ---> general 1049 : rr <- as(e2, "generalMatrix") 1050 : diag(rr) <- r 1051 : rr 1052 : } 1053 : } 1054 : maechler 1845 1055 : mmaechler 2811 1056 : setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "triangularMatrix"), 1057 : diagOtri) 1058 : ## For the reverse, Ops == "Arith" | "Compare" | "Logic" 1059 : ## 'Arith' := '"+"', '"-"', '"*"', '"^"', '"%%"', '"%/%"', '"/"' 1060 : setMethod("Arith", signature(e1 = "triangularMatrix", e2 = "diagonalMatrix"), 1061 : function(e1,e2) 1062 : { ## this must only trigger for *dense* e1 1063 : switch(.Generic, 1064 : "+" = .Call(dtrMatrix_addDiag, as(e1,"dtrMatrix"), .diag.x(e2)), 1065 : "-" = .Call(dtrMatrix_addDiag, as(e1,"dtrMatrix"), - .diag.x(e2)), 1066 : "*" = { 1067 : n <- e2@Dim[1L] 1068 : d2 <- if(e2@diag == "U") { # unit-diagonal 1069 : d <- rep.int(as1(e2@x), n) 1070 : e2@x <- d 1071 : e2@diag <- "N" 1072 : d 1073 : } else e2@x 1074 : e2@x <- diag(e1) * d2 1075 : e2 1076 : }, 1077 : "^" = { ## will be dense ( as ^ 0 == 1 ): 1078 : e1 ^ as(e2, "denseMatrix") 1079 : }, 1080 : ## otherwise: 1081 : callGeneric(e1, diag2Tsmart(e2,e1))) 1082 : }) 1083 : 1084 : ## Compare --> 'swap' (e.g. e1 < e2 <==> e2 > e1 ): 1085 : setMethod("Compare", signature(e1 = "triangularMatrix", e2 = "diagonalMatrix"), 1086 : .Cmp.swap) 1087 : ## '&' and "|' are commutative: 1088 : setMethod("Logic", signature(e1 = "triangularMatrix", e2 = "diagonalMatrix"), 1089 : function(e1,e2) callGeneric(e2, e1)) 1090 : 1091 : maechler 1655 ## For almost everything else, diag* shall be treated "as sparse" : 1092 : maechler 1295 ## These are cheap implementations via coercion 1093 : 1094 : maechler 2144 ## For disambiguation --- define this for "sparseMatrix" , then for "ANY"; 1095 : ## and because we can save an .M.kind() call, we use this explicit 1096 : ## "hack" for all diagonalMatrix *subclasses* instead of just "diagonalMatrix" : 1097 : ## 1098 : ## ddi*: 1099 : setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "sparseMatrix"), 1100 : mmaechler 2175 function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "d"), e2)) 1101 : maechler 2144 setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "ddiMatrix"), 1102 : mmaechler 2175 function(e1,e2) callGeneric(e1, diag2Tsmart(e2,e1, "d"))) 1103 : maechler 2144 ## ldi* 1104 : setMethod("Ops", signature(e1 = "ldiMatrix", e2 = "sparseMatrix"), 1105 : mmaechler 2175 function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "l"), e2)) 1106 : maechler 2144 setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "ldiMatrix"), 1107 : mmaechler 2175 function(e1,e2) callGeneric(e1, diag2Tsmart(e2,e1, "l"))) 1108 : maechler 1654 1109 : mmaechler 2820 ## Ops: Arith --> numeric : "dMatrix" 1110 : ## Compare --> logical 1111 : ## Logic --> logical: "lMatrix" 1112 : mmaechler 2175 1113 : mmaechler 2820 ## Other = "numeric" : stay diagonal if possible 1114 : mmaechler 2175 ## ddi*: Arith: result numeric, potentially ddiMatrix 1115 : mmaechler 2813 for(arg2 in c("numeric","logical")) 1116 : setMethod("Arith", signature(e1 = "ddiMatrix", e2 = arg2), 1117 : maechler 2144 function(e1,e2) { 1118 : mmaechler 3167 n <- e1@Dim[1L] 1119 : mmaechler 3287 if(length(e2) == 0L) 1120 : return(if(n) numeric() else e1) 1121 : maechler 2144 f0 <- callGeneric(0, e2) 1122 : mmaechler 2945 if(all0(f0)) { # remain diagonal 1123 : maechler 2157 L1 <- (le <- length(e2)) == 1L 1124 : mmaechler 2820 if(e1@diag == "U") { 1125 : if(any((r <- callGeneric(1, e2)) != 1)) { 1126 : e1@diag <- "N" 1127 : e1@x[seq_len(n)] <- r # possibly recycling r 1128 : } ## else: result = e1 (is "U" diag) 1129 : } else { 1130 : maechler 2144 r <- callGeneric(e1@x, e2) 1131 : mmaechler 2820 ## "future fixme": if we have idiMatrix, and r is 'integer', use idiMatrix 1132 : e1@x[] <- if(L1) r else r[1L + ((n+1)*(0:(n-1L))) %% le] 1133 : } 1134 : e1 1135 : } else 1136 : callGeneric(diag2tT.u(e1,e2, "d"), e2) 1137 : maechler 2144 }) 1138 : maechler 1655 1139 : mmaechler 2813 for(arg1 in c("numeric","logical")) 1140 : setMethod("Arith", signature(e1 = arg1, e2 = "ddiMatrix"), 1141 : maechler 2144 function(e1,e2) { 1142 : mmaechler 3167 n <- e2@Dim[1L] 1143 : mmaechler 3287 if(length(e1) == 0L) 1144 : return(if(n) numeric() else e2) 1145 : maechler 2144 f0 <- callGeneric(e1, 0) 1146 : mmaechler 2945 if(all0(f0)) { # remain diagonal 1147 : maechler 2157 L1 <- (le <- length(e1)) == 1L 1148 : mmaechler 2820 if(e2@diag == "U") { 1149 : if(any((r <- callGeneric(e1, 1)) != 1)) { 1150 : e2@diag <- "N" 1151 : e2@x[seq_len(n)] <- r # possibly recycling r 1152 : } ## else: result = e2 (is "U" diag) 1153 : } else { 1154 : maechler 2144 r <- callGeneric(e1, e2@x) 1155 : mmaechler 2820 ## "future fixme": if we have idiMatrix, and r is 'integer', use idiMatrix 1156 : e2@x[] <- if(L1) r else r[1L + ((n+1)*(0:(n-1L))) %% le] 1157 : } 1158 : e2 1159 : } else 1160 : callGeneric(e1, diag2tT.u(e2,e1, "d")) 1161 : maechler 2144 }) 1162 : mmaechler 2175 1163 : ## ldi* Arith --> result numeric, potentially ddiMatrix 1164 : mmaechler 2813 for(arg2 in c("numeric","logical")) 1165 : setMethod("Arith", signature(e1 = "ldiMatrix", e2 = arg2), 1166 : mmaechler 2175 function(e1,e2) { 1167 : mmaechler 3167 n <- e1@Dim[1L] 1168 : mmaechler 3287 if(length(e2) == 0L) 1169 : return(if(n) numeric() 1170 : else copyClass(e1, "ddiMatrix", c("diag", "Dim", "Dimnames"), check=FALSE)) 1171 : mmaechler 2175 f0 <- callGeneric(0, e2) 1172 : mmaechler 2945 if(all0(f0)) { # remain diagonal 1173 : mmaechler 2175 L1 <- (le <- length(e2)) == 1L 1174 : mmaechler 2984 E <- copyClass(e1, "ddiMatrix", c("diag", "Dim", "Dimnames"), check=FALSE) 1175 : mmaechler 2820 ## storage.mode(E@x) <- "double" 1176 : if(e1@diag == "U") { 1177 : if(any((r <- callGeneric(1, e2)) != 1)) { 1178 : E@diag <- "N" 1179 : E@x[seq_len(n)] <- r # possibly recycling r 1180 : } ## else: result = E (is "U" diag) 1181 : } else { 1182 : mmaechler 2175 r <- callGeneric(e1@x, e2) 1183 : mmaechler 2820 ## "future fixme": if we have idiMatrix, and r is 'integer', use idiMatrix 1184 : E@x[seq_len(n)] <- if(L1) r else r[1L + ((n+1)*(0:(n-1L))) %% le] 1185 : mmaechler 2814 } 1186 : mmaechler 2820 E 1187 : } else 1188 : callGeneric(diag2tT.u(e1,e2, "l"), e2) 1189 : mmaechler 2175 }) 1190 : 1191 : mmaechler 2813 for(arg1 in c("numeric","logical")) 1192 : setMethod("Arith", signature(e1 = arg1, e2 = "ldiMatrix"), 1193 : mmaechler 2175 function(e1,e2) { 1194 : mmaechler 3167 n <- e2@Dim[1L] 1195 : mmaechler 3287 if(length(e1) == 0L) 1196 : return(if(n) numeric() 1197 : else copyClass(e2, "ddiMatrix", c("diag", "Dim", "Dimnames"), check=FALSE)) 1198 : mmaechler 2175 f0 <- callGeneric(e1, 0) 1199 : mmaechler 2945 if(all0(f0)) { # remain diagonal 1200 : mmaechler 2175 L1 <- (le <- length(e1)) == 1L 1201 : mmaechler 2984 E <- copyClass(e2, "ddiMatrix", c("diag", "Dim", "Dimnames"), check=FALSE) 1202 : mmaechler 2820 ## storage.mode(E@x) <- "double" 1203 : if(e2@diag == "U") { 1204 : if(any((r <- callGeneric(e1, 1)) != 1)) { 1205 : E@diag <- "N" 1206 : E@x[seq_len(n)] <- r # possibly recycling r 1207 : } ## else: result = E (is "U" diag) 1208 : } else { 1209 : mmaechler 2175 r <- callGeneric(e1, e2@x) 1210 : mmaechler 2820 ## "future fixme": if we have idiMatrix, and r is 'integer', use idiMatrix 1211 : E@x[seq_len(n)] <- if(L1) r else r[1L + ((n+1)*(0:(n-1L))) %% le] 1212 : mmaechler 2814 } 1213 : mmaechler 2820 E 1214 : } else 1215 : callGeneric(e1, diag2tT.u(e2,e1, "l")) 1216 : mmaechler 2175 }) 1217 : 1218 : mmaechler 2820 ## ddi*: for "Ops" without "Arith": or --> result logical, potentially ldi 1219 : ## 1220 : ## Note that ("numeric", "ddiMatrix") is simply swapped, e.g., 1221 : if(FALSE) { 1222 : selectMethod("<", c("numeric","lMatrix"))# Compare 1223 : selectMethod("&", c("numeric","lMatrix"))# Logic 1224 : } ## so we don't need to define a method here : 1225 : 1226 : mmaechler 2813 for(arg2 in c("numeric","logical")) 1227 : setMethod("Ops", signature(e1 = "ddiMatrix", e2 = arg2), 1228 : mmaechler 2175 function(e1,e2) { 1229 : mmaechler 3167 n <- e1@Dim[1L] 1230 : mmaechler 3287 if(length(e2) == 0L) 1231 : return(if(n) logical() 1232 : else copyClass(e1, "ldiMatrix", c("diag", "Dim", "Dimnames"), check=FALSE)) 1233 : mmaechler 2175 f0 <- callGeneric(0, e2) 1234 : mmaechler 2945 if(all0(f0)) { # remain diagonal 1235 : mmaechler 2175 L1 <- (le <- length(e2)) == 1L 1236 : mmaechler 2984 E <- copyClass(e1, "ldiMatrix", c("diag", "Dim", "Dimnames"), check=FALSE) 1237 : mmaechler 2820 ## storage.mode(E@x) <- "logical" 1238 : if(e1@diag == "U") { 1239 : if(any((r <- callGeneric(1, e2)) != 1)) { 1240 : E@diag <- "N" 1241 : E@x[seq_len(n)] <- r # possibly recycling r 1242 : } ## else: result = E (is "U" diag) 1243 : } else { 1244 : mmaechler 2175 r <- callGeneric(e1@x, e2) 1245 : mmaechler 2820 ## "future fixme": if we have idiMatrix, and r is 'integer', use idiMatrix 1246 : E@x[seq_len(n)] <- if(L1) r else r[1L + ((n+1)*(0:(n-1L))) %% le] 1247 : mmaechler 2814 } 1248 : mmaechler 2820 E 1249 : } else 1250 : callGeneric(diag2tT.u(e1,e2, "d"), e2) 1251 : mmaechler 2175 }) 1252 : 1253 : mmaechler 2820 ## ldi*: for "Ops" without "Arith": or --> result logical, potentially ldi 1254 : mmaechler 2813 for(arg2 in c("numeric","logical")) 1255 : setMethod("Ops", signature(e1 = "ldiMatrix", e2 = arg2), 1256 : maechler 2144 function(e1,e2) { 1257 : mmaechler 3167 n <- e1@Dim[1L] 1258 : mmaechler 3287 if(length(e2) == 0L) 1259 : return(if(n) logical() else e1) 1260 : maechler 2144 f0 <- callGeneric(FALSE, e2) 1261 : mmaechler 2945 if(all0(f0)) { # remain diagonal 1262 : maechler 2157 L1 <- (le <- length(e2)) == 1L 1263 : mmaechler 2820 1264 : if(e1@diag == "U") { 1265 : if(any((r <- callGeneric(TRUE, e2)) != 1)) { 1266 : e1@diag <- "N" 1267 : e1@x[seq_len(n)] <- r # possibly recycling r 1268 : } ## else: result = e1 (is "U" diag) 1269 : } else { 1270 : maechler 2144 r <- callGeneric(e1@x, e2) 1271 : mmaechler 2820 ## "future fixme": if we have idiMatrix, and r is 'integer', use idiMatrix 1272 : e1@x[] <- if(L1) r else r[1L + ((n+1)*(0:(n-1L))) %% le] 1273 : } 1274 : e1 1275 : } else 1276 : callGeneric(diag2tT.u(e1,e2, "l"), e2) 1277 : maechler 2144 }) 1278 : maechler 1655 1279 : maechler 2144 1280 : ## Not {"sparseMatrix", "numeric} : {"denseMatrix", "matrix", ... } 1281 : mmaechler 2196 for(other in c("ANY", "Matrix", "dMatrix")) { 1282 : ## ddi*: 1283 : setMethod("Ops", signature(e1 = "ddiMatrix", e2 = other), 1284 : mmaechler 2493 function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "d"), e2)) 1285 : mmaechler 2196 setMethod("Ops", signature(e1 = other, e2 = "ddiMatrix"), 1286 : mmaechler 2493 function(e1,e2) callGeneric(e1, diag2Tsmart(e2,e1, "d"))) 1287 : mmaechler 2196 ## ldi*: 1288 : setMethod("Ops", signature(e1 = "ldiMatrix", e2 = other), 1289 : mmaechler 2493 function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "l"), e2)) 1290 : mmaechler 2196 setMethod("Ops", signature(e1 = other, e2 = "ldiMatrix"), 1291 : mmaechler 2493 function(e1,e2) callGeneric(e1, diag2Tsmart(e2,e1, "l"))) 1292 : mmaechler 2196 } 1293 : mmaechler 2331 1294 : ## Direct subclasses of "denseMatrix": currently ddenseMatrix, ldense... : 1295 : mmaechler 3069 if(FALSE) # now also contains "geMatrix" 1296 : mmaechler 2331 dense.subCl <- local({ dM.scl <- getClass("denseMatrix")@subclasses 1297 : mmaechler 2813 names(dM.scl)[vapply(dM.scl, slot, 0, "distance") == 1] }) 1298 : mmaechler 3069 dense.subCl <- paste0(c("d","l","n"), "denseMatrix") 1299 : mmaechler 2268 for(DI in diCls) { 1300 : mmaechler 2814 dMeth <- if(extends(DI, "dMatrix")) 1301 : function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "d"), e2) 1302 : else # "lMatrix", the only other kind for now 1303 : function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "l"), e2) 1304 : mmaechler 2331 for(c2 in c(dense.subCl, "Matrix")) { 1305 : mmaechler 2814 for(Fun in c("*", "&")) { 1306 : mmaechler 2268 setMethod(Fun, signature(e1 = DI, e2 = c2), 1307 : function(e1,e2) callGeneric(e1, Diagonal(x = diag(e2)))) 1308 : setMethod(Fun, signature(e1 = c2, e2 = DI), 1309 : function(e1,e2) callGeneric(Diagonal(x = diag(e1)), e2)) 1310 : } 1311 : mmaechler 2814 setMethod("^", signature(e1 = c2, e2 = DI), 1312 : function(e1,e2) callGeneric(Diagonal(x = diag(e1)), e2)) 1313 : for(Fun in c("%%", "%/%", "/")) ## 0 0 |--> NaN for these. 1314 : setMethod(Fun, signature(e1 = DI, e2 = c2), dMeth) 1315 : mmaechler 2175 } 1316 : } 1317 : maechler 2144 1318 : mmaechler 3002 ## Group methods "Math", "Math2" in --> ./Math.R 1319 : 1320 : mmaechler 2175 ### "Summary" : "max" "min" "range" "prod" "sum" "any" "all" 1321 : mmaechler 2345 ### ---------- the last 4: separately here 1322 : mmaechler 2175 for(cl in diCls) { 1323 : setMethod("any", cl, 1324 : function (x, ..., na.rm) { 1325 : if(any(x@Dim == 0)) FALSE 1326 : else if(x@diag == "U") TRUE else any(x@x, ..., na.rm = na.rm) 1327 : }) 1328 : mmaechler 2341 setMethod("all", cl, function (x, ..., na.rm) { 1329 : mmaechler 3167 n <- x@Dim[1L] 1330 : mmaechler 2341 if(n >= 2) FALSE 1331 : else if(n == 0 || x@diag == "U") TRUE 1332 : else all(x@x, ..., na.rm = na.rm) 1333 : }) 1334 : setMethod("prod", cl, function (x, ..., na.rm) { 1335 : mmaechler 3167 n <- x@Dim[1L] 1336 : mmaechler 2341 if(n >= 2) 0 1337 : else if(n == 0 || x@diag == "U") 1 1338 : else ## n == 1, diag = "N" : 1339 : prod(x@x, ..., na.rm = na.rm) 1340 : }) 1341 : mmaechler 2175 1342 : setMethod("sum", cl, 1343 : function(x, ..., na.rm) { 1344 : r <- sum(x@x, ..., na.rm = na.rm)# double or integer, correctly 1345 : mmaechler 3167 if(x@diag == "U" && !is.na(r)) r + x@Dim[1L] else r 1346 : mmaechler 2175 }) 1347 : } 1348 : 1349 : ## The remaining ones are max, min, range : 1350 : 1351 : setMethod("Summary", "ddiMatrix", 1352 : function(x, ..., na.rm) { 1353 : if(any(x@Dim == 0)) callGeneric(numeric(0), ..., na.rm=na.rm) 1354 : else if(x@diag == "U") 1355 : callGeneric(x@x, 0, 1, ..., na.rm=na.rm) 1356 : else callGeneric(x@x, 0, ..., na.rm=na.rm) 1357 : }) 1358 : setMethod("Summary", "ldiMatrix", 1359 : function(x, ..., na.rm) { 1360 : if(any(x@Dim == 0)) callGeneric(logical(0), ..., na.rm=na.rm) 1361 : else if(x@diag == "U") 1362 : callGeneric(x@x, FALSE, TRUE, ..., na.rm=na.rm) 1363 : else callGeneric(x@x, FALSE, ..., na.rm=na.rm) 1364 : }) 1365 : 1366 : 1367 : 1368 : maechler 1109 ## similar to prTriang() in ./Auxiliaries.R : 1369 : prDiag <- 1370 : function(x, digits = getOption("digits"), justify = "none", right = TRUE) 1371 : { 1372 : cf <- array(".", dim = x@Dim, dimnames = x@Dimnames) 1373 : cf[row(cf) == col(cf)] <- 1374 : mmaechler 3120 vapply(diag(x), format, "", digits = digits, justify = justify) 1375 : maechler 1109 print(cf, quote = FALSE, right = right) 1376 : invisible(x) 1377 : } 1378 : 1379 : mmaechler 2237 ## somewhat consistent with "print" for sparseMatrix : 1380 : setMethod("print", signature(x = "diagonalMatrix"), prDiag) 1381 : 1382 : maechler 1109 setMethod("show", signature(object = "diagonalMatrix"), 1383 : maechler 1592 function(object) { 1384 : d <- dim(object) 1385 : cl <- class(object) 1386 : mmaechler 2203 cat(sprintf('%d x %d diagonal matrix of class "%s"', 1387 : mmaechler 3167 d[1L], d[2L], cl)) 1388 : if(d[1L] < 50) { 1389 : mmaechler 2203 cat("\n") 1390 : prDiag(object) 1391 : } else { 1392 : cat(", with diagonal entries\n") 1393 : show(diag(object)) 1394 : invisible(object) 1395 : } 1396 : maechler 1592 }) 1397 : mmaechler 2813 1398 : mmaechler 2998 rm(arg1, arg2, other, DI, Fun, cl, c1, c2, 1399 : mmaechler 2898 dense.subCl, diCls)# not used elsewhere 1400 : mmaechler 2820 1401 : setMethod("summary", signature(object = "diagonalMatrix"), 1402 : function(object, ...) { 1403 : d <- dim(object) 1404 : r <- summary(object@x, ...) 1405 : attr(r, "header") <- 1406 : sprintf('%d x %d diagonal Matrix of class "%s"', 1407 : mmaechler 3167 d[1L], d[2L], class(object)) 1408 : mmaechler 2820 ## use ole' S3 technology for such a simple case 1409 : class(r) <- c("diagSummary", class(r)) 1410 : r 1411 : }) 1412 : 1413 : print.diagSummary <- function (x, ...) { 1414 : cat(attr(x, "header"),"\n") 1415 : class(x) <- class(x)[-1] 1416 : print(x, ...) 1417 : invisible(x) 1418 : }

 root@r-forge.r-project.org ViewVC Help Powered by ViewVC 1.0.0
Thanks to: