Programming languages for numerical work

In summary: C is the fastest language for computation, but Python is quick to code and has a huge scientific library. C is ungodly fast, but Python is almost as fast, with the tradeoff coming from intermediate virtual machine code (CPython, Jython) or interpretive compilation. If you care about speed, then consider FORTRAN 77.
  • #1
StatMechGuy
223
2
I'm trying to decide what programming language I should learn for computational aspects of my research. Right now, I've boiled it down to learning either C or Python. I know C is ungodly fast, but I've heard Python is quick to code and has a huge scientific library.

Anybody able to help me out one way or the other?
 
Physics news on Phys.org
  • #2
I've never used Python but I also heard its great for scientific computing
 
  • #3
Think of C as analogous to Windows, and Python as analagous to MacOS. Everywhere you go you will encounter things written in C, and Python may be better depending on your personality but Python has in general less documentation and community support. C is ugly, Python is pretty. C is very fast, Python is almost as fast, with the tradeoff coming from intermediate virtual machine code (CPython, Jython) or interpretive compilation.

There are two cases, either speed and memory are important, or not. If you care about speed, then consider FORTRAN 77. This is the fastest 'high-level language', and although it has been supplanted by C, this is only because C traded performance for readibility, viz. the programming industry realized the enormity of the task of using old code, and suddenly readibility became a priority.

C was designed as a systems programming language (UNIX), but FORTRAN was designed as a scientific language. The only reason C is more readable (on an industrial scale) than FORTRAN is because it supports abstraction at a higher level. For the individual who is not designing a massive end-user program, FORTRAN is easier and faster to write then C.

In my work it is almost always the case that human time is more important than machine time, which is why I use and recommend Mathematica. Mathematica is a universal programming language, just like C or Python (although Mathematica is proprietary). The difference is that it contains, in an integrated way, the world's largest library for scientific computing, containing all the best of known algorithms as well as some that are not known outside of Wolfram Research. The other difference is that is 1000 times slower than C (not as bad as it sounds, because in fact the limiting factor always has to do with the size of the problem and multiplication by even such a large constant is not too terrible in most cases).

Unless your intended tasks are very basic to program (for example solving a particular differential equation for a variety of conditions using the same method ). If instead your work involves considerable variety, and in addition you want to consider visualizations of the data, Mathematica would be an excellent choice.
 
  • #4
Matlab is a very good program to use, shallow learning curve and you can be up and running in about half a day. There are a couple of books out there that you can get which have tutorials to get you started. I have called it "C-lite" because the syntax is similar.
 
  • #5
StatMechGuy said:
I'm trying to decide what programming language I should learn for computational aspects of my research. Right now, I've boiled it down to learning either C or Python. I know C is ungodly fast, but I've heard Python is quick to code and has a huge scientific library.

Anybody able to help me out one way or the other?

Perhaps you could go with C, because more people like me (i.e. undergrad and grad students) know it.
 
  • #6
I have to strongly urge you to avoid programming in MATLAB. It is interpreted, and therefore by its very nature slow. The notation is clunky and difficult to use and my experience has been that MATLAB is not worth the hassle. Moreover, if you ever have the need to program in parallel, MATLAB is utter garbage as the licensing is such that you would need one copy of MATLAB for each processor... yeah try to get 128 licences of MATLAB to run a monster program on a cluster - RIGHT.

That being said, C is the standard for most purposes so it should be learned first. In addition, FORTRAN would be useful as there are many tasks for which it is better suited (ie formula translation :-p). I would also advise avoiding C++ as it introduced the object oriented paradigm, which is just another way to write code that is slow { for example, I was trying to find a solid random number generator for a stochastic simulator over the summer. I ran across the Mersenne twister in c++ and gave it a shot. With the number of random numbers I was generating (on the order of millions) the object oriented inheritance garbage was two orders of magnitude slower than the Miller-Parks bit shifting algorithm in straight C }.

I'm not sure of the specifics on Python, but I believe you should first learn to program in C, and then once you know how to program it is a simple matter to look in a book and learn the syntax of a new language.
 
  • #7
I would also like to second the idea of learning Mathematica. I haven't done much programming in it, but it is a wizard for solving complex nasty integrals.
 
  • #8
As for the comments about Mathematica and Matlab, I agree that both are good at scientific programming as far as their math abilities. I used Maple quite a deal in my calc 3 class, and like mathematica, it is a wiz at solving those nasty integrals. But i'd have to agree with ptabor that it'd be better to learn a dedicated language like C, Fortran, Python, or dare I start this debate.. Java ( I love Java)
Programs like Maple, Mathematica, and Matlab are great at solving integrals, but I think learning to use a stand alone language like C or Java to do the same, will further your knowledge of those math skills, and it will be a great skill for almost any career in science
 
  • #9
what you really need to do first is determine and define the type of computing your research will require. Then figure out which programming language fits those needs best.

Are you going to be numerically solving equations? Will you end up working with large matrices? Are you performing simulations? Monte carlo work? The computing needs of your research will dictate the best programming language for you to use. All of the suggestions provided are good choices, but some work better for particular types of problems than others.

And remember, speed doesn't come just from the language. Proper coding and good algorithms play a huge role in how fast a program will run. Sloppy coding can make a compiled C program run god-awfully slow.
 
  • #10
ptabor said:
I have to strongly urge you to avoid programming in MATLAB. It is interpreted, and therefore by its very nature slow. The notation is clunky and difficult to use and my experience has been that MATLAB is not worth the hassle. Moreover, if you ever have the need to program in parallel, MATLAB is utter garbage as the licensing is such that you would need one copy of MATLAB for each processor... yeah try to get 128 licences of MATLAB to run a monster program on a cluster - RIGHT.

Running Matlab on a parallel cluster is a mess, I will agree. Now, if the program is garbage then why are there many, mant users in academia, industry and the govt using it? You can compile the code to run faster and I have seen Matlab code run faster than compiled C code because you do not have to run loops, it is already optimized to run in matrix and vector form.

This thread asked what one could use to program and I now almost as many peole who used Matlab for their dissertation work as Fortran or C and Matlab has built-in library functions whereas C and Fortran these routines either need to be imported, written from scratch or purchased which makes it a viable option.
 
  • #11
On interpreted vs. compiled languages:

If your program is doing "normal" things, such as thousands of saxpys on large matrices and vectors, then your program spends almost all of its time in the highly-optimized saxpy routine and almost no time in the interpreter... so the interpreter vs. compiled distinction is irrelevant.

But if you are doing something "abnormal" and have to explicitly do an operation for every entry in your vector, then you need to use a compiled language, because otherwise you have to go back to the interpreter after every little operation and your program slows to a crawl.




On C++:

C++ is actually an excellent language for numerics, if you are using a (good) library written for numeric computation. (Blitz++, for example) The template mechanism is an incredibly powerful tool for writing highly optimized code, and C++'s operator overloading feature makes the user code very readable.
 
  • #12
I agree with Hurkyl, C++ code is hott if coded right and had tons of 3rd party libraries out there. My main language I code in is C++ so I my be bias well i know i am. :)
 
  • #13
I'm also in favor of C++ for much of the same reasons mentioned above. Most importantly being that I know it the best. FORTRAN is my #2.
 
  • #14
There seems to be quite a few bad points mentioned about MATLAB - yes, it is slower (quite a bit slower..) than C but when I'm working with C i always find it useful to write the code or test the idea in Matlab and then finalize and translate into C. For me, it's definitely useful in this way but ultimately this is a choice up to personal preference. (If, however, you're writing code that pretty much requires that it be run on various computers because of it's size, then heading straight to C might be the better option - only if there's no way to break it down and first analyze the smaller chunks!)

Otherwise, C is excellent.
 
  • #15
C would probably be best with JAVA and python second. Try out different ones and see what you like. Maple has a feature where you can take equations and output it as C or JAVA. Also, you may few what methods they use for their calculations. For example in ODE numerical approximations they use the Runge Kutta method.

Although, not the best for your needs or for learning, I use LISP for just about everything now. Plus, it makes a nifty and quick calculator when writing a paper in emacs. Sorry, I just have to promote LISP when I can. If I was in your position I would probably go with C or PYTHON.
 
  • #16
A comment about FORTRAN vs C

In the 1970s, when scientists abandoned FORTRAN for C, their primary motivation was to go with open source (C) vs proprietary (FORTRAN, owned by IBM, comparable to working in microsoft's C# these days).

Speed => FORTRAN
Anything else => Mathematica

The problem with Maple and Matlab is that they really are just watered down C with libraries and pretty printing. Mathematica is, as a language, mile,s ahead of C, Lisp, Python etc. Mathematica is a functional language, in a more complete way then LISP (which was always too decentralized), and it is such a beautiful paradigm for programming: Everything is composition of functions! Given the input, you apply a sequence of functions to produce the output.
 

1. What is the difference between a compiled language and an interpreted language for numerical work?

Compiled languages, such as C or Fortran, are translated into machine code before execution, resulting in faster performance. Interpreted languages, such as Python or MATLAB, are executed line by line, making them easier to use but slower for numerical work.

2. Which programming language is the best for numerical computation?

There is no single "best" language for numerical work, as each has its own strengths and weaknesses. Some popular choices include MATLAB for its extensive mathematical functions and Python for its flexibility and large community support.

3. Can I use any programming language for numerical work?

Technically, yes, you can use any programming language for numerical work. However, some languages may be more suitable than others due to their built-in mathematical functions, performance, or ease of use.

4. Are there any free programming languages for numerical work?

Yes, there are several free and open-source options for numerical work, such as Python, R, and Julia. These languages offer a wide range of numerical libraries and are constantly being updated and improved by their respective communities.

5. Do I need to know a specific programming language to do numerical work?

It is helpful to have a basic understanding of at least one programming language for numerical work, but it is not a requirement. Some software packages, such as MATLAB, have user-friendly interfaces that allow for numerical computation without extensive programming knowledge.

Similar threads

  • STEM Academic Advising
Replies
12
Views
1K
  • STEM Academic Advising
Replies
3
Views
804
  • STEM Academic Advising
Replies
3
Views
643
  • STEM Academic Advising
Replies
6
Views
779
  • STEM Academic Advising
Replies
4
Views
2K
  • STEM Academic Advising
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
832
  • Programming and Computer Science
4
Replies
107
Views
5K
  • STEM Academic Advising
Replies
7
Views
936
  • STEM Academic Advising
Replies
8
Views
1K
Back
Top