User guide

NumPy User Guide, Release 1.9.0
The places in the code corresponding to the actual computations for the ufunc are marked with /*BEGIN main ufunc
computation*/ and /*END main ufunc computation*/. The code in between those lines is the primary thing that must
be changed to create your own ufunc.
#include "Python.h"
#include "math.h"
#include "numpy/ndarraytypes.h"
#include "numpy/ufuncobject.h"
#include "numpy/halffloat.h"
/
*
*
multi_type_logit.c
*
This is the C code for creating your own
*
Numpy ufunc for a logit function.
*
*
Each function of the form type_logit defines the
*
logit function for a different numpy dtype. Each
*
of these functions must be modified when you
*
create your own ufunc. 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 definitions must precede the PyMODINIT_FUNC.
*
/
static void long_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];
long double tmp;
for (i = 0; i < n; i++) {
/
*
BEGIN main ufunc computation
*
/
tmp =
*
(long double
*
)in;
tmp /= 1-tmp;
*
((long double
*
)out) = logl(tmp);
/
*
END main ufunc computation
*
/
in += in_step;
out += out_step;
}
}
static void double_logit(char
**
args, npy_intp
*
dimensions,
5.3. Writing your own ufunc 87