[Numpy-discussion] Different behaviour logical_and/and

Pearu Peterson pearu at cens.ioc.ee
Wed Mar 20 23:58:24 CST 2002


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.

Pearu





More information about the Numpy-discussion mailing list