[Tutor] Hi

Steven D'Aprano steve at pearwood.info
Sat Apr 11 15:41:13 CEST 2015


Hi Vick, and welcome!

My replies to your questions are below, interleaved with your comments.

On Sat, Apr 11, 2015 at 04:32:38PM +0400, Vick wrote:

> However I recently talked to a guy online and he told me the following,
> which actually intrigued and surprised me:
> 
> "The vast majority of numerical codes in science, including positional
> astronomy, are written in Fortran and C/C++.  If you wish to use these codes
> in minority and less efficient languages such as Python and VBA, learning to
> translate this code into those languages is a skill you will have to
> acquire."
[...]
> But his statement is insinuating that Python is inferior to Fortran as a
> mathematical tool and that all of the scientific community prefers to use
> Fortran.
>
> My question is simple: Is he right or wrong?

He is partly right, partly wrong. But mostly wrong. Twenty years ago, if 
you wanted to do scientific computing, C and Fortran were essential. 
They are still very important, but they are no longer essential.

There are many professional-quality mathematical libraries. Older 
libraries are nearly all written in C/C++ or Fortran. Some are older 
libraries are written in Pascal, but because few people use Pascal any 
more, you won't find many of them still in use. Some newer libraries are 
written in Java. In the future, I would expect new scientific libraries 
to start coming out written in Julia:

http://julialang.org/

Although these libraries are written in C or Fortran, you don't need to 
program in those two languages to *use* those libraries. Most scripting 
languages like Python, Lua or Ruby include ways to call code in C or 
Fortran libraries as if they were written in Python (or Lua, Ruby, and 
so forth). We call languages like Python a "glue language", because it 
is very good for "gluing together" code from different libraries. You 
want to use this library for planetary orbit calculations, written in 
Fortran, and that library for calculating lunar eclipses written in C, 
so you use a Python script to bring them together.

Many mathematicians, statisticians and scientists use products such as 
Mathematica, Matlab, R, SAS, Minitab, Maple, and similar. These are all 
good products that have been around for a long time, with their own 
strengths and weaknesses.

One Python-based product which is becoming widely used in scientific 
circles is Numpy, together with its sister-library Scipy. The core 
numeric routines in Numpy and Scipy are mostly written in C and Fortran, 
but the interface is designed for use with Python.

http://www.numpy.org/
http://www.scipy.org/

Another Python product is SAGE, which aims to be a free alternative to 
Maple, Mathematica and other similar products.

http://www.sagemath.org/

The idea is to create a group of powerful maths, statistics and 
scientific libraries. Those libraries can use whatever language they 
want, and often they are written in C and Fortran, but the glue that 
holds them together is Python. Together, they combine the power of 
low-level, hard-to-use but powerful fast languages like Fortran and C 
with the ease of use and convenience of modern high-level languages like 
Python.

The core of scientific computing with Python includes:

Numpy - fast N-dimensional array calculations
Scipy - scientific computing libraries
Matplotlib - 2D graphing
IPython - enhanced interactive console with Mathematica-like notebook
Sympy - symbolic mathematics 
Pandas - data analysis
SAGE - integrated scientific computing system

Parts of these are written in Python, and parts in C, Fortran, 
Javascript, and potentially any language imaginable.

I also expect that people can start using PyPy to run pure-Python code 
almost as fast as C:

http://pypy.org/

Or they can use Cython to write C code using Python syntax:

http://cython.org/
‎
All of these are parts of the Python ecosystem, and are why Python is 
currently becoming the "go-to" language for scientific computing. Where 
scientists used to write code in Fortran (hard and slow to write, but 
fast to run) or expensive proprietary products like Mathematica or 
Matlab, many people are gradually moving towards the Python ecosystem of 
Python, Numpy, IPython notebooks, and others.

This post explains Python's advantages over Matlab, why the author 
thinks that scientific computing will continue to move towards Python, 
and links to a number of other good articles from people who already 
have done so.

http://cyrille.rossant.net/why-using-python-for-scientific-computing/


My advice if you want to be a good scientific programmer:

- Stay with Python. Anyone who tries to tell you that you need to know 
  how to program in Fortran or C to do scientific computing is wrong.
- Learn how to program in Python as well as possible.
- Learn when *not* to program in Python, and know when to use existing 
  tools already written in C, Fortran or other languages. Chances are
  very good that Numpy, Scipy and others above already solve some of 
  your problems.
- If you really need high-speed computing power that Python alone 
  cannot provide, consider PyPy, Cython or Numba first. If and only if 
  they still don't provide what you need should you bother learning
  C or Fortran and using those.




-- 
Steve


More information about the Tutor mailing list