Which programming languages might a physics major learn and why?

  • Programs
  • Thread starter Frion
  • Start date
  • #1
30
0
Other than C++, which is obvious, and MATLAB, which is unavoidable.
 

Answers and Replies

  • #2
D H
Staff Emeritus
Science Advisor
Insights Author
15,415
687
Python, and don't forget Fortran. A lot of physics programming is still done with Fortran.
 
  • #3
335
14
Eh, don't bother learning fortran. Old code is in fortran 77, which is a simple language to pick up. Learn c++, and fortran will come easily IF you end up needing it.
 
  • #4
fluidistic
Gold Member
3,790
152
All physics majors must learn some Fortran in my university, in a numerical analysis course.
 
  • #5
11
0
Excuse me for asking, but why is C++ obvious? Or even fortran?
 
  • #6
73
1
C/C++/python/Bash

I've been coding for my physics dept for 2 years now, and that's what they want here at least.
 
  • #7
30
0
I did sort-of forget Fortran. Is it still used much?

Also, two of you recommended Python. Why? Wouldn't Python be unsuited for numerical work because it's an interpreted language?

Excuse me for asking, but why is C++ obvious? Or even fortran?

I mean 'obvious' in the sense that if there's serious programming to be done, C/C++ is just going to show up.
 
  • #8
fluidistic
Gold Member
3,790
152
For Fortran, read this opinion: http://www.ibiblio.org/pub/languages/fortran/ch1-2.html.
The guy summarizes Fortran vs C comparison as:
a) Scientifically oriented
b) Better optimized code
c) A lot of existing code
d) Easier to learn
e) More efficient mathematics
f) Easier to use and more robust
g) Better diagnostics

Personally I think it's also very fast compared to most (all?) other programming languages.
 
  • #9
73
1
I did sort-of forget Fortran. Is it still used much?

Also, two of you recommended Python. Why? Wouldn't Python be unsuited for numerical work because it's an interpreted language?



I mean 'obvious' in the sense that if there's serious programming to be done, C/C++ is just going to show up.

Python has a huge library just like C, and not everything needs to be done on a supercomputing level, just as you said why C is obvious, Python just seems to be the standard...

Python is really not that much slower than C, especially when it's compiled, it's more than sufficient for most purposes.
 
  • #10
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,950
19
Python is really not that much slower than C, especially when it's compiled, it's more than sufficient for most purposes.
The big thing is not trying to compile python, but that python mixes well with C. Packages like NumPy provide convenient python objects that, when manipulated, invoke C functions.
 
  • #11
559
8
A program in python is usually a lot shorter than a program in C, C++, and java. Also C++ and fortran have their similarities and java and python also have some similarities.
 
  • #12
bcrowell
Staff Emeritus
Science Advisor
Insights Author
Gold Member
6,723
425
Learning a specific programming language is irrelevant to your education. Just pick one and learn it. Learning programming language A versus programming language B is the kind of thing you worry about if you're getting a certificate from a trade school.
 
  • #13
492
2
A lot of the more popular and structured programs are starting to implement API's with Python. Also, Python is sort of like MATLAB in the sense that you can quickly write something up to do a small calculation. Also, like others mentioned, it has lots of libraries which almost do everything for you. IDL is a commercialized implementation of Python (back when Python sucked, but now they've caught up), and is used by a lot of people in government labs and stuff.

FORTRAN is something you should definitely learn, as well as C++, but the latter is something that will take a lot more time/effort. You should also familiarize yourself with command-line interfaces in Linux.
 
  • #14
93
0
Learning programming language A versus programming language B is the kind of thing you worry about if you're getting a certificate from a trade school.

It's also something that only people that don't already know how to program worry about.

Once you get one language under your belt, you realize that they're pretty much all the same. That and that you can pick up enough of a new one to be useful over a weekend.
 
  • #15
chiro
Science Advisor
4,797
133
Other than C++, which is obvious, and MATLAB, which is unavoidable.

As a general rule, anything that gets the job done as quick as possible.

If you have an existing platform that lets you do everything quickly, then use it. Practically it is hard to do this though, because you tend to get specializations for particular areas and the platforms and their particular design reflects that.

MATLAB's design is based on the fundamental design structures being matrices, and is built around that premise.

Math platforms are usually designed around solving math problems, and as you would expect certain applications would be hard or take too long to implement on that platform.

In a math platform (like MATLAB, Maple, Mathematica and so on), solving a PDE numerically is going to be a piece of cake. But importing data from an ODBC source "may" be a non-trivial exercise, even if the designers built an API to allow 3rd party programmers to do this very thing.

Thats one reason why legacy systems are still used. They do specific stuff that needs doing straight away. You could always make the argument for better designs that reflect all the different tasks that you need to do, but that's going to cost you resources (programmers, their time, designers time, general project time), and usually that's not an option because you need stuff done now, so you use what you know works.
 
  • #16
22
0
From a practical perspective, yes C++/python are probably going to be most used. The only reason someone would still want to use fortran nowadays is probably either nostalgia or legacy libraries (the article comparing fortran vs. C above sounded like it was written 10+ years ago, in the meantime a lot has happened in the field of computing).

