1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Programming languages for numerical work

  1. Feb 20, 2007 #1
    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?
  2. jcsd
  3. Feb 20, 2007 #2
    I've never used Python but I also heard its great for scientific computing
  4. Feb 20, 2007 #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 recomend 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.
  5. Feb 20, 2007 #4

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    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.
  6. Feb 20, 2007 #5
    Perhaps you could go with C, because more people like me (i.e. undergrad and grad students) know it.
  7. Feb 20, 2007 #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 accross 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.
  8. Feb 20, 2007 #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.
  9. Feb 20, 2007 #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
  10. Feb 20, 2007 #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.
  11. Feb 20, 2007 #10

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    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.
  12. Feb 20, 2007 #11


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
  13. Feb 21, 2007 #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. :)
  14. Feb 21, 2007 #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.
  15. Feb 21, 2007 #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 theres no way to break it down and first analyze the smaller chunks!)

    Otherwise, C is excellent.
  16. Feb 21, 2007 #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.
  17. Feb 21, 2007 #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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Programming languages for numerical work
  1. Programming Languages (Replies: 6)