[Numpy-discussion] proposal on ufunc shift operators.

Anne Archibald peridot.faceted at gmail.com
Wed May 28 15:34:48 EDT 2008


2008/5/28 Charles R Harris <charlesr.harris at gmail.com>:
> Hi All,
>
> Currently we have:
>
> In [2]: ones(1,dtype=int8) << ones(1,dtype=uint8)
> Out[2]: array([2], dtype=int16)
>
> In [4]: ones(1,dtype=int64) << ones(1,dtype=uint64)
> Out[4]: array([2], dtype=object)
>
> Note the increased size in the first case and the return of a Python long
> integer object in the second. I propose that these operators should preserve
> the type of the first argument, although this is not easy to do with the
> current ufunc setup. It is impossible to use a type of sufficient size for
> all shift values and preserving the type of the first argument is what I
> think most folks would expect.

That sounds eminently sensible to me. Of course this will complicate
explaining type rules for ufuncs. An alternative is to give it some
reasonably simple non-exceptional rule and let users specify the
output dtype if they care. Still, I think in the interest of minimal
surprise, getting rid of the conversion to an object array of longs,
at least, would be a good idea.

Do we have a cyclic-shift ufunc? That is, uint8(1)<<<8==1?

Anne



More information about the NumPy-Discussion mailing list