Programming Languages for the Physicist

In summary, the conversation discussed the importance of coding skills for a physics student and the languages that are useful for scientific computing. The languages mentioned include C/C++, Matlab, Python, Mathematica, and Fortran. Some participants also mentioned the usefulness of tools like GNUplot and tkinter, as well as the importance of learning how to give logical instructions to a computer. It was also noted that Lisp is a good choice for AI programming in physics. The conversation ended with a reminder that programming may not be as easy for physicists as some believe and that legacy codes in languages like Fortran may still need to be worked with.
  • #1
txr534
5
0
Hi all,

I am a sophomore physics and math major who has realized the importance of being able to code, so I decided to start learning. My question is, what languages should a young physicist learn? I was going to start with C but would like to know where to go from there.

Thanks for the help!
 
Technology news on Phys.org
  • #2
This depends on what exactly you want to do. I would expect a PhD student to know or be able to learn the following rather quickly:
C/C++
Matlab or Python
Mathematica

Of course, the real skill is to be able to give a computer a sequence of logical instructions. The syntax of specific languages is just particulars that you can adapt to.
 
  • Like
Likes mastrofoffi
  • #3
Starting w/ C is fine, as it is the basis for many modern languages (Java, etc). The important things is, as Orodruin said, to learn how to formulate problems as sets of logical instructions that can then be put into the syntax of whatever language you are using.
 
  • #4
I think its better to start with python. Its easier and you can use scipy or sage for scientific computing. Then you can learn C++. After that it'll be really easy to start with any other language.
As for MatLab, I didn't even learn it. I started using it right away. No big deal. Don't worry about it. if you know a little programming you can do anything with it. Just search the internet for the code you need. I think mathematica is like this too.
 
  • Like
Likes Fooality
  • #5
Okay, good to know I'm on the right track. Thanks guys!
 
  • #6
If you learn C it might be worth it looking at integrating GNUplots graphing capabilities. It's quite simple but something to keep in mind simply for ease-of-use.

If you want a graphical interface I'm very impressed with tkinter in Python (although it gives me the heebie jeebies with the significance of tabs/spaces and the lack of semi-colons)
 
  • #7
+1 for C. I guess it is a must-know for everyone involved in scientific programming and probably the best choice for a beginner; the advantage of C against most other languages is that it is quite a low-level language and it makes you understand how some things are working "behind the scenes". When you will feel confident with it you should have no issues learning other languages(except maybe functional programming languages, which need kind of a different approach, and I think also learning one of those languages can be a nice and interesting exercise).
Nowadays Python is also getting more and more popular and there are a lot of great scientific libraries, so probably at some point you may want to take a look at it.
I would also recommend R for data analysis and statistical computing; with MatLab you can probably replace R and have a lot more functionalities, but well, R it's free.

Another thing I found useful(not really necessary though), if you're working on UNIX systems, is to learn some basics of bash scripting; it can come in quite handy if you need to automate program runs with many different sets of data and stuff like this
 
  • #8
I think C is the right place to start. Beyond that, I think it depends on your long range plans.

If you want to get into high performance programming for physics then the two main languages are C/C++ and Fortran. I think Fortran is better for this purpose but others may disagree.

I am not a fan of C++ but the good thing about it is if you know C you can gradually start using some object-oriented features if you need to. But you are never forced entirely into the OOP paradigm as you are with the other so-called OOP languages.

Since no one else mentioned Fortran here is a link to why it is still in use.

http://www.moreisdifferent.com/2015/07/16/why-physicsts-still-use-fortran/

If you are looking for free compilers for C, C++ or Fortran then consider MinGW.

http://www.mingw.org

There are good numerical programming books for both C/C++ and Fortran. Perhaps this kind of programming should be your focus as a physics person.

In case you ever want to get involved in AI programming for physics then Lisp is a good choice. There's a free console-based Lisp interpreter and compiler at

http://www.clisp.org.

Lisp is definitely not a mainstream language but I find it very useful for some things.

Others have mentioned tools like Mathematica which are widely used, so I won't repeat that.

One caution: if you talk to some physics professors, they will say programming is trivial for we physics types because we are so smart and programming is easy if you can understand QM. I would not take such an opinion as gospel. You will find out if you ever get into advanced programming.
 
Last edited by a moderator:
  • #9
Octave is a free clone of Matlab with nearly the same functionality and the best part, you can grab Matlab code from online and it usually will run in Octave.

My [itex]\$ 0.02[/itex], in no particular order, C/C++, Fortran, Python, Java. Matlab is not a language per say, if you know C++, you can learn the Matlab syntax in about a day, the best part about it is that you get all the ease of graphics functionality built in that you don't have with C++ or Fortran. Now, I have Fortran on the list, some would shudder at the thought, but there is a butt-load of legacy code out there and when you get to the real world, you'll find that your going to have to look at old code and work with it, I worked for 15 years with a couple of codes that were a mix of Fortran and C/C++, we were talking about re-writing one of the codes, but management didn't want to set aside about 6-8 man-years of money over the next couple of years to shut down our operation and write codes.
 
  • #10
