[Tutor] On learning Fortran and C++ for scientific computing

Vick vick1975 at orange.mu
Sun Apr 12 08:25:54 CEST 2015


Hello,

 

Thanks for the answer.

 

I quite get the drift of your explanations. But my question was looking for
a specific practical answer.

 

As my codes involve a lot of mathematical formulae and some of them require
ultra-high precision, hence I was looking for an answer that either Fortran
is better suited or Python is better at it.

 

One of the example, I'm thinking about is the commoving distance of redshift
in the universe and hence to calculate the radius of the observable
universe.see Hogg's paper on it:

 

http://arxiv.org/abs/astro-ph/9905116

 

This is a mathematical integration of several parameters in cosmology. This
particular calculation requires the best precision you can get because by
reducing it, you get different answers for the age of the universe and hence
the radius of the universe.

 

Example:

S for integration

S 0 to z ..> dz'/SQRT(omega_radiation (1+z)^4 + omega_matter (1+z)^3 +
omega_curvature (1+z)^2 + Omega_dark energy)

 

S 0 to 1e+31....> 1/sqrt(.000086 *(1+z)^4 + .282 * (1+z)^3 - .000086
*(1+z)^2   + .718)  if you try this integration you will get completely
wrong numbers on computing devices that do not possess ultra-high precision
and accuracy.

 

I had to use mpmath with precision of 250 and integration method called Tanh
Sinh Quadrature with accuracy 120 to get the desired results. I checked the
answer with Mathematica and they concur. But the calculation is done in
about 3 seconds on my laptop with intel core i5 at 2.5 GHz with 4 GB RAM.

 

So can Fortran crunch 250 digits numbers in an integration formula under 3
seconds with the same computing parameters as above? Or is Python better at
it?

 

Thanks

Vick

 

 

 

 

-----Original Message-----
From: Laura Creighton [mailto:lac at openend.se] 
Sent: Saturday, 11 April, 2015 20:57
To: William Ray Wing
Cc: Vick; webmaster at python.org; tutor at python.org; lac at openend.se
Subject: Re: [Tutor] On learning Fortran and C++ for scientific computing

 

These days, most important scientific libraries are wrapped so that you call
call them directly from python. Google for "python bindings <name of the
library you want>" and see if you get any hits.  If you have a library that
doesn't have python bindings, you can probably make them.  Start reading
here:

 
<http://intermediate-and-advanced-software-carpentry.readthedocs.org/en/late
st/c++-wrapping.html>
http://intermediate-and-advanced-software-carpentry.readthedocs.org/en/lates
t/c++-wrapping.html

 
<http://scicomp.stackexchange.com/questions/2283/which-package-should-i-use-
to-wrap-modern-fortran-code-with-python>
http://scicomp.stackexchange.com/questions/2283/which-package-should-i-use-t
o-wrap-modern-fortran-code-with-python

 

to see about wrapping C++ and Fortran code.

 

But chances are somebody has already done it.

And for astronomical calculations, I just use pyephem.

 <http://rhodesmill.org/pyephem/index.html>
http://rhodesmill.org/pyephem/index.html

 

It is difficult, and usually pointless to argue with somebody who prefers to
use Fortran or C++ about using some other language.  People like what they
are used to.  Moreover, in the case of Fortran and C++, these languages are
significantly harder to use than Python, where harder, in this case, means
-- the code you write is more prone to errors.

 

Also, it is very easy to write inefficient programs in C++ or Fortran, and
scientists, who are not professional programmers are extremely prone to
doing this.  So while a Fortran or C++ library may be fast (if the library
author knew what he or she was doing, or the community has fixed
inefficiencies over the years) that doesn't mean that your own C++ or
Fortran code is guaranteed to be fast.

 

And, of course, if it takes you 5 times as long in your personal time to
write the code, do you actually care how fast the result runs?  For very
long running calculations, where time is money, the answer may be yes.

But if all you want to know is where Uranus is going to be next Thursday so
you can point your telescope at it, not a bit.

 

If you need something that runs faster than the standard version of python,
you may be able to get it with numpy and the other scientific tools here:
<http://www.scipy.org/> http://www.scipy.org/ or with pypy
<http://speed.pypy.org/> http://speed.pypy.org/ .

The number of problems where you can legitimately claim that you badly need
the speed that C++ and Fortran provide is shrinking all the time, as
machines grow faster.  Plus, in modern computing the new challenge is to use
multiple cores.

 

It's a very hard problem, and all the old C++ and Fortran libraries were
created with the assumption that there is only one core.  Right now we don't
actually know how to use multiple cores efficiently -- it is hot research in
computer science right now, with no particularly good answers.

But once we figure out how to do this, all the old C++ and Fortran libraries
are probably going to have to be rewritten with this in mind.

(A solution that doesn't require such re-writing would, of course be highly
desirable, but things are not looking very good on that front.)

 

If your real question is 'would it be desirable to learn C++ and Fortran
from a historical perspective, to get an understanding of how important
computational libraries in my field are structured', then I am the wrong
person to ask.  I knew both of these languages before there was a Python.

I think learning these langauges will indeed make understanding the old
libraries easier, but _using_ these languages to do real work isn't needed.
Or rather, if you are one of the people who needs to use them, then you
would already know that you are, and why, and wouldn't be asking the
question.  The world is full of things to learn, so you need to do your own
prioritisation of Fortran vs everything else you haven't learned yet. :)

 

Best of luck,

Laura Creighton

 

p.s. I gave this topic a more descriptive subject line than 'Hi' but it
should still thread with the original.



More information about the Tutor mailing list