Book Image

Learning SciPy for Numerical and Scientific Computing Second Edition - Second Edition

Book Image

Learning SciPy for Numerical and Scientific Computing Second Edition - Second Edition

Overview of this book

This book targets programmers and scientists who have basic Python knowledge and who are keen to perform scientific and numerical computations with SciPy.
Table of Contents (10 chapters)
9
Index

Interaction with Fortran


SciPy provides a simple way of including Fortran code—f2py. This is a utility shipped with the NumPy libraries, which is operative when distutils from SciPy are available. This is always the case when we install SciPy.

The f2py utility is supposed to run outside Python, and it is used to create from any Fortran file a Python module that can be easily called in our sessions. Under any *nix system, we call it from the terminal. Under Windows, we recommend you run it in the native terminal, or even better, through a cygwin session.

Before being compiled with f2py, any Fortran code needs to undergo three basic changes, which are as follows:

  • Removal of all allocations

  • Transformation of the whole program into a subroutine

  • If anything special needs to be passed to f2py, we must add it with the comment string "!f2py" or "cf2py"

Let's illustrate the process with a simple example. The following naive subroutine, which we store in the primefactors.f90 file, performs a factorization...