Numeric don't compare arrays correctly

Tim Hochberg tim.hochberg at ieee.org
Sat Jun 7 16:19:56 CEST 2003


Erik Max Francis wrote:
> Tim Hochberg wrote:
> 
> 
>>An ill considered thought: I wonder if it would be feasible to change
>>the definition of "a < b < c" from:
>>   (a < b) and (b < c)
>>to:
>>   (a < b) & (b < c)
> 
> 
> How would this help?

Bitwise & is overridable while 'and' is not. As a result, in Numeric 
where this thread started, "a < b < c" produces results that are 
technically right, but practically useless. From the original post by 
Maurix:

    >>> a=array([1.1,2.2,3.3,4.4,5.5])
    >>> print 2.<a<4.
    [1 1 1 0 0] # 1.1 is not between 2 and 4 !!
    >>> print (a>2.)and(a<4.)
    [1 1 1 0 0]
    >>> print (a>2.)*(a<4.)
    [0 1 1 0 0] # this is good!

(2.>a) & (a<4.), as proposed above, also works out correctly. In Numeric 
the proposed scheme would work nicely out of the box. I'm not familiar 
with what other (ab)users of rich comparisons are doing, but this would 
at least make it feasible to put together a consistent set of rich 
comparison operations.


> 
>>Since "x and y" and "x & y" are equivalent when x, y are in (0,1), ...
> 
> 
> There's an obvious difference, which is that `and' does short circuiting
> whereas & does not.

Good point. Another beautiful idea ruined by an ugly fact. That makes 
the backwards compatibility issues considerably worse. And, 
unfortunately, non short circuiting behaviour is probably required to 
get the chained comparisons in Numeric and similar abusers of rich 
comparisons to work correctly.

back to logical_and(a < b, b < c).


-tim






More information about the Python-list mailing list