Steve Mitchell
Steve Mitchell

Reputation: 2045

How to create a Numpy ufunc over user-defined data types?

The Numpy C API contains a function PyUFunc_FromFuncAndData for creating a PyUFuncObject data structure. This function takes a char[] types argument that specifies the function's built-in argument types (int, float, etc.). But, the type number for a user-defined data type is an int, not a char. If I use the C-API to create a custom (user-defined) data type, how do I create a ufunc that operates on this data type?

Upvotes: 1

Views: 286

Answers (1)

Steve Mitchell
Steve Mitchell

Reputation: 2045

I don't see this explained in the Numpy documentation, but there are examples around.

int my_type_num = PyArray_RegisterDataType(&my_custom_type_descr);

PyUFuncObject* ufunc = (PyUFuncObject*)PyUFunc_FromFuncAndData(
    NULL, NULL, NULL, 0, nin, nout, identity, name, doc, 0);
PyUFunc_RegisterLoopForType(ufunc, my_type_num, my_loop_func, NULL, NULL);

This was discussed relative to an older version of Numpy on the Numpy mailing list. The source code for these API functions is here.

Upvotes: 1

Related Questions