Which programming languages might a physics major learn and why?

In summary: Python is a popular language for general programming, while Fortran and C++ are used for more specialized applications.
  • #1
Frion
30
0
Other than C++, which is obvious, and MATLAB, which is unavoidable.
 
Physics news on Phys.org
  • #2
Python, and don't forget Fortran. A lot of physics programming is still done with Fortran.
 
  • #3
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
All physics majors must learn some Fortran in my university, in a numerical analysis course.
 
  • #5
Excuse me for asking, but why is C++ obvious? Or even fortran?
 
  • #6
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
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?

hamsa0 said:
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
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
Frion said:
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
B-80 said:
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
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
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
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
bcrowell said:
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
Frion said:
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 let's 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
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
sfs01 said:
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
I second C++.
 
  • #19
sfs01 said:
... (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
Ryker said:
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
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
fluidistic said:
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
fluidistic said:
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
anonymity said:
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
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.
 
  • #26
zif. said:
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.

However , the root of most current languages can be found in C , so learning that was for me an absolute prerequisite.

If you skip on C , you skip on the glue that holds all together.
 
  • #27
twofish-quant said:
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.



Any non-trivial physics computational research involves lots of CS skills. That's why physicists get hired as programmers.
So basically Fortran wouldn't be a good choice because soon or later you'll be confronted to its limitations where C can do the job?
Just curious if this overweight the fact that Fortran is simpler to learn than C.
And about physicists hired as programmers, I wasn't aware of that but seems nice.
 
  • #28
fluidistic said:
So basically Fortran wouldn't be a good choice because soon or later you'll be confronted to its limitations where C can do the job?

You shouldn't choose. Any nontrivial code is going to have multiple languages, and for computational physics, you will have to be a competent programmer in C++/C/Fortran/Python, and know how to mix all of the code together.

Just curious if this overweight the fact that Fortran is simpler to learn than C.

It's not really. If you want to learn all of Fortran 95 including the object syntax, you will have do spend as much time as you would learning the same thing in C++ or Java,
 
  • #29
twofish-quant said:
You shouldn't choose. Any nontrivial code is going to have multiple languages, and for computational physics, you will have to be a competent programmer in C++/C/Fortran/Python, and know how to mix all of the code together.

It's not really. If you want to learn all of Fortran 95 including the object syntax, you will have do spend as much time as you would learning the same thing in C++ or Java,
That seems to be the way scientific programming has gone over the past decade. I see more programming mixing Python/C++/Fortran. On a recent project, we were told that the preference was for C++ over Fortran.
 

1. What is the most commonly used programming language in physics?

The most commonly used programming language in physics is Python. It is a versatile language that is easy to learn and has a wide range of libraries and tools that are useful for scientific computing and data analysis.

2. Why do physics majors need to learn programming?

Physics majors need to learn programming in order to perform complex calculations, analyze data, and create simulations. It is an essential tool for modern scientific research and allows for automation and increased efficiency in data analysis.

3. Are there any other programming languages besides Python that are useful for physics majors?

Yes, there are several other programming languages that are useful for physics majors. Some examples include C++, MATLAB, and Fortran. Each language has its own strengths and is used for different purposes in physics research.

4. How does learning programming benefit a physics major's career?

Learning programming can benefit a physics major's career by making them more marketable to potential employers. It also allows for greater flexibility in the types of jobs they can pursue, such as data analysis or software development roles.

5. Is it necessary for a physics major to have prior programming experience before starting their degree?

No, it is not necessary for a physics major to have prior programming experience before starting their degree. However, having some basic knowledge of programming can make it easier to learn more advanced concepts and apply them to physics problems.

Similar threads

  • STEM Academic Advising
Replies
3
Views
289
  • STEM Academic Advising
Replies
16
Views
2K
  • STEM Academic Advising
Replies
10
Views
2K
  • STEM Academic Advising
Replies
4
Views
811
  • STEM Academic Advising
Replies
8
Views
1K
Replies
20
Views
2K
  • STEM Academic Advising
Replies
13
Views
1K
  • STEM Academic Advising
Replies
8
Views
929
  • STEM Academic Advising
Replies
30
Views
2K
  • STEM Academic Advising
Replies
3
Views
748
Back
Top