[Numpy-discussion] Truth value of an array
Fri Apr 18 07:33:28 CDT 2008
I certainly didn't mean that "A==B" should return a boolean!!
"A==B" should return an array of boolean as it does now. This is all right.
*However* "bool(A==B)" should return a boolean, *not* raise an
exception. Why raise an exception? What is ambiguous about
This is what happens when you write "if A==B" because then
"bool(A==B)" is somehow triggered and bang, an exception is raised.
As I tried to explain, the default behaviour should be that "bool(A)"
return "A.all()" but not an exception. Why is there an exception
raising at all?
I hope that my question is clearer now...
On 18/04/2008, David Douard <firstname.lastname@example.org> wrote:
> 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):
> 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:
> if numpy.sometrue(u<0):
> Same remark.
> > Or is there a good reason why this is not possible?
> > Thank you!
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpyemail@example.com
> > 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
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
> -----END PGP SIGNATURE-----
> Numpy-discussion mailing list
More information about the Numpy-discussion