[Numpy-discussion] Returning tuple of numpy arrays from a Numpy C-extension?

Chris Barker chris.barker at noaa.gov
Tue Dec 27 16:37:41 EST 2011


sorry to be a proselytizer, but this would be trivial with Cython:

http://cython.org/

-Chris


On Tue, Dec 27, 2011 at 1:52 AM, Åke Kullenberg
<ake.kullenberg at gmail.com> wrote:
> After diving deeper in the docs I found the PyTuple_New alternative to
> building tuples instead of Py_BuildValue. It seems to work fine.
>
> But I am unsure of the INCREF/DECREF refcounting thing. Will I need any of
> those in the code below?
>
> Also, for generic c extensions, how can I check the refcounts of the
> variables to see they're ok?
>
> static PyObject *
> test(PyObject *self, PyObject *args)
> {
>     PyArrayObject *py_in, *py_out, *py_out2;
>     double *in, *out, *out2;
>     int i, n, dims[2];
>     if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &py_in))
>         return NULL;
>     if (NULL == py_in || not_doublevector(py_in)) return NULL;
>     n = py_in->dimensions[0];
>     dims[0] = n;
>     dims[1] = 1;
>     py_out = (PyArrayObject *) PyArray_FromDims(1, dims, NPY_DOUBLE);
>     py_out2 = (PyArrayObject *) PyArray_FromDims(1, dims, NPY_DOUBLE);
>     in = pyvector_to_Carrayptrs(py_in);
>     out = pyvector_to_Carrayptrs(py_out);
>     out2 = pyvector_to_Carrayptrs(py_out2);
>     for (i=0; i<n; i++) {
>         out[i] = in[i] * 2.0;
>         out2[i] = in[i] * 3.0;
>     }
>     PyObject *tupleresult = PyTuple_New(2);
>     PyTuple_SetItem(tupleresult, 0, PyArray_Return(py_out));
>     PyTuple_SetItem(tupleresult, 1, PyArray_Return(py_out2));
>     return tupleresult;
> }
>
> On Tue, Dec 27, 2011 at 5:11 PM, Åke Kullenberg <ake.kullenberg at gmail.com>
> wrote:
>>
>> I have put together a few c extensions following the documentation
>> on http://www.scipy.org/Cookbook/C_Extensions/NumPy_arrays. There is however
>> one thing that stumps me.
>>
>> To illustrate with a simple code snippet, the test function below
>> multiplies the input numpy double array by two. So far so good. But how
>> about if I want the function to return a tuple of two numpy arrays so I
>> could do 'a, b = myCLib.test(c)' from a Python script? It's straight forward
>> to convert C data structures to Python objects with Py_BuildValue, but how
>> can I do this for numpy arrays instead?
>>
>> static PyObject *
>> test(PyObject *self, PyObject *args)
>> {
>>     PyArrayObject *py_in, *py_out;
>>     double *in, *out;
>>     int i, n, dims[2];
>>     if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &py_in))
>>         return NULL;
>>     if (NULL == py_in || not_doublevector(py_in)) return NULL;
>>     n = py_in->dimensions[0];
>>     dims[0] = n;
>>     dims[1] = 1;
>>     py_out = (PyArrayObject *) PyArray_FromDims(1, dims, NPY_DOUBLE);
>>     in = pyvector_to_Carrayptrs(py_in);
>>     out = pyvector_to_Carrayptrs(py_out);
>>     for (i=0; i<n; i++) {
>>         out[i] = in[i] * 2.0;
>>     }
>>     return PyArray_Return(py_out);
>> }
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list