Hi, The current behavior of numpy's binary_repr is the following:
binary_repr(1,width=2) '01'
binary_repr(0,width=2) '0'
This seems inconsistent and I'd suggest always padding with zeros to make sure that the return string always has length=width. Objections ? David
David Huard wrote:
Hi,
The current behavior of numpy's binary_repr is the following:
binary_repr(1,width=2) '01'
binary_repr(0,width=2) '0'
This seems inconsistent and I'd suggest always padding with zeros to make sure that the return string always has length=width.
Objections ?
This sounds like a good idea. I don't think it would break anything, but it would be good to test. -Travis O.
------------------------------------------------------------------------
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Change done. All tests pass. Should I document the change somewhere ? Although it's a small change, I'm guessing it could be very annoying to debug for someone depending on the previous behavior. 2007/12/13, Travis E. Oliphant <oliphant@enthought.com>:
David Huard wrote:
Hi,
The current behavior of numpy's binary_repr is the following:
binary_repr(1,width=2) '01'
binary_repr(0,width=2) '0'
This seems inconsistent and I'd suggest always padding with zeros to make sure that the return string always has length=width.
Objections ?
This sounds like a good idea. I don't think it would break anything, but it would be good to test.
-Travis O.
------------------------------------------------------------------------
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
What's the correct way to do something like this? a=array( (0,1,1,0) ) b=array( (4,3,2,1) ) c=array( (1,2,3,4) ) where( (a<1 or b<3), b,c) Python throws a ValueError I would expect to get an array that looks like [4,2,2,1] I think Thanks, Ross ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs
use '+' instead of 'or' for bool arrays. In [8]: numpy.where((a<1) + (b<3), b, c) Out[8]: array([4, 2, 2, 1]) hth, L. On Dec 16, 2007 8:10 PM, Ross Harder <brussel13@yahoo.com> wrote:
What's the correct way to do something like this?
a=array( (0,1,1,0) ) b=array( (4,3,2,1) ) c=array( (1,2,3,4) )
where( (a<1 or b<3), b,c)
Python throws a ValueError I would expect to get an array that looks like [4,2,2,1] I think
Thanks, Ross
____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
"or" is logical or. You want "|" which is bitwise/elementwise or. Also, watch the order of operations -- | has higher precedence than <. Thus, you want where( (a<1) | (b<3), b,c) Ross Harder wrote:
What's the correct way to do something like this?
a=array( (0,1,1,0) ) b=array( (4,3,2,1) ) c=array( (1,2,3,4) )
where( (a<1 or b<3), b,c)
Python throws a ValueError I would expect to get an array that looks like [4,2,2,1] I think
Thanks, Ross
____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Am Sonntag, 16. Dezember 2007 20:10:41 schrieb Ross Harder:
What's the correct way to do something like this?
a=array( (0,1,1,0) ) b=array( (4,3,2,1) ) c=array( (1,2,3,4) )
where( (a<1 or b<3), b,c)
Now + and | have been proposed to you, but it looks to me as if the "correct way" would be logical_or. All solutions give the same result, but logical_or better expresses what you're trying to do: In [9]: print where( (logical_or(a<1, b<3)), b,c) [4 2 2 1] (Think of the Zen.) Ciao, / / /--/ / / ANS
Hans Meine wrote:
where( (a<1 or b<3), b,c)
Now + and | have been proposed to you, but it looks to me as if the "correct way" would be logical_or. All solutions give the same result, but logical_or better expresses what you're trying to do:
In [9]: print where( (logical_or(a<1, b<3)), b,c) [4 2 2 1] (Think of the Zen.)
I'm not sure the Zen answers this one for us. It's really a matter of taste, and many of us prefer infix notation for this kind of thing. I think: where( ( (a<1) | (b<3) ), b,c) reads better than: where( (logical_or(a<1, b<3)), b,c) The only reason we need to use "|" (which means logical_or for boolean arrays) rather than "or" is that we can't overload the python "or" or "and" because they are designed to short-circuit. If you want to be really explicit, you could do: where( (logical_or(less(a,1), less(b,3))), b,c) or, since I like to use namespaces (also see the Zen): import numpy as N N.where( (N.logical_or(N.less(a,1), N.less(b,3))), b,c) which are even harder to read. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
On Donnerstag 20 Dezember 2007, Christopher Barker wrote:
In [9]: print where( (logical_or(a<1, b<3)), b,c) [4 2 2 1] (Think of the Zen.)
I'm not sure the Zen answers this one for us.
As you have guessed correctly, I was thinking of "explicit is better than implicit".
It's really a matter of taste, and many of us prefer infix notation for this kind of thing.
You're right, "where( ( (a<1) | (b<3) ), b,c)" *does* look better. I should have written "for completeness, I want to mention that logical or is called logical_or in numpy", and for boolean array, "|" seems to be "the correct way". Ciao, / / .o. /--/ ..o / / ANS ooo
participants (8)
-
Andrew Straw -
Christopher Barker -
David Huard -
Hans Meine -
lorenzo bolla -
Ross Harder -
Stefan van der Walt -
Travis E. Oliphant