help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: overload arithmetic assignment operators?


From: CdeMills
Subject: Re: overload arithmetic assignment operators?
Date: Fri, 24 Dec 2010 01:04:29 -0800 (PST)


Steve White wrote:
> 
> 
> Hi,
> 
> I gather from
>     http://www.gnu.org/software/octave/doc/interpreter/Assignment-Ops.html
> and
>    
> http://www.gnu.org/software/octave/doc/interpreter/Operator-Overloading.html
> that there is no way in octave to directly overload arithmetic assignment
> operators (+=, *=, etc.); 
> evidently the assignment operators are based on the binary arithmetic
> ones.
> 
> This is really a pity, efficiency-wise, for classes that might use these
> operators.
> The binary operators typically require the constructon of a new class
> object,
> while assignment operators don't.  If efficiency were important, then the
> binary
> operators should by default be defined in terms of the arithmetic
> assignment operators.
> 
> What do you think?
> 
> Is it worth a wish?
> 
> 

I suggest you to get from svn my latest implementation of 'dataframe'. I
overloaded all the ordinary operators, and there is already a big list: *,
.*, left division, right division, ... I tested under 3.2:
>x=dataframe(randn(3, 3))
x = dataframe with 3 rows and 3 columns
_1       X1       X2       X3          
Nr   double   double   double          
 1 -1.29346  0.16751 -1.16217          
 2  1.10311  0.19891 -0.59656          
 3 -0.51160 -1.20329  0.85394    
>x+=3
x+=3
x = dataframe with 3 rows and 3 columns
_1     X1     X2     X3                
Nr double double double                
 1 1.7065 3.1675 1.8378                
 2 4.1031 3.1989 2.4034                
 3 2.4884 1.7967 3.8539    

So, it appears that, with the 'plus' overloaded, you get the '+=' for the
same price. OTOH, the interface of plus is : function returning one output
which is the addition of its two inputs. As Octave use only the
pass-by-value semantic, a temporary copy is always created. In-place
operation is not part of this mechanism.

Regards

Pascal
-- 
View this message in context: 
http://octave.1599824.n4.nabble.com/overload-arithmetic-assignment-operators-tp3162713p3163036.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]