[Numpy-discussion] Numexpr does broadcasting.

Tim Hochberg tim.hochberg at cox.net
Wed Jun 21 15:02:27 EDT 2006

Numexpr can now handle broadcasting. As an example, check out this 
implementation of the distance-in-a-bunch-of-dimenstions function that's 
been going around. This is 80% faster than the most recent one posted on 
my box and considerably easier to read.

    expr = numexpr("(a - b)**2", [('a', float), ('b', float)])
    def dist_numexpr(A, B):
        return sqrt(sum(expr(A[:,newaxis], B[newaxis,:]), axis=2))

Now, if we just could do 'sum' inside the numexpr, I bet that this would 
really scream.  This is something that David has talked about adding at 
various points. I just made his life a bit harder by supporting 
broadcasting, but I still don't think it would be all that hard to add 
reduction operations like sum and product as long as they were done at 
the outermost level of the expression. That is, "sum(x*2 + 5)" should be 
doable, but "5 + sum(x**2)" would likely be difficult.

Anyway, I thought that was cool, so I figured I'd share ;-)

[Bizzarely, numexpr seems to run faster on my box when compiled with 
"-O1" than when compiled with "-O2" or "-O2 -funroll-all-loops". Go figure.]


More information about the NumPy-Discussion mailing list