[Numpy-discussion] What's the most numpythonic way to support multiple types in a C extension?

Nathaniel Smith njs at pobox.com
Wed Jun 27 12:25:45 EDT 2012

On Tue, Jun 26, 2012 at 10:53 PM, John Salvatier
<jsalvati at u.washington.edu> wrote:
> I want to support multiple types in the index_increment function that I've
> written here:
> https://github.com/jsalvatier/numpy/blob/master/numpy/core/src/multiarray/mapping.c
> I need to check that the first argument's type can support addition, cast
> the dataptr to the appropriate type and do the addition operation for that
> type. It looks like some of the numpy code uses .c.src files to do
> templating. Is that what I want to do here? Is the syntax described
> somewhere?

The proper way would be use the ufunc machinery, which already knows
how to perform addition on arbitrary numpy dtypes... unfortunately
this may be more complicated than you are hoping :-/.

Since there's nothing about this operation that is specific to the
addition operation or to the double type, I guess the ideal API would
actually be something like, an extra method added to binary ufuncs
  np.add.inplace_indexed(a, idx, b)
which would be equivalent to
  a[idx] += b
except that duplicate indices would be handled properly, and it would
avoid making a copy in the case of fancy indexing. You could look at
the implementation of ufunc.reduceat
(numpy/core/src/umath/ufunc_object.c:PyUFunc_Reduceat) for an idea of
how such fancy ufunc methods can be done.

(An even more ideal API would find some way to make this work
naturally with where=, but it's not obvious to me how that would


More information about the NumPy-Discussion mailing list