User guide

NumPy User Guide, Release 1.9.0
import_array();
import_umath();
/
*
Create a new ufunc object
*
/
add_triplet = PyUFunc_FromFuncAndData(NULL, NULL, NULL, 0, 2, 1,
PyUFunc_None, "add_triplet",
"add_triplet_docstring", 0);
dtype_dict = Py_BuildValue("[(s, s), (s, s), (s, s)]",
"f0", "u8", "f1", "u8", "f2", "u8");
PyArray_DescrConverter(dtype_dict, &dtype);
Py_DECREF(dtype_dict);
dtypes[0] = dtype;
dtypes[1] = dtype;
dtypes[2] = dtype;
/
*
Register ufunc for structured dtype
*
/
PyUFunc_RegisterLoopForDescr(add_triplet,
dtype,
&add_uint64_triplet,
dtypes,
NULL);
d = PyModule_GetDict(m);
PyDict_SetItemString(d, "add_triplet", add_triplet);
Py_DECREF(add_triplet);
#if defined(NPY_PY3K)
return m;
#endif
}
5.3.7 PyUFunc_FromFuncAndData Specification
What follows is the full specification of PyUFunc_FromFuncAndData, which automatically generates a ufunc from a
C function with the correct signature.
PyObject
*
PyUFunc_FromFuncAndData( PyUFuncGenericFunction
*
func,
void
**
data, char
*
types, int ntypes, int nin, int nout, int identity,
char
*
name, char
*
doc, int check_return)
func
A pointer to an array of 1-d functions to use. This array must be at least ntypes long. Each entry
in the array must be a PyUFuncGenericFunction function. This function has the following
signature. An example of a valid 1d loop function is also given.
void loop1d(char
**
args, npy_intp
*
dimensions,
npy_intp
*
steps, void
*
data)
args
96 Chapter 5. Using Numpy C-API