Language for theoretical physics

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Stoiker
Posts: 2
Joined: Sat Oct 23, 2010 10:31 am UTC

Language for theoretical physics

Postby Stoiker » Sat Oct 23, 2010 10:50 am UTC

I have to start a bigger project in the many body field and I have to do it in Fortran. I also want to use another language parallel for this task to reach a high compatibility with code from other universities ... etc

So my question is: What language should I use ? C, C++, Phyton ?

best regards

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Language for theoretical physics

Postby headprogrammingczar » Sat Oct 23, 2010 8:32 pm UTC

C and Python are both quite compatible with universities, but for your purposes, Haskell might be a good choice. It's not a commonly used language, but universities love it for how deep into CS theory you can go with it. Math translates to Haskell way nicer than any other language I have ever used, which I expect will come in handy if you are doing theoretical physics with it. One downside is that it is a complete departure from most other languages, so it could be tough to learn.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

cogman
Posts: 112
Joined: Sun May 18, 2008 2:17 pm UTC

Re: Language for theoretical physics

Postby cogman » Sun Oct 24, 2010 2:33 pm UTC

headprogrammingczar wrote:C and Python are both quite compatible with universities, but for your purposes, Haskell might be a good choice. It's not a commonly used language, but universities love it for how deep into CS theory you can go with it. Math translates to Haskell way nicer than any other language I have ever used, which I expect will come in handy if you are doing theoretical physics with it. One downside is that it is a complete departure from most other languages, so it could be tough to learn.

You know, I really disagree with all the suggestions. For something like physics which is heavily math based, it is hard to go with anything else but matlab. It is pretty much standard in most universities/engineering fields. While it isn't omni-present in the CS department, it is known.

Matlab can interface with most compiled languages so you can mix and match it if needs be.

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Language for theoretical physics

Postby headprogrammingczar » Sun Oct 24, 2010 3:01 pm UTC

I haven't used it myself, but I have heard nothing but complaints about it from physics majors I know. Your mileage may vary, I suppose.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

cogman
Posts: 112
Joined: Sun May 18, 2008 2:17 pm UTC

Re: Language for theoretical physics

Postby cogman » Sun Oct 24, 2010 3:18 pm UTC

headprogrammingczar wrote:I haven't used it myself, but I have heard nothing but complaints about it from physics majors I know. Your mileage may vary, I suppose.

It is slower then say C++ (but so is python), however, it gets the job done just like any other procedural language. Couple that with the fact that it has the useful ability to do things like take derivatives of polynomials on the fly and you have a physics winner.

Axidos
Posts: 167
Joined: Tue Jan 20, 2009 12:02 pm UTC
Location: trapped in a profile factory please send help

Re: Language for theoretical physics

Postby Axidos » Sun Oct 24, 2010 3:32 pm UTC

headprogrammingczar wrote:I haven't used it myself, but I have heard nothing but complaints about it from physics majors I know. Your mileage may vary, I suppose.

"There are only two kinds of languages: the ones people complain about and the ones nobody uses" - Bjarne Stroustrup

poohat
Posts: 230
Joined: Mon Apr 07, 2008 6:21 am UTC

Re: Language for theoretical physics

Postby poohat » Mon Oct 25, 2010 2:43 am UTC

Im not really buying the Haskell recommendation (you dont want to learn computational theory, you just want to implement shit). Also you arent going to find (m)any other physicists using it so it fails your compatibility requirement. Matlab is pretty standard as someone said. Its a high level language so tends to be much easier and quicker to write code than C/Fortran, also because its interpreted its easier to use for prototyping and visualiation . Also they complement each other nicely since you can write the computationally expensive functions in C/Fortran so that they run fast, and then call them from Matlab, so you end up with 80% of your code written in Matlab (to take advantage of how easy it is to write and prototype) and then the speed critical 20% in C/Fortran.

Whatever you use, youre going to want to get familiar with the GSL which is a big library full of math function implementations. Its written in C but you can interface it from Fortran and use it there too.

C/C++/Fortran/Matlab are by far the most widely used scientific languages afaik, although you do find more specialised ones in certain areas (Maple/Mathematica for pure maths, R for statistics, etc)

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Language for theoretical physics

Postby Cosmologicon » Mon Oct 25, 2010 3:47 pm UTC

headprogrammingczar wrote:Haskell might be a good choice. It's not a commonly used language, but universities love it for how deep into CS theory you can go with it.

Clarification: CS departments in universities love it. Physics departments not so much.

rflrob
Posts: 235
Joined: Wed Oct 31, 2007 6:45 pm UTC
Location: Berkeley, CA, USA, Terra, Sol
Contact:

