Python for Fortran programmers III: Learning Python

Learning Python is easier than learning Fortran. First, because the language has a simpler syntax and its interactive behaviour makes errors easier to catch and more understandable. Second, because there are lots of resources, many of them free: tutorials, books, etc.
These two interactive web-pages are a good example of the type of help you cannot find with Fortran:

That said, at some point you might feel dismayed by the size of the language. Python is much larger than Fortran. Both the core language and the available extensions. You’ll have to assume that, unless you become too hooked on it, there’ll be many areas of the language that you will – and can – ignore. With the risk of blasphemy, I dare say you can write hundreds of useful scripts completely ignoring Object Oriented Programming.

The number of available packages is also overwhelming. Take, for example, the calculation of the square root. In most tutorials you are told to import the math module which contains a sqrt function. math.sqrt will give an error for negative arguments. math.sqrt only returns floats. If you want deal with complex numbers, you need to import cmath. But cmath.sqrt always returns a complex number, even if its imaginary part is zero. All this is similar to the Fortran sqrt function (which implicitly calls two different functions for real and complex arguments). But with python you can both have your cake and eat it. There is a third sqrt function which returns a complex number only when the argument is negative. It can be found in the numpy.lib.scimath module. This third function can be useful to you, but you can also live without it. Getting used to ignoring all these potentially useful features is part of the learning process. Eventually you will remember and look for the ones you really need.

One last confusing issue is which Python version to learn. As with Fortran, the newest versions are better, but the problem with Python is that versions 3.x are not backward compatible with the wide-spread versions 2.x. My recommendation is that you learn python 3.x. Mainly because it is the future. Most packages have already been translated into Python 3. I think installing python3, numpy, scipy, sympy, and ipython is enough to start using python in scientific problems. Make sure you install these packages for python3. Each python version can host its own modules completely independently. If you are using an older distribution, you can also install them quite easily with pip. Of course, things can get more complex, as Konrad Hinsen reports in his interesting blog.

Despite their incompatibility, Python3 and Python2 are not that different. Probably, they are less different than FORTRAN77 and Fortran 2000. The most significant differences that you will find are that print() is a function in Python3 (it uses parenthesis) and that input() works differently.

And if you start programming, please do it in a nice way.


Python for Fortran programmers II: why Python?

Python is a general-purpose language, used in extremely different fields. Take a look at Many of the projects are available at the Python repository PyPI. That means the language is active and adequate for many applications. But of course, we want it to be also good at number crunching and data visualization.

For that you need some packages. Packages are extensions of the core language, a kind of library in Fortran. They need to be imported before they are used. Some packages are a must for scientists: numpy, matplotlib and possible, scipy. Installing python packages is easy. I will explain that in the future, but these three packages are on most Linux repositories (certainly in Ubuntu) and that is the simplest way to install them.

Because python is an interpreted language (It’s gonna be very slow!! Wait, wait…) you can use different ‘shells’. I recommend iPython. That, together with the previous packages, turns python into a powerful scientific development tool. If you have time (I promise to keep this post short) watch this amazing talk by Fernando Perez, the author of ipython:!

If you are still not convinced, take a look at this survey which compares Python to Fortran:

Convinced? Then start by typing import this and start absorbing the Zen of python. Then impress your colleagues by defying gravity with import antigravity (only in Python 3). Aha! You look more pythonic now…

If you are new to Python and want to install it you will have to decide whether to use Python 2 or Python 3. In the next post we will see how to make this decision. The short answer is ‘use Python 3’.

Python for Fortran programmers I: why?

Being trained as a scientist (as I am), you have a certain probability of having been taught Fortran. Are there reasons to try to learn another language? And why Python?

I use computers all the time. But most of the computationally intensive tasks that run in my cluster are performed by highly optimized codes that I have not written: Gromacs, Orca, Gaussian, etc. What I usually need to do is to play with the data these codes generate. Here is where I frequently need to code something. But playing with the data rarely involves only number crunching. Okay, sometimes I may need to do an interpolation, or a least square fitting, but much more often I need to graph the results, or parse software outputs to get these data. So, should I use Fortran?

For the number crunching, a naïve answer would be ‘yes’. I disagree. If what you want to do is a common task, such as the ones mentioned (or a diagonalization, or a minimization, or…) you can probably find packages or libraries where that is already implemented. So why reinvent the wheel? You can find that in Fortran, but you can find that in Python too. And the installation, the documentation and the ease of use will be better in Python. Do you want an example? I need the eigenvalues of a matrix stored in a text file ‘a.dat’. And you only have python installed. Nothing else.

$ sudo apt-get install python-numpy
$ python
>>> import numpy 
>>> a=numpy.loadtxt('a.dat') 
>>> numpy.linalg.eigvals(a) 
array([ 16.13476875,  -8.29479695,  -0.3399718 ])

Now, beat that with Fortran… Of course, sometimes you’ll have to code your computationally intensive task –we are scientists, we are supposed to be doing new things–, and Fortran is an efficient language for that.

But most of the time I am not solving linear equations. Most of the time I am struggling to get the energies of a set of outputs and correlate that with distances that are in a set of trajectory files. Or to modify tens of input files in a systematic way. Or to plot these results automatically, without manual copy&paste to a spreadsheet. And for that, Fortran is almost useless.

As John D. Cook puts it: “I’d rather do math in a general-purpose language than try to do general-purpose programming in a math language”

I am not saying you should abandon Fortran. I am just advocating for another tool that can ease many of your common tasks.

Getting started

This is my first entry in my brand new blog. I’m a bit afraid of this new adventure…

The idea of this post is to share some of my research experience and results. I’ll start with several posts on python tips for Fortran programmers. This is because I am now preparing a course on that, and because I haven’t found that much information on this subject on the web. There are plenty of great Python tutorials, but few assume you are an experienced Fortran programmer. This is what I was (or I am) and there are several computational chemists like me, who could profit from learning python. So, let get started…