User guide
NumPy User Guide, Release 1.9.0
identity
Either PyUFunc_One, PyUFunc_Zero, PyUFunc_None. This specifies what should be re-
turned when an empty array is passed to the reduce method of the ufunc.
name
A NULL -terminated string providing the name of this ufunc (should be the Python name it will be
called).
doc
A documentation string for this ufunc (will be used in generating the response to
{ufunc_name}.__doc__). Do not include the function signature or the name as this is gen-
erated automatically.
check_return
Not presently used, but this integer value does get set in the structure-member of similar name.
The returned ufunc object is a callable Python object. It should be placed in a (module) dictionary under the same
name as was used in the name argument to the ufunc-creation routine. The following example is adapted from the
umath module
static PyUFuncGenericFunction atan2_functions[] = {
PyUFunc_ff_f, PyUFunc_dd_d,
PyUFunc_gg_g, PyUFunc_OO_O_method};
static void
*
atan2_data[] = {
(void
*
)atan2f,(void
*
) atan2,
(void
*
)atan2l,(void
*
)"arctan2"};
static char atan2_signatures[] = {
NPY_FLOAT, NPY_FLOAT, NPY_FLOAT,
NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE,
NPY_LONGDOUBLE, NPY_LONGDOUBLE, NPY_LONGDOUBLE
NPY_OBJECT, NPY_OBJECT, NPY_OBJECT};
...
/
*
in the module initialization code
*
/
PyObject
*
f,
*
dict,
*
module;
...
dict = PyModule_GetDict(module);
...
f = PyUFunc_FromFuncAndData(atan2_functions,
atan2_data, atan2_signatures, 4, 2, 1,
PyUFunc_None, "arctan2",
"a safe and correct arctan(x1/x2)", 0);
PyDict_SetItemString(dict, "arctan2", f);
Py_DECREF(f);
...
5.4 Beyond the Basics
The voyage of discovery is not in seeking new landscapes but in having
new eyes.
— Marcel Proust
Discovery is seeing what everyone else has seen and thinking what no
one else has thought.
98 Chapter 5. Using Numpy C-API