[Numpy-discussion] Truth values of arrays:

Tim Hochberg tim.hochberg at cox.net
Fri Mar 3 10:36:06 CST 2006


Sasha wrote:

>On 3/3/06, Tim Hochberg <tim.hochberg at cox.net> wrote:
>  
>
>> ...
>>My feeling is that only shape-() arrays should be usable as truth
>>values. Allowing size-1 arrays to be compared as well is just adding a
>>weird, not particularly useful, corner case that's going to bite someone
>>eventually.
>>    
>>
>
>I agree, but size-1 arrays are also special with respect to
>broadcasting where they behave much like scalars:
>
>  
>
>>>>x = arange(5)
>>>>x + [1]
>>>>        
>>>>
>array([1, 2, 3, 4, 5])
>  
>

True,  but there's a good reason for that in that there's really not a 
good way to say "broadcast on this dimension", so being of dimension 1 
has to stand in for that. There are plenty of good ways to spell compare 
the first (and only) element of this array however. The obvious being 
(a[0]<b[0]).

-tim






More information about the Numpy-discussion mailing list