User guide

NumPy User Guide, Release 1.9.0
valid setup.py file allowing distribution of the add.f module (as part of the package f2py_examples so that it would be
loaded as f2py_examples.add) is:
def configuration(parent_package=’’, top_path=None)
from numpy.distutils.misc_util import Configuration
config = Configuration(’f2py_examples’,parent_package, top_path)
config.add_extension(’add’, sources=[’add.pyf’,’add.f’])
return config
if __name__ == ’__main__’:
from numpy.distutils.core import setup
setup(
**
configuration(top_path=’’).todict())
Installation of the new package is easy using:
python setup.py install
assuming you have the proper permissions to write to the main site- packages directory for the version of Python you
are using. For the resulting package to work, you need to create a file named __init__.py (in the same directory as
add.pyf). Notice the extension module is defined entirely in terms of the “add.pyf and “add.f” files. The conversion
of the .pyf file to a .c file is handled by numpy.disutils.
Conclusion
The interface definition file (.pyf) is how you can fine-tune the interface between Python and Fortran. There is decent
documentation for f2py found in the numpy/f2py/docs directory where-ever NumPy is installed on your system (usu-
ally under site-packages). There is also more information on using f2py (including how to use it to wrap C codes) at
http://www.scipy.org/Cookbook under the “Using NumPy with Other Languages” heading.
The f2py method of linking compiled code is currently the most sophisticated and integrated approach. It allows clean
separation of Python with compiled code while still allowing for separate distribution of the extension module. The
only draw-back is that it requires the existence of a Fortran compiler in order for a user to install the code. However,
with the existence of the free-compilers g77, gfortran, and g95, as well as high-quality commerical compilers, this
restriction is not particularly onerous. In my opinion, Fortran is still the easiest way to write fast and clear code for
scientific computing. It handles complex numbers, and multi-dimensional indexing in the most straightforward way.
Be aware, however, that some Fortran compilers will not be able to optimize code as well as good hand- written
C-code.
5.2.4 weave
Weave is a scipy package that can be used to automate the process of extending Python with C/C++ code. It can be
used to speed up evaluation of an array expression that would otherwise create temporary variables, to directly “inline”
C/C++ code into Python, or to create a fully-named extension module. You must either install scipy or get the weave
package separately and install it using the standard python setup.py install. You must also have a C/C++-compiler
installed and useable by Python distutils in order to use weave.
Somewhat dated, but still useful documentation for weave can be found at the link http://www.scipy/Weave. There are
also many examples found in the examples directory which is installed under the weave directory in the place where
weave is installed on your system.
Speed up code involving arrays (also see scipy.numexpr)
This is the easiest way to use weave and requires minimal changes to your Python code. It involves placing quotes
around the expression of interest and calling weave.blitz. Weave will parse the code and generate C++ code using
Blitz C++ arrays. It will then compile the code and catalog the shared library so that the next time this exact string is
66 Chapter 5. Using Numpy C-API