Operator overloading

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Operator overloading

Tine-2
Hi!

I was just wondering is there anyway to overload operator for custom class.
For example:
I have two matrices A and B with same dimensions. I want to overload
operator '+' with my own function.
Or even better can I create my own (new) operator.
Example:
A+-B where '+-' would be some function.

bye,
  Tine

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Operator overloading

Peter Dalgaard
Tine wrote:

> Hi!
>
> I was just wondering is there anyway to overload operator for custom
> class.
> For example:
> I have two matrices A and B with same dimensions. I want to overload
> operator '+' with my own function.
> Or even better can I create my own (new) operator.
> Example:
> A+-B where '+-' would be some function.
>
You can define an S3 method

"+.myclass" <- function(a,b) {...}

Notice that dispatch is only on the 1st argument. This is a generic
weakness of  S3 methods. The Matrix package implements similar things
with S4 methods, but it does tend to get rather complicated.

There's a general method for user defined binary operators using
functions with names of the form %any% (see ?Syntax):

> "%_%" <- function(a,b) paste(a,b,sep="")
> "Hello," %_% " " %_% "world"
[1] "Hello, world"



> bye,
>  Tine
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Operator overloading

hadley wickham
On Thu, Jul 10, 2008 at 5:09 AM, Peter Dalgaard
<[hidden email]> wrote:

> Tine wrote:
>> Hi!
>>
>> I was just wondering is there anyway to overload operator for custom
>> class.
>> For example:
>> I have two matrices A and B with same dimensions. I want to overload
>> operator '+' with my own function.
>> Or even better can I create my own (new) operator.
>> Example:
>> A+-B where '+-' would be some function.
>>
> You can define an S3 method
>
> "+.myclass" <- function(a,b) {...}
>
> Notice that dispatch is only on the 1st argument. This is a generic
> weakness of  S3 methods. The Matrix package implements similar things
> with S4 methods, but it does tend to get rather complicated.

That's not completely true is it?

> 2 * unit(2, "cm")
[1] 2*2cm
> unit(2, "cm") * 2
[1] 2*2cm

(But I'm not sure why that works)

Hadley

--
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Operator overloading

Peter Dalgaard
hadley wickham wrote:

> On Thu, Jul 10, 2008 at 5:09 AM, Peter Dalgaard
> <[hidden email]> wrote:
>  
>> Tine wrote:
>>    
>>> Hi!
>>>
>>> I was just wondering is there anyway to overload operator for custom
>>> class.
>>> For example:
>>> I have two matrices A and B with same dimensions. I want to overload
>>> operator '+' with my own function.
>>> Or even better can I create my own (new) operator.
>>> Example:
>>> A+-B where '+-' would be some function.
>>>
>>>      
>> You can define an S3 method
>>
>> "+.myclass" <- function(a,b) {...}
>>
>> Notice that dispatch is only on the 1st argument. This is a generic
>> weakness of  S3 methods. The Matrix package implements similar things
>> with S4 methods, but it does tend to get rather complicated.
>>    
>
> That's not completely true is it?
>
>  
>> 2 * unit(2, "cm")
>>    
> [1] 2*2cm
>  
>> unit(2, "cm") * 2
>>    
> [1] 2*2cm
>
> (But I'm not sure why that works)
>  
Group generics. Yes, I forgot about those. They dispatch on _any_
argument being of a given class. The problem is that it is a single
dispatch, which is why R gets itself in a twist when you try to add
Dates and difftimes:

> as.Date("2008-7-10")+as.difftime(2,units="days")
[1] "2008-07-12"
Warning message:
Incompatible methods ("+.Date", "Ops.difftime") for "+"
> as.difftime(2,units="days")+as.Date("2008-7-10")
Time difference of 14072 days
Warning message:
Incompatible methods ("Ops.difftime", "+.Date") for "+"


--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Operator overloading

Spencer Graves
      The 'polynom' package provides one example of how to do this.  
However, getAnywhere("+.polynomial") just told me "no object named
‘+.polynomial’ was found";  I don't know why.  The documentation says it
uses S3 classes.  The 'Matrix' package should provide other examples,
using S4 classes.  You can get the package source for both as "*.tar.gz"
files from CRAN, unzip the file and see what they did.

      Hope this helps.
      Spencer    

Peter Dalgaard wrote:

> hadley wickham wrote:
>  
>> On Thu, Jul 10, 2008 at 5:09 AM, Peter Dalgaard
>> <[hidden email]> wrote:
>>  
>>    
>>> Tine wrote:
>>>    
>>>      
>>>> Hi!
>>>>
>>>> I was just wondering is there anyway to overload operator for custom
>>>> class.
>>>> For example:
>>>> I have two matrices A and B with same dimensions. I want to overload
>>>> operator '+' with my own function.
>>>> Or even better can I create my own (new) operator.
>>>> Example:
>>>> A+-B where '+-' would be some function.
>>>>
>>>>      
>>>>        
>>> You can define an S3 method
>>>
>>> "+.myclass" <- function(a,b) {...}
>>>
>>> Notice that dispatch is only on the 1st argument. This is a generic
>>> weakness of  S3 methods. The Matrix package implements similar things
>>> with S4 methods, but it does tend to get rather complicated.
>>>    
>>>      
>> That's not completely true is it?
>>
>>  
>>    
>>> 2 * unit(2, "cm")
>>>    
>>>      
>> [1] 2*2cm
>>  
>>    
>>> unit(2, "cm") * 2
>>>    
>>>      
>> [1] 2*2cm
>>
>> (But I'm not sure why that works)
>>  
>>    
> Group generics. Yes, I forgot about those. They dispatch on _any_
> argument being of a given class. The problem is that it is a single
> dispatch, which is why R gets itself in a twist when you try to add
> Dates and difftimes:
>
>  
>> as.Date("2008-7-10")+as.difftime(2,units="days")
>>    
> [1] "2008-07-12"
> Warning message:
> Incompatible methods ("+.Date", "Ops.difftime") for "+"
>  
>> as.difftime(2,units="days")+as.Date("2008-7-10")
>>    
> Time difference of 14072 days
> Warning message:
> Incompatible methods ("Ops.difftime", "+.Date") for "+"
>
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.