Beginner Language for Physics Majors: Tips & Compilers

In summary: And any hints would be welcomed :wink:Agree. Just pick one and focus on general programming concepts: variables, data types, loops, selection (if-statements), functions, subroutines, objects, etc.; and on general principles of program design.
  • #1
EJC
42
4
I'm a physics major and would like to pick up a programming language or two.

Any insight as to what language(s) would be most applicable, best compilers (for Windows), or any other general tips?

I'm very new to programming (just used MATLAB really), so please explain your answers as much as possible for a complete beginner to understand them.
 
Technology news on Phys.org
  • #2
1) Don't underestimate MATLAB as a computer language. A lot of engineers do all their analysis in MATLAB.
2) A lot of very good programmers love Python, especially for scientific / physics computations. And it is free.
3) C and C++ are the most prevalent languages. And they are the jumping-off point for a lot of current languages (Java, C#, etc.)
 
  • #3
EJC said:
Any insight as to what language(s) would be most applicable, best compilers (for Windows), or any other general tips
That depends a lot of what you would like to do. Will this programming be be scientific related ? Will it be distributed in small circle or cross platform ? Is performance going to be an issue or not ?
Any hints would be welcomed :wink:
 
  • Like
Likes FactChecker
  • #4
Boing3000 said:
That depends a lot of what you would like to do. Will this programming be be scientific related ? Will it be distributed in small circle or cross platform ? Is performance going to be an issue or not ?
Agree. For instance, there is a big speed difference for the ones targeted for physics calculations. MATLAB is SLOW (maybe 100 times slower than C). Python is slow (maybe 15 times slower than C). C and C++ are about as fast. as you can get.
 
  • Like
Likes EJC
  • #5
I agree with the Python suggestion very much...it is a great whole ecosystem with modules for whatever you want.

AND, I am, of course, going to bring up (modern) Fortran...if you could pick up Fortran 90, that would be very handy...in your field, I am thinking you are going to run into Fortran sooner than later. Fortran is great for keeping you from memory leaks, it good for high performance computing, parallel processing, etc. It handles arrays a-la-matlab, has native complex numbers, etc. Fortran continues to be relevant for a bunch of reasons.
 
  • Like
Likes EJC
  • #6
Boing3000 said:
That depends a lot of what you would like to do. Will this programming be be scientific related ? Will it be distributed in small circle or cross platform ? Is performance going to be an issue or not ?
Any hints would be welcomed :wink:

FactChecker said:
Agree. For instance, there is a big speed difference for the ones targeted for physics calculations. MATLAB is SLOW (maybe 100 times slower than C). Python is slow (maybe 15 times slower than C). C and C++ are about as fast. as you can get.

Yeah, I read about the varying computational speeds recently. MATLAB is pretty straightforward with it's syntax, and I've heard C is too, but not quite as easy as MATLAB.

In your opinions, is the tradeoff between learning C and the fact that it is probably slightly more difficult at first than MATLAB worth converting entirely to C?

gsal said:
I agree with the Python suggestion very much...it is a great whole ecosystem with modules for whatever you want.

AND, I am, of course, going to bring up (modern) Fortran...if you could pick up Fortran 90, that would be very handy...in your field, I am thinking you are going to run into Fortran sooner than later. Fortran is great for keeping you from memory leaks, it good for high performance computing, parallel processing, etc. It handles arrays a-la-matlab, has native complex numbers, etc. Fortran continues to be relevant for a bunch of reasons.

Thanks. Can you elaborate on how Fortran and Python differ from languages like C and MATLAB. I am very green on the subject.
 
  • #7
What I believe would be really useful to me is a description of the popular languages and an explanation of the differences between them, and what each language handles best/worst.
 
  • #8
The question "which programming language?" gets discussed rather often here, and there are a lot of opinions. I suggest you try some of the "Similar discussions" listed at the bottom of this page, and likewise at the bottom of those pages.

A Google search for "comparison of programming languages" turns up some pages which look promising for general comparisons of features etc.

My personal opinion is that so long as you choose a general-purpose language that is widely used and that you can easily find resources for (compilers, tutorials, textbooks, etc.), and you don't have a specific application in mind initially, it doesn't make a lot of difference which language you start with. Just pick one and focus on general programming concepts: variables, data types, loops, selection (if-statements), functions, subroutines, objects, etc.; and on general principles of program design. If you do a lot of programming, you will learn several different languages during your school and working career. The general concepts and principles apply to all of them.

I think either C++ or Python would be a reasonable start. I taught introductory programming in C++ for several years. I've never used Python, but I've read a lot of good things about it, and it's on my list of things to learn after I retire in a couple of years. Others will make reasonable cases for other languages.
 
Last edited:
  • Like
Likes FactChecker, Boing3000 and EJC
  • #9
At the risk of being thrown out of polite society, I'm going to strongly recommend BASIC (Beginners All-purpose Symbolic Instruction Code). There is an excellent BASIC available from True BASIC, a revision of the original by the authors of the original, Kemmeny and Kurtz. It is easy to write fully formatted code (no GOTOs, unless you want to use them, no line numbers, unless you want to use them).

People worry much about how fast a program is, and from time to time this is relevant, but often it is not. Do you really care whether the results appear on your screen instantly, or an eye blink later? Does this really matter to you?

I'll tell you what matters to me: How much effort do I have to put into developing the code in order to get the computer to do the heavy lifting? I have found over the past 30 years that I can generate correct, working code by far faster in BASIC than in any other language. Sure it is SLOW, but not nearly as slow as I, a mere human, am. It is so SLOW, that sometimes, I actually have time for sip of soda before I see results; just completely intolerable. I have used BASIC to solve literally thousands of engineering problems including elaborate simulations and eigensolutions.

I never use GOTOs, but I do use line number infrequently. I use them mostly when I intend to publish a block of code, and want to be able to discuss the code. Line numbers (which can be generated automatically with any interval you want) are very handy for this purpose.

I do not recommend BASIC for real time applications where speed is truly important, but for anything else, it is BASICally very simple.
 
  • Like
Likes EJC
  • #10
Well, I am not going to throw OldEngr63 out, but I will certainly vote against Basic...it just does not have the ecosystem, development and popularity that Python has. Python has risen past several other early 90's languages (tcl, Java, Perl) to become not only the scripting language of choice for many open source programs but also for commercial ones and even the language many open source programs are written in.

If you learn Python, you will also know how to develop and do scripting for many other programs.

Like Matlab, Python (numpy, scipy, matplotlib) has just about every toolbox (module) you may need for specific tasks like physics, linear algebra, graph theory, networking, molecular biology, optimization, visualization, etc.

As far as Python's similarity to Matlab is concerned, there are introductory Python pages out there for Matlab users:
http://sebastianraschka.com/Articles/2014_matlab_vs_numpy.html
http://researchcomputing.github.io/meetup_fall_2014/pdfs/fall2014_meetup13_python_matlab.pdf
Python vs Matlab
Numpy for Matlab Users

(I have been using Fortran for sometime and not C/C++, thus, I will not comment on the latter, needless to say, they are also very popular and powerful choices)

Modern Fortran (90 and beyond) has taken quite a leap from Fortran77...it is very friendly, handles arrays a-la-matlab, has modules, derived types, interfaces, overloading, encapsulation, (even object oriented paradigm starting on Fortran2003) and a huge amount of Fortran code out there that you may inherit.

Fortran is also very compatible and easily called from Python (f2py)...when Python is used as a "glue" language. Here is a page comparing Python and Fortran.
 
Last edited by a moderator:
  • Like
Likes EJC
  • #11
Excel has an embedded Basis called VBA. It might be a good place to start, as you learn the basic programming structures common to all (or most?) languages. It's already loaded if you have Excel.
 
  • #12
EJC said:
is the tradeoff between learning C and the fact that it is probably slightly more difficult at first than MATLAB worth converting entirely to C?
C and MATLAB make a good combination. It's nice to know both. You can develop algorithms in MATLAB and rewrite in C when there is a reason to. There are often reasons to rewrite in C. It's harder to get all the calculations correct in C, so you can check it by comparing with MATLAB results. I also think it's a good idea to put comments in the C code that are the MATLAB lines I am trying to implement. Almost half the people I work with are primarily MATLAB programmers and the other half are primarily C programmers (but that is not a typical situation)
 
Last edited:
  • Like
Likes EJC
  • #13
Python is what we teach as a first language in Computer Science and Electrical Engineering here. Yes it is slower in execution than the compiled languages but the development time is usually much shorter. It also depends on your aim, proof of concept problems like shorter development times. Matlab is next in Engineering, although Python with Numpy, Scipy and Sympy are pretty powerful.
 
  • #14
I give a second vote for basic as a beginner language, but just enable IIS web server, embed it in a .asp file and pull it up with your web browser. Unless you really need the speed for heavy computations, it is so much easier to work with a language that does not need to be compiled. What I like about basic is that the routines you learn are very portable between different languages such as PHP, BASH, ect.
 
  • #15
I second the comments that recommend a scripting language like Python or Basic (I like Perl). I earlier recommended the combination of MATLAB and C/C++. You are already familiar with MATLAB, but licenses are expensive and many computers will not have it. C/C++ are established standards, but are harder to develop in. So in between MATLAB and C/C++, it is nice to know an easily scripted language that is free and/or universally available. Python and Basic are like that. So I recommend 3 across the spectrum: MATLAB, Python/Basic, C/C++. Sorry if that seems like a cop-out, but that's the way a programmer's life is: more languages than you know what to do with. All of them good for something.
 
  • Like
Likes EJC
  • #16
EJC said:
I'm a physics major and would like to pick up a programming language or two.

Any insight as to what language(s) would be most applicable, best compilers (for Windows), or any other general tips?

I'm very new to programming (just used MATLAB really), so please explain your answers as much as possible for a complete beginner to understand them.
If you were planning to start as a beginner and build up to devising your own programs from scratch, I think Pascal is a good first language because it enforces good habits. But does anyone teach Pascal any more??

Most likely, in the work setting you will find yourself taking someone else's program and just modifying it a bit to fit your purposes, and for this I recommend Python. It is easy to comprehend a Python program, to spot what section needs changing in order to accomplish what you seek, and the range of functions available in Python and its specialised add-on libraries is awesome. (Daunting, almost.)
 
  • Like
Likes Tadzrina)07 and EJC
  • #17
