# [Numpy-discussion] Truth value of an array

David Douard david.douard@logilab...
Fri Apr 18 06:55:17 CDT 2008

```On Fri, Apr 18, 2008 at 01:11:37PM +0200, Olivier Verdier wrote:
> In mathematics, if I compare two function, it means that I compare on
> all its "coordinates". If I say "f < g" I mean "f(x) < g(x) for all
> x".
>
> The same holds for a vector, if I write "v == w" I mean "v[i] == w[i]
> for all i".
>
> How come this doesn't work in numpy? And why the message about the
> truth value of an array being ambiguous? What is ambiguous?
>
> In my opinion any boolean array should be cast with all()
> automatically so that people can simply write:
> if v == w:
>   ...

No. A comparison operator on ndarray produce an ndarray of booleans. So
you have to write

if numpy.alltrue(u == v):
blah

This is a very good idea, since it allows you to also write things
like:

u[v<0] = 0

Which you could definitely not do if the comparison returns a scalar

More, you may also want to write things like:

if numpy.sometrue(u<0):
blah

Same remark.

>
> Or is there a good reason why this is not possible?
>
> Thank you!
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>

--
David Douard                        LOGILAB, Paris (France), +33 1 45 32 03 12
Formations Python, Zope, Debian :   http://www.logilab.fr/formations
Développement logiciel sur mesure : http://www.logilab.fr/services
Informatique scientifique :         http://www.logilab.fr/science
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080418/570ef9aa/attachment.bin
```