numpy / numpy

The fundamental package for scientific computing with Python.
https://numpy.org
Other
27.99k stars 10.07k forks source link

BUG: add NpyIter_New etc to __init__.pxd #21014

Open jbrockmendel opened 2 years ago

jbrockmendel commented 2 years ago

The __init__.pxd file that downstream libraries use to access the numpy C API via cython has the old PyArray_IterNew etc but not the new NpyIter_New etc.

It'd be nice to have the newer ones.

Side-note: it'd be really convenient to have a nicer API for getitem/setitem for cnp.flatiter and cnp.broadcast objects. At the moment doing a ND-compatible out[i] = func(values[i]) requires:

item = <object>(<PyObject**>cnp.PyArray_MultiIter_DATA(mi, 1))[0]

res = func(item)

(<int64_t*>cnp.PyArray_MultiIter_DATA(mi, 0))[0] = res
seberg commented 2 years ago

Indeed, these should be added.

The current API for setting an individual item in NumPy is PyArray_SETITEM(arr, char *data_ptr, object value). Although internally, we now have PyArray_Pack (since 1.19.x), and I think that is much better and should be exposed, but I have not done that yet. (The problem is that PyArray_SETITEM cannot get casts right, reasonably – doesn't matter in a lot of cases, but it needs to be phased out eventually IMO.)

(It might actually be reasonable to modify PyArray_SETITEM to bend it to the new API though...)

EDIT: To be clear, it seems that PyArray_SETITEM is also missing from the pxd

jbrockmendel commented 2 years ago

The current API for setting an individual item in NumPy is PyArray_SETITEM(arr, char *data_ptr, object value)

Assuming I can figure out how to cimport it despite it being missing from the pxd, how do i get char *data_ptr from mi?

seberg commented 2 years ago

@jbrockmendel the pointer is just PyArray_MultiIter_DATA(mi, i)? The array is mainly needed to fetch the dtype, but in this case I guess you have the arrays available, so that is fine.