Best programming language for physics

In summary, the best programming language for physics is Python. It is a versatile and powerful language that is widely used in scientific computing and data analysis. Its user-friendly syntax makes it easy to learn and it has a large community of users and libraries dedicated to scientific applications. Additionally, its integration with popular scientific tools and languages, such as MATLAB and Fortran, makes it an ideal choice for physicists.
  • #1
Crake
66
1
Hello,

I'm not sure if this is the right place for this thread, but I think it is (This forum is for computational physics as well)

So, my question is quite "soft". What is (in your opinion, obviously) the best programming language for physics? Also, what programming language is most used in physics?

Nowadays, if you had to learn a new language, as your first programming language, what would you pick?
 
Physics news on Phys.org
  • #2
This question is asked about once a week. I suggest a forum search.
 
  • #3
BASIC is a friendly, easy to learn language with simple variable definitions and statements.

If this is for intensive calculations like modeling/simulation, "C" would be more appropriate to the task.

Perhaps I'm showing my age when I say, if there's a lot of math (not meaning reiterative), I'd consider FORTRAN. :)
 
  • #5
The two principal languages would be Fortran (F90 or later) and C++. These days, object-oriented programming is widely used. There is a huge volume of legacy codes written in earlier versions of Fortran, particularly Fortran 77, and more recently C++.

http://www.dmoz.org/Computers/Programming/Languages/Fortran/Source_Code/Physics/

Most physics problems involve solving systems of differential equations, both ordinary and partial, and in many cases, non-linear.

Python is often used to support programs written in Fortran or C++.

Then there are packages like Matlab, Mathematica, Maple, . . . .

Linking to other threads is useful.
 
  • #6
There is also Java with the Open Source Physics toolkit (www.compadre.org/osp). It comes with many examples of how to do a variety of different simulations and its open source. Programs are developed using Eclipse or Netbeans.
 
  • #7
Python is probably the most user friendly and intuitive language for beginners IMO.

It's relatively weak when doing numerical calculations, which is why people use it to implement numerical algorithms in Fortran or C++ (both strong in that area).
 
  • #8
I read from one book that Python also comes with several different implementations, one is PyPy and NumPy. The latter is said to be quite good in numerical computation while the former is faster than the standard Python.
 
  • #9
The first language I'd recommend you learn is C. That makes learning C++ rather simple, and you can focus more on OOP than syntax. I'm not sure how I feel about python, it just doesn't perform all that well unless you invest a lot of time in it, but it is open. I'm not a fan of Java at all.
 
  • #10
In answer to the last question of the OP, I would choose Scala. It seems general purpose languages are moving toward functional programmings in conjunction with OO.

When you look at legacy systems, the oldest apps are written in FORTRAN, more recent ones are in C++ and the latest ones are in Java. I see this trend moving towards Scala in the coming years. What makes Scala so appealing is its ability to work well with Java libraries while at the same time bringing a new way of thinking about software design...
 
  • #11
Start learning programming with python then move on to another lower level language if you need it. Python will allow you to do the most without needing to write the same code again and again. You can easily use modules to replace other programs like MATLAB etc.

The older guys use fortran and c++. The younger people use python,java,and c++. You can do GPGPU in python and java if you need it or move down to lower level c++. There is no point at getting down lower than C++ because compilers are optimized pretty well to the point that your java code won't lose a significant amount of performance relative to a lower language. There is a point of diminishing returns and if there wasnt we would all be writing in Assembly which physicists usually dont.
 
  • #12
And the professionals don't bother to argue about stuff like this, they choose the best language to get the job done and then go home to do other things...
 
  • #13
jedishrfu said:
And the professionals don't bother to argue about stuff like this, they choose the best language to get the job done and then go home to do other things...
Adding my two bits to what you wrote: There is no single language that stands out as the "best programming language for physics". A good programmer knows several languages. The old adage "learn a new language / tool every year" is more applicable now than ever.

We get these kinds of threads on a very regular basis, and they invariably decay into a religious war over computer languages. From what I've seen, the detractors of language XYZ (pick your language) invariably do not know what they're writing about.
 
  • #14
jedishrfu said:
And the professionals don't bother to argue about stuff like this, they choose the best language to get the job done and then go home to do other things...

thats not actionable advice.

D H said:
From what I've seen, the detractors of language XYZ (pick your language) invariably do not know what they're writing about.
Different languages have their strengths and weakness which means some languages have features which make detractions reasonable. Assembly has trade offs when compared to Python is a simple example of such tradeoffs.

