[Numpy-discussion] Truth value of an array
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
> 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):
This is a very good idea, since it allows you to also write things
u[v<0] = 0
Which you could definitely not do if the comparison returns a scalar
bool instead of a ndarray.
More, you may also want to write things like:
> Or is there a good reason why this is not possible?
> Thank you!
> Numpy-discussion mailing list
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
Size: 189 bytes
Desc: Digital signature
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080418/570ef9aa/attachment.bin
More information about the Numpy-discussion