[Numpy-discussion] boolean operations on boolean arrays

Charles R Harris charlesr.harris at gmail.com
Sat Apr 12 11:52:55 EDT 2014

On Sat, Apr 12, 2014 at 8:02 AM, Alan G Isaac <alan.isaac at gmail.com> wrote:

> This is a very basic question.
> Suppose `a` and `b` are boolean arrays with the same shape.
> Are there any considerations besides convenience in choosing
> between:
> a&b           a*b         logical_and(a,b)
> a|b           a+b         logical_or(a,b)
> ~a            True-a      logical_not(a)
> I somewhat expect the last column to be slowest
> as well as least convenient, since it is built to
> first convert non-booleans to booleans.
> Are there other differences?
> Also, is this made clear anywhere in the docs?
The Python operators for ndarrays can be dynamically set, but they are
initialized by the ufunc module (don't ask) as follows.

#define BOOL_invert BOOL_logical_not
#define BOOL_negative BOOL_logical_not
#define BOOL_add BOOL_logical_or
#define BOOL_bitwise_and BOOL_logical_and
#define BOOL_bitwise_or BOOL_logical_or
#define BOOL_bitwise_xor BOOL_logical_xor
#define BOOL_multiply BOOL_logical_and
#define BOOL_subtract BOOL_logical_xor

So they are all equivalent to logical_* functions.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140412/6844cadf/attachment.html>

More information about the NumPy-Discussion mailing list