[Numpy-discussion] Truth value of an array
Olivier Verdier
zelbier at gmail.com
Fri Apr 18 08:33:28 EDT 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
"bool(A==B)"??
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...
Thanks.
On 18/04/2008, David Douard <david.douard at logilab.fr> 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):
> 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
> bool instead of a ndarray.
>
> 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 at 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
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
>
> iD8DBQFICIwl15pXfQ0A2uMRAm/3AJ9nqNHSH7vY5NpIULCXjgXdqkCUUgCfdxyi
> NIYA8A4grCPDp5lShTKhcoY=
> =S6VB
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
>
More information about the NumPy-Discussion
mailing list