Dr Transport said:
Now, I have Fortran on the list, some would shudder at the thought, but there is a butt-load of legacy code out there and when you get to the real world, you'll find that your going to have to look at old code and work with it, I worked for 15 years with a couple of codes that were a mix of Fortran and C/C++, we were talking about re-writing one of the codes, but management didn't want to set aside about 6-8 man-years of money over the next couple of years to shut down our operation and write codes.
I'm not a physicist, but I'd have to agree with these remarks about Fortran, based on the number of physics-related questions we get here at PF, especially from members who are tasked with maintaining or updating legacy code.
 
  • #11
I find FORTRAN to be outdated (at least for the physics community). Obviously working with objects is way more necessary for a physicist, and its usage is soon to die out, since no new physicist is learning how to work on it for building projects (alas! because it's a very ugly one)...
it may still be in parts of old-written codes which you won't really have to reach except for if it's your work to do so. I expect one day that someone will invest time to make the transitions... New codes are written in C++ or python... In my work I've never seen FORTRAN but I know it exists.
From friends doing their PhD is also working with Mathematica, Matlab, (even java!) and rarely I've heard of Fortran.
 
  • #12
ChrisVer said:
I find FORTRAN to be outdated (at least for the physics community). Obviously working with objects is way more necessary for a physicist, and its usage is soon to die out, since no new physicist is learning how to work on it for building projects (alas! because it's a very ugly one)...
it may still be in parts of old-written codes which you won't really have to reach except for if it's your work to do so. I expect one day that someone will invest time to make the transitions... New codes are written in C++ or python... In my work I've never seen FORTRAN but I know it exists.
From friends doing their PhD is also working with Mathematica, Matlab, (even java!) and rarely I've heard of Fortran.

Ye of little faith, the death of Fortran has been prognosticated for more than 25 years, the academic community may have moved away from it, but until all the computers in this world refuse to run it, the industrial base will continue to utilize it. There have been 3-4 updates and standardization's, it isn't dying anytime soon. I said the same thing over 25 years ago, since then, I wrote the code for my dissertation using it and have continued to write in it on and off and the job I just interviewed for and was given an offer was to extend a major analysis code which is written entirely in it.
 
  • #13
C will work. You're not really sure what your next requirement might be. Might be Fortran, Java, Python, Matlab, who knows?

But the more you do and the better you get in C, the easier the next language will be to learn when needed.
 
  • #14
Dr Transport said:
the death of Fortran has been prognosticated for more than 25 years,
and we've only had C as part of academic courses for the last (maybe) 10-15 years... I don't know for around how many years we have C++.
 
  • #15
ChrisVer said:
and we've only had C as part of academic courses for the last (maybe) 10-15 years... I don't know for around how many years we have C++.

Where I went to school they were teaching C in the late '80's and where I went to graduate school they didn't start with C until ~'95. No clue when C++ took over.
 
  • #16
ChrisVer said:
and we've only had C as part of academic courses for the last (maybe) 10-15 years... I don't know for around how many years we have C++.
I took a class in C programming in 1985 at a local community college.That's 32 years ago.
 
  • #17
Mark44 said:
I took a class in C programming in 1985 at a local community college.That's 32 years ago.
that was one amazing physics course program you had at your college!
 
  • #18
ChrisVer said:
that was one amazing physics course program you had at your college!
It wasn't a physics course -- it was just an introductory programming course at the community college (= two-year college) where I was teaching. The language used in the class was C. I was already familiar with Basic, Fortran, Pascal, and Modula-2, but wanted to start learning C.
 

FAQ: Programming Languages for the Physicist

What is a programming language?

A programming language is a set of instructions and rules used to communicate with a computer and create programs. It allows a scientist to write code that can be executed by a computer to perform specific tasks.

Why do physicists need to learn programming languages?

Physicists need to learn programming languages in order to analyze and process large amounts of data, create simulations and models, and automate complex calculations. Programming languages also allow for more efficient and accurate data analysis, which is essential in the field of physics.

What are some common programming languages used in physics?

Some common programming languages used in physics include Python, MATLAB, and Fortran. Each language has its own strengths and weaknesses, but all are capable of handling complex calculations and data analysis tasks.

Do I need to be an expert in programming to use these languages for physics?

No, you do not need to be an expert in programming to use these languages for physics. However, it is important to have a basic understanding of programming principles and syntax in order to effectively use these languages for scientific purposes.

Are there any resources available for learning programming languages for physics?

Yes, there are many resources available for learning programming languages for physics. Online tutorials, books, and courses are all great options for learning the basics of these languages. Additionally, many universities and research institutions offer workshops and classes specifically focused on using programming languages for scientific purposes.

Back
Top