[Numpy-discussion] ufunc oddities
Charles R Harris
charlesr.harris@gmail....
Sat May 24 21:57:23 CDT 2008
On Sat, May 24, 2008 at 8:48 PM, Robert Kern <robert.kern@gmail.com> wrote:
> On Sat, May 24, 2008 at 9:46 PM, Keith Goodman <kwgoodman@gmail.com>
> wrote:
> > On Sat, May 24, 2008 at 7:36 PM, Robert Kern <robert.kern@gmail.com>
> wrote:
> >> On Sat, May 24, 2008 at 9:28 PM, Keith Goodman <kwgoodman@gmail.com>
> wrote:
> >>> I think it's interesting how python and numpy bools behave differently.
> >>>
> >>>>> x = np.array([True, True], dtype=bool)
> >>>>> x[0] + x[1]
> >>> True
> >>>>> x[0] & x[1]
> >>> True
> >>>>>
> >>>>> x = [True, True]
> >>>>> x[0] + x[1]
> >>> 2
> >>>>> x[0] & x[1]
> >>> True
> >>
> >> The difference arises straightforwardly from the principle that numpy
> >> tries not to upcast when you do an operation on two arrays of the same
> >> dtype; True+True==True is of somewhat more use than True+True==False.
> >> Python bools are just ints subclasses to give a nice string
> >> representation.
> >
> > Sounds like there is no perfect solution. I like it the way it is but
> > these are differences I never noticed.
> >
> >>> x = np.array([True, True], dtype=bool)
> >>> x.sum()
> > 2
>
> Yes, the default accumulator dtype for integer types is at least the
> size of the native int type, so we don't have the situation of
> "bool+bool=bool".
>
How about
In [14]: x += 5
In [15]: x
Out[15]: array([ True, True], dtype=bool)
In [16]: x.tostring()
Out[16]: '\x01\x01'
In [17]: x + 5
Out[17]: array([6, 6])
In [18]: (x + 5).dtype
Out[18]: dtype('int32')
In [19]: (x.astype(int8) + 5).dtype
Out[19]: dtype('int8')
I have to write tests for 64 of these buggers and some poor sod has to write
the documentation. All these inconsistencies are going to drive both of us
mad.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080524/b40c1b9b/attachment.html
More information about the Numpy-discussion
mailing list