[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-devel
Subject:    Re: [Rd] : Operator overloading for custom classes
From:       Chidambaram Annamalai <quantumelixir () gmail ! com>
Date:       2010-03-27 7:34:03
Message-ID: 7021740b1003270022q5e3bf94dt4ac24abbbf463001 () mail ! gmail ! com
[Download RAW message or body]

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@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@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic