Hello,
a < b < c (or any equivalent expression) is python syntactic sugar for
(a < b) and (b < c).
Now, for numpy arrays, a < b gives an array with boolean True or False
where the elements of a are less than those of b. So this gives us two
arrays that python now wants to "and" together. To do this, python
tries to convert the array "a < b" to a single True or False value,
and the array "b < c" to a single True or False value, which it then
knows how to "and" together. Except that "a < b" could contain many
True or False elements, so how to convert them to a single one?
There's no obvious way to guess -- typically, one uses "any" or "all"
to convert a boolean array to a single true or false value, depending,
obviously, on what one needs.
So this explains the error you see, but has nothing to do with the
results you desire... you need to and-together two boolean arrays
*element-wise* -- which is something Python doesn't know how to do
with the builtin "and" operator (which cannot be overridden). To do
this, you need to use the bitwise logic operators:
(a < b) & (b < c).
So:
def sin_half_period(x): return where((0.0 <= x) & (x <= pi), sin(x),
0.0)
Zach
On Oct 1, 2009, at 12:55 PM, Dr. Phillip M. Feldman wrote:
>
> I've defined the following one-line function that uses numpy.where:
>
> def sin_half_period(x): return where(0.0 <= x <= pi, sin(x), 0.0)
>
> When I try to use this function, I get an error message:
>
> In [4]: z=linspace(0,2*pi,9)
> In [5]: sin_half_period(z)
> ---------------------------------------------------------------------------
> ValueError Traceback (most recent
> call last)
>
> The truth value of an array with more than one element is ambiguous.
> Use
> a.any
> () or a.all()
>
> Any suggestions will be appreciated.
> --
> View this message in context: http://www.nabble.com/difficulty-with-numpy.where-tp25702676p25702676.html
> Sent from the Numpy-discussion mailing list archive at Nabble.com.
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion