Re: [Numpy-discussion] element-wise logical operations on numpy arrays
2008/7/9 Catherine Moroney <[EMAIL PROTECTED]>: > 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. Good luck, Anne ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] element-wise logical operations on numpy arrays
On Wed, Jul 9, 2008 at 10:21 AM, Catherine Moroney < [EMAIL PROTECTED]> wrote: > Hello, > > 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. > They perform bitwise operations element by element. They only work for integer/bool arrays and you should avoid mixing signed/unsigned types because of the type promotion rules. Other than that, things should work fine. > 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? > Generally, yes, but they are more restrictive in the types they accept. Chuck ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] element-wise logical operations on numpy arrays
Hello, 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? Thanks, Catherine ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion