Programming Languages for the Physicist

  • Thread starter txr534
  • Start date
  • #1
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!
 

Answers and Replies

  • #2
Orodruin
Staff Emeritus
Science Advisor
Homework Helper
Insights Author
Gold Member
17,332
7,202
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
phinds
Science Advisor
Insights Author
Gold Member
17,187
8,474
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
2,800
602
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.
 
  • #5
5
0
Okay, good to know I'm on the right track. Thanks guys!
 
  • #6
491
189
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
52
12
+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
Aufbauwerk 2045
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
Dr Transport
Science Advisor
Gold Member
2,517
639
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
35,285
7,128
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
ChrisVer
Gold Member
3,381
462
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
Dr Transport
Science Advisor
Gold Member
2,517
639
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
Dr. Courtney
Education Advisor
Insights Author
Gold Member
2020 Award
3,332
2,498
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
ChrisVer
Gold Member
3,381
462
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
Dr Transport
Science Advisor
Gold Member
2,517
639
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
35,285
7,128
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
ChrisVer
Gold Member
3,381
462
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
35,285
7,128
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.
 

Related Threads on Programming Languages for the Physicist

  • Last Post
Replies
4
Views
1K
  • Last Post
Replies
1
Views
821
  • Last Post
Replies
19
Views
7K
  • Last Post
Replies
2
Views
5K
  • Last Post
Replies
16
Views
3K
  • Last Post
2
Replies
29
Views
2K
  • Last Post
Replies
2
Views
593
Replies
14
Views
1K
Replies
27
Views
8K
Top