[Numpy-discussion] Efficient operator overloading

Xavier Gnata gnata@obs.univ-lyon1...
Wed Apr 18 17:19:11 CDT 2007


Sturla Molden wrote:
>> Timothy Hochberg wrote:
>>     
>
>   
>> Correct me if I'm wrong but it is not possible to code something working
>> like boost++ in python.
>> It is due to the fact we cannot overload the = operator in python. As a
>> result you cannot implement the way boost++ works : + * - / operators
>> build a tree and all the loops are done in the = operator code.
>>
>> I do not say that it is impossible to code an efficient way to deal with
>> operators and matrix in python but only that it looks not possible to
>> match the boost++ way of thinking.
>>     
>
> I have three comments:
>
> 1. Boost++ depends on template metaprogramming. For this to work, array
> bounds must be known in advance. If you don't know the dimensions in
> advance, the compiler cannot get rid of the temporary storage and
> redundant loops. Basically, Boost++ and Blitz++ can be efficient for small
> tight loops. But for small arrays, Boost++ and Blitz++ can do miracles.
>
> 2. Template metaprogramming and symbolic maths are very different
> subjects. Evaluating symbols run-time is very different for evaluating
> templates compile-time. If you know the eval() function in Python and Lisp
> it is probably easier to understand what I suggested; eval() and Lisp
> macros are not generally understood by the C++ community.
>
> 3. My suggestion did not require overloading the assignment operator (=).
> I suggested to let the "Jekyll & Hyde array" evaluate itself when it's
> content is requested (e.g. using the indexing operator [] as one such
> signal). Actually, not overloading the assignment operator is important as
> it allows us to procrastinate evaluation of expressions as long as we can.
> It may seem strange, but it is an important optimization technique. The
> things you don't do don't take any time.
>
>   

yes yes and yes :).

I don't know if I'm a python or a c++ programmer but I fully understand 
what eval() is.
eval() is not evil in a python/lisp context. Not at all! I do like to 
have the capability to play with str and eval in python (I also do like 
template meta prog...)

My point was only to say that the way to provide the user with this 
lazzy operators can't be the same as in c++. Sorry if my message was not 
clear :(.
What you want is symbolics maths with matrix and a way to trigger the 
evaluation.
Well, as far as I have seen for now, it looks pretty hard to code that 
with a pythonic syntax and a way to report in case of errors. It is also 
pretty hard to avoid nasty infinite recursions playing with "to be 
evaluated objects".

I'm going to have a look to this scipy sandbox module.

Xavier

-- 
############################################
Xavier Gnata
CRAL - Observatoire de Lyon
9, avenue Charles André
69561 Saint Genis Laval cedex
Phone: +33 4 78 86 85 28
Fax: +33 4 78 86 83 86
E-mail: gnata@obs.univ-lyon1.fr
############################################ 



More information about the Numpy-discussion mailing list