[Numpy-discussion] restrictions on fancy indexing
Eric Firing
efiring@hawaii....
Fri Sep 17 14:09:14 CDT 2010
On 09/17/2010 08:04 AM, Anne Archibald wrote:
> On 17 September 2010 13:47, Neal Becker<ndbecker2@gmail.com> wrote:
>> It's nice I can do:
>>
>> f = np.linspace (0, 1, 100)
>> u[f<.1] = 0
>>
>> cool, this seems to work also:
>>
>> u[np.abs(f)<.1] = 0
>>
>> cool! But exactly what kind of expressions are possible here? Certainly
>> not arbitrary code.
>
> The short answer is, anything that yields a boolean or integer array.
> There's no syntactical magic here. It might be clearer to write it as:
>
> c = np.abs(f)<.1
> u[c] = 0
>
> As for what generates boolean arrays, well, they're just numpy arrays,
> you can mangle them any way you want. But in particular,< > == != are
> operators that take two arrays and yield a boolean array. Also useful
> are ~ | and&, which are the logical operators on boolean arrays.
It can be important to bear in mind that they are not actually logical
operators, they are bitwise operators pressed into service.
Functionally, they substitute for logical operators on boolean arrays,
but one must watch out for their high precedence. This typically
requires using parentheses where they would not be needed for the true
python logical operators:
With python scalars: a > b and c < d
With numpy arrays: (a > b) & (c < d)
Eric
>
> Anne
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list