[Numpy-discussion] C-extensions to numpy - a little help is needed
Mads Ipsen
mpi at osc.kiku.dk
Sun Mar 12 04:22:02 EST 2006
Hey,
I am trying to understand how to write numpy extensions, in order to be
able to do various things fast, that I need in my own apps. I have looked
at the examples from the Numeric package, which I managed to get working
with numpy, so I guess my questions holds for this forum.
There's one detail, that I don't understand in the examples provided with
the Numeric documentation, namely the use of
Py_DECREF(...)
which is used in the first example but not in the last. Let me be more
specific:
In the example, showing how to implement a trace() function, you
access the data using
array = (PyArrayObject *)
PyArray_ContiguousFromObject(input, PyArray_DOUBLE, 2, 2);
do some summing to find the trace and then decrement and return the
result
Py_DECREF(array);
return PyFloat_FromDouble(sum);
Now if you look at the other example for the matrix-vector multiply,
the matrix and vector data are accessed using a similar approach
PyObject *input1, *input2;
PyArrayObject *matrix, *vector, *result;
...
if (!PyArg_ParseTuple(args, "dOO", factor, &input1, &input2))
return NULL;
matrix = (PyArrayObject *)
PyArray_ContiguousFromObject(input1, PyArray_DOUBLE, 2, 2);
...
vector = (PyArrayObject *)
PyArray_ContiguousFromObject(input2, PyArray_DOUBLE, 1, 1);
...
result = (PyArrayObject *)
PyArray_FromDims(1, dimensions, PyArray_DOUBLE);
Then calculate the DGEMV storing the result in result->data and return
with
return PyArray_Return(result);
Finally here's my question:
----------------------------
Since the two pointers *matrix and *vector are created just like
*array in the trace() example, how come they are not decremented with
two calls to
Py_DECREF(matrix);
Py_DECREF(vector);
Any help is appreciated.
// Mads
More information about the NumPy-Discussion
mailing list