User guide

NumPy User Guide, Release 1.9.0
#include "numpy/npy_3kcompat.h"
/
*
*
single_type_logit.c
*
This is the C code for creating your own
*
Numpy ufunc for a logit function.
*
*
In this code we only define the ufunc for
*
a single dtype. The computations that must
*
be replaced to create a ufunc for
*
a different funciton are marked with BEGIN
*
and END.
*
*
Details explaining the Python-C API can be found under
*
’Extending and Embedding’ and ’Python/C API’ at
*
docs.python.org .
*
/
static PyMethodDef LogitMethods[] = {
{NULL, NULL, 0, NULL}
};
/
*
The loop definition must precede the PyMODINIT_FUNC.
*
/
static void double_logit(char
**
args, npy_intp
*
dimensions,
npy_intp
*
steps, void
*
data)
{
npy_intp i;
npy_intp n = dimensions[0];
char
*
in = args[0],
*
out = args[1];
npy_intp in_step = steps[0], out_step = steps[1];
double tmp;
for (i = 0; i < n; i++) {
/
*
BEGIN main ufunc computation
*
/
tmp =
*
(double
*
)in;
tmp /= 1-tmp;
*
((double
*
)out) = log(tmp);
/
*
END main ufunc computation
*
/
in += in_step;
out += out_step;
}
}
/
*
This a pointer to the above function
*
/
PyUFuncGenericFunction funcs[1] = {&double_logit};
/
*
These are the input and return dtypes of logit.
*
/
static char types[2] = {NPY_DOUBLE, NPY_DOUBLE};
static void
*
data[1] = {NULL};
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"npufunc",
84 Chapter 5. Using Numpy C-API