[Rd] setMethod() woes

From: Robin Hankin <r.hankin_at_noc.soton.ac.uk>
Date: Fri 15 Sep 2006 - 13:56:56 GMT

Hello everybody

R version 2.4.0 alpha (2006-09-15 r39323), MacOSX 10.4.7

Next S4 problem.

I have "brob" objects that are large real numbers, and now I want "glub" numbers that are to be a pair of glubs that represent complex numbers.

I want to define binary operator "+" so that if either the left or right argument are glubs, it uses .ArithGlub.

If either argument is a brob (but neither is a glub), use .ArithGlub

If neither is a brob or a glub, use standard "+"

(just like real/complex "+").

My attempt (self contained, minimal) is included below. But, with this, the following session shows that something is wrong:

> a <- new("brob",x=1:5,positive=rep(T,5))
> b <- new("glub",real=a,imag=a)
> a+b

Error in a + b : binary operator " + " not defined for Brobdingnagian numbers
In addition: Warning message:
Ambiguous method selection for "+", target "brob#glub" (the first of the signatures shown will be used)


in: .findInheritedMethods(classes, fdef, mtable)

I don't understand what to do to avoid getting the warning message [and it's also using the wrong function: I want it to call .ArithGlub() here, not .ArithBrob() ]

I need to use signature(e1="brob", e2="anything except a glub") but I can't figure out how to do this.


          representation = representation (x="numeric",positive="logical"),

          prototype      = list(x=numeric(),positive=logical())

          representation = representation(real="brob",imag="brob"),
          prototype      = list(real=new("brob"), imag=new("brob"))

.ArithBrob <- function(e1,e2){

   stop(paste("binary operator \"", .Generic, "\" not defined for Brobdingnagian numbers"))

.ArithGlub <- function(e1,e2){

   stop(paste("binary operator \"", .Generic, "\" not defined for Glub numbers"))

setMethod("Arith",signature(e1 = "brob", e2="brob"), .ArithBrob)
setMethod("Arith",signature(e1 = "brob", e2="ANY" ), .ArithBrob)
setMethod("Arith",signature(e1 = "ANY" , e2="brob"), .ArithBrob)

setMethod("Arith",signature(e1 = "glub"), .ArithGlub) setMethod("Arith",signature(e2 = "glub"), .ArithGlub)

Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

R-devel@r-project.org mailing list
Received on Sat Sep 16 00:05:27 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 15 Sep 2006 - 15:30:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.