The simplest answer is to learn the programming language that the people around you know so you could have someone to look for answer to questions.
 
  • #15
I don't know if this is helpful but in my experience the best coding language is often going to be dictated by the group you are in and what language that group writes their codes in. My group wrote theirs in C++ so I needed to learn C/C++. Also in my brief experience it seems it is important to be able to competently code with one language of ones choice (at minimum) but be able to read code in other languages.
 
  • #16
The languages most often used are still FORTRAN and C/C++ so, as a general answer I would tell a physicist wannabe to learn those. If you have a specific group you know you will be working with them ask them what language they use and follow their lead.
 
  • #17
Are people really serious when they advocate FORTRAN of all things as anyones first or primary language? Like really?
 
  • #18
Gauss M.D. said:
Are people really serious when they advocate FORTRAN of all things as anyones first or primary language? Like really?
Really. The question wasn't the best language(s) to learn in general. The question at hand is what are the best language(s) for physics, and Fortran (note the spelling) most certainly is in that category.

Note well: I am not speaking as a Fortran aficionado. I personally haven't used it for 20 years. There are still times when I regret the switch to C/C++. Fortran is quite widely used for physics and related applications where sheer number crunching power is paramount.
 
  • #19
D H,
even in Fortran[1] is still widely used, most of the applications occur either in legacy codes or in codes directly depending on other Fortran codes. And it is one of the few languages which one can truly classify as "bad" without further qualifying this statement. It is not even only the language, the language implementations are also sub-par: Take five different compilers, and you will find five different subset of the Fortran 95(!) standard which work as standardized.

While being able to read and write Fortran can still be important, I cannot see how it could possibly be a good idea to use this language for starting programming. Many *very* important idoms in programming, including very basic ones (like, compound data structures) simply cannot be expressed in any meaningful way in this language. Starting programming with this is bound to create many problems when later moving to more powerful languages. However, the other way around (say, starting with Python and C++ or Java and then using Fortran if you really have to) does not have this problem at all.

([1] and it used to be spelled in upper case)
 
  • #20
Fortran is mostly around as legacy code or code written by some theorist in high energy and it is losing market share among physicists. Programs are being rewritten to other languages like C++, an example of which is Pythia which used to be in FORTRAN in early versions.

ROOT is in C++. CMSSW is C++ based.

GPGPU is done using CUDA and OpenCL which the base libraries are done in C++.

Matplotlib is used by physicists in many fields to generate graphs and can be used to do GPGPU using pyopencl.
 
  • #21
There are numerous programming languages useful for Physics. More importantly, it depends on the level of physics you are doing and how much previous programming experience you have. Fortran is a very old yet popular language among physicists. One language that I particularly like is Mathematica. It is used by numerous people for advanced mathematics, physics etc. Other options include Python for basic projects, C++ or Java for more large-scale endeavors.
 
  • #22
It's true that fortran is around mostly in legacy code but some that code will still be around when you plan to retire in twenty years or more. Many physical models are written in fortran and thoroughly tested and entities that depend on these models would rather port them than rewrite them in a more modern language.

When I worked at GE some years ago they were still using this ancient machine that ran one application at a time rather than rewrite the software because it ran some sort of stock maintenance program. We thought it was so silly but nevertheless the management was adamant that it be used. It was so important that they were afraid that they would lose their reputation if they screwed up a run by rewriting it. It had a lot of complex business logic in it and its results were highly visible.
 
  • #23
jedishrfu said:
It's true that fortran is around mostly in legacy code but some that code will still be around when you plan to retire in twenty years or more. Many physical models are written in fortran and thoroughly tested and entities that depend on these models would rather port them than rewrite them in a more modern language.

When I worked at GE some years ago they were still using this ancient machine that ran one application at a time rather than rewrite the software because it ran some sort of stock maintenance program. We thought it was so silly but nevertheless the management was adamant that it be used. It was so important that they were afraid that they would lose their reputation if they screwed up a run by rewriting it. It had a lot of complex business logic in it and its results were highly visible.
In a business environment like GE you will have different incentives at play. COBOL is still used in some business settings and will be still used in some business settings in 20 years from now but it will be less common than now.

In physics there isn't the same incentives to stay at the status quo especially in a research environment hence why rewrites like the one for PYTHIA to go from FORTRAN to C++ were able to be done.
 
