[Numpy-discussion] Efficient operator overloading
Wed Apr 18 16:15:04 CDT 2007
On Apr 18, 2007, at 2:14 PM, Sturla Molden wrote:
> Let us assume again we have an expression like
> y = a * b + c * d
> where a,b,c and d are all arrays or matrices. In this case, the
> overloaded * and + operators woud not return a temporary array but an
> unevaluated expression of class Expr. Thus we would get
> tmp1 = Expr('__mul__',a,b) # symbolic representation of 'a * b'
> tmp2 = Expr('__mul__',c,d) # symbolic representation of 'c * d'
> tmp3 = Expr('__add__',tmp1,tmp1) # symbolic "a * b + c * d"
> del tmp1
> del tmp2
> y = tmp3 # y becomes a reference to an unevaluated expression
> Finally, we need a mechanism to 'flush' the unevaluated expression.
> Python does not allow the assignment operator to be evaluated, so one
> could not depend on that. But one could a 'flush on read/write'
At the risk of being remembered only for dredging up hacks, there is
something similar that would at least serve for someone to try work
along these lines. Nothing prevents you from overloading one of the
augmented operators (e.g., |=) to trigger the evaluation. True, it
uses up one of these that can be used for a numeric purpose, but it
can at least be used to test the idea to see how workable it is (I
forget who originally suggested this but I heard it suggested it at
the Long Beach Python Conference; it might have been Tim Peters).
More information about the Numpy-discussion