1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Best programming language for physics

  1. Oct 30, 2013 #1

    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?
  2. jcsd
  3. Oct 30, 2013 #2


    User Avatar
    Gold Member

    This question is asked about once a week. I suggest a forum search.
  4. Oct 30, 2013 #3


    User Avatar
    Gold Member

    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. Oct 30, 2013 #4
  6. Oct 30, 2013 #5


    User Avatar
    Staff Emeritus
    Science Advisor

    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++.


    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.
  7. Oct 30, 2013 #6


    Staff: Mentor

    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.
  8. Oct 30, 2013 #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).
  9. Oct 30, 2013 #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.
  10. Oct 30, 2013 #9


    User Avatar
    Education Advisor
    Gold Member

    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.
  11. Oct 30, 2013 #10


    Staff: Mentor

    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...
  12. Oct 30, 2013 #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 wont 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.
  13. Oct 30, 2013 #12


    Staff: Mentor

    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...
  14. Oct 30, 2013 #13

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
  15. Oct 30, 2013 #14
    thats not actionable advice.

    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.
  16. Oct 30, 2013 #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.
  17. Oct 31, 2013 #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.
  18. Oct 31, 2013 #17
    Are people really serious when they advocate FORTRAN of all things as anyones first or primary language? Like really?
  19. Oct 31, 2013 #18

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
  20. Oct 31, 2013 #19


    User Avatar
    Science Advisor

    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)
  21. Oct 31, 2013 #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.
  22. Oct 31, 2013 #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.
  23. Oct 31, 2013 #22


    Staff: Mentor

    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.
  24. Oct 31, 2013 #23
    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 isnt 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: Oct 31, 2013
  25. Oct 31, 2013 #24


    Staff: Mentor

    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.
  26. Oct 31, 2013 #25


    User Avatar
    Science Advisor
    Gold Member

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook