# [Numpy-discussion] Truth value of an array

Olivier Verdier zelbier@gmail....
Sat Apr 19 02:21:40 CDT 2008

```Anne, thank you, this was the example I was looking for. Indeed A!=B
would not work as expected if the bool(A) always returned A.all(). Now
I can teach my student why there is no automatic conversion from
boolean arrays to booleans.

== Olivier

On 18/04/2008, Anne Archibald <peridot.faceted@gmail.com> wrote:
> On 18/04/2008, Olivier <zelbier@gmail.com> wrote:
>  > Let's restrict the discussion the case to boolean arrays (dtype bool),
>  >  since all the comparisons (A==B, A!=B, A<B etc. return boolean
>  >  arrays).
>  >
>  >  So I have an array filled with booleans. Is there a reason not to map
>  >  "bool(A)" to "A.all()" but instead raise an exception?
>  >
>  >  As far as I can see, "if A==B" is clear enough; one always means
>  >  "(A==B).all()". Isn't that so?
>  >
>  >  I would like to see examples where there is clearly an ambiguity so
>  >  that I understand the benefit of having an exception raised for
>  >  boolean matrices instead of simply using all().
>  >
>  >  If there is no such clear example, then why not map "bool(A)" to
>  >  "A.all()" in numpy?
>
>
> In [1]: import numpy as np
>
>  In [2]: A = np.array([0,1])
>
>  In [3]: B = np.array([0,0])
>
>  In [4]: (A==B).all()
>  Out[4]: False
>
>  In [5]: (A!=B).all()
>  Out[5]: False
>
>  One would expect A!=B to be the logical opposite of A==B, but with
>  your proposed suggestion it is not.
>
>  In math, when comparing functions, one can compare the functions as a
>  whole, or one can compare them pointwise. numpy's implementation does
>  pointwise comparison, for a variety of good reasons. As for why
>  converting an array to a boolean doesn't automatically do all(), if
>  you don't know you are dealing with an array and using all(), you will
>  surely shoot yourself in the foot sooner rather than later (as the
>  above example shows). If you do, simply wrapping it in all() is easy
>  and clear.
>
>
>  Anne
>
> _______________________________________________
>  Numpy-discussion mailing list
>  Numpy-discussion@scipy.org
>  http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
```