Which programming language is best for computational physics?

In summary, the conversation revolved around the best programming language for someone interested in computational physics. Suggestions were given for MATLAB, C++, Java, C#, and Python, with the consensus being that it is more important to learn general programming skills rather than just focusing on one specific language. It was also mentioned that learning one language well can make it easier to learn others.
  • #1
frug
7
0
I am new to programing in general, and I want to learn a language that will be useful to me as a physicist. I am currently looking mostly at C++, but Java and C# also seem like useful tools. I don't want to waste my time studying anything that I won't use. Any suggestions from those of you who have experience doing computational physics? I have Mathematica as well, but right now I am interested in learning an actual language. Thanks!
 
Physics news on Phys.org
  • #2
Have you considered matlab? It's almost the same as c++. In college I knew a lot of electrical engineers on computer tracks who used MATLAB extensively. In general, Java seems to be more for web designers, and is easy to pick up anyway.

If you're good with code then once you become fluent in one language you can learn others in no time at all. It won't be a life altering decision for you if you pick a bad language.
 
  • #3
mrmiller1 said:
Have you considered matlab? It's almost the same as c++. In college I knew a lot of electrical engineers on computer tracks who used MATLAB extensively. In general, Java seems to be more for web designers, and is easy to pick up anyway.

If you're good with code then once you become fluent in one language you can learn others in no time at all. It won't be a life altering decision for you if you pick a bad language.

That is true to an extent, but learning to code MATLAB well can be difficult for people entrenched in looping logic (from having learned c++ and the like).

I'd recommend learning c++ then MATLAB. For engineering, you don't need to go very in depth in either. For c++, just buy a nicely rated textbook that introduces the topic (and surf the web for coding projects to test your skills by googling things like "introduction to c++" "lab" site:.edu). Then, for MATLAB, you can use a quick introduction to it like this one:

http://web.eecs.utk.edu/~roberts/WebAppendices/A-MATLAB.pdf

You barely need to learn anything more to play with MATLAB well after having learned C++. You just need to learn how it manipulates matrices and about its massive collection of built-in functions and its minor differences in syntax/program creation (which should be covered in that .pdf).
 
  • #4
frug said:
I don't want to waste my time studying anything that I won't use.

If you have some definite task or project that you want to do right now, then learn whatever language seems the best one to do it in (or whatever you are required to do it in).

On the other hand, if you want to learn "programming in general", you should be focusing on "learning to program" not "learning language XYZ". If you are programming as part of your work you will probably finsih up leraning a new language every 2 or 3 years anyway, so what matters is learning the transferrable skills that apply to any language, not the details of whatever is popular right now.

Ideally you want to choose a language that makes it easy learn good habits rather than bad, supports modern software design practices, doesn't have a lot of historical "baggage" which gives you 57 different ways to do everything because of backwards compatibiltiy with stuff that became obsolete 20 years ago. You also want a good library of standard algorithms and data structures, so you get the idea that programming is NOT about endlessly "re-inventing the wheel" but re-using wheels that already exist.

I would say Java or C# score better on those criteria than C++. You might also look at Python. In fact learning say C# and Python in parallel might be better than learning a single language, because that will avoid the trap of thinking there is only "one right way" to do things, which will be a hindrance when you need to learn your third, or fourth language.

I've lost count of hte number of languages I have used for work over the years - the number is well into double figures, and I've completely forgotten most of the language-specific details about the ones I haven't used for 10 or 15 years. But programming is still programming, whatever language you do it in.
 
  • #5
frug said:
I am new to programing in general, and I want to learn a language that will be useful to me as a physicist. I am currently looking mostly at C++, but Java and C# also seem like useful tools. I don't want to waste my time studying anything that I won't use. Any suggestions from those of you who have experience doing computational physics? I have Mathematica as well, but right now I am interested in learning an actual language. Thanks!

Here is the truth.
If you learn to program in ONE language very well, you'll find learning and
programming in any other language comes to you very easily.

It's not the language that is hard to learn, but it is the

programming logic
flow of a program
sequence of programming instructions
how to use functions, subroutines, etc (they are called by various names)
etc.

These are the backbones of all programming languages. My final words are
if you learn C or C++ or Java very well, you'd be able to tackle any other
language with little efforts.
 
  • #6
Neandethal00 said:
Here is the truth.
If you learn to program in ONE language very well, you'll find learning and
programming in any other language comes to you very easily.

It's not the language that is hard to learn, but it is the...

