[Numpy-discussion] ufunc oddities

Charles R Harris charlesr.harris at gmail.com
Sat May 24 23:32:39 EDT 2008


On Sat, May 24, 2008 at 9:18 PM, Robert Kern <robert.kern at gmail.com> wrote:

> On Sat, May 24, 2008 at 10:12 PM, Charles R Harris
> <charlesr.harris at gmail.com> wrote:
> >
> > On Sat, May 24, 2008 at 9:06 PM, Robert Kern <robert.kern at gmail.com>
> wrote:
> >>
> >> On Sat, May 24, 2008 at 10:02 PM, Charles R Harris
> >> <charlesr.harris at gmail.com> wrote:
> >> > So what about the rule that the array type takes precedence over the
> >> > scalar
> >> > type? That is broken for booleans.
> >>
> >> Yes, and if it wasn't an intentional special case (I don't recall
> >> discussing it on the list, but it might have been), then it's a bug
> >> and suitable for changing. The other behaviors are intentional and
> >> thus not suitable for changing.
> >
> > So how do we know which is which? How does one write a test for an
> > unspecified behavior? And it is clear that add.reduce breaks the rules
> for
> > booleans.
>
> It breaks it for every integer type, in fact.
>
> In [11]: add.reduce(array([1]*257, dtype=uint8))
> Out[11]: 257
>
> I thought we only did the accumulator-dtype changing for the .sum()
> method, but I was wrong.
>

I think the sum behavior has gone through changes over the last year and
that it is time to write down how things are supposed to work. So that we
can test that they actually work that way. This means specifying and
checking the type of all the outputs. And there are exceptions here also

In [33]: x = ones(2,dtype=int8)

In [34]: x.sum().dtype
Out[34]: dtype('int32')

In [35]: x = ones(2,dtype=int64)

In [36]: x.sum().dtype
Out[36]: dtype('int64')

So we have a default accumulator unless the precision is greater than the
default. Unless the numbers are floats

In [37]: x = ones(2,dtype=float32)

In [38]: x.sum().dtype
Out[38]: dtype('float32')

But the accumalator is of the same kind unless the kind is boolean, in which
case it is integer. Clear as a bell.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080524/e8659f31/attachment.html>


More information about the NumPy-Discussion mailing list