Last edited:
  • #24
I hate to disagree but there are still some research environments where they use very old Fortran models and they won't change languages any time soon due to acceptance and stability of the models. Its been proposed and rejected many times.
 
  • #25
While doing my PhD in physics I used MatLab, LabView, and Java ... Java for all of the complex analysis and image processing.

In my previous professional programming/engineering life I used - from beginning to end:
Assember, Fortran II, Fortran IV, Cobol, APL, Snobol, lots more Cobol, RPG I & II, lots of PL/I, dBase II, IBM PC assembler, C, Visual Basic, C++, then the stuff listed above.

Along the way I also wrote Fortran compilers, and taught computer programming (in three different languages) for a few years.

As others have said: use the tool that works for the job at hand. If you are modifying old code, then use that.
Each environment and application requires different tools. Most people only see a fraction of what is possible because they have only worked in limited environments.

So I don't disagree with any of the previous answers - but there are many correct answers; the best one for your situation is something that you will determine in the future. For example, the comments about Fortran by D H are certainly correct - especially if you are doing computational physics. But when I took a course in soft materials simulations the course was taught in Java. And in the engineering courses almost everything was MatLab.

One addition here: if you are an engineer you should probably start with MatLab.
 
  • #26
At a bare minimum, I think every physicist should be proficient in Mathematica. It is powerful, has many built in functions, can take care of much analytic work in addition to numeric, and it is reasonably fast for what it is.

If you are in HE (or nuclear) exp, you need to learn C++. There's no way around this.

If you are doing other experiment (CME, AMO Exp, etc), you should learn Labview as well.

I do theoretical/computational condensed matter physics and I use Mathematica, C++, Fortran, and Python with great regularity.
 
  • #27
ZombieFeynman said:
At a bare minimum, I think every physicist should be proficient in Mathematica. It is powerful, has many built in functions, can take care of much analytic work in addition to numeric, and it is reasonably fast for what it is.

If you are in HE (or nuclear) exp, you need to learn C++. There's no way around this.

If you are doing other experiment (CME, AMO Exp, etc), you should learn Labview as well.

I do theoretical/computational condensed matter physics and I use Mathematica, C++, Fortran, and Python with great regularity.

I disagree. Mathematica is a wonderful language. It would make there life much easier (that's obvious considering the functional program paradigm as opposed to imperative). But that in no way means they should HAVE to learn it. A language such as C++, Java, C, FORTRAN etc should easily suffice. Same thing for nuclear engineering. All languages (for the most part with the exception of some BASIC dialects) are sufficient for most tasks. As long as you are proficient in a decent language with a nice library, you can easily do all your tasks.
 

Related to Best programming language for physics

What is the best programming language for physics?

There is no one definitive answer to this question as it largely depends on the specific needs of the physicist and the problem they are trying to solve. However, some commonly used languages in physics include Python, C++, and Fortran.

What is the advantage of using Python for physics?

Python is a high-level, interpreted language that is known for its simple and intuitive syntax, making it easier to learn and use compared to other languages. It also has a wide range of libraries and packages specifically designed for scientific computing and data analysis, making it a popular choice among physicists.

Is C++ a good choice for physics programming?

Yes, C++ is a powerful and versatile language that is commonly used in high-performance computing and simulations, making it a great choice for complex and computationally intensive physics problems. It also allows for low-level memory management, which can be beneficial for certain applications.

Why is Fortran still used in physics?

Fortran is a language specifically designed for scientific and mathematical computing, and it is known for its speed and efficiency. Therefore, it is still widely used in physics for simulations and numerical calculations where performance is crucial.

Do I need to know multiple programming languages for physics?

It is always beneficial to have a basic understanding of multiple programming languages, as each has its own strengths and weaknesses. However, it is not necessary to be an expert in all languages, and it is common for physicists to primarily use one or two languages that best suit their needs.

Similar threads

  • STEM Academic Advising
Replies
3
Views
1K
  • STEM Career Guidance
Replies
23
Views
3K
  • STEM Academic Advising
Replies
16
Views
913
Replies
6
Views
471
  • STEM Academic Advising
2
Replies
60
Views
4K
  • STEM Academic Advising
Replies
4
Views
721
Replies
1
Views
1K
Replies
16
Views
2K
  • STEM Academic Advising
Replies
23
Views
1K
Back
Top