When doing boolean array comparison, is there any advantage / convention to using & in place of * or | in place of +? Are these always equivalent?
(if these are in the documentation, a link would probably be an acceptable answer, but my naive search for ‘numpy ampersand’ and ‘numpy elementwise boolean comparison’ didn’t yield anything relevant)
In numpy
&and|are equivalent tonp.bitwise_andandnp.bitwise_or. You can also use^fornp.bitwise_xor. This is all documented in the Arithmetic and comparison operations section of the ndarray docs. There are also ufuncs fornp.logical_and,np.logical_orandnp.logical_xor.If your arrays are all of dtype
boolthere shouldn’t be any difference. I personally lean towards&and|, even though if you are not strict about thebooldtype it can get you in troubles like this: