[Numpy-discussion] performing operations in-place in numpy
Citi, Luca
lciti at essex.ac.uk
Wed Jul 8 20:51:13 EDT 2009
Hello
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:
Py_INCREF(obj1);
Py_INCREF(obj2);
obj3 = PyArray_Sum((PyAO *)obj1, (PyAO *)obj2, NULL);
Py_DECREF(obj1);
Py_DECREF(obj2);
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.
Best,
Luca
More information about the NumPy-Discussion
mailing list