User guide

NumPy User Guide, Release 1.9.0
*
ufunc computation is carried out are marked
*
with comments.
*
*
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_logitprod(char
**
args, npy_intp
*
dimensions,
npy_intp
*
steps, void
*
data)
{
npy_intp i;
npy_intp n = dimensions[0];
char
*
in1 = args[0],
*
in2 = args[1];
char
*
out1 = args[2],
*
out2 = args[3];
npy_intp in1_step = steps[0], in2_step = steps[1];
npy_intp out1_step = steps[2], out2_step = steps[3];
double tmp;
for (i = 0; i < n; i++) {
/
*
BEGIN main ufunc computation
*
/
tmp =
*
(double
*
)in1;
tmp
*
=
*
(double
*
)in2;
*
((double
*
)out1) = tmp;
*
((double
*
)out2) = log(tmp/(1-tmp));
/
*
END main ufunc computation
*
/
in1 += in1_step;
in2 += in2_step;
out1 += out1_step;
out2 += out2_step;
}
}
/
*
This a pointer to the above function
*
/
PyUFuncGenericFunction funcs[1] = {&double_logitprod};
/
*
These are the input and return dtypes of logit.
*
/
static char types[4] = {NPY_DOUBLE, NPY_DOUBLE,
NPY_DOUBLE, NPY_DOUBLE};
static void
*
data[1] = {NULL};
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
92 Chapter 5. Using Numpy C-API