![]() | Consider a function with an if test:
def somefunc(x): if x < 0: return 0 else: return sin(x) # or def somefunc(x): return 0 if x < 0 else sin(x) |
![]() | This function works with a scalar x but not an array |
![]() | Problem: x<0 results in a boolean array, not a boolean value
that can be used in the if test
>>> x = linspace(-1, 1, 3); print x [-1. 0. 1.] >>> y = x < 0 >>> y array([ True, False, False], dtype=bool) >>> bool(y) # turn object into a scalar boolean value ... ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() |