Re: [Rd] : Operator overloading for custom classes

From: Chidambaram Annamalai <quantumelixir_at_gmail.com>
Date: Sat, 27 Mar 2010 12:52:03 +0530

Thank you for replying! But I have since figured out how to perform the operator overloading after reading the "Not so short tutorial to S4 classes" and a bit of Robert Gentleman's deceptively titled book on R.

(I had mistakenly posted to r-devel earlier and when I posted to r-help subsequently I got a pointer to the resources on rwiki that dealt with OOP)

Like you say, many already existing packages inside of R use operator overloading. For instance I later observed that the "+" operator was overloaded [by running the command methods('+')] for the Date and POSIXt classes. I will have to redefine the generic function "+" for my classes to get the output I want.

Regards,
Chillu

On Sat, Mar 27, 2010 at 3:46 AM, Peter Ruckdeschel <peter.ruckdeschel_at_web.de
> wrote:

> >
> > I need some help to get some of the object orientation, specifically the
> > methods that overload the basic arithmetic operations, from sample C++
> > code to R. I don't have experience with such advanced language features
> > inside of R. So I was wondering if some of you could help me out in this
> > regard.
> >
> > I have written a simple demonstration of a forward mode automatic
> > differentiator in C++ and it is currently hosted on github:
> > http://github.com/quantumelixir/ad-demo/blob/master/simple.cpp. It uses
> > simple operator overloading techniques to modify the meaning of the
> > basic arithmetic operations (+, -, *, /) for the "derivative" type Dual
> > number class that I have defined. Could you show me how this could be
> > equivalently done in R? I want to know how to define custom classes and
> > define the meaning of arithmetic for them.
> >
> > I had checked for operator overloading in R but could only find the
> > equivalence of a + b and '+'(a, b) in the R language definition. Could
> > you show how I could extend the simple object oriented-ness in the C++
> > code neatly to R?
> >
> > Thanks a bunch!
> > Chillu
> >
> > [[alternative HTML version deleted]]
> >
>
> First of all you should read about S4 classes, in particular
> the books of John Chambers should be helpful ;-)
> (others on this list might point you to further sources).
>
> The key is to define S4 classes for your operands
> [i.e; if I have understood correctly you would implement
> your Dual number class as S4 class].
>
> Then you would define new methods by setMethod().
>
> Not sure whether this gives you the indications you look for,
> but we have overloaded arithmetic operators to act on distribution
> classes in package distr (cf CRAN, developped under r-forge).
> You might want to look at the code in NormalDistribution.R resp.
> ContDistribution.R
>
> HTH Peter Ruckdeschel
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat 27 Mar 2010 - 07:25:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 27 Mar 2010 - 14:21:22 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.

list of date sections of archive