EJC said:
What I believe would be really useful to me is a description of the popular languages and an explanation of the differences between them, and what each language handles best/worst.

That's difficult to do fairly and comprehensively. There are a lot of popular (and perfectly good less popular) programming languages and a lot of things that differentiate them. Preference for certain programming languages can also be a matter of opinion, which can vary wildly (e.g. C++ can be an extremely powerful and flexible super language or a brittle, fractally complicated swamp of misfeatures, depending on who you ask). Your own preferences, interest, and motivation also matter, e.g. do you want to learn something immediately useful for a project, or do you think programming is interesting to learn for its own sake and you're willing to develop it as a skill over several years?

That said, many people here are suggesting you start with Python. I think that's probably as good a first language as any. I don't know Python terribly well personally, but from what I've seen there are several things that make it a good default choice as a first language, particularly for a physics student:
  • It's interpreted and, more importantly, supports an interactive interpreter. This is great for learning by experimentation. You can type example code into the REPL and see immediately what it does. Some people use Python (with additional libraries) as a Matlab replacement this way.
  • It's got good support for scientific and mathematical computing. Arbitrary size integers, exact rationals, real, and complex numbers are built into the language. There are addon, commonly used libraries for numerical coding, linear algebra, etc. (NumPy and SciPy), plotting (matplotlib), and symbolic computing (SymPy), among others.
  • It's got a large, friendly community of users and developers. This means lots of support in the form of online help, tutorials, and libraries.
  • It's free and maintained as an open source project. You don't have to worry about a commercial company developing it going out of business. You're not dependent on your university or company paying expensive license fees and your code won't become useless if your student/work environment changes.
  • It's multi-platform. If you buy a Mac or switch to Linux in 2 years, you can take your Python code with you. You can share code with friends and colleagues who use different operating systems (this is quite important in a field like physics where not everyone uses Windows).
  • It's multi-paradigm. It supports at least imperative, object-oriented, and to some extent functional-style programming, without being ideological about them. So you can start learning in the usual imperative style and (optionally) later learn the other styles Python supports (and what they're useful for) when you're ready.
  • Python is implemented in C so, naturally, it provides support for interoperating with C code. (May be useful and educational when you reach a certain level of experience.)
  • Overall, Python is a good, well-designed, relatively modern, general-purpose programming language with good built-in types and data structures (strings, lists, hash tables). As long as you don't have some specific requirement (like efficiency), you can probably get whatever you want done reasonably easily in Python.

I don't mean for this to read like an endorsement for Python over all other languages (like I said, I haven't even used Python myself that much). Most of these points aren't unique to Python, and there are sometimes good reasons for some of them not to apply to another language. But if you just want to start coding and don't want to worry too much about language choice, it seems like a reasonable place to start.
 
Last edited:
  • Like
Likes EJC
  • #18
  • Like
Likes EJC
  • #19
I use Cran R & C for what ever I need. R & C will load in anything. Then R will take care of loading anything else it needs. Neither one are too sharp on user interfaces. I can hack together something in Excel or Open Office Spread Sheet if I have to and export it as a CSV or XLS file to R or get some one to write something for me in Java, C++, Visual Basic or something and we can interface it to R.

R will use code from Fortran, Java, C, C++, BASH Shell, PEARL, Python Windows OS, its native language LISP and more. It's LISP like in it requires more parentheses in mathematical expressions than most languages.

I like it because the same code is portable among Windows, Linux and Unix. The calls to the OS are smoothly integrated in the program. In fact all the calls are smoothly integrated once the libraries are called.

GC.
 
  • Like
Likes EJC
  • #20
I think based on the sheer number of people vouching for C and Python, and based on my specific needs, those will be where I start. With that being said, more input on compilers and their pros and cons would be useful and appreciated :oldsmile:
 
  • #21
Are you running Windows, Mac OS, or Linux?
 
  • #22
For Python that's easy: you can get the main Python interpreter from the python.org website. For C, probably the simplest to start with is http://mingw-w64.org/doku.php, which is free and open source (no online registration or such is necessary) and should support the most recent revision to the C language standard.

Alternatively, you could get a Linux-like shell environment and toolset on Windows by installing Cygwin, and then grab Python and one of the C compilers (Clang or GCC) via the Cygwin package manager (if they don't already come installed by default).

Either way I'd recommend a simple environment (command-line compiler, lightweight text editor) to start with.
 
Last edited:
  • Like
Likes EJC
  • #23
Two recommendations:

If you want to beef up your object oriented programming understanding while gaining familiarity with data structures to collect, store and manipulate data, I'd recommend Java for two reasons:

1. There's a ton of documentation available for reference and self-teaching, including the Oracle's own Java class library, literally an encyclopedia of every built in program, and its behaviors and attributes.

2. It has a forgiving syntax. Finding the line where you forgot a closing parentheses is a lot easier than finding where you missed an indent, in my opinion.

Second, as an EE student, I've found MATLAB has a very easy to learn GUI toolkit, in which you can graphically display, manipulate and animate systems of equations in a very neat way.
 
  • Like
Likes EJC
  • #24
jtbell said:
Are you running Windows, Mac OS, or Linux?

I'm running Windows.
 
  • #25
there's always dev c++ and notepad++
 
  • #26
I started programming some 35 years ago in high school using basic on a pdp-11, learned pascal on an apple 2e, used fortran77 during my study and thesis. In the last years I was mostly doing statistical programming with SAS (Stress And Suffer, not recommended) and used Fortran 90, Java and Python for some spare time projects.
My advice is the following: If you are going to do heavy numerical work, C, C++ or maybe Fortran90 is the way to go. If you are going to do a thesis in engineering or physics, you probably will spent most of the time diagonalizing big matrices and these languages are fast. If you are working in a field touching life sciences, you will probably need scripting languages like perl, python in combination with C.
In the outside world, most people are working on shared applications, e.g. for the web, data storage etc. and the faster number crunching languages are of little market value. You need to know there object oriented languages like java and phython (which also has some nice statistics packages called Panda) and others who are more modern than I.

Python may be quite a good starting point as it has a readable syntax, comes with handy packages for almost everything.
I would recommend not to rely on an IDE for the beginning, but at least to use a classical editor for the first steps.
This doesn't leave you completely helpless when you have to change a program in some other language on a remote computer via a vt100 terminal.
Personally, I don't understand why so many persons are using Matlab. It is slow and quite expensive if you aren't a student.
 
  • #27
DrDu said:
Personally, I don't understand why so many persons are using Matlab. It is slow and quite expensive if you aren't a student.
Matlab isn't nearly as slow as most people think. Most of the time the time consuming bits (e..g actually solving a ODE or inverting a matrix) are are actually done by functions that are coded in C. Matlab actually uses some very well-optimized libraries "under the hood" (LINPACK, FFTW etc) and with the parallel programming toolbox you can also use multiple cores and GPU accelerated commands. Nowadays you can also get away with using loops since the JIT compiler is reasonably efficient if you think a bit about how you write the program (I've been using Matlab for about 20 years, loops were very horribly slow until they introduced the JIT compiler in 2004 or whenever it was).

That said, I don't consider Matlab to be a proper programming language; it is a scripting language for doing numerical calculations and is simply too different from a conventional language . Hence, I would also vote for Python as first language. I would stay away from C as a first language, it is too easy to get into bad habits and it is also a bit outdated (although still widely used). C++ or perhaps C# would be a better option but perhaps it would be best to start with Python.
 
  • #28
wle said:
Either way I'd recommend a simple environment (command-line compiler, lightweight text editor) to start with.

Agreed. Sophisticated integrated development environments (IDEs) are valuable for managing large programs with lots of modules, subroutines and/or files. However, in the beginning they add another level of complexity beyond the language syntax and semantics issues that the student should be focusing on. I taught two-semester intro programming courses for many years, that used command-line tools and a simple text editor. I still use such tools for my personal programming.
 
  • #29
FactChecker said:
Agree. For instance, there is a big speed difference for the ones targeted for physics calculations. MATLAB is SLOW (maybe 100 times slower than C). ...
That depends. MATLAB can be hard to beat for some large matrix operations, say inverting a 1k x 1k. MATLAB's code is optimized for that kind of thing, I suspect running LAPACK/BLAS internally, and on a multicore machine it will run the operation in parallel across the cores. Doing all that in C, or in anything else, takes some work.
 
  • Like
Likes FactChecker

1. What is the best programming language for physics majors?

The best programming language for physics majors depends on the specific needs and preferences of the individual. However, some commonly used languages in physics include Python, MATLAB, and Fortran.

2. Do I need to know how to code to major in physics?

While coding may not be a requirement for all physics majors, having some knowledge of programming languages can be highly beneficial in conducting research and analyzing data. It is recommended for physics majors to have at least a basic understanding of coding.

3. What are some essential tips for beginners learning to code in the context of physics?

Some essential tips for beginners learning to code in the context of physics include starting with a simple language, such as Python, practicing regularly, breaking down complex problems into smaller parts, and seeking help from online resources or peers.

4. How important is it to use a compiler when writing code for physics?

Using a compiler can significantly improve the efficiency and accuracy of your code. A compiler checks for errors and translates the code into a language that the computer can understand, making it essential for writing complex codes in physics.

5. Are there any resources available for beginners learning to code for physics?

Yes, there are many online resources available for beginners learning to code for physics, such as online tutorials, forums, and coding communities. Many universities also offer introductory coding courses specifically for physics majors.

Similar threads

Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
867
  • Programming and Computer Science
2
Replies
69
Views
4K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Science and Math Textbooks
Replies
7
Views
626
Back
Top