Suppose I have a code like:
import numpy as np
def value_error(x):
if x > 10:
return 0.
else:
return np.sin(x)
This could give me a ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() if called upon an numpy array.
Now I could do this instead:
def alright(x):
return np.sin(x) * (x <= 10.)
print alright(np.ones(100) * 100)
print value_error(np.ones(100) * 10)
My function (in this case np.sin) could be an expensive one. It is, however, called for every element of x, even ones where I know the answer because x > 10, without an expensive call. How can I get the best of both worlds?