[Numpy-discussion] logical priority

Stephen Walton stephen.walton at csun.edu
Tue Feb 8 17:00:36 EST 2005

Fernando Perez wrote:

> Travis Oliphant wrote:
>> Currently, I don't think Python allows "over-riding" the "and" operation.
> You are correct:

What I want must be possible somehow, though:

In [6]: a=[True,False]

In [7]: b=[False,False]

In [8]: a and b
Out[8]: [False, False]

In [9]: a or b
Out[9]: [True, False]

In [10]: not a
Out[10]: False

So, standard Python does apply logical "and" and "or", but not "not", 
element by element to a list or tuple of booleans.  Is there no way to 
get access to this for a numarray bool array?  Can we get Guido to 
change "not" to also operate element-wise on a list of booleans?

I have a really common task in MATLAB I want to have work here.  In 
MATLAB I can extract the overlapping good values from two data sets by doing

f = data1 ~= flagval & data2 ~= flagval;

or even more compactly, since I use IEEE NaN for flagval

f = ~isnan(data1) & ~isnan(data2);

Right now in numarray I have to use

f = (data1 != flagval) & (data2 != flagval)

with the extra parentheses and, confusingly, using bitwise AND instead 
of logical AND.  Even if I use ieeespecial nan as flagval, there's no 
"notisnan(data1)" and no way to say "!isnan(data1)"

Of course, masked arrays are another option for this kind of usage but 
they have their own difficulties:  matplotlib does not at present allow 
one to plot from masked arrays, for example.

More information about the NumPy-Discussion mailing list