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 2144, Tue Mar 18 23:08:12 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
# Line 43  Line 61 
61      ## make sure we had all matrices:      ## make sure we had all matrices:
62      if(!(is.matrix(dims) && nrow(dims) == 2))      if(!(is.matrix(dims) && nrow(dims) == 2))
63          stop("some arguments are not matrices")          stop("some arguments are not matrices")
64      csdim <- rbind(rep.int(0:0, 2),      csdim <- rbind(rep.int(0L, 2),
65                     apply(sapply(mlist, dim), 1, cumsum))                     apply(sapply(mlist, dim), 1, cumsum))
66      ret <- new("dgTMatrix", Dim = as.integer(csdim[nrow(csdim),]))      ret <- new("dgTMatrix", Dim = as.integer(csdim[nrow(csdim),]))
67      add1 <- matrix(1:0, 2,2)      add1 <- matrix(1:0, 2,2)
# Line 58  Line 76 
76      as(ret, "CsparseMatrix")      as(ret, "CsparseMatrix")
77  }  }
78    
79  diag2T <- function(from) {  
80      i <- if(from@diag == "U") integer(0) else seq_len(from@Dim[1]) - 1:1  .diag2tT <- function(from, uplo = "U", kind = .M.kind(from)) {
81      new(paste(.M.kind(from), "tTMatrix", sep=''),      ## to triangular Tsparse
82        i <- if(from@diag == "U") integer(0) else seq_len(from@Dim[1]) - 1L
83        new(paste(kind, "tTMatrix", sep=''),
84          diag = from@diag, Dim = from@Dim, Dimnames = from@Dimnames,          diag = from@diag, Dim = from@Dim, Dimnames = from@Dimnames,
85            uplo = uplo,
86          x = from@x, # <- ok for diag = "U" and "N" (!)          x = from@x, # <- ok for diag = "U" and "N" (!)
87          i = i, j = i)          i = i, j = i)
88  }  }
89    
90  setAs("diagonalMatrix", "triangularMatrix", diag2T)  .diag2sT <- function(from, uplo = "U", kind = .M.kind(from)) {
91  setAs("diagonalMatrix", "sparseMatrix", diag2T)      ## to symmetric Tsparse
92        n <- from@Dim[1]
93        i <- seq_len(n) - 1L
94        new(paste(kind, "sTMatrix", sep=''),
95            Dim = from@Dim, Dimnames = from@Dimnames,
96            i = i, j = i, uplo = uplo,
97            x = if(from@diag == "N") from@x else ## "U"-diag
98            rep.int(switch(kind,
99                           "d" = 1.,
100                           "l" =,
101                           "n" = TRUE,
102                           ## otherwise
103                           stop("'", kind,"' kind not yet implemented")), n))
104    }
105    
106    ## diagonal -> triangular,  upper / lower depending on "partner":
107    diag2tT.u <- function(d, x, kind = .M.kind(d))
108        .diag2tT(d, uplo = if(is(x,"triangularMatrix")) x@uplo else "U", kind)
109    
110    
111    ## In order to evade method dispatch ambiguity warnings,
112    ## and because we can save a .M.kind() call, we use this explicit
113    ## "hack"  instead of signature  x = "diagonalMatrix" :
114    ##
115    ## ddi*:
116    diag2tT <- function(from) .diag2tT(from, "U", "d")
117    setAs("ddiMatrix", "triangularMatrix", diag2tT)
118    setAs("ddiMatrix", "sparseMatrix", diag2tT)
119  ## needed too (otherwise <dense> -> Tsparse is taken):  ## needed too (otherwise <dense> -> Tsparse is taken):
120  setAs("diagonalMatrix", "TsparseMatrix", diag2T)  setAs("ddiMatrix", "TsparseMatrix", diag2tT)
121  ## is better than this:  setAs("ddiMatrix", "CsparseMatrix",
122  ## setAs("diagonalMatrix", "sparseMatrix",        function(from) as(.diag2tT(from, "U", "d"), "CsparseMatrix"))
123  ##       function(from)  setAs("ddiMatrix", "symmetricMatrix",
124  ##        as(from, if(is(from, "dMatrix")) "dgCMatrix" else "lgCMatrix"))        function(from) .diag2sT(from, "U", "d"))
125  setAs("diagonalMatrix", "CsparseMatrix",  ##
126        function(from) as(diag2T(from), "CsparseMatrix"))  ## ldi*:
127    diag2tT <- function(from) .diag2tT(from, "U", "l")
128    setAs("ldiMatrix", "triangularMatrix", diag2tT)
129    setAs("ldiMatrix", "sparseMatrix", diag2tT)
130    ## needed too (otherwise <dense> -> Tsparse is taken):
131    setAs("ldiMatrix", "TsparseMatrix", diag2tT)
132    setAs("ldiMatrix", "CsparseMatrix",
133          function(from) as(.diag2tT(from, "U", "l"), "CsparseMatrix"))
134    setAs("ldiMatrix", "symmetricMatrix",
135          function(from) .diag2sT(from, "U", "l"))
136    
137    
138    setAs("diagonalMatrix", "nMatrix",
139          function(from) {
140              n <- from@Dim[1]
141              i <- if(from@diag == "U") integer(0) else which(isN0(from@x)) - 1L
142              new("ntTMatrix", i = i, j = i, diag = from@diag,
143                  Dim = from@Dim, Dimnames = from@Dimnames)
144          })
145    
146    
147  setAs("diagonalMatrix", "matrix",  setAs("diagonalMatrix", "matrix",
148        function(from) {        function(from) {
# Line 85  Line 152 
152                 nrow = n, ncol = n)                 nrow = n, ncol = n)
153        })        })
154    
155    setMethod("as.vector", signature(x = "diagonalMatrix", mode="missing"),
156              function(x, mode) {
157                  n <- x@Dim[1]
158                  mod <- mode(x@x)
159                  r <- vector(mod, length = n^2)
160                  if(n)
161                      r[1 + 0:(n - 1) * (n + 1)] <-
162                          if(x@diag == "U")
163                              switch(mod, "integer"= 1L,
164                                     "numeric"= 1, "logical"= TRUE)
165                          else x@x
166                  r
167              })
168    
169  setAs("diagonalMatrix", "generalMatrix", # prefer sparse:  setAs("diagonalMatrix", "generalMatrix", # prefer sparse:
170        function(from) as(from, paste(.M.kind(from), "gCMatrix", sep='')))        function(from) as(as(from, "CsparseMatrix"), "generalMatrix"))
171    
172  .diag.x <- function(m) {  .diag.x <- function(m) {
173      if(m@diag == "U")      if(m@diag == "U")
174          rep.int(if(is.numeric(m@x)) 1. else TRUE,          rep.int(if(is.numeric(m@x)) 1. else TRUE, m@Dim[1])
                 m@Dim[1])  
175      else m@x      else m@x
176  }  }
177    
# Line 100  Line 180 
180      m      m
181  }  }
182    
183    if(FALSE) {
184  ## given the above, the following  4  coercions should be all unneeded;  ## given the above, the following  4  coercions should be all unneeded;
185  ## we prefer triangular to general:  ## we prefer triangular to general:
186  setAs("ddiMatrix", "dgTMatrix",  setAs("ddiMatrix", "dgTMatrix",
187        function(from) {        function(from) {
188            .Deprecated("as(, \"sparseMatrix\")")            .Deprecated("as(, \"sparseMatrix\")")
189            n <- from@Dim[1]            n <- from@Dim[1]
190            i <- seq_len(n) - 1:1            i <- seq_len(n) - 1L
191            new("dgTMatrix", i = i, j = i, x = .diag.x(from),            new("dgTMatrix", i = i, j = i, x = .diag.x(from),
192                Dim = c(n,n), Dimnames = from@Dimnames) })                Dim = c(n,n), Dimnames = from@Dimnames) })
193    
# Line 119  Line 200 
200            n <- from@Dim[1]            n <- from@Dim[1]
201            if(from@diag == "U") { # unit-diagonal            if(from@diag == "U") { # unit-diagonal
202                x <- rep.int(TRUE, n)                x <- rep.int(TRUE, n)
203                i <- seq_len(n) - 1:1                i <- seq_len(n) - 1L
204            } else { # "normal"            } else { # "normal"
205                nz <- nz.NA(from@x, na. = TRUE)                nz <- nz.NA(from@x, na. = TRUE)
206                x <- from@x[nz]                x <- from@x[nz]
207                i <- which(nz) - 1:1                i <- which(nz) - 1L
208            }            }
209            new("lgTMatrix", i = i, j = i, x = x,            new("lgTMatrix", i = i, j = i, x = x,
210                Dim = c(n,n), Dimnames = from@Dimnames) })                Dim = c(n,n), Dimnames = from@Dimnames) })
211    
212  setAs("ldiMatrix", "lgCMatrix",  setAs("ldiMatrix", "lgCMatrix",
213        function(from) as(as(from, "lgTMatrix"), "lgCMatrix"))        function(from) as(as(from, "lgTMatrix"), "lgCMatrix"))
214    }
215    
216    
217  if(FALSE) # now have faster  "ddense" -> "dge"  if(FALSE) # now have faster  "ddense" -> "dge"
# Line 176  Line 258 
258        })        })
259    
260    
261  setMethod("diag", signature(x = "diagonalMatrix"),  ## In order to evade method dispatch ambiguity warnings,
262            function(x = 1, nrow, ncol = n) .diag.x(x))  ## we use this hack instead of signature  x = "diagonalMatrix" :
263    diCls <- names(getClass("diagonalMatrix")@subclasses)
264    for(cls in diCls) {
265        setMethod("diag", signature(x = cls),
266                  function(x = 1, nrow, ncol) .diag.x(x))
267    }
268    
269    
270  subDiag <- function(x, i, j, drop) {  subDiag <- function(x, i, j, ..., drop) {
271      x <- as(x, "sparseMatrix")      x <- as(x, "sparseMatrix")
272      x <- if(missing(i))      x <- if(missing(i))
273          x[, j, drop=drop]          x[, j, drop=drop]
# Line 188  Line 275 
275          x[i, , drop=drop]          x[i, , drop=drop]
276      else      else
277          x[i,j, drop=drop]          x[i,j, drop=drop]
278      if(isDiagonal(x)) as(x, "diagonalMatrix") else x      if(isS4(x) && isDiagonal(x)) as(x, "diagonalMatrix") else x
279  }  }
280    
281  setMethod("[", signature(x = "diagonalMatrix", i = "index",  setMethod("[", signature(x = "diagonalMatrix", i = "index",
282                           j = "index", drop = "logical"), subDiag)                           j = "index", drop = "logical"), subDiag)
283  setMethod("[", signature(x = "diagonalMatrix", i = "index",  setMethod("[", signature(x = "diagonalMatrix", i = "index",
284                          j = "missing", drop = "logical"),                          j = "missing", drop = "logical"),
285            function(x, i, drop) subDiag(x, i=i, drop=drop))            function(x, i, j, ..., drop) subDiag(x, i=i, drop=drop))
286  setMethod("[", signature(x = "diagonalMatrix", i = "missing",  setMethod("[", signature(x = "diagonalMatrix", i = "missing",
287                           j = "index", drop = "logical"),                           j = "index", drop = "logical"),
288            function(x, j, drop) subDiag(x, j=j, drop=drop))            function(x, i, j, ..., drop) subDiag(x, j=j, drop=drop))
289    
290  ## When you assign to a diagonalMatrix, the result should be  ## When you assign to a diagonalMatrix, the result should be
291  ## diagonal or sparse ---  ## diagonal or sparse ---
292  ## FIXME: this now fails because the "denseMatrix" methods come first in dispatch  ## FIXME: this now fails because the "denseMatrix" methods come first in dispatch
293  replDiag <- function(x, i, j, value) {  ## Only(?) current bug:  x[i] <- value  is wrong when  i is *vector*
294    replDiag <- function(x, i, j, ..., value) {
295      x <- as(x, "sparseMatrix")      x <- as(x, "sparseMatrix")
296      if(missing(i))      if(missing(i))
297          x[, j] <- value          x[, j] <- value
298      else if(missing(j))      else if(missing(j)) { ##  x[i , ] <- v  *OR*   x[i] <- v
299            na <- nargs()
300    ##         message("diagnosing replDiag() -- nargs()= ", na)
301            if(na == 4)
302          x[i, ] <- value          x[i, ] <- value
303      else          else if(na == 3)
304                x[i] <- value
305            else stop("Internal bug: nargs()=",na,"; please report")
306        } else
307          x[i,j] <- value          x[i,j] <- value
308      if(isDiagonal(x)) as(x, "diagonalMatrix") else x      if(isDiagonal(x)) as(x, "diagonalMatrix") else x
309  }  }
310    
311  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",
312                                  j = "index", value = "replValue"), replDiag)                                  j = "index", value = "replValue"), replDiag)
313    
314  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "index",
315                                  j = "missing", value = "replValue"),                                  j = "missing", value = "replValue"),
316                   function(x, i, value) replDiag(x, i=i, value=value))                   function(x,i,j, ..., value) {
317                         ## message("before replDiag() -- nargs()= ", nargs())
318                         if(nargs() == 3)
319                             replDiag(x, i=i, value=value)
320                         else ## nargs() == 4 :
321                             replDiag(x, i=i, , value=value)
322                     })
323    
324    setReplaceMethod("[", signature(x = "diagonalMatrix", i = "matrix", # 2-col.matrix
325                                    j = "missing", value = "replValue"),
326                     function(x,i,j, ..., value) {
327                         if(ncol(i) == 2) {
328                             if(all((ii <- i[,1]) == i[,2])) { # replace in diagonal only
329                                 x@x[ii] <- value
330                                 x
331                             } else { ## no longer diagonal, but remain sparse:
332                                 x <- as(x, "sparseMatrix")
333                                 x[i] <- value
334                                 x
335                             }
336                         }
337                         else { # behave as "base R": use as if vector
338                             x <- as(x, "matrix")
339                             x[i] <- value
340                             Matrix(x)
341                         }
342                     })
343    
344  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing",  setReplaceMethod("[", signature(x = "diagonalMatrix", i = "missing",
345                                  j = "index", value = "replValue"),                                  j = "index", value = "replValue"),
346                   function(x, j, value) replDiag(x, j=j, value=value))                   function(x,i,j, ..., value) replDiag(x, j=j, value=value))
347    
348    
349  setMethod("t", signature(x = "diagonalMatrix"),  setMethod("t", signature(x = "diagonalMatrix"),
# Line 232  Line 354 
354  setMethod("isTriangular", signature(object = "diagonalMatrix"),  setMethod("isTriangular", signature(object = "diagonalMatrix"),
355            function(object) TRUE)            function(object) TRUE)
356  setMethod("isSymmetric", signature(object = "diagonalMatrix"),  setMethod("isSymmetric", signature(object = "diagonalMatrix"),
357            function(object) TRUE)            function(object, ...) TRUE)
358    
359  setMethod("chol", signature(x = "ddiMatrix"),# pivot = "ANY"  setMethod("symmpart", signature(x = "diagonalMatrix"), function(x) x)
360            function(x, pivot) {  setMethod("skewpart", signature(x = "diagonalMatrix"), setZero)
361                if(any(x@x < 0)) stop("chol() is undefined for diagonal matrix with negative entries")  
362    setMethod("chol", signature(x = "ddiMatrix"),
363              function(x, pivot, ...) {
364                  if(any(x@x < 0))
365                      stop("chol() is undefined for diagonal matrix with negative entries")
366                x@x <- sqrt(x@x)                x@x <- sqrt(x@x)
367                x                x
368            })            })
369  ## chol(L) is L for logical diagonal:  ## chol(L) is L for logical diagonal:
370  setMethod("chol", signature(x = "ldiMatrix"), function(x, pivot) x)  setMethod("chol", signature(x = "ldiMatrix"), function(x, pivot, ...) x)
   
 setMethod("!", "ldiMatrix", function(e1) {  
     if(e1@diag == "N")  
         e1@x <- !e1@x  
     else { ## "U"  
         e1@diag <- "N"  
         e1@x <- rep.int(FALSE, e1@Dim[1])  
     }  
     e1  
 })  
371    
372  ## Basic Matrix Multiplication {many more to add}  ## Basic Matrix Multiplication {many more to add}
373  ##       ---------------------  ##       ---------------------
# Line 346  Line 462 
462  ##           })  ##           })
463    
464  setMethod("crossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),  setMethod("crossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
465            function(x, y = NULL) { x <- as(x, "sparseMatrix"); callGeneric() })            function(x, y = NULL) crossprod(as(x, "sparseMatrix"), y))
466    
467  setMethod("crossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),  setMethod("crossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
468            function(x, y = NULL) { y <- as(y, "sparseMatrix"); callGeneric() })            function(x, y = NULL) crossprod(x, as(y, "sparseMatrix")))
469    
470  setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),  setMethod("tcrossprod", signature(x = "diagonalMatrix", y = "sparseMatrix"),
471            function(x, y = NULL) { x <- as(x, "sparseMatrix"); callGeneric() })            function(x, y = NULL) tcrossprod(as(x, "sparseMatrix"), y))
472    
473  setMethod("tcrossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),  setMethod("tcrossprod", signature(x = "sparseMatrix", y = "diagonalMatrix"),
474            function(x, y = NULL) { y <- as(y, "sparseMatrix"); callGeneric() })            function(x, y = NULL) tcrossprod(x, as(y, "sparseMatrix")))
475    
476    
477  ## 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()
# Line 396  Line 512 
512  setMethod("solve", signature(a = "diagonalMatrix", b = "Matrix"),  setMethod("solve", signature(a = "diagonalMatrix", b = "Matrix"),
513            solveDiag)            solveDiag)
514    
515    ## Schur()  ---> ./eigen.R
516    
517    
518    
# Line 428  Line 545 
545  setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "ddiMatrix"),  setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "ddiMatrix"),
546            diagOdiag)            diagOdiag)
547    
548    ## FIXME:    diagonal  o  triangular  |-->  triangular
549    ## -----     diagonal  o  symmetric   |-->  symmetric
550    ##    {also when other is sparse: do these "here" --
551    ##     before conversion to sparse, since that loses "diagonality"}
552    
553  ## For almost everything else, diag* shall be treated "as sparse" :  ## For almost everything else, diag* shall be treated "as sparse" :
554  ## These are cheap implementations via coercion  ## These are cheap implementations via coercion
555    
556  ## for disambiguation  ## For disambiguation --- define this for "sparseMatrix" , then for "ANY";
557  setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "sparseMatrix"),  ## and because we can save an .M.kind() call, we use this explicit
558            function(e1,e2) callGeneric(as(e1, "sparseMatrix"), e2))  ## "hack" for all diagonalMatrix *subclasses* instead of just "diagonalMatrix" :
559  setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "diagonalMatrix"),  ##
560            function(e1,e2) callGeneric(e1, as(e2, "sparseMatrix")))  ## ddi*:
561  ## in general:  setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "sparseMatrix"),
562  setMethod("Ops", signature(e1 = "diagonalMatrix", e2 = "ANY"),            function(e1,e2) callGeneric(diag2tT.u(e1,e2, "d"), e2))
563            function(e1,e2) callGeneric(as(e1,"sparseMatrix"), e2))  setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "ddiMatrix"),
564  setMethod("Ops", signature(e1 = "ANY", e2 = "diagonalMatrix"),            function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1, "d")))
565            function(e1,e2) callGeneric(e1, as(e2,"sparseMatrix")))  ## ldi*
566    setMethod("Ops", signature(e1 = "ldiMatrix", e2 = "sparseMatrix"),
567              function(e1,e2) callGeneric(diag2tT.u(e1,e2, "l"), e2))
568    setMethod("Ops", signature(e1 = "sparseMatrix", e2 = "ldiMatrix"),
569              function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1, "l")))
570    
571    ##  other = "numeric" : stay diagonal if possible
572    ## ddi*:
573    setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "numeric"),
574              function(e1,e2) {
575                  n <- e1@Dim[1]; nsq <- n*n
576                  f0 <- callGeneric(0, e2)
577                  if(all(is0(f0))) { # remain diagonal
578                      if(e1@diag == "U" && (r <- callGeneric(1, e2)) != 1)
579                          e1@diag <- "N"
580                      else
581                          r <- callGeneric(e1@x, e2)
582                      e1@x <- if(length(e2) == nsq) r else rep(r, length.out = nsq)
583                      return(e1)
584                  }
585                  callGeneric(diag2tT.u(e1,e2, "d"), e2)
586              })
587    
588    setMethod("Ops", signature(e1 = "numeric", e2 = "ddiMatrix"),
589              function(e1,e2) {
590                  n <- e2@Dim[1]; nsq <- n*n
591                  f0 <- callGeneric(e1, 0)
592                  if(all(is0(f0))) { # remain diagonal
593                      if(e2@diag == "U" && (r <- callGeneric(e1, 1)) != 1)
594                          e2@diag <- "N"
595                      else
596                          r <- callGeneric(e1, e2@x)
597                      e2@x <- if(length(e1) == nsq) r else rep(r, length.out = nsq)
598                      return(e2)
599                  }
600                  callGeneric(e1, diag2tT.u(e2,e1, "d"))
601              })
602    ## ldi*:
603    setMethod("Ops", signature(e1 = "ldiMatrix", e2 = "numeric"),
604              function(e1,e2) {
605                  n <- e1@Dim[1]; nsq <- n*n
606                  f0 <- callGeneric(FALSE, e2)
607                  if(all(is0(f0))) { # remain diagonal
608                      if(e1@diag == "U" && (r <- callGeneric(TRUE, e2)) != 1)
609                          e1@diag <- "N"
610                      else
611                          r <- callGeneric(e1@x, e2)
612                      e1@x <- if(length(e2) == nsq) r else rep(r, length.out = nsq)
613                      return(e1)
614                  }
615                  callGeneric(diag2tT.u(e1,e2, "l"), e2)
616              })
617    
618    setMethod("Ops", signature(e1 = "numeric", e2 = "ldiMatrix"),
619              function(e1,e2) {
620                  n <- e2@Dim[1]; nsq <- n*n
621                  f0 <- callGeneric(e1, FALSE)
622                  if(all(is0(f0))) { # remain diagonal
623                      if(e2@diag == "U" && (r <- callGeneric(e1, TRUE)) != 1)
624                          e2@diag <- "N"
625                      else
626                          r <- callGeneric(e1, e2@x)
627                      e2@x <- if(length(e1) == nsq) r else rep(r, length.out = nsq)
628                      return(e2)
629                  }
630                  callGeneric(e1, diag2tT.u(e2,e1, "l"))
631              })
632    
633    
634    
635    ## Not {"sparseMatrix", "numeric} :  {"denseMatrix", "matrix", ... }
636    ## ddi*:
637    setMethod("Ops", signature(e1 = "ddiMatrix", e2 = "ANY"),
638              function(e1,e2) callGeneric(diag2tT.u(e1,e2, "d"), e2))
639    setMethod("Ops", signature(e1 = "ANY", e2 = "ddiMatrix"),
640              function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1, "d")))
641    ## ldi*:
642    setMethod("Ops", signature(e1 = "ldiMatrix", e2 = "ANY"),
643              function(e1,e2) callGeneric(diag2tT.u(e1,e2, "l"), e2))
644    setMethod("Ops", signature(e1 = "ANY", e2 = "ldiMatrix"),
645              function(e1,e2) callGeneric(e1, diag2tT.u(e2,e1, "l")))
646    
647    
648    

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

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