[Numpy-discussion] suggestion for generalizing numpy functions

Charles R Harris charlesr.harris@gmail....
Fri Jul 17 11:04:05 CDT 2009


On Fri, Jul 17, 2009 at 9:44 AM, Darren Dale <dsdale24@gmail.com> wrote:

> 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?
>

PyObject* *PyTuple_GetItem*(PyObject *p, Py_ssize_t pos) Return value: Borrowed
reference.
Return the object at position pos in the tuple pointed to by p. If pos is
out of bounds, return NULL and sets an IndexError exception. It's a borrowed
reference so you need to call Py_INCREF on it. I find this Python C-API
documentation <http://www.python.org/doc/2.5/api/api.html>useful.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090717/9ad17fd3/attachment.html 


More information about the NumPy-Discussion mailing list