User guide
NumPy User Guide, Release 1.9.0
5.3.2 Example Non-ufunc extension
For comparison and general edificaiton of the reader we provide a simple implementation of a C extension of logit that
uses no numpy.
To do this we need two files. The first is the C file which contains the actual code, and the second is the setup.py file
used to create the module.
#include <Python.h>
#include <math.h>
/
*
*
spammodule.c
*
This is the C code for a non-numpy Python extension to
*
define the logit function, where logit(p) = log(p/(1-p)).
*
This function will not work on numpy arrays automatically.
*
numpy.vectorize must be called in python to generate
*
a numpy-friendly function.
*
*
Details explaining the Python-C API can be found under
*
’Extending and Embedding’ and ’Python/C API’ at
*
docs.python.org .
*
/
/
*
This declares the logit function
*
/
static PyObject
*
spam_logit(PyObject
*
self, PyObject
*
args);
/
*
*
This tells Python what methods this module has.
*
See the Python-C API for more information.
*
/
static PyMethodDef SpamMethods[] = {
{"logit",
spam_logit,
METH_VARARGS, "compute logit"},
{NULL, NULL, 0, NULL}
};
/
*
*
This actually defines the logit function for
*
input args from Python.
*
/
static PyObject
*
spam_logit(PyObject
*
self, PyObject
*
args)
{
double p;
/
*
This parses the Python argument into a double
*
/
if(!PyArg_ParseTuple(args, "d", &p)) {
return NULL;
}
/
*
THE ACTUAL LOGIT FUNCTION
*
/
p = p/(1-p);
p = log(p);
5.3. Writing your own ufunc 81