[Numpy-discussion] Efficient operator overloading

Sturla Molden sturla@molden...
Wed Apr 18 15:52:09 CDT 2007

> 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.

More information about the Numpy-discussion mailing list