User guide
NumPy User Guide, Release 1.9.0
will install the module in your site-packages file.
See the distutils section of
’Extending and Embedding the Python Interpreter’
at docs.python.org for more information.
’’’
from distutils.core import setup, Extension
module1 = Extension(’spam’, sources=[’spammodule.c’],
include_dirs=[’/usr/local/lib’])
setup(name = ’spam’,
version=’1.0’,
description=’This is my spam package’,
ext_modules = [module1])
Once the spam module is imported into python, you can call logit via spam.logit. Note that the function used above
cannot be applied as-is to numpy arrays. To do so we must call numpy.vectorize on it. For example, if a python
interpreter is opened in the file containing the spam library or spam has been installed, one can perform the following
commands:
>>> import numpy as np
>>> import spam
>>> spam.logit(0)
-inf
>>> spam.logit(1)
inf
>>> spam.logit(0.5)
0.0
>>> x = np.linspace(0,1,10)
>>> spam.logit(x)
TypeError: only length-1 arrays can be converted to Python scalars
>>> f = np.vectorize(spam.logit)
>>> f(x)
array([ -inf, -2.07944154, -1.25276297, -0.69314718, -0.22314355,
0.22314355, 0.69314718, 1.25276297, 2.07944154, inf])
THE RESULTING LOGIT FUNCTION IS NOT FAST! numpy.vectorize simply loops over spam.logit. The loop is
done at the C level, but the numpy array is constantly being parsed and build back up. This is expensive. When the
author compared numpy.vectorize(spam.logit) against the logit ufuncs constructed below, the logit ufuncs were almost
exactly 4 times faster. Larger or smaller speedups are, of course, possible depending on the nature of the function.
5.3.3 Example Numpy ufunc for one dtype
For simplicity we give a ufunc for a single dtype, the ‘f8’ double. As in the previous section, we first give the .c file
and then the setup.py file used to create the module containing the ufunc.
The place 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"
5.3. Writing your own ufunc 83