[Numpy-discussion] performing operations in-place in numpy

Citi, Luca lciti@essex.ac...
Wed Jul 8 19:51:13 CDT 2009


The problem is not PyArray_Conjugate itself.
The problem is that whenever you call a function from the C side
and one of the inputs has ref_count 1, it can be overwritten.
This is not a problem from the python side because if the
ufunc sees a ref_count=1 it means that no python object is referencing to it.

Let us consider an ufunc like 
sumdiff : c,d = sumdiff(a,b)   <=>   c,d = a+b,a-b
if you implement it in python it is fine.
If it is implemented in C, it has to be implemented like this:

obj3 = PyArray_Sum((PyAO *)obj1, (PyAO *)obj2, NULL);
obj4 = PyArray_Diff((PyAO *)obj1, (PyAO *)obj2, NULL);

Without the Py_INCREF/Py_DECREF pair, if sumdiff is called with
arrays not bounded to any python variable,
such as sumdiff(arange(10), ones(10)),
PyArray_Sum can overwrite one of the inputs and make the result of
PyArray_Diff wrong.
With the Py_INCREF/Py_DECREF pair, PyArray_Sum cannot overwrite
its inputs (while PyArray_Diff can).

Moving the Py_INCREF/Py_DECREF inside the ufuncs makes
the patch unuseful because no array could have ref_count 1.

You are right, existing C-extension modules that use the
Numpy/Numeric API, might give wrong results.


More information about the NumPy-Discussion mailing list