Python · Uncategorized

Scientific Python

From time to time students ask me what might be the best programming language for computational physics and numerical applications. For a long time my standard answer to this question was twofold: The language does not matter, learn C++!

On the one hand, I think one should not bother to much with this issue because at a very high level, popular programming languages are quite similar. If one has acquired a certain degree of competence in a specific language one will be able to learn another language quite easily. On the other hand, I really like  C++ and recommend to learn this language. As a compiled multi-paradigm language it gives me both high performance and the right degree of abstraction to write expressive code.

Recently, my standard answer to the question what may be the best programming language has changed slightly and became threefold: The language does not matter, learn C++ or maybe Python!

Strongly typed, compiled languages as C++ are not always the most adequate tool. For prototyping and data processing dynamically typed scripting languages may be more suitable. Thus, I played around with various scripting languages for numerical applications, starting with the write-only language Perl and followed by the beautiful but poorly documented (available English literature as compared to other scripting languages) language Ruby. I used and still use Matlab extensively, which is excellent for computational physics and engineering applications because it was designed as an interactive or scripting language for numerical applications. But because it is also an expensive closed-source software product I was looking for an alternative. Some time ago, I started to use Python which finally became my language of choice for prototyping and data processing in computational physics applications.

Python is an easy to learn multi-paradigm open-source language that supports object orientated programming as well as imperative and functional programming styles. What it makes particularly useful for computational science applications and distinguishes it from other scripting languages, however, is the large number of excellent actively maintained numerical and scientific libraries. Just to mention a few:

  • PyGSL provides a Python interface for the GNU scientific library.
  • NumPy adds a fast, compact, multidimensional array facility to Python.
  • SciPy (pronounced “Sigh Pie”) is an open source library of scientific tools for Python. SciPy supplements the popular NumPy module, gathering a variety of high level science and engineering modules together as a single package. SciPy includes modules for linear algebra, optimization, integration, special functions, signal and image processing, statistics, genetic algorithms, ODE solvers, and others.
  • matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in Python scripts, the python and ipython shell (ala Matlab or Mathematica), web application servers, and six graphical user interface toolkits and features full LaTeX support.
  • PyTables is a package for managing hierarchical datasets and designed to efficiently and easily cope with extremely large amounts of data. PyTables is built on top of the HDF5 library.

With these libraries it very easy to solve many common computational science problems in a few lines of Python code.

I plan to give some examples in a series of subsequent articles. Stay tuned! Readers who would like to learn more about Python in computational science applications are also recommended to read the May 2007 issue of  Computing in Science & Engineering which features several Python related articles. Finally, there are also some books on Python in computational science applications on the market.

  • Python Scripting for Computational Science by Hans Petter Langtangen, Springer, 2004
  • A Primer on Scientific Programming with Python by Hans Petter Langtangen, Springer, 2009
  • Numerical Methods in Engineering with Python by Jaan Kiusalaas, Cambridge University Press, 2010
  • Matplotlib for Python Developers by Sandro Tosi, Packt Publishing, 2009

Leave a Reply

Your email address will not be published.