Re: [Numpy-discussion] Numpy-discussion Digest, Vol 22, Issue 32
On Jul 9, 2008, at 10:00 AM, numpy-discussion-request@scipy.org wrote:
Send Numpy-discussion mailing list submissions to numpy-discussion@scipy.org
To subscribe or unsubscribe via the World Wide Web, visit http://projects.scipy.org/mailman/listinfo/numpy-discussion or, via email, send a message with subject or body 'help' to numpy-discussion-request@scipy.org
You can reach the person managing the list at numpy-discussion-owner@scipy.org
When replying, please edit your Subject line so it is more specific than "Re: Contents of Numpy-discussion digest..." Today's Topics:
1. Re: element-wise logical operations on numpy arrays (Anne Archibald)
From: "Anne Archibald"
Date: July 9, 2008 9:35:20 AM PDT To: "Discussion of Numerical Python" Subject: Re: [Numpy-discussion] element-wise logical operations on numpy arrays Reply-To: Discussion of Numerical Python 2008/7/9 Catherine Moroney
: I have a question about performing element-wise logical operations on numpy arrays.
If "a", "b" and "c" are numpy arrays of the same size, does the following syntax work?
mask = (a > 1.0) & ((b > 3.0) | (c > 10.0))
It seems to be performing correctly, but the documentation that I've read indicates that "&" and "|" are for bitwise operations, not element-by- element operations in arrays.
I'm trying to avoid using "logical_and" and "logical_or" because they make the code more cumbersome and difficult to read. Are "&" and "|" acceptable substitutes for numpy arrays?
Yes. Unfortunately it is impossible to make python's usual logical operators, "and", "or", etcetera, behave correctly on numpy arrays. So the decision was made to use the bitwise operators to express logical operations on boolean arrays. If you like, you can think of boolean arrays as containing single bits, so that the bitwise operators *are* the logical operators.
Confusing, but I'm afraid there really isn't anything the numpy developers can do about it, besides write good documentation.
Do "&" and "|" work on all types of numpy arrays (i.e. floats and 16 and 32-bit integers), or only on arrays of booleans? The short tests I've done seem to indicate that it does, but I'd like to have some confirmation.
Good luck, Anne
Catherine
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
On Wed, Jul 9, 2008 at 11:11 AM, Catherine Moroney < Catherine.M.Moroney@jpl.nasa.gov> wrote:
On Jul 9, 2008, at 10:00 AM, numpy-discussion-request@scipy.org wrote:
Send Numpy-discussion mailing list submissions to numpy-discussion@scipy.org
To subscribe or unsubscribe via the World Wide Web, visit http://projects.scipy.org/mailman/listinfo/numpy-discussion or, via email, send a message with subject or body 'help' to numpy-discussion-request@scipy.org
You can reach the person managing the list at numpy-discussion-owner@scipy.org
When replying, please edit your Subject line so it is more specific than "Re: Contents of Numpy-discussion digest..." Today's Topics:
1. Re: element-wise logical operations on numpy arrays (Anne Archibald)
From: "Anne Archibald"
Date: July 9, 2008 9:35:20 AM PDT To: "Discussion of Numerical Python" Subject: Re: [Numpy-discussion] element-wise logical operations on numpy arrays Reply-To: Discussion of Numerical Python 2008/7/9 Catherine Moroney
: I have a question about performing element-wise logical operations on numpy arrays.
If "a", "b" and "c" are numpy arrays of the same size, does the following syntax work?
mask = (a > 1.0) & ((b > 3.0) | (c > 10.0))
It seems to be performing correctly, but the documentation that I've read indicates that "&" and "|" are for bitwise operations, not element-by- element operations in arrays.
I'm trying to avoid using "logical_and" and "logical_or" because they make the code more cumbersome and difficult to read. Are "&" and "|" acceptable substitutes for numpy arrays?
Yes. Unfortunately it is impossible to make python's usual logical operators, "and", "or", etcetera, behave correctly on numpy arrays. So the decision was made to use the bitwise operators to express logical operations on boolean arrays. If you like, you can think of boolean arrays as containing single bits, so that the bitwise operators *are* the logical operators.
Confusing, but I'm afraid there really isn't anything the numpy developers can do about it, besides write good documentation.
Do "&" and "|" work on all types of numpy arrays (i.e. floats and 16 and 32-bit integers), or only on arrays of booleans? The short tests I've done seem to indicate that it does, but I'd like to have some confirmation.
They work for all integer types but not for float or complex types: In [1]: x = ones(3) In [2]: x | x --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /home/charris/<ipython console> in <module>() TypeError: unsupported operand type(s) for |: 'float' and 'float' Comparisons always return boolean arrays, so you don't have to worry about that. Chuck
participants (2)
-
Catherine Moroney
-
Charles R Harris