SCM

SCM Repository

[matrix] Diff of /pkg/Matrix/R/diagMatrix.R
ViewVC logotype

Diff of /pkg/Matrix/R/diagMatrix.R

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1805, Tue Mar 27 16:46:03 2007 UTC revision 2206, Wed Jun 25 14:57:02 2008 UTC
# Line 16  Line 16 
16      if(missing(x)) ## unit diagonal matrix      if(missing(x)) ## unit diagonal matrix
17          new("ddiMatrix", Dim = c(n,n), diag = "U")          new("ddiMatrix", Dim = c(n,n), diag = "U")
18      else {      else {
19          stopifnot(length(x) == n)          lx <- length(x)
20            stopifnot(lx == 1 || lx == n) # but keep 'x' short for now
21          if(is.logical(x))          if(is.logical(x))
22              cl <- "ldiMatrix"              cl <- "ldiMatrix"
23          else if(is.numeric(x)) {          else if(is.numeric(x)) {
# Line 26  Line 27 
27          else if(is.complex(x)) {          else if(is.complex(x)) {
28              cl <- "zdiMatrix"  # will not yet work              cl <- "zdiMatrix"  # will not yet work
29          } else stop("'x' has invalid data type")          } else stop("'x' has invalid data type")
30          new(cl, Dim = c(n,n), diag = "N", x = x)          new(cl, Dim = c(n,n), diag = "N",
31                x = if(lx == 1) rep.int(x,n) else x)
32      }      }
33  }  }
34    
35    ## Pkg 'spdep' had (relatively slow) versions of this as_dsCMatrix_I()
36    .symDiagonal <- function(n, x = rep.int(1,n), uplo = "U") {
37        stopifnot(n == (n. <- as.integer(n)), (n <- n.) >= 0)
38        if((lx <- length(x)) == 1) x <- rep.int(x, n)
39        else if(lx != n) stop("length(x) must be 1 or n")
40        cls <-
41            if(is.double(x)) "dsCMatrix"
42            else if(is.logical(x)) "lsCMatrix"
43            else { ## for now
44                storage.mode(x) <- "double"
45                "dsCMatrix"
46            }
47        new(cls, Dim = c(n,n), x = x, uplo = uplo,
48            i = if(n) 0:(n - 1L) else integer(0), p = 0:n)
49    }
50    
51  ### This is modified from a post of Bert Gunter to R-help on  1 Sep 2005.  ### This is modified from a post of Bert Gunter to R-help on  1 Sep 2005.
52  ### Bert's code built on a post by Andy Liaw who most probably was influenced  ### Bert's code built on a post by Andy Liaw who most probably was influenced
53  ### by earlier posts, notably one by Scott Chasalow on S-news, 16 Jan 2002  ### by earlier posts, notably one by Scott Chasalow on S-news, 16 Jan 2002
54  ### who posted his bdiag() function written in December 1995.  ### who posted his bdiag() function written in December 1995.
55    if(FALSE)##--- currently unused:
56  bdiag <- function(...) {  .bdiag <- function(lst) {
57      if(nargs() == 0) return(new("dgCMatrix"))      ### block-diagonal matrix [a dgTMatrix] from list of matrices
58      ## else :      stopifnot(is.list(lst), length(lst) >= 1)
59      mlist <- if (nargs() == 1) as.list(...) else list(...)      dims <- sapply(lst, dim, USE.NAMES=FALSE)
     dims <- sapply(mlist, dim)  
60      ## make sure we had all matrices:      ## make sure we had all matrices:
61      if(!(is.matrix(dims) && nrow(dims) == 2))      if(!(is.matrix(dims) && nrow(dims) == 2))
62          stop("some arguments are not matrices")          stop("some arguments are not matrices")
63      csdim <- rbind(rep.int(0:0, 2),      csdim <- rbind(rep.int(0L, 2),
64                     apply(sapply(mlist, dim), 1, cumsum))                     apply(dims, 1, cumsum))
65      ret <- new("dgTMatrix", Dim = as.integer(csdim[nrow(csdim),]))      r <- new("dgTMatrix")
66        r@Dim <- as.integer(csdim[nrow(csdim),])
67      add1 <- matrix(1:0, 2,2)      add1 <- matrix(1:0, 2,2)
68      for(i in seq_along(mlist)) {      for(i in seq_along(lst)) {
69          indx <- apply(csdim[i:(i+1),] + add1, 2, function(n) n[1]:n[2])          indx <- apply(csdim[i:(i+1),] + add1, 2, function(n) n[1]:n[2])
70          if(is.null(dim(indx))) ## non-square matrix          if(is.null(dim(indx))) ## non-square matrix
71              ret[indx[[1]],indx[[2]]] <- mlist[[i]]              r[indx[[1]],indx[[2]]] <- lst[[i]]
72          else ## square matrix          else ## square matrix
73              ret[indx[,1],indx[,2]] <- mlist[[i]]              r[indx[,1], indx[,2]] <- lst[[i]]
74      }      }
75      ## slightly debatable if we really should return Csparse.. :      r
76      as(ret, "CsparseMatrix")  }
77    ### Doug Bates needed something like bdiag() for lower-triangular
78    ### (Tsparse) Matrices and provided a much more efficient implementation:
79    .bdiag <- function(lst) {
80        ### block-diagonal matrix [a dgTMatrix] from list of matrices
81        stopifnot(is.list(lst), (nl <- length(lst)) >= 1)
82    
83        Tlst <- lapply(lapply(lst, Matrix:::as_Csp2), # includes "diagU2N"
84                       as, "TsparseMatrix")
85    
86        if(nl == 1) return(Tlst[[1]])
87        ## else
88        i_off <- c(0L, cumsum(sapply(Tlst, nrow)))
89        j_off <- c(0L, cumsum(sapply(Tlst, ncol)))
90        new("dgTMatrix", Dim = c(i_off[nl+1], j_off[nl + 1]),
91            i = unlist(lapply(1:nl, function(k) Tlst[[k]]@i + i_off[k])),
92            j = unlist(lapply(1:nl, function(k) Tlst[[k]]@j + j_off[k])),
93            x = unlist(lapply(Tlst, slot, "x")))
94  }  }
95    
96  diag2T <- function(from) {  bdiag <- function(...) {
97      i <- if(from@diag == "U") integer(0) else seq_len(from@Dim[1]) - 1:1      if((nA <- nargs()) == 0) return(new("dgCMatrix"))
98      new(paste(.M.kind(from), "tTMatrix", sep=''),      if(nA == 1 && !is.list(...))
99            return(as(..., "CsparseMatrix"))
100        alis <- if(nA == 1 && is.list(..1)) ..1 else list(...)
101        if(length(alis) == 1)
102            return(as(alis[[1]], "CsparseMatrix"))
103    
104        ## else : two or more arguments
105        as(.bdiag(alis), "CsparseMatrix")
106    }
107    
108    
109    .diag2tT <- function(from, uplo = "U", kind = .M.kind(from)) {
110        ## to triangular Tsparse
111        i <- if(from@diag == "U") integer(0) else seq_len(from@Dim[1]) - 1L
112        new(paste(kind, "tTMatrix", sep=''),
113          diag = from@diag, Dim = from@Dim, Dimnames = from@Dimnames,          diag = from@diag, Dim = from@Dim, Dimnames = from@Dimnames,
114            uplo = uplo,
115          x = from@x, # <- ok for diag = "U" and "N" (!)          x = from@x, # <- ok for diag = "U" and "N" (!)
116          i = i, j = i)          i = i, j = i)
117  }  }
118    
119  setAs("diagonalMatrix", "triangularMatrix", diag2T)  .diag2sT <- function(from, uplo = "U", kind = .M.kind(from)) {
120  setAs("diagonalMatrix", "sparseMatrix", diag2T)      ## to symmetric Tsparse
121        n <- from@Dim[1]
122        i <- seq_len(n) - 1L
123        new(paste(kind, "sTMatrix", sep=''),
124            Dim = from@Dim, Dimnames = from@Dimnames,
125            i = i, j = i, uplo = uplo,
126            x = if(from@diag == "N") from@x else ## "U"-diag
127            rep.int(switch(kind,
128                           "d" = 1.,
129                           "l" =,
130                           "n" = TRUE,
131                           ## otherwise
132                           stop("'", kind,"' kind not yet implemented")), n))
133    }
134    
135    ## diagonal -> triangular,  upper / lower depending on "partner":
136    diag2tT.u <- function(d, x, kind = .M.kind(d))
137        .diag2tT(d, uplo = if(is(x,"triangularMatrix")) x@uplo else "U", kind)
138    
139    ## diagonal -> sparse {triangular OR symmetric} (upper / lower) depending on "partner":
140    diag2Tsmart <- function(d, x, kind = .M.kind(d)) {
141        clx <- getClassDef(class(x))
142        if(extends(clx, "symmetricMatrix"))
143            .diag2sT(d, uplo = x@uplo, kind)
144        else
145            .diag2tT(d, uplo = if(extends(clx,"triangularMatrix")) x@uplo else "U", kind)
146    }
147    
148    
149    ## In order to evade method dispatch ambiguity warnings,
150    ## and because we can save a .M.kind() call, we use this explicit
151    ## "hack"  instead of signature  x = "diagonalMatrix" :
152    ##
153    ## ddi*:
154    diag2tT <- function(from) .diag2tT(from, "U", "d")
155    setAs("ddiMatrix", "triangularMatrix", diag2tT)
156    setAs("ddiMatrix", "sparseMatrix", diag2tT)
157  ## needed too (otherwise <dense> -> Tsparse is taken):  ## needed too (otherwise <dense> -> Tsparse is taken):
158  setAs("diagonalMatrix", "TsparseMatrix", diag2T)  setAs("ddiMatrix", "TsparseMatrix", diag2tT)
159  ## is better than this:  setAs("ddiMatrix", "CsparseMatrix",
160  ## setAs("diagonalMatrix", "sparseMatrix",        function(from) as(.diag2tT(from, "U", "d"), "CsparseMatrix"))
161  ##       function(from)  setAs("ddiMatrix", "symmetricMatrix",
162  ##        as(from, if(is(from, "dMatrix")) "dgCMatrix" else "lgCMatrix"))        function(from) .diag2sT(from, "U", "d"))
163  setAs("diagonalMatrix", "CsparseMatrix",  ##
164        function(from) as(diag2T(from), "CsparseMatrix"))  ## ldi*:
165    diag2tT <- function(from) .diag2tT(from, "U", "l")
166    setAs("ldiMatrix", "triangularMatrix", diag2tT)
167    setAs("ldiMatrix", "sparseMatrix", diag2tT)
168    ## needed too (otherwise <dense> -> Tsparse is taken):
169    setAs("ldiMatrix", "TsparseMatrix", diag2tT)
170    setAs("ldiMatrix", "CsparseMatrix",
171          function(from) as(.diag2tT(from, "U", "l"), "CsparseMatrix"))
172    setAs("ldiMatrix", "symmetricMatrix",
173          function(from) .diag2sT(from, "U", "l"))
174    
175  setAs("diagonalMatrix", "matrix",  
176    setAs("diagonalMatrix", "nMatrix",
177        function(from) {        function(from) {
178            n <- from@Dim[1]            n <- from@Dim[1]
179            diag(x = if(from@diag == "U") { if(is.numeric(from@x)) 1. else TRUE            i <- if(from@diag == "U") integer(0) else which(isN0(from@x)) - 1L
180                                       } else from@x,            new("ntTMatrix", i = i, j = i, diag = from@diag,
181                 nrow = n, ncol = n)                Dim = from@Dim, Dimnames = from@Dimnames)
182        })        })
183    
 setAs("diagonalMatrix", "generalMatrix", # prefer sparse:  
       function(from) as(from, paste(.M.kind(from), "gCMatrix", sep='')))  
   
 .diag.x <- function(m) {  
     if(m@diag == "U")  
         rep.int(if(is.numeric(m@x)) 1. else TRUE,  
                 m@Dim[1])  
     else m@x  
 }  
184    
185  .diag.2N <- function(m) {  ## Cheap fast substitute for diag() which *does* preserve the mode of x :
186      if(m@diag == "U") m@diag <- "N"  mkDiag <- function(x, n) {
187      m      y <- matrix(as0(mod=mode(x)), n,n)
188        if (n > 0) y[1L + 0:(n - 1L) * (n + 1L)] <- x
189        y
190  }  }
191    
192  ## given the above, the following  4  coercions should be all unneeded;  setAs("diagonalMatrix", "matrix",
 ## we prefer triangular to general:  
 setAs("ddiMatrix", "dgTMatrix",  
193        function(from) {        function(from) {
194            .Deprecated("as(, \"sparseMatrix\")")            ## want "ldiMatrix" -> <logical> "matrix" :
195            n <- from@Dim[1]            mkDiag(if(from@diag == "U") as1(from@x) else from@x,
196            i <- seq_len(n) - 1:1                   n = from@Dim[1])
197            new("dgTMatrix", i = i, j = i, x = .diag.x(from),        })
               Dim = c(n,n), Dimnames = from@Dimnames) })  
198    
199  setAs("ddiMatrix", "dgCMatrix",  setMethod("as.vector", signature(x = "diagonalMatrix", mode="missing"),
200        function(from) as(as(from, "sparseMatrix"), "dgCMatrix"))            function(x, mode) {
201                  n <- x@Dim[1]
202                  mod.x <- mode(x@x)
203                  r <- vector(mod.x, length = n^2)
204                  if(n)
205                      r[1 + 0:(n - 1) * (n + 1)] <-
206                          if(x@diag == "U") as1(mod=mod.x) else x@x
207                  r
208              })
209    
210  setAs("ldiMatrix", "lgTMatrix",  setAs("diagonalMatrix", "generalMatrix", # prefer sparse:
211        function(from) {        function(from) as(as(from, "CsparseMatrix"), "generalMatrix"))
           .Deprecated("as(, \"sparseMatrix\")")  
           n <- from@Dim[1]  
           if(from@diag == "U") { # unit-diagonal  
               x <- rep.int(TRUE, n)  
               i <- seq_len(n) - 1:1  
           } else { # "normal"  
               nz <- nz.NA(from@x, na. = TRUE)  
               x <- from@x[nz]  
               i <- which(nz) - 1:1  
           }  
           new("lgTMatrix", i = i, j = i, x = x,  
               Dim = c(n,n), Dimnames = from@Dimnames) })  
