Multiple conditions in control flow #19761
-
Hi all, I would like to implement a jitted indicator function with multiple conditions. If I put two conditions in jnp.where, it gives an error (TracerBoolConversionError: Attempted boolean conversion of traced array with shape bool[]..):
I was able to get around this by separating the two conditions:
Could anyone explain what is going on here? Is there any other ways better than my second implementation? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Thanks for the question! Instead of Note that due to Python's operator precedence conventions, the parentheses in the expression above cannot be omitted. Similarly, in this context you would use |
Beta Was this translation helpful? Give feedback.
Thanks for the question! Instead of
x < 3 and x > 1
, you should use(x < 3) & (x > 1)
. The reason this is required is that there is no way to overload theand
operator in Python, so we follow NumPy's convention of using bitwise&
for element-wise boolean logic.Note that due to Python's operator precedence conventions, the parentheses in the expression above cannot be omitted.
Similarly, in this context you would use
|
instead ofor
,~
instead ofnot
, and as a bonus you get^
for XOR, which has no python keyword equivalent.