For any numerical applications that really need a lot of performance, C/C++ is going to be the choice. But not because it is intrinsically faster (modern higher level languages such as Java, Python, C# etc. are just as fast for all practical purposes nowadays and are much easier to work with), but because if you need real performance, you will need to look into hardware technologies such as GPU programming or FPGAs, which typically are programmed in C/C++-like languages. For most applications your normal PC is fast enough though, so something like Python is perfectly fine.

If you want to become good at programming though, I'd recommend also looking into at least one functional programming language (such as Scala, Erlang, Scheme, Haskell, Ocaml etc.) to familiarise yourself with the concept. They have a completely different (more mathematical, ironically) way of looking at programming that will also change the way you look at writing programs in imperative languages afterwards. Admittedly, they are currently only used for numerical applications in niche areas though, so are of limited direct use at present.
 
  • #17
6,817
16
For any numerical applications that really need a lot of performance, C/C++ is going to be the choice. But not because it is intrinsically faster (modern higher level languages such as Java, Python, C# etc. are just as fast for all practical purposes nowadays and are much easier to work with)

When you are running a massive simulation, you are often pushing the machine at close to its limits. C++/C is useful because you can write code that is close to "bare metal." You can do useful things with python because the underlying libraries are written in C for Fortran. Java is useful for anything involving server-clients, but there are some very serious problems with using it as an algorithmic language.

For most applications your normal PC is fast enough though, so something like Python is perfectly fine.

Depends on what you are doing. If you have a simulation that runs in one hour, then you get things done faster using a slower language. If you have a simulation that takes two weeks to run, things are different.


If you want to become good at programming though, I'd recommend also looking into at least one functional programming language (such as Scala, Erlang, Scheme, Haskell, Ocaml etc.) to familiarise yourself with the concept. They have a completely different (more mathematical, ironically) way of looking at programming that will also change the way you look at writing programs in imperative languages afterwards. Admittedly, they are currently only used for numerical applications in niche areas though, so are of limited direct use at present.

Look at boost::math. The reason that C++ can be very useful is that you can do functional programming in a compiled setting. C++11 has lambda functions.
 
  • #18
210
1
I second C++.
 
  • #19
1,086
2
... (the article comparing fortran vs. C above sounded like it was written 10+ years ago, in the meantime a lot has happened in the field of computing).
That's because it was (copyright 1996 - 1998) :biggrin:
 
  • #20
fluidistic
Gold Member
3,790
152
That's because it was (copyright 1996 - 1998) :biggrin:

And so what?...
For a physics major I think Fortran is enough, i.e. will do the job you need to do and will do it great. Since it's simpler than C/C++, why bother learning these languages that might help you almost only if you physics related computing?
The OP asked for a physics major, not a computer science major in which case the answer would have been different. I'd still stick to Fortran for the mentioned reasons.
If I was on the route to be a statistician I'd learn R instead of C simply because it could do my job very well, despite "C being/getting more popular" in many areas including being used each time more by mathematicians.
 
  • #21
163
0
How useful is java compared to c/c++, aside from the fact that java is based on c and would therefore make learning c very easy
 
  • #22
143
0
And so what?...
For a physics major I think Fortran is enough, i.e. will do the job you need to do and will do it great. Since it's simpler than C/C++, why bother learning these languages that might help you almost only if you physics related computing?
The OP asked for a physics major, not a computer science major in which case the answer would have been different. I'd still stick to Fortran for the mentioned reasons.
If I was on the route to be a statistician I'd learn R instead of C simply because it could do my job very well, despite "C being/getting more popular" in many areas including being used each time more by mathematicians.

Right? Just like 640KB RAM ought to be enough for anybody.
 
  • #23
6,817
16
And so what?...
For a physics major I think Fortran is enough, i.e. will do the job you need to do and will do it great. Since it's simpler than C/C++, why bother learning these languages that might help you almost only if you physics related computing?

Because any large scale physics code nowadays can contain 300K lines of code, and if you don't use modern OOP techniques, you end up with something that is unmaintainable. Also there are things that you can do with C++ that you just can't do with Fortran. Look at boost::math. Instead of hand coding the algorithms what you do is you use templates to set up a specification of the algorithm and then you let the compiler do metaprogramming magic.

If you are writing a 200 line routine, you can write it in any language. But modern computational physics involves maintain code that contains several hundred thousand lines of code.

The OP asked for a physics major, not a computer science major in which case the answer would have been different.

Any non-trivial physics computational research involves lots of CS skills. That's why physicists get hired as programmers.
 
  • #24
6,817
16
How useful is java compared to c/c++, aside from the fact that java is based on c and would therefore make learning c very easy

As a training language, it's decent, since anyone that is a good java programmer can program decent C++.

Java is a bad language for doing heavy duty algorithmic calcuations since the virtual machine prevents you from having fine grained control over where the data is. In C++ or fortran, you can write compiler routines that tell the computer to put things onto the L2 cache or use vectorized routines, but you don't have that control in java.
 
  • #25
19
0
IDL is very widely used in astronomy (and some other physics fields, I believe) and is very similar to Matlab (the only big difference I have come across is that arrays start with 'zero' index rather than '1')- so it should be fairly easy to learn if you know Matlab.
 

Related Threads on Which programming languages might a physics major learn and why?

  • Last Post
2
Replies
44
Views
7K
Replies
14
Views
4K
  • Last Post
Replies
20
Views
661
Replies
7
Views
7K
Replies
2
Views
1K
  • Last Post
Replies
12
Views
4K
  • Last Post
Replies
6
Views
975
Replies
13
Views
3K
  • Last Post
Replies
15
Views
4K
Top