[Numpy-discussion] Antwort: Re: numpy.left_shift with negative x2
Ralf Gommers
ralf.gommers@googlemail....
Wed Feb 3 08:30:49 CST 2010
On Wed, Feb 3, 2010 at 9:33 PM, <markus.proeller@ifm.com> wrote:
>
> >> On Wed, Feb 3, 2010 at 8:43 PM, <markus.proeller@ifm.com> wrote:
> >>
> >> Hello,
> >>
> >> the following operation seems strange to me
> >>
> >> >>> np.left_shift(2,-1)
> >> 0
> >>
> >> I would have expected a right_shift by one.
> >
> > I wouldn't expect anything, the behavior is simply not defined.
>
> But it would prevent a statement like
>
> if x2 > 0 then
> ...
> else
> ...
>
> Right now I think the left_shift ufunc calls the Python C API, so it just
does the same as Python. Which seems like the right thing to do.
If you want a bit_shift function which is a combination of left and right
shift, this is straightforward to do right? Something like:
In [42]: x2
Out[42]: array([-2, -1, 0, 1, 2])
In [43]: def bit_shift(x1, x2):
return np.choose(x2>0, [np.right_shift(x1, -x2), np.left_shift(x1, x2)])
....:
In [45]: bit_shift(2, x2)
Out[45]: array([0, 1, 2, 4, 8])
Cheers,
Ralf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100203/0f60892c/attachment.html
More information about the NumPy-Discussion
mailing list