[Numpy-discussion] Different behaviour logical_and/and
Huaiyu Zhu
huaiyu_zhu at yahoo.com
Sat Mar 23 00:32:02 CST 2002
On Thu, 21 Mar 2002, Pearu Peterson wrote:
> On 20 Mar 2002, Jochen Küpper wrote:
> > >>>> a
> > Aureli> array([0, 1, 0, 0, 1, 0, 1, 0, 0, 1])
> > >>>> b
> > Aureli> array([1, 1, 0, 1, 0, 0, 1, 1, 1, 0])
> >
> > >>>> Numeric.logical_and(a,b)
> > Aureli> array([0, 1, 0, 0, 0, 0, 1, 0, 0, 0])
> >
> > This look's correct...
> >
> > >>>> a and b
> > Aureli> array([1, 1, 0, 1, 0, 0, 1, 1, 1, 0])
> >
> > ... and this suspiciously like b.
>
> . and also correct. It is default behaviour of Python `and'
> operation. From Python Language Reference:
>
> The expression `x and y' first evaluates `x'; if `x' is false, its value
> is returned; otherwise, `y' is evaluated and the resulting value is
> returned.
>
> So, `and' operation is "object"-operation (unless redefined to something
> else) while logical_and is "elementwise"-operation.
There is a section in PEP-225 for elementwise/objectwise operators to
extend the meaning of ~ to an "elementizer", so that
[1, 0, 1, 0] and [0, 1, 1, 0] => [0, 1, 1, 0]
[1, 0, 1, 0] ~and [0, 1, 1, 0] => [0, 0, 1, 0]
There are several other places, entirely unrelated to numerical
computation, that elementization of an operator makes sense.
Huaiyu
More information about the Numpy-discussion
mailing list