# Numarray question

Discussion in 'Python' started by Matt Feinstein, May 13, 2005.

1. ### Matt FeinsteinGuest

If I try

>>> 2 < array([1,2,3])

I get:

array([0, 0, 1], type=Bool)

which is pretty slick, However if I set

>>> q = 2 < array([1,2,3])
>>> q and q

I get a runtime error: "An array doesn't make sense as a truth value."

So.. why not? It seems to me that if I could vectorize logical
expressions (a la Matlab), it would be a lot easier to write functions
that will take either scalar or vector input values. Am I missing
something?

Matt Feinstein

--
There is no virtue in believing something that can be proved to be true.

Matt Feinstein, May 13, 2005

2. ### Robert KernGuest

Matt Feinstein wrote:
> If I try
>
>
>>>>2 < array([1,2,3])

>
>
> I get:
>
> array([0, 0, 1], type=Bool)
>
> which is pretty slick, However if I set
>
>
>>>>q = 2 < array([1,2,3])
>>>>q and q

>
>
> I get a runtime error: "An array doesn't make sense as a truth value."
>
> So.. why not? It seems to me that if I could vectorize logical
> expressions (a la Matlab), it would be a lot easier to write functions
> that will take either scalar or vector input values. Am I missing
> something?

logical_and(), logical_or(), etc. These will handle arrays of truth
values rather than treating the array as a single truth value like "and"
and "or" will do. The reason why the latter is left undefined is because
there isn't much solid agreement about what the semantics should be. In
the face of ambiguity, numarray refuses the temptation to guess and
forces the user to explicitly request certain behavior via alltrue() or
sometrue() or whatever strikes their fancy.

--
Robert Kern

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Robert Kern, May 13, 2005