I think this is very true.

For this reason, if you are starting programming, I think the things you should be asking are:

- How good a job will this language do of introducing me to the things I need to understand in programming?
- Will this language "grow with me"? That is, as I get better with the language and want to do more complicated things, will the language start to feel confining?

The language I actually recommend to people as a "first language" at this point is Python.

- On the first point, Python allows you to get started with simple things very quickly, but has enough power that you can still learn important "deeper" concepts. C++ forces you to learn a lot of complicated low-level things, like memory management, before you can do a lot of the most basic things. Java on the other hand forces you to understand a lot of complicated high-level object-oriented programming theory ideas before you can do your basic things-- you cannot write your fist Java program without having to confront the question of "what does 'public static void main' mean?" Neither of these things are really the healthiest first thing to confront when you really want to be learning the most basic things about breaking down a task.

- Python is what I would think of as "scalable"-- it works in lots of different environments. Any language, you'll find, have domains where it works well and domains where it doesn't work. Python's domain is startlingly large. Learn, say, Perl and you'll find it's amazing for small programs but you run into big problems when you want to write something large and complex. C++ will work great for high-performance stuff and can run absolutely anywhere, but gets ungainly when you are trying to do certain kinds of high level things-- try to find someone who uses it for web programming, for example. Java is difficult to deploy in many situations; C# less so, but if you write in C# you'll at some point probably have to address the problem of "...and I have to get this fifty-megabyte runtime onto this person's computer before they can run my program". (Note: Deploying Python for executables that you distribute to other people will sometimes give you this sort of headache also, but if you learn some tricks I think it's a little more manageable). Python can run in a lot of different weird situations, like it can actually run inside of the Java or C#/CLR runtimes. Basically if you start off doing python, it is a good idea to someday learn other languages like C++ or Java but you'll actually find you can if you really want to use Python for whatever your problem is.

Incidentally I have some personal objections to C# but I think it gets pretty good scores on both of my two criteria here.
 
  • #7
I'd choose what language to learn based on what language your expected/favorite school's introduction to coding course uses. You may as well kill two birds with one stone.

e.g.
ece206 [intro to programming] uses c++ here
 
  • #8
Wow! Thanks for all of the great advice. I am working mostly with C++ as some of you have suggested. I do have interest in Matlab, but I don't have interest in paying for it right now, haha. But some responses may be getting away from my original question, or maybe I wasn't clear enough. I am focused mainly on programming geared toward problem solving in the sciences and engineering. I am not doing any web development or software development (at this time).

While Python has been presented to me before as a great place to start, I have not heard of it's applications in science, but I welcome anyone to change my mind on the matter. As of now, I like the idea of starting with C++ (I have a few great books!) and moving into Matlab later. I have also contacted (upon suggestion) the CS department of my school to determine which language is used in the classes I may encounter as a physics major.

I have also heard that CERN is converting all of it's FORTRAN files to C++ which is a mark in the pros column for C++ also.

Again, thanks to all, and I welcome anymore input!
 
  • #9
You might find Java easier to deal with, but similar to C++ in many respects. Probably one of the more important things you ought to learn if you're going to be dealing with some big code projects is object-oriented programming. Both Java and C++ are OOP languages, but Java doesn't require as much memory management stuff and might be less frustrating to work with as a beginner. MATLAB is sort of like the grandchild of FORTRAN, but FORTRAN is still used a lot (and isn't an OOP language, nor does it require memory management from the programmer) so it's a good idea to learn that as well
 
  • #10
As someone who does a lot of computational physics, PERL is VERY commonly used (at least in my department). I used C once or twice early on, but found data manipulation a lot easier in PERL.
 
  • #11
frug said:
I am new to programing in general, and I want to learn a language that will be useful to me as a physicist. I am currently looking mostly at C++, but Java and C# also seem like useful tools. I don't want to waste my time studying anything that I won't use. Any suggestions from those of you who have experience doing computational physics? I have Mathematica as well, but right now I am interested in learning an actual language. Thanks!

I would focus on learning just programming in general. Yes, a language is necessary for that, but learning the basic concepts will apply almost universally. For example, once you understand the concept of iteration, all you have to do to learn it in a new language is just google "iteration in X," where X is the new language.

That said, for it's ubiquity and coverage of almost all useful paradigms, I recommend C++. Get a good book or tutorial, and follow it IN ORDER. Personally, I believe I learned everything I need to know from http://www.cprogramming.com/tutorial.html#c++tutorial.

EDIT: About C# and Java, I can only speak from experience about Java. I took AP CS, which uses Java, this semester, and found my knowledge of C++ over-prepared me for the class. I went through the whole thing without paying attention to a single lecture and got an A in the class. My conclusion: learning C++ well will prepare you to do well with Java with a nearly non-existent learning curve. About the only part that was reasonably different was that Java is more restrictive (no overloading operators, no root namespace methods/functions, etc.) and the semantics of Java references.)

[NOTE: I don't have a degree, nor am I in a degree seeking college program (I've taken a few classes through dual enrollment, though), but I believe I've self learned enough to make a useful suggestion. But, take my opinion with a grain of salt.]
 
Last edited:
  • #12
n1person said:
As someone who does a lot of computational physics, PERL is VERY commonly used (at least in my department). I used C once or twice early on, but found data manipulation a lot easier in PERL.

Really? What kind of work do you do? I've never heard of anyone using PERL (in fact, I don't know too much about the language and how it works at all). Why use it over something else? I'm not sure what languages are comparable to PERL (C, Python, ?).
 
  • #13
I mostly do light curve analysis of non-variable stars, calibration of telescope using field stars, and soon some general R.R. Lyrae stuff.

As far as using it compared to something else, I find it very easy to read in data from a text file, and pretty simple to do various operations on arrays.
 
  • #14
Also one good and bad thing about C++ is that you can never completely master it. I've been programming C++ for 23 years, and I still haven't learned all of it.
 
  • #15
I think C/C++ would be the way to start, but I think Matlab is very easy as well
and it is much more easy if you want to plot results in graphs or to make graphics
or sound. Also MATLAB have a lot of functions for math, i.e. FFT etc.

But like someone said above, if you know one language the rest are easy to learn.
 
  • #16
Definitely start out with Python. It's helped immensely in my understanding of C.
 
  • #17
hadsed said:
Really? What kind of work do you do? I've never heard of anyone using PERL (in fact, I don't know too much about the language and how it works at all). Why use it over something else? I'm not sure what languages are comparable to PERL (C, Python, ?).
Perl is great for small numbers of large computational tasks because it has build it bignum support. (Obviously, it may not be a good idea to use it for large tasks, like fluid dynamics, n-body problem, etc. For example of a reasonable scale, I've used it to generate a list of the first 1,000,000 primes in a couple minutes.) It also was the first language to have build in support for regex. Other than the nice features, it's just another C-like scripting language copycat, with some variation.
 
  • #18
A big +1 for python. Just discovered it recently and for a scripting language it is deceptively powerful. Think of it this way, python was written by a mathematician. It has built in complex number support, fraction support, big number support among many others and is great for "glue" code if you need the speed of compiled C. If not for anything else it is unbeatable (imho) as a prototyping language. Alternatively my friend doing research in CFD swears by Fortran for heavy number crunching.
 
  • #19
Seriously, since you are a physicist I assume you wish to do some heavy calculations. If that is the case then forget about python and the such, you want a compiled language as they are much faster. Even though it is archaic, FORTRAN is still the fastest language to program in for physics applications. With it you also have the awesome numerical libraries such as LAPACK or ARPACK at your disposal. Also FORTRAN can have unlimited digit precision.

Personally, I program in C, it is about 50% slower than FORTRAN but is it much more mature and easier to deal with. For diagonalizations and the such I found that GSL was good although it is less optimized than LAPACK and so you see another 4-fold decrease in speed. When I do GUI programming though I like to use C++ stuff or when memory allocation is needed because with the vector class you don't need to free memory. C++ is slower than C though so again you sacrifice speed for functionality. Python is just terrible (check this: ).

So really for mathy stuff you should use FORTRAN or C/C++, interpretted languages are too slow. Since C or C++ are time tested and can do it all they are good choices although if history teaches us anything, you will see that nearly all quantum codes and solid state physics codes are written in fortran (http://en.wikipedia.org/wiki/List_of_quantum_chemistry_and_solid_state_physics_software) for the simple reason that it's the fastest.
 
Last edited by a moderator:
  • #20
frug said:
I am new to programing in general, and I want to learn a language that will be useful to me as a physicist. I am currently looking mostly at C++, but Java and C# also seem like useful tools. I don't want to waste my time studying anything that I won't use.

In any real world programming environment, you'll use everything. The good news is that if you are an excellent Java programmer, it's not hard to be a decent C++ programmer and vice versa.

What's important is not the specific language, but getting general programming skill.
 
  • #21
Spectrosc said:
Seriously, since you are a physicist I assume you wish to do some heavy calculations. If that is the case then forget about python and the such, you want a compiled language as they are much faster.

What happens in the "real world" is that people use C++/C/Fortran to create python models and then use python as "glue" to put them together. Check out numpy/scipy. They write the math heavy stuff in C/Fortran, and then you can call that with python.

Even though it is archaic, FORTRAN is still the fastest language to program in for physics applications.

No it's not. The best optimizing C++ code are as fast as FORTRAN.

With it you also have the awesome numerical libraries such as LAPACK or ARPACK at your disposal. Also FORTRAN can have unlimited digit precision.

Same for C++. You can call fortran routines for C++. Also in order to get top performance from LAPACK, you have to use optimized routines that are written in machine language. You can also get unlimited digit precision with C++ libraries from boost.

Personally, I program in C, it is about 50% slower than FORTRAN but is it much more mature and easier to deal with.

It really shouldn't be slower.

C++ is slower than C though so again you sacrifice speed for functionality.

Not true. If you use template/functor so that the compiler can vectorize inner loops, then you can end up with C++ code that is faster than C.

Python is just terrible

If you do everything in python sure. But what you do is to structure the code so that all of the time critical parts are done in compiled fortran code.

although if history teaches us anything, you will see that nearly all quantum codes and solid state physics codes are written in fortran (http://en.wikipedia.org/wiki/List_of_quantum_chemistry_and_solid_state_physics_software) for the simple reason that it's the fastest.

Fortran had a speed advantage until around 1995, when people figured out how to write optimizing compilers and fast numerical code in C++. Fortran is a dying language, because people are tweaking and improving C++ whereas no one is doing the same for Fortran.

When you are writing numerical code, one thing to remember is that ultimately you are writing in machine language, and the language is just a short-hand to get you to the machine language.
 
  • #22
twofish-quant said:
What happens in the "real world" is that people use C++/C/Fortran to create python models and then use python as "glue" to put them together. Check out numpy/scipy. They write the math heavy stuff in C/Fortran, and then you can call that with python.

As someone who does a fair amount of scientific programming, I need to say twofish is right on the money here. Write your code in Python, then use C/C++ to speed up the computationally intensive stuff. This is the quickest way to get to a working application. I would recommend learning Python first, then use this as a base to extend to C/C++. I find the combination of Python, C/C++, and Mathematica does everything that I need.
 
  • #23
I guess then that if you were to use only a single language then python would not be a good choice although it may be good to mix it up as you say. For small parts of your research sometimes it's better to focus on one language though, especially if you don't know the others. I like C/C++ for that since you can do pretty much everything in it.
 

1. What is the most commonly used programming language for computational physics?

The most commonly used programming language for computational physics is Fortran. It has been the go-to language for scientific computing for decades due to its efficiency and high performance.

2. Can other programming languages be used for computational physics?

Yes, there are other programming languages that can be used for computational physics, such as Python, C++, and MATLAB. These languages have libraries and packages specifically designed for scientific computing, making them popular choices among researchers.

3. Which programming language is best for beginners in computational physics?

For beginners in computational physics, Python is often considered the best choice. It has a relatively simple syntax and a large community, making it easier to find resources and support when needed.

4. Is it necessary to learn multiple programming languages for computational physics?

It is not necessary to learn multiple programming languages for computational physics, but it can be beneficial. Different languages have their own strengths and weaknesses, and being familiar with multiple languages can give researchers more options and flexibility in their work.

5. How important is it to have programming knowledge for computational physics?

Having programming knowledge is essential for computational physics. It allows researchers to write and modify code to solve complex problems and analyze large datasets. Without programming skills, it would be difficult to perform advanced simulations and data analysis in this field.

Similar threads

  • STEM Academic Advising
Replies
12
Views
1K
  • STEM Academic Advising
Replies
3
Views
817
  • STEM Academic Advising
Replies
3
Views
108
  • STEM Academic Advising
Replies
10
Views
1K
  • STEM Academic Advising
Replies
16
Views
2K
  • STEM Academic Advising
Replies
6
Views
1K
  • STEM Academic Advising
Replies
3
Views
916
  • STEM Academic Advising
Replies
3
Views
1K
  • STEM Academic Advising
Replies
16
Views
365
Back
Top