[Rd] Conflict from saved implicit generics in methods package for rcond, norm, backsolve

Patrick Aboyoun patrick.aboyoun at oracle.com
Mon Feb 27 17:41:50 CET 2012


This issue ties loosely into other recent S4 topics on this board.

The methods package defines a number of implicit generics for linear 
algebra related functions (rcond, norm, backsolve) that, when used, 
interfere with base package operations. Here is the cut-and-paste 
version of the code the illustrates the problem:

# rcond
x1 <- cbind(1, 1:10)
rcond(x1)
setGeneric("rcond")
rcond(x1)

# norm
example(norm)
setGeneric("norm")
example(norm)

# backsolve
example(backsolve)
setGeneric("backsolve")
example(backsolve)


Here is an example run:

R> # rcond
R> x1 <- cbind(1, 1:10)
R> rcond(x1)
[1] 0.05278005
R> setGeneric("rcond")
Creating a generic function for 'rcond' from 'base' in the global 
environment
     (from the saved implicit definition)
[1] "rcond"
R> rcond(x1)
Error in match.arg(norm) : argument "norm" is missing, with no default
R>
R> # norm
R> example(norm)

normR> (x1 <- cbind(1,1:10))
       [,1] [,2]
  [1,]    1    1
  [2,]    1    2
  [3,]    1    3
  [4,]    1    4
  [5,]    1    5
  [6,]    1    6
  [7,]    1    7
  [8,]    1    8
  [9,]    1    9
[10,]    1   10

normR> norm(x1)
[1] 55

normR> norm(x1, "I")
[1] 11

normR> norm(x1, "M")
[1] 10

normR> stopifnot(all.equal(norm(x1, "F"),
norm+                     sqrt(sum(x1^2))))

normR> hilbert <- function(n) { i <- 1:n; 1 / outer(i - 1, i, "+") }

normR> h9 <- hilbert(9)

normR> ## all 4 types of norm:
normR> (nTyp <- eval(formals(base::norm)$type))
[1] "O" "I" "F" "M"

normR> sapply(nTyp, norm, x=h9)
        O        I        F        M
2.828968 2.828968 1.755872 1.000000
R> setGeneric("norm")
[1] "norm"
R> example(norm)

normR> (x1 <- cbind(1,1:10))
       [,1] [,2]
  [1,]    1    1
  [2,]    1    2
  [3,]    1    3
  [4,]    1    4
  [5,]    1    5
  [6,]    1    6
  [7,]    1    7
  [8,]    1    8
  [9,]    1    9
[10,]    1   10

normR> norm(x1)
Error in base::norm(x, type, ...) :
   argument "type" is missing, with no default
R>
R> # backsolve
R> example(backsolve)

bckslvR> ## upper triangular matrix 'r':
bckslvR> r <- rbind(c(1,2,3),
bckslv+            c(0,1,1),
bckslv+            c(0,0,2))

bckslvR> ( y <- backsolve(r, x <- c(8,4,2)) ) # -1 3 1
[1] -1  3  1

bckslvR> r %*% y # == x = (8,4,2)
      [,1]
[1,]    8
[2,]    4
[3,]    2

bckslvR> backsolve(r, x, transpose = TRUE) # 8 -12 -5
[1]   8 -12  -5
R> setGeneric("backsolve")
[1] "backsolve"
R> example(backsolve)

bckslvR> ## upper triangular matrix 'r':
bckslvR> r <- rbind(c(1,2,3),
bckslv+            c(0,1,1),
bckslv+            c(0,0,2))

bckslvR> ( y <- backsolve(r, x <- c(8,4,2)) ) # -1 3 1
Error in base::backsolve(r, x, k = k, upper.tri = upper.tri, transpose = 
transpose,  :
   argument "k" is missing, with no default
R> sessionInfo()
R Under development (unstable) (2012-02-25 r58492)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_2.15.0


The controversial bit is how to address the issue:
a) patch them in the methods package
b) patch them and move them along with related implicit generics such as 
chol2inv from the methods package to the Matrix package
c) remove them along with similar implicit generics from the methods package


Patrick



More information about the R-devel mailing list