[Numpy-discussion] logical priority
stephen.walton at csun.edu
Tue Feb 8 17:00:36 CST 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 : a=[True,False]
In : b=[False,False]
In : a and b
Out: [False, False]
In : a or b
Out: [True, False]
In : not a
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