In Python (not numba
) the function works:
In [412]: def foo():
...: a = np.ones(1, np.bool_)
...: if a > 0:
...: print('truebr')
...: else:
...: print('falsebr')
...:
In [413]: foo()
truebr
But if a
is an array with more values:
In [414]: def foo():
...: a = np.ones(2, np.bool_)
...: if a > 0:
...: print('truebr')
...: else:
...: print('falsebr')
...:
In [415]: foo()
...
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
If I try your function in njit
I get a long traceback; too long to show or analyze, but it essentially tells me it can't be done in njit
mode. Given the above value error, I'm not surprised. njit
isn't allowing for 'just-one' Truth value array.
As a general rule, when using numba
you should iterate. That's its main purpose - to run numpy/python
problems that would otherwise be too expensive to iterate. Don't count on numba
to handle all the nuances of Python.
If I change the function to test each element of a
, it works:
In [421]: @numba.njit
...: def foo():
...: a = np.array([True])
...: for i in a:
...: if i > 0:
...: print('truebr')
...: else:
...: print('falsebr')
...:
In [422]: foo()
truebr
An all
(or any
) wrapper also works:
In [423]: @numba.njit
...: def foo():
...: a = np.array([True])
...: if (a > 0).all():
...: print('truebr')
...: else:
...: print('falsebr')
...:
In [424]: foo()
truebr