Capping a sub-expression in numexpr
Maybe something like this would work?
In [11]: import numpy as npIn [12]: import numexpr as ne In [13]: In [13]: x = np.linspace(0.02, 5.0, 1e7)In [14]: y = np.sin(x)In [15]: In [15]: timeit z0 = ((x-y) - ((x-y) > 1) * (x-y - 1))/(x+y)1 loops, best of 3: 1.02 s per loopIn [16]: timeit z1 = ne.evaluate("((x-y) - ((x-y) > 1.) * ((x-y) - 1.))/(x+y)")10 loops, best of 3: 120 ms per loop In [17]: timeit z2 = ne.evaluate("((x-y)/(x+y))")10 loops, best of 3: 103 ms per loop
There's a penalty for the capping above the division, but it's not too bad. Unfortunately when I tried it for some larger arrays it segfaulted. :-/
Update: this is much prettier, and a little faster too:
In [40]: timeit w0 = ne.evaluate("where(x-y>1,1,x-y)/(x+y)")10 loops, best of 3: 114 ms per loop