[Numpy-discussion] ufunc oddities

Keith Goodman kwgoodman@gmail....
Sat May 24 21:28:40 CDT 2008


On Sat, May 24, 2008 at 7:09 PM, Charles R Harris
<charlesr.harris@gmail.com> wrote:
>
>
> On Sat, May 24, 2008 at 7:47 PM, Robert Kern <robert.kern@gmail.com> wrote:
>>
>> On Sat, May 24, 2008 at 8:31 PM, Charles R Harris
>> <charlesr.harris@gmail.com> wrote:
>> > Hi All,
>> >
>> > I'm writing tests for ufuncs and turned up some oddities:
>> >
>> > In [4]: degrees(True)
>> > Out[4]: 57.29578
>> >
>> > In [5]: radians(True)
>> > Out[5]: 0.017453292
>> >
>> > In [6]: sin(True)
>> > Out[6]: 0.84147096
>> >
>> > Do we want numeric functions to apply to booleans?
>>
>> I don't see a good reason to prevent it. They are just 0 and 1 under
>> the covers and behave like it everywhere else (e.g. True + True == 2
>> and the very useful boolean_mask.sum()).
>
> True + True == 1
>
> In [5]: x + x
> Out[5]: array([ True,  True], dtype=bool)
>
> In [6]: (x + x).astype(int)
> Out[6]: array([1, 1])
>
> That is how the inner loop is implemented.

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


More information about the Numpy-discussion mailing list