Re: Language for theoretical physics

Postby rflrob » Mon Oct 25, 2010 6:31 pm UTC

About Matlab: Much of the same functionality is replicated in the python NumPy and SciPy packages, sometimes down to the same function names and parameter order. I don't claim that it's a complete replacement for everything that lots of engineers use Matlab for, but it's so far done everything I've needed, and more, and with cleaner syntax and easier debugging tools.
Ten is approximately infinity (It's very large)
Ten is approximately zero (It's very small)

User avatar
thoughtfully
Posts: 2253
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: Language for theoretical physics

Postby thoughtfully » Mon Oct 25, 2010 8:06 pm UTC

Oh, and they're free, too. You know, if you wanted to play around with it at home or something.. or use it when you aren't in university anymore. Or use it on multiple platforms without getting extra licenses.
There's also octave.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Language for theoretical physics

Postby You, sir, name? » Tue Oct 26, 2010 3:25 am UTC

headprogrammingczar wrote:I haven't used it myself, but I have heard nothing but complaints about it from physics majors I know. Your mileage may vary, I suppose.


MATLAB is easy to use, and it's quick and easy to do very complex procedures. For general problems, it's quite slow, though, and C or fortran programs will almost always outperform it. MATLAB does matrices fast and well, and sucks at everything else. So MATLABs suitability is proportional to how efficiently the problem can be expressed in terms of matrix operations.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

Ayren
Posts: 18
Joined: Thu Feb 18, 2010 1:25 pm UTC

Re: Language for theoretical physics

Postby Ayren » Tue Oct 26, 2010 8:11 pm UTC

If he's doing a many-body problem, I'd say that expressing it in matrix form is a fine choice. I implemented my bachelor's thesis (mobile ad-hoc routing simulation) in MATLAB and it worked just fine. As the problems are both of the numeric simulation type, you should have little problems.

However, if you want to dazzle your professors, you can express the entire main loop in Haskell just by writing "iterate nextPositionFunction startPosition", for some suitable definitions of nextPositionFunction and startPosition.

Dark567
First one to notify the boards of Rick and Morty Season 3
Posts: 3686
Joined: Thu Jun 25, 2009 5:12 pm UTC
Location: Everywhere(in the US, I don't venture outside it too often, unfortunately)

Re: Language for theoretical physics

Postby Dark567 » Wed Oct 27, 2010 2:50 am UTC

You, sir, name? wrote:
headprogrammingczar wrote:I haven't used it myself, but I have heard nothing but complaints about it from physics majors I know. Your mileage may vary, I suppose.


MATLAB is easy to use, and it's quick and easy to do very complex procedures. For general problems, it's quite slow, though, and C or fortran programs will almost always outperform it. MATLAB does matrices fast and well, and sucks at everything else. So MATLABs suitability is proportional to how efficiently the problem can be expressed in terms of matrix operations.


If you know how to use it, it's not slow at all. You just can't look at it like procedural languages. Anytime you want to do a for loop, don't. Use matrices. Trying to use an if-statement? No, do some math instead. Objects, why the fuck did they even implement them in this language?

Proper Matlab involves learning a new paradigm, in a very similar way learning your first functional language is learning a completely new paradigm. The main problem with it is that the developers added every paradigm under the sun, so new Matlab programmers start by basically typing out C code.

(Honestly Matlab sucks at for-loops, which is why its slow. Don't use them. Anything else is probably okay, just not good style)
I apologize, 90% of the time I write on the Fora I am intoxicated.


Yakk wrote:The question the thought experiment I posted is aimed at answering: When falling in a black hole, do you see the entire universe's future history train-car into your ass, or not?

poohat
Posts: 230
Joined: Mon Apr 07, 2008 6:21 am UTC

Re: Language for theoretical physics

Postby poohat » Wed Oct 27, 2010 3:38 am UTC

Dark567 wrote:If you know how to use it, it's not slow at all.

Well its relative. You can make it faster by doing loops in a clever way etc, but its still an interpreted language and will be many times slower than compiled C. C.

For a lot of analysis matlab will be perfectly fine, and the built-in functions can be extremely fast (since they arent written in matlab). But if youre doing very computationally intensive large scale simulations then you may want to consider writing the most expensive functions in C/FORTRAN and then calling them from MATLAB. Thats a best-of-both-worlds solution.
Last edited by poohat on Wed Oct 27, 2010 3:49 am UTC, edited 2 times in total.

Dark567
First one to notify the boards of Rick and Morty Season 3
Posts: 3686
Joined: Thu Jun 25, 2009 5:12 pm UTC
Location: Everywhere(in the US, I don't venture outside it too often, unfortunately)

Re: Language for theoretical physics

Postby Dark567 » Wed Oct 27, 2010 3:44 am UTC

poohat wrote:
Dark567 wrote:If you know how to use it, it's not slow at all.

Yeah it is. You can make it faster by doing loops in a clever way etc, but its still an interpreted language and will be many times slower than compiled C.

If youre doing very computationally intensive large scale simulations then youll probably want to write the most expensive functions in C and then call them from MATLAB. Thats a best-of-both-worlds solution.


Your right it is interpreted, and slower than C, if you write very good C code. If you write Matlab right, the extra overhead caused by interpreting the language is minor. Sometimes the optimizations in Matlab will actually be able to make certain mathematical functions faster than C(assuming you don't make all those same optimizations in C, which most of us won't).
Last edited by Dark567 on Wed Oct 27, 2010 3:47 am UTC, edited 1 time in total.
I apologize, 90% of the time I write on the Fora I am intoxicated.


Yakk wrote:The question the thought experiment I posted is aimed at answering: When falling in a black hole, do you see the entire universe's future history train-car into your ass, or not?

poohat
Posts: 230
Joined: Mon Apr 07, 2008 6:21 am UTC

Re: Language for theoretical physics

Postby poohat » Wed Oct 27, 2010 3:46 am UTC

Dark567 wrote:
poohat wrote:
Dark567 wrote:If you know how to use it, it's not slow at all.

Yeah it is. You can make it faster by doing loops in a clever way etc, but its still an interpreted language and will be many times slower than compiled C.

If youre doing very computationally intensive large scale simulations then youll probably want to write the most expensive functions in C and then call them from MATLAB. Thats a best-of-both-worlds solution.


Your right it is interpreted, and slower than C, if you right very good C code. If you right Matlab right, the extra overhead caused by interpreted language is minor. Sometimes the optimizations in Matlab will actually be able to make certain mathematical functions faster than C(assuming you don't make all those same optimizations in C, which most of us won't).

I dont use MATLAB much but I use R intensively, and normally get around a 8-15x speed increase when I recode functions in C.

This link suggests that MATLAB is about twice as fast as R. So thats quite a bit slower than C



Although for many programming applications, the time taken to write code is longer than the time taken to run it, and you can generally write matlab/R code much faster than C.

poohat
Posts: 230
Joined: Mon Apr 07, 2008 6:21 am UTC

Re: Language for theoretical physics

Postby poohat » Wed Oct 27, 2010 3:52 am UTC

Also not all loops can be vectorized; if your problem is fundamentally iterative/sequential (eg youre processing data that is time-ordered, or you have an algorithm that is iterative like Newton-Rhapson, or youre optimizing a function and each evaluation is conditional on the previous one, etc etc) then youre going to need to use a for loop. That accounts for quite a lot of numerical computation; 90% of the code I write consists of sequential operations and hence cant be vectorized (which is partially why I get such a big speed up in C)

Even something basic like sorting a list will usually require a for loop or recursion

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Language for theoretical physics

Postby You, sir, name? » Wed Oct 27, 2010 6:58 am UTC

Yeah. As I said, not all problems translate well to matrices.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

Dark567
First one to notify the boards of Rick and Morty Season 3
Posts: 3686
Joined: Thu Jun 25, 2009 5:12 pm UTC
Location: Everywhere(in the US, I don't venture outside it too often, unfortunately)

Re: Language for theoretical physics

Postby Dark567 » Wed Oct 27, 2010 12:55 pm UTC

You, sir, name? wrote:Yeah. As I said, not all problems translate well to matrices.

Algorithms? Matlabs not really for sorting stuff. Thats not really what its their for. Lots of math(and physics) things can be solved using matrices that you wouldn't think of doing that way. PDE's are the first example that come to mind.
I apologize, 90% of the time I write on the Fora I am intoxicated.


Yakk wrote:The question the thought experiment I posted is aimed at answering: When falling in a black hole, do you see the entire universe's future history train-car into your ass, or not?

gorcee
Posts: 1501
Joined: Sun Jul 13, 2008 3:14 am UTC

Re: Language for theoretical physics

Postby gorcee » Wed Oct 27, 2010 3:20 pm UTC

poohat wrote:Although for many programming applications, the time taken to write code is longer than the time taken to run it, and you can generally write matlab/R code much faster than C.


This.

Sure, you can write some scripts in C. Now you want to visualize those results. Ok, time to go hit the internet for some open-source visualization tools. Now you have to implement them. Since they're open source, documentation is going to be spotty. You need to get the report in to your advisor by Tuesday, and you still have no idea if your code is really behaving properly. So now you're working over the weekend to implement this plotting functionality. By Sunday morning, you've gotten it working, but you'll be damned if you can figure out how to configure the plot to look good. By Monday night, you've figured it out, and you're quite proud of yourself, so you get some well-earned sleep, and sleep through your 10 AM meeting with your advisor on Tuesday.

Meanwhile, I write the code in MATLAB, and then type plot(thing1, thing2), and utilize complete (if not intuitive) documentation on how to get the plot looking right. I'm done by Friday evening, and I spend Friday at the bar, and then Saturday I head out maybe for a nice fall hike. I sleep in on Sunday, and make myself a nice pancake brunch.

MATLAB is for development environments, testing, and simulation. When performance is the critical benchmark, you can then write things in C, or even use EMLC to auto-generate C code from your MATLAB scripts.

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Language for theoretical physics

Postby Cosmologicon » Wed Oct 27, 2010 3:30 pm UTC

Uh, why don't you just output the calculations to a text file, and then do the plotting in MATLAB (or one of several other handy plotting tools that don't take all weekend to use)? Are you saying MATLAB can't handle data from other sources?

gorcee
Posts: 1501
Joined: Sun Jul 13, 2008 3:14 am UTC

Re: Language for theoretical physics

Postby gorcee » Wed Oct 27, 2010 3:40 pm UTC

Cosmologicon wrote:Uh, why don't you just output the calculations to a text file, and then do the plotting in MATLAB (or one of several other handy plotting tools that don't take all weekend to use)? Are you saying MATLAB can't handle data from other sources?


Sure, you could.

And then you'll say, "dammit, why didn't I just use MATLAB in the first place."

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Language for theoretical physics

Postby Cosmologicon » Wed Oct 27, 2010 3:51 pm UTC

Because it's slower... obv? In my situation, MATLAB wasn't available on the cluster I was running my simulations on.

Look, I agree that lots of the time MATLAB is the way to go. I haven't used it, but I've solved plenty of problems in Mathematica, Python, and IDL. But there are certainly problems where it makes more sense to do the heavy-duty work in something faster, even if that something is not built for visualization. I'd say a rule of thumb is when you expect your computation times to take at least ~1 day. (Keep in mind you'll probably be running it more than once.)

gorcee
Posts: 1501
Joined: Sun Jul 13, 2008 3:14 am UTC

Re: Language for theoretical physics

Postby gorcee » Wed Oct 27, 2010 4:09 pm UTC

Cosmologicon wrote:Because it's slower... obv? In my situation, MATLAB wasn't available on the cluster I was running my simulations on.

Look, I agree that lots of the time MATLAB is the way to go. I haven't used it, but I've solved plenty of problems in Mathematica, Python, and IDL. But there are certainly problems where it makes more sense to do the heavy-duty work in something faster, even if that something is not built for visualization. I'd say a rule of thumb is when you expect your computation times to take at least ~1 day. (Keep in mind you'll probably be running it more than once.)


Right, but in such a case, that violates the assumption made earlier of "development time > run time."

If your estimated run time is going to be on the order of n * days, then it's clear that your going to want some sort of optimized solution. Development time is typically on the order of n * days for sufficiently complicated problems (especially the kind that take n * days to run). In such a case, there's no argument.

But if your code is going to take n * minutes, then there's no real good reason to develop in C/C++ when MATLAB is available (particularly when it reduces the development time significantly). In the engineering world, MATLAB has additional benefits with Simulink, Real Time Workshop, and a host of toolboxes with already-designed-and-optimized routines for common things.

User avatar
Jplus
Posts: 1721
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: Language for theoretical physics

Postby Jplus » Wed Oct 27, 2010 4:22 pm UTC

Stoiker wrote:I have to start a bigger project in the many body field and I have to do it in Fortran. I also want to use another language parallel for this task to reach a high compatibility with code from other universities ... etc

So my question is: What language should I use ? C, C++, Phyton ?

best regards
(emphasis mine)

So it totally depends on what programming languages are being used in the many body field at other universities. It might actually be the case that those other universities aren't compatible with each other whatsoever. Or maybe they're all using Fortran already and that's actually the reason you have to do it in Fortran.
In any case, you'll have to do some field research before choosing a language. I think that, all other things being equal, you should choose a procedural language which allows for easy translation from Fortran without introducing efficiency issues. If you're working with matrices you must also make sure that the language provides an efficient way to do so, for example Boost.uBLAS in C++ or default in MATLAB.
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests