Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning SciPy for Numerical and Scientific Computing Second Edition

You're reading from   Learning SciPy for Numerical and Scientific Computing Second Edition Quick solutions to complex numerical problems in physics, applied mathematics, and science with SciPy

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher Packt
ISBN-13 9781783987702
Length 188 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Toc

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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime