[Numpy-discussion] Another suggestion for making numpy's functions generic

Darren Dale dsdale24@gmail....
Tue Oct 20 07:04:19 CDT 2009


Hi Travis,

On Mon, Oct 19, 2009 at 6:29 PM, Travis Oliphant <oliphant@enthought.com> wrote:
>
> On Oct 17, 2009, at 7:49 AM, Darren Dale wrote:
[...]
>> When calling numpy functions:
>>
>> 1) __input_prepare__ provides an opportunity to operate on the inputs
>> to yield versions that are compatible with the operation (they should
>> obviously not be modified in place)
>>
>> 2) the output array is established
>>
>> 3) __array_prepare__ is used to determine the class of the output
>> array, as well as any metadata that needs to be established before the
>> operation proceeds
>>
>> 4) the ufunc performs its operations
>>
>> 5) __array_wrap__ provides an opportunity to update the output array
>> based on the results of the computation
>>
>> Comments, criticisms? If PEP 3124^ were already a part of the standard
>> library, that could serve as the basis for generalizing numpy's
>> functions. But I think the PEP will not be approved in its current
>> form, and it is unclear when and if the author will revisit the
>> proposal. The scheme I'm imagining might be sufficient for our
>> purposes.
>
> This seems like it could work.    So, basically ufuncs will take any
> object as input and call it's __input__prepare__ method?   This should
> return a sub-class of an ndarray?

ufuncs would call __input_prepare__ on the input declaring the highest
__array_priority__, just like ufuncs do with __array_wrap__, passing a
tuple of inputs and the ufunc itself (provided for context).
__input_prepare__ would return a tuple of inputs that the ufunc would
use for computation, I'm not sure if these need to be arrays or not, I
think I can give a better answer once I start the implementation (next
few days I think).

Darren


More information about the NumPy-Discussion mailing list