212    
213  setAs("ldiMatrix", "lgCMatrix",  .diag.x <- function(m) if(m@diag == "U") rep.int(as1(m@x), m@Dim[1]) else m@x
       function(from) as(as(from, "lgTMatrix"), "lgCMatrix"))  
214    
215    .diag.2N <- function(m) {
216        if(m@diag == "U") m@diag <- "N"
217        m
218    }
219    
 if(FALSE) # now have faster  "ddense" -> "dge"  
220  setAs("ddiMatrix", "dgeMatrix",  setAs("ddiMatrix", "dgeMatrix",
221        function(from) as(as(from, "matrix"), "dgeMatrix"))        function(from) .Call(dup_mMatrix_as_dgeMatrix, from))
222    setAs("ddiMatrix", "ddenseMatrix",
223          function(from) as(as(from, "triangularMatrix"),"denseMatrix"))
224    setAs("ldiMatrix", "ldenseMatrix",
225          function(from) as(as(from, "triangularMatrix"),"denseMatrix"))
226    
227    
228  setAs("matrix", "diagonalMatrix",  setAs("matrix", "diagonalMatrix",
229        function(from) {        function(from) {
# Line 177  Line 266 
266    
267    
268  setMethod("diag", signature(x = "diagonalMatrix"),  setMethod("diag", signature(x = "diagonalMatrix"),
269            function(x = 1, nrow, ncol = n) .diag.x(x))            function(x = 1, nrow, ncol) .diag.x(x))
   
270    
271  subDiag <- function(x, i, j, drop) {  subDiag <- function(x, i, j, ..., drop) {
272      x <- as(x, "sparseMatrix")      x <- as(x, "sparseMatrix")
273      x <- if(missing(i))      x <- if(missing(i))
274          x[, j, drop=drop]          x[, j, drop=drop]
# Line 188  Line 276 
276          x[i, , drop=drop]          x[i, , drop=drop]
277      else      else
278          x[i,j, drop=drop]          x[i,j, drop=drop]
279      if(isDiagonal(x)) as(x, "diagonalMatrix") else x      if(isS4(x) && isDiagonal(x)) as(x, "diagonalMatrix") else x
280  }  }
281    
282  setMethod("[", signature(x = "diagonalMatrix", i = "index",  setMethod("[", signature(x = "diagonalMatrix", i = "index",
283                           j = "index", drop = "logical"), subDiag)                           j = "index", drop = "logical"), subDiag)
284  setMethod("[", signature(x = "diagonalMatrix", i = "index",  setMethod("[", signature(x = "diagonalMatrix", i = "index",
285                          j = "missing", drop = "logical"),                          j = "missing", drop = "logical"),
286            function(x, i, drop) subDiag(x, i=i, drop=drop))            function(x, i, j, ..., drop) subDiag(x, i=i, drop=drop))
287  setMethod("[", signature(x = "diagonalMatrix", i = "missing",  setMethod("[", signature(x = "diagonalMatrix", i = "missing",
288                           j = "index", drop = "logical"),                           j = "index", drop = "logical"),
289            function(x, j, drop) subDiag(x, j=j, drop=drop))            function(x, i, j, ..., drop) subDiag(x, j=j, drop=drop))
290    
291  ## When you assign to a diagonalMatrix, the result should be  ## When you assign to a diagonalMatrix, the result should be
292  ## diagonal or sparse ---  ## diagonal or sparse ---
293  ## FIXME: this now fails because the "denseMatrix" methods come first in dispatch  ## FIXME: this now fails because the "denseMatrix" methods come first in dispatch
294  replDiag <- function(x, i, j, value) {  ## Only(?) current bug:  x[i] <- value  is wrong when  i is *vector*
295    replDiag <- function(x, i, j, ..., value) {
296      x <- as(x, "sparseMatrix")      x <- as(x, "sparseMatrix")
297      if(missing(i))      if(missing(i))
298          x[, j] <- value          x[, j] <- value
299      else if(missing(j))      else if(missing(j)) { ##  x[i , ] <- v  *OR*   x[i] <- v
300            na <- nargs()
301    ##         message("diagnosing replDiag() -- nargs()= ", na)
302            if(na == 4)
303          x[i, ] <- value          x[i, ] <- value
304      else          else if(na == 3)
305                x[i] <- value
306            else stop("Internal bug: nargs()=",na,"; please report")
307        } else
308          x[i,j] <- value          x[i,j] <- value
309      if(isDiagonal(x)) as(x, "diagonalMatrix") else x      if(isDiagonal(x)) as(x, "diagonalMatrix") else x
310  }  }
311    
312  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",
313                                  j = "index", value = "replValue"), replDiag)                                  j = "index", value = "replValue"), replDiag)
314    
315  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",
316                                  j = "missing", value = "replValue"),                                  j = "missing", value = "replValue"),
317                   function(x, i, value) replDiag(x, i=i, value=value))                   function(x,i,j, ..., value) {
318                         ## message("before replDiag() -- nargs()= ", nargs())
319                         if(nargs() == 3)
320                             replDiag(x, i=i, value=value)
321                         else ## nargs() == 4 :
322                             replDiag(x, i=i, , value=value)
323                     })
324    
325    setReplaceMethod("[", signature(x = "diagonalMatrix",
326                                    i = "matrix", # 2-col.matrix
327                                    j = "missing", value = "replValue"),
328                     function(x,i,j, ..., value) {
329                         if(ncol(i) == 2) {
330                             if(all((ii <- i[,1]) == i[,2])) { # replace in diagonal only
331                                 if(x@diag == "U") {
332                                     one <- as1(x@x)
333                                     if(any(value != one | is.na(value))) {
334                                         x@diag <- "N"
335                                         x@x <- rep.int(one, x@Dim[1])
336                                     }
337                                 }
338                                 x@x[ii] <- value
339                                 x
340                             } else { ## no longer diagonal, but remain sparse:
341                                 x <- as(x, "sparseMatrix")
342                                 x[i] <- value
343                                 x
344                             }
345                         }
346                         else { # behave as "base R": use as if vector
347                             x <- as(x, "matrix")
348                             x[i] <- value
349                             Matrix(x)
350                         }
351                     })
352    
353  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing",  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing",
354                                  j = "index", value = "replValue"),                                  j = "index", value = "replValue"),
355                   function(x, j, value) replDiag(x, j=j, value=value))                   function(x,i,j, ..., value) replDiag(x, j=j, value=value))
356    
357    
358  setMethod("t", signature(x = "diagonalMatrix"),  setMethod("t", signature(x = "diagonalMatrix"),
# Line 232  Line 363 
363  setMethod("isTriangular", signature(object = "diagonalMatrix"),  setMethod("isTriangular", signature(object = "diagonalMatrix"),
364            function(object) TRUE)            function(object) TRUE)
365  setMethod("isSymmetric", signature(object = "diagonalMatrix"),  setMethod("isSymmetric", signature(object = "diagonalMatrix"),
366            function(object) TRUE)            function(object, ...) TRUE)
367    
368  setMethod("chol", signature(x = "ddiMatrix"),# pivot = "ANY"  setMethod("symmpart", signature(x = "diagonalMatrix"), function(x) x)
369            function(x, pivot) {  setMethod("skewpart", signature(x = "diagonalMatrix"), setZero)
370                if(any(x@x < 0)) stop("chol() is undefined for diagonal matrix with negative entries")  
371    setMethod("chol", signature(x = "ddiMatrix"),
372              function(x, pivot, ...) {
373                  if(x@diag == "U") return(x)
374                  ## else
375                  if(any(x@x < 0))
376                      stop("chol() is undefined for diagonal matrix with negative entries")
377                x@x <- sqrt(x@x)                x@x <- sqrt(x@x)
378                x                x
379            })            })
380  ## chol(L) is L for logical diagonal:  ## chol(L) is L for logical diagonal:
381  setMethod("chol", signature(x = "ldiMatrix"), function(x, pivot) x)  setMethod("chol", signature(x = "ldiMatrix"), function(x, pivot, ...) x)
382    
383  setMethod("!", "ldiMatrix", function(e1) {  setMethod("determinant", signature(x = "diagonalMatrix", logarithm = "logical"),
384      if(e1@diag == "N")            function(x, logarithm, ...) mkDet(.diag.x(x), logarithm))
385          e1@x <- !e1@x  
386      else { ## "U"  setMethod("norm", signature(x = "diagonalMatrix", type = "character"),
387          e1@diag <- "N"            function(x, type, ...) {
388          e1@x <- rep.int(FALSE, e1@Dim[1])                if((n <- x@Dim[1]) == 0) return(0) # as for "sparseMatrix"
389                  type <- toupper(substr(type[1], 1, 1))
390                  isU <- (x@diag == "U") # unit-diagonal
391                  if(type == "F") sqrt(if(isU) n else sum(x@x^2))
392                  else { ## norm == "I","1","O","M" :
393                      if(isU) 1 else max(abs(x@x))
394      }      }
     e1  
395  })  })
396    
397    
398    
399  ## Basic Matrix Multiplication {many more to add}  ## Basic Matrix Multiplication {many more to add}
400  ##       ---------------------  ##       ---------------------
401  ## Note that "ldi" logical are treated as numeric  ## Note that "ldi" logical are treated as numeric
402  diagdiagprod <- function(x, y) {  diagdiagprod <- function(x, y) {
403      if(any(dim(x) != dim(y))) stop("non-matching dimensions")      n <- dimCheck(x,y)[1]
404      if(x@diag != "U") {      if(x@diag != "U") {
405          if(y@diag != "U") {          if(y@diag != "U") {
406              nx <- x@x * y@x              nx <- x@x * y@x
# Line 285  Line 428 
428    
429    
430  diagmatprod <- function(x, y) {  diagmatprod <- function(x, y) {
431        ## x is diagonalMatrix
432      dx <- dim(x)      dx <- dim(x)
433      dy <- dim(y)      dy <- dim(y)
434      if(dx[2] != dy[1]) stop("non-matching dimensions")      if(dx[2] != dy[1]) stop("non-matching dimensions")
435      n <- dx[1]      n <- dx[1]
436      as(if(x@diag == "U") y else x@x * y, "Matrix")      as(if(x@diag == "U") y else x@x * y, "Matrix")
437  }  }
   
438  setMethod("%*%", signature(x = "diagonalMatrix", y = "matrix"),  setMethod("%*%", signature(x = "diagonalMatrix", y = "matrix"),
439            diagmatprod)            diagmatprod)
440    ## sneaky .. :
441  formals(diagmatprod) <- alist(x=, y=NULL)  formals(diagmatprod) <- alist(x=, y=NULL)
442  setMethod("crossprod", signature(x = "diagonalMatrix", y = "matrix"),  setMethod("crossprod", signature(x = "diagonalMatrix", y = "matrix"),
443            diagmatprod)            diagmatprod)
 setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "matrix"),  
           diagmatprod)  
444    
445  diagdgeprod <- function(x, y) {  diagGeprod <- function(x, y) {
446      dx <- dim(x)      dx <- dim(x)
447      dy <- dim(y)      dy <- dim(y)
448      if(dx[2] != dy[1]) stop("non-matching dimensions")      if(dx[2] != dy[1]) stop("non-matching dimensions")
# Line 308  Line 450 
450          y@x <- x@x * y@x          y@x <- x@x * y@x
451      y      y
452  }  }
453  setMethod("%*%", signature(x = "diagonalMatrix", y = "dgeMatrix"),  setMethod("%*%", signature(x= "diagonalMatrix", y= "dgeMatrix"), diagGeprod)
454            diagdgeprod, valueClass = "dgeMatrix")  setMethod("%*%", signature(x= "diagonalMatrix", y= "lgeMatrix"), diagGeprod)
455  formals(diagdgeprod) <- alist(x=, y=NULL)  formals(diagGeprod) <- alist(x=, y=NULL)
456  setMethod("crossprod", signature(x = "diagonalMatrix", y = "dgeMatrix"),  setMethod("crossprod", signature(x = "diagonalMatrix", y = "dgeMatrix"),
457            diagdgeprod, valueClass = "dgeMatrix")            diagGeprod, valueClass = "dgeMatrix")
458    setMethod("crossprod", signature(x = "diagonalMatrix", y = "lgeMatrix"),
459              diagGeprod)
460    
461  setMethod("%*%", signature(x = "matrix", y = "diagonalMatrix"),  matdiagprod <- function(x, y) {
           function(x, y) {  
462                dx <- dim(x)                dx <- dim(x)
463                dy <- dim(y)                dy <- dim(y)
464                if(dx[2] != dy[1]) stop("non-matching dimensions")                if(dx[2] != dy[1]) stop("non-matching dimensions")
465                as(if(y@diag == "U") x else x * rep(y@x, each = dx[1]), "Matrix")      Matrix(if(y@diag == "U") x else x * rep(y@x, each = dx[1]))
466            })  }
467    setMethod("%*%", signature(x = "matrix", y = "diagonalMatrix"),
468              matdiagprod)
469    formals(matdiagprod) <- alist(x=, y=NULL)
470    setMethod("tcrossprod", signature(x = "matrix", y = "diagonalMatrix"),
471              matdiagprod)
472    
473  setMethod("%*%", signature(x = "dgeMatrix", y = "diagonalMatrix"),  gediagprod <- function(x, y) {
           function(x, y) {  
474                dx <- dim(x)                dx <- dim(x)
475                dy <- dim(y)                dy <- dim(y)
476                if(dx[2] != dy[1]) stop("non-matching dimensions")                if(dx[2] != dy[1]) stop("non-matching dimensions")
477                if(y@diag == "N")                if(y@diag == "N")
478                    x@x <- x@x * rep(y@x, each = dx[1])                    x@x <- x@x * rep(y@x, each = dx[1])
479                x                x
480            })  }
481    setMethod("%*%", signature(x= "dgeMatrix", y= "diagonalMatrix"), gediagprod)
482    setMethod("%*%", signature(x= "lgeMatrix", y= "diagonalMatrix"), gediagprod)
483    formals(gediagprod) <- alist(x=, y=NULL)
484    setMethod("tcrossprod", signature(x = "dgeMatrix", y = "diagonalMatrix"),
485              gediagprod)
486    setMethod("tcrossprod", signature(x = "lgeMatrix", y = "diagonalMatrix"),
487              gediagprod)
488    
489  ## crossprod {more of these}  ## crossprod {more of these}
490    
491  ## tcrossprod --- all are not yet there: do the dense ones here:  ## tcrossprod --- all are not yet there: do the dense ones here:
492    
493    setMethod("%*%", signature(x = "diagonalMatrix", y = "denseMatrix"),
494              function(x, y) if(x@diag == "U") y else x %*% as(y, "generalMatrix"))
495    setMethod("%*%", signature(x = "denseMatrix", y = "diagonalMatrix"),
496              function(x, y) if(y@diag == "U") x else as(x, "generalMatrix") %*% y)
497    
498    
499  ## FIXME:  ## FIXME:
500  ## setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "denseMatrix"),  ## setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "denseMatrix"),
501  ##        function(x, y = NULL) {  ##        function(x, y = NULL) {
# Line 346  Line 506 
506  ##           })  ##           })
507    
508  setMethod("crossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),  setMethod("crossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
509            function(x, y = NULL) { x <- as(x, "sparseMatrix"); callGeneric() })            function(x, y = NULL) crossprod(as(x, "sparseMatrix"), y))
510    
511  setMethod("crossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),  setMethod("crossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
512            function(x, y = NULL) { y <- as(y, "sparseMatrix"); callGeneric() })            function(x, y = NULL) crossprod(x, as(y, "sparseMatrix")))
513    
514  setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),  setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
515            function(x, y = NULL) { x <- as(x, "sparseMatrix"); callGeneric() })            function(x, y = NULL) tcrossprod(as(x, "sparseMatrix"), y))
516    
517  setMethod("tcrossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),  setMethod("tcrossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
518            function(x, y = NULL) { y <- as(y, "sparseMatrix"); callGeneric() })            function(x, y = NULL) tcrossprod(x, as(y, "sparseMatrix")))
519    
520    
521  ## FIXME?: In theory, this can be done *FASTER*, in some cases, via tapply1()  ## FIXME?: In theory, this can be done *FASTER*, in some cases, via tapply1()
522  setMethod("%*%", signature(x = "diagonalMatrix", y = "sparseMatrix"),  setMethod("%*%", signature(x = "diagonalMatrix", y = "sparseMatrix"),
523            function(x, y) as(x, "sparseMatrix") %*% y)            function(x, y) as(x, "sparseMatrix") %*% y)
524    setMethod("%*%", signature(x = "sparseMatrix", y = "diagonalMatrix"),
525              function(x, y) x %*% as(y, "sparseMatrix"))
526  ## NB: The previous is *not* triggering for  "ddi" o "dgC" (= distance 3)  ## NB: The previous is *not* triggering for  "ddi" o "dgC" (= distance 3)
527  ##     since there's a "ddense" o "Csparse" at dist. 2 => triggers first.  ##     since there's a "ddense" o "Csparse" at dist. 2 => triggers first.
528  ## ==> do this:  ## ==> do this:
# Line 372  Line 534 
534  ## TODO: Write tests in ./tests/ which ensure that many "ops" with diagonal*  ## TODO: Write tests in ./tests/ which ensure that many "ops" with diagonal*
535  ##       do indeed work by going through sparse (and *not* ddense)!  ##       do indeed work by going through sparse (and *not* ddense)!
536    
 setMethod("%*%", signature(x = "sparseMatrix", y = "diagonalMatrix"),  
           function(x, y) x %*% as(y, "sparseMatrix"))  
537    
538    
539  setMethod("solve", signature(a = "diagonalMatrix", b = "missing"),  setMethod("solve", signature(a = "diagonalMatrix", b = "missing"),
# Line 396  Line 556 
556  setMethod("solve", signature(a = "diagonalMatrix", b = "Matrix"),  setMethod("solve", signature(a = "diagonalMatrix", b = "Matrix"),
557            solveDiag)            solveDiag)
558    
559    ## Schur()  ---> ./eigen.R
560    
561    
562    
563  ### ---------------- diagonal  o  sparse  -----------------------------  ###---------------- <Ops> (<Arith>, <Logic>, <Compare> ) ----------------------
   
564    
565  ## Use function for several signatures, in order to evade  ## Use function for several signatures, in order to evade
566  ## ambiguous dispatch for "ddi", since there's also Arith(ddense., ddense.)  ## ambiguous dispatch for "ddi", since there's also Arith(ddense., ddense.)
567  diagOdiag <- function(e1,e2) { # result should also be diagonal  diagOdiag <- function(e1,e2) {
568        ## result should also be diagonal _ if possible _
569      r <- callGeneric(.diag.x(e1), .diag.x(e2)) # error if not "compatible"      r <- callGeneric(.diag.x(e1), .diag.x(e2)) # error if not "compatible"
570        ## Check what happens with non-diagonals, i.e. (0 o 0), (FALSE o 0), ...:
571        r00 <- callGeneric(if(is.numeric(e1@x)) 0 else FALSE,
572                           if(is.numeric(e2@x)) 0 else FALSE)
573        if(is0(r00)) { ##  r00 == 0 or FALSE --- result *is* diagonal
574      if(is.numeric(r)) {      if(is.numeric(r)) {
575          if(is.numeric(e2@x)) {          if(is.numeric(e2@x)) {
576              e2@x <- r; return(.diag.2N(e2)) }              e2@x <- r; return(.diag.2N(e2)) }
# Line 419  Line 584 
584      e1@x <- r      e1@x <- r
585      .diag.2N(e1)      .diag.2N(e1)
586  }  }
587        else { ## result not diagonal, but at least symmetric:
588            isNum <- (is.numeric(r) || is.numeric(r00))
589            isLog <- (is.logical(r) || is.logical(r00))
590    
591            if(getOption("verbose"))
592                message("exploding  <diag>  o  <diag>  into dense matrix")
593            d <- e1@Dim
594            n <- d[1]
595            stopifnot(length(r) == n)
596            xx <- as.vector(matrix(rbind(r, matrix(r00,n,n)), n,n))
597            newcl <-
598                paste(if(isNum) "d" else if(isLog) {
599                    if(!any(is.na(r)) && !any(is.na(r00))) "n" else "l"
600                } else stop("not yet implemented .. please report")
601                      ,
602                      "syMatrix", sep='')
603    
604            new(newcl, Dim = e1@Dim, Dimnames = e1@Dimnames, x = xx)
605        }
606    }
607    
608    ### This would be *the* way, but we get tons of "ambiguous method dispatch"
609    ## we use this hack instead of signature  x = "diagonalMatrix" :
610    diCls <- names(getClass("diagonalMatrix")@subclasses)
611    if(FALSE) {
612  setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "diagonalMatrix"),  setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "diagonalMatrix"),
613            diagOdiag)            diagOdiag)
614  ## These two are just for method disambiguation:  } else { ## These are just for method disambiguation:
615  setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "diagonalMatrix"),      for(c1 in diCls)
616            diagOdiag)          for(c2 in diCls)
617  setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "ddiMatrix"),              setMethod("Ops", signature(e1 = c1, e2 = c2), diagOdiag)
618            diagOdiag)  }
619    
620    ## FIXME:    diagonal  o  triangular  |-->  triangular
621    ## -----     diagonal  o  symmetric   |-->  symmetric
622    ##    {also when other is sparse: do these "here" --
623    ##     before conversion to sparse, since that loses "diagonality"}
624    
625  ## For almost everything else, diag* shall be treated "as sparse" :  ## For almost everything else, diag* shall be treated "as sparse" :
626  ## These are cheap implementations via coercion  ## These are cheap implementations via coercion
627    
628  ## for disambiguation  ## For disambiguation --- define this for "sparseMatrix" , then for "ANY";
629  setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "sparseMatrix"),  ## and because we can save an .M.kind() call, we use this explicit
630            function(e1,e2) callGeneric(as(e1, "sparseMatrix"), e2))  ## "hack" for all diagonalMatrix *subclasses* instead of just "diagonalMatrix" :
631  setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "diagonalMatrix"),  ##
632            function(e1,e2) callGeneric(e1, as(e2, "sparseMatrix")))  ## ddi*:
633  ## in general:  setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "sparseMatrix"),
634  setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "ANY"),            function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "d"), e2))
635            function(e1,e2) callGeneric(as(e1,"sparseMatrix"), e2))  setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "ddiMatrix"),
636  setMethod("Ops", signature(e1 = "ANY", e2 = "diagonalMatrix"),            function(e1,e2) callGeneric(e1, diag2Tsmart(e2,e1, "d")))
637            function(e1,e2) callGeneric(e1, as(e2,"sparseMatrix")))  ## ldi*
638    setMethod("Ops", signature(e1 = "ldiMatrix", e2 = "sparseMatrix"),
639              function(e1,e2) callGeneric(diag2Tsmart(e1,e2, "l"), e2))
640    setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "ldiMatrix"),
641              function(e1,e2) callGeneric(e1, diag2Tsmart(e2,e1, "l")))
642    
643    ## Ops:  Arith  --> numeric : "dMatrix"
644    ##       Compare --> logical
645    ##       Logic   --> logical: "lMatrix"
646    
647    ##  other = "numeric" : stay diagonal if possible
648    ## ddi*: Arith: result numeric, potentially ddiMatrix
649    setMethod("Arith", signature(e1 = "ddiMatrix", e2 = "numeric"),
650              function(e1,e2) {
651                  n <- e1@Dim[1]; nsq <- n^2
652                  f0 <- callGeneric(0, e2)
653                  if(all(is0(f0))) { # remain diagonal
654                      L1 <- (le <- length(e2)) == 1L
655                      if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
656                      if(e1@diag == "U" && any((r <- callGeneric(1, e2)) != 1)) {
657                          e1@diag <- "N"
658                          if(L1) r <- rep.int(r, n)
659                      } else
660                          r <- callGeneric(e1@x, e2)
661                      e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
662                      return(e1)
663                  }
664                  callGeneric(diag2tT.u(e1,e2, "d"), e2)
665              })
666    
667    setMethod("Arith", signature(e1 = "numeric", e2 = "ddiMatrix"),
668              function(e1,e2) {
669                  n <- e2@Dim[1]; nsq <- n^2
670                  f0 <- callGeneric(e1, 0)
671                  if(all(is0(f0))) { # remain diagonal
672                      L1 <- (le <- length(e1)) == 1L
673                      if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
674                      if(e2@diag == "U" && any((r <- callGeneric(e1, 1)) != 1)) {
675                          e2@diag <- "N"
676                          if(L1) r <- rep.int(r, n)
677                      } else
678                          r <- callGeneric(e1, e2@x)
679                      e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
680                      return(e2)
681                  }
682                  callGeneric(e1, diag2tT.u(e2,e1, "d"))
683              })
684    
685    ## ldi* Arith --> result numeric, potentially ddiMatrix
686    setMethod("Arith", signature(e1 = "ldiMatrix", e2 = "numeric"),
687              function(e1,e2) {
688                  n <- e1@Dim[1]; nsq <- n^2
689                  f0 <- callGeneric(0, e2)
690                  if(all(is0(f0))) { # remain diagonal
691                      L1 <- (le <- length(e2)) == 1L
692                      if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
693                      if(e1@diag == "U" && any((r <- callGeneric(1, e2)) != 1)) {
694                          e1@diag <- "N"
695                          if(L1) r <- rep.int(r, n)
696                      } else
697                          r <- callGeneric(e1@x, e2)
698                      e1 <- copyClass(e1, "ddiMatrix", c("diag", "Dim", "Dimnames"))
699                      e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
700                      return(e1)
701                  }
702                  callGeneric(diag2tT.u(e1,e2, "d"), e2)
703              })
704    
705    setMethod("Arith", signature(e1 = "numeric", e2 = "ldiMatrix"),
706              function(e1,e2) {
707                  n <- e2@Dim[1]; nsq <- n^2
708                  f0 <- callGeneric(e1, 0)
709                  if(all(is0(f0))) { # remain diagonal
710                      L1 <- (le <- length(e1)) == 1L
711                      if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
712                      if(e2@diag == "U" && any((r <- callGeneric(e1, 1)) != 1)) {
713                          e2@diag <- "N"
714                          if(L1) r <- rep.int(r, n)
715                      } else
716                          r <- callGeneric(e1, e2@x)
717                      e2 <- copyClass(e2, "ddiMatrix", c("diag", "Dim", "Dimnames"))
718                      e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
719                      return(e2)
720                  }
721                  callGeneric(e1, diag2tT.u(e2,e1, "d"))
722              })
723    
724    ## ddi*: for "Ops" without Arith --> result logical, potentially ldi
725    setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "numeric"),
726              function(e1,e2) {
727                  n <- e1@Dim[1]; nsq <- n^2
728                  f0 <- callGeneric(0, e2)
729                  if(all(is0(f0))) { # remain diagonal
730                      L1 <- (le <- length(e2)) == 1L
731                      if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
732                      if(e1@diag == "U" && any((r <- callGeneric(1, e2)) != 1)) {
733                          e1@diag <- "N"
734                          if(L1) r <- rep.int(r, n)
735                      } else
736                          r <- callGeneric(e1@x, e2)
737                      e1 <- copyClass(e1, "ldiMatrix", c("diag", "Dim", "Dimnames"))
738                      e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
739                      return(e1)
740                  }
741                  callGeneric(diag2tT.u(e1,e2, "l"), e2)
742              })
743    
744    setMethod("Ops", signature(e1 = "numeric", e2 = "ddiMatrix"),
745              function(e1,e2) {
746                  n <- e2@Dim[1]; nsq <- n^2
747                  f0 <- callGeneric(e1, 0)
748                  if(all(is0(f0))) { # remain diagonal
749                      L1 <- (le <- length(e1)) == 1L
750                      if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
751                      if(e2@diag == "U" && any((r <- callGeneric(e1, 1)) != 1)) {
752                          e2@diag <- "N"
753                          if(L1) r <- rep.int(r, n)
754                      } else
755                          r <- callGeneric(e1, e2@x)
756                      e2 <- copyClass(e2, "ldiMatrix", c("diag", "Dim", "Dimnames"))
757                      e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
758                      return(e2)
759                  }
760                  callGeneric(e1, diag2tT.u(e2,e1, "l"))
761              })
762    
763    ## ldi*: for "Ops" without Arith --> result logical, potentially ldi
764    setMethod("Ops", signature(e1 = "ldiMatrix", e2 = "numeric"),
765              function(e1,e2) {
766                  n <- e1@Dim[1]; nsq <- n^2
767                  f0 <- callGeneric(FALSE, e2)
768                  if(all(is0(f0))) { # remain diagonal
769                      L1 <- (le <- length(e2)) == 1L
770                      if(!L1 && le != nsq) e2 <- rep(e2, length.out = nsq)
771                      if(e1@diag == "U" && any((r <- callGeneric(TRUE, e2)) != 1)) {
772                          e1@diag <- "N"
773                          if(L1) r <- rep.int(r, n)
774                      } else
775                          r <- callGeneric(e1@x, e2)
776                      e1@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
777                      return(e1)
778                  }
779                  callGeneric(diag2tT.u(e1,e2, "l"), e2)
780              })
781    
782    setMethod("Ops", signature(e1 = "numeric", e2 = "ldiMatrix"),
783              function(e1,e2) {
784                  n <- e2@Dim[1]; nsq <- n^2
785                  f0 <- callGeneric(e1, FALSE)
786                  if(all(is0(f0))) { # remain diagonal
787                      L1 <- (le <- length(e1)) == 1L
788                      if(!L1 && le != nsq) e1 <- rep(e1, length.out = nsq)
789                      if(e2@diag == "U" && any((r <- callGeneric(e1, TRUE)) != 1)) {
790                          e2@diag <- "N"
791                          if(L1) r <- rep.int(r, n)
792                      } else
793                          r <- callGeneric(e1, e2@x)
794                      e2@x <- if(L1) r else r[1L + (n+1L)*(0:(n-1L))]
795                      return(e2)
796                  }
797                  callGeneric(e1, diag2tT.u(e2,e1, "l"))
798              })
799    
800    
801    
802    ## Not {"sparseMatrix", "numeric} :  {"denseMatrix", "matrix", ... }
803    for(other in c("ANY", "Matrix", "dMatrix")) {
804        ## ddi*:
805        setMethod("Ops", signature(e1 = "ddiMatrix", e2 = other),
806                  function(e1,e2) callGeneric(diag2tT.u(e1,e2, "d"), e2))
807        setMethod("Ops", signature(e1 = other, e2 = "ddiMatrix"),
808                  function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1, "d")))
809        ## ldi*:
810        setMethod("Ops", signature(e1 = "ldiMatrix", e2 = other),
811                  function(e1,e2) callGeneric(diag2tT.u(e1,e2, "l"), e2))
812        setMethod("Ops", signature(e1 = other, e2 = "ldiMatrix"),
813                  function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1, "l")))
814    }
815    ## This should *not* dispatch to <dense> methods (in ./Ops.R ), as
816    ##  FALSE & <anything> |-> FALSE : hence result should be diagonal:
817    for(cl in diCls) {
818        setMethod("&", signature(e1 = cl, e2 = "ANY"),
819                  function(e1,e2) e1 & as(e2,"Matrix"))
820        setMethod("&", signature(e1 = "ANY", e2 = cl),
821                  function(e1,e2) as(e1,"Matrix") & e2)
822        for(c2 in c("denseMatrix", "Matrix")) {
823            setMethod("&", signature(e1 = cl, e2 = c2),
824                      function(e1,e2) e1 & Diagonal(x = diag(e2)))
825            setMethod("&", signature(e1 = c2, e2 = cl),
826                      function(e1,e2) Diagonal(x = diag(e1)) & e2)
827        }
828    }
829    
830    
831    ### "Summary" : "max"   "min"   "range" "prod"  "sum"   "any"   "all"
832    ### ----------  any, all: separately here
833    for(cl in diCls) {
834    setMethod("any", cl,
835              function (x, ..., na.rm) {
836                  if(any(x@Dim == 0)) FALSE
837                  else if(x@diag == "U") TRUE else any(x@x, ..., na.rm = na.rm)
838              })
839    setMethod("all",  cl, function (x, ..., na.rm) any(x@Dim == 0))
840    setMethod("prod", cl, function (x, ..., na.rm) as.numeric(any(x@Dim == 0)))
841    
842    setMethod("sum", cl,
843              function(x, ..., na.rm) {
844                  r <- sum(x@x, ..., na.rm = na.rm)# double or integer, correctly
845                  if(x@diag == "U" && !is.na(r)) r + x@Dim[1] else r
846              })
847    }
848    
849    ## The remaining ones are  max, min, range :
850    
851    setMethod("Summary", "ddiMatrix",
852              function(x, ..., na.rm) {
853                  if(any(x@Dim == 0)) callGeneric(numeric(0), ..., na.rm=na.rm)
854                  else if(x@diag == "U")
855                      callGeneric(x@x, 0, 1, ..., na.rm=na.rm)
856                  else callGeneric(x@x, 0, ..., na.rm=na.rm)
857              })
858    setMethod("Summary", "ldiMatrix",
859              function(x, ..., na.rm) {
860                  if(any(x@Dim == 0)) callGeneric(logical(0), ..., na.rm=na.rm)
861                  else if(x@diag == "U")
862                      callGeneric(x@x, FALSE, TRUE, ..., na.rm=na.rm)
863                  else callGeneric(x@x, FALSE, ..., na.rm=na.rm)
864              })
865    
866    
867    
# Line 459  Line 880 
880            function(object) {            function(object) {
881                d <- dim(object)                d <- dim(object)
882                cl <- class(object)                cl <- class(object)
883                cat(sprintf('%d x %d diagonal matrix of class "%s"\n',                cat(sprintf('%d x %d diagonal matrix of class "%s"',
884                            d[1], d[2], cl))                            d[1], d[2], cl))
885                  if(d[1] < 50) {
886                      cat("\n")
887                prDiag(object)                prDiag(object)
888                  } else {
889                      cat(", with diagonal entries\n")
890                      show(diag(object))
891                      invisible(object)
892                  }
893            })            })

Legend:
Removed from v.1805  
changed lines
  Added in v.2206

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