How do I efficiently express the following using numexpr
?
z = min(x-y, 1.0) / (x+y)
Here, x
and y
are some large NumPy arrays of the same shape.
In other words, I am trying to cap x-y
to 1.0
before dividing it by x+y
.
I would like to do this using a single numexpr
expression (x
and y
are huge, and I don't want to have to iterate over them more than once).
Maybe something like this would work?
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: