[Numpy-discussion] suggestion for generalizing numpy functions
Darren Dale
dsdale24@gmail....
Fri Jul 17 10:44:55 CDT 2009
On Fri, Jul 17, 2009 at 10:03 AM, Darren Dale <dsdale24@gmail.com> wrote:
> On Mon, Jul 13, 2009 at 7:12 PM, Darren Dale <dsdale24@gmail.com> wrote:
>
>> 2009/7/13 Stéfan van der Walt <stefan@sun.ac.za>
>>
>>> Hi Darren
>>>
>>> 2009/7/13 Darren Dale <dsdale24@gmail.com>:
>>> > I've put together a first cut at implementing __array_prepare__, which
>>> > appears to work, and I would like to request feedback. Here is an
>>> overview
>>> > of the approach:
>>>
>>> This is pretty neat! Do you have a quick snippet at hand illustrating
>>> its use?
>>
>>
>> That would be helpful, wouldn't it? The attached script is a modified
>> version of RealisticInfoArray from
>> http://docs.scipy.org/doc/numpy/user/basics.subclassing.html . It should
>> yield the following output:
>>
>>
>> starting with [0 1 2 3 4]
>> which is of type <class '__main__.MyArray'>
>> and has info attribute = "information"
>> subtracting 3 from [0 1 2 3 4]
>> subtract calling __array_prepare__ on [0 1 2 3 4] input
>> output array is now of type <class '__main__.MyArray'>
>> output array values are still uninitialized:
>> [139911601789568 39578752 139911614885536 39254560
>> 48]
>> __array_prepare__ is updating info attribute on output
>> __array_prepare__ finished, subtract ufunc is taking over
>> subtract calling __array_wrap__ on [0 1 2 3 4] input
>> output array has initial value: [-3 -2 -1 0 1]
>> __array_wrap__ is setting output endpoints to 0
>> yielding [ 0 -2 -1 0 0]
>> which is of type <class '__main__.MyArray'>
>> and has info attribute = "new_information"
>>
>
> This is a gentle ping, hoping to get some feedback so this feature has a
> chance of being included in the next release.
>
I have a question about the C-api. If I want to make the default
implementation of __array_prepare__ (or __array_wrap__, is anyone out
there?) simply pass through the output array:
static PyObject *
array_preparearray(PyArrayObject *self, PyObject *args)
{
PyObject *arr;
if (PyTuple_Size(args) < 1) {
PyErr_SetString(PyExc_TypeError,
"only accepts 1 argument");
return NULL;
}
arr = PyTuple_GET_ITEM(args, 0);
if (!PyArray_Check(arr)) {
PyErr_SetString(PyExc_TypeError,
"can only be called with ndarray object");
return NULL;
}
return arr;
}
Is this sufficient, or do I need to worry about calling Py_INCREF?
Thanks,
Darren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090717/060226ff/attachment.html
More information about the NumPy-Discussion
mailing list