[Numpy-discussion] Efficient operator overloading

Xavier Gnata gnata at obs.univ-lyon1.fr
Wed Apr 18 16:13:52 EDT 2007


Timothy Hochberg wrote:
>
>
> On 4/18/07, *Sturla Molden* <sturla at molden.no 
> <mailto:sturla at molden.no>> wrote:
>
>
>     On 4/18/2007 7:33 AM, Anne Archibald wrote:
>
>     >copying. And the scope of improvement would be very limited; an
>     expression like A*B+C*D would be much more efficient, probably, if the
>     whole expression were evaluated at once for each element (due to
>     memory locality and temporary allocation). But it is impossible for
>     numpy, sitting inside python as it does, to do that.
>
>
>
>     Most numerical array/matrix libraries dependent on operator
>     overloading
>     generates temporaries. That is why Fortran is usually perceived as
>     superior to C++ for scientific programming. The Fortran compiler knows
>     about arrays and can avoid allocating three temporary arrays to
>     evaluate
>     and expression like
>
>     y = a * b + c * d
>     [SNIP]
>
>
>
>     I'd really like to see a Python extension library do this one day. It
>     would be very cool and (almost) as efficient as plain Fortran - though
>     not quite, we would still get some small temporary objects
>     created. But
>     that is a sacrifice I am willing to pay to use Python. We would gain
>     some efficacy over Fortran by postponing indefinitely evaluation of
>     computations that are not needed, when this is not known at compile
>     time.
>
>
>     Any comments?
>
>
> I periodically wonder if something like this could be built on top of 
> numexpr without too much pain. Simply build up the expression on 
> operations and then evaluate when data is requested. You'd need some 
> sort of Jekyl & Hyde matrix that spent the first part of it's life 
> unevaluated and then converted itself to something very much like an 
> array when it evaluated itself. If might end up being tricky to make 
> sure that everything got transparently evaluated at the right time 
> though.
>
> I've never had time to try it though.
>
>  
>
>     Sturla Molden
>     Ph.D.
>
Hi,

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.

Comments?

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 at obs.univ-lyon1.fr
############################################ 




More information about the NumPy-Discussion mailing list