views:

427

answers:

8

Which language would you propose for solving a system with:

  • first order differential equations
  • complex variables
  • N-dimensions

using 4th order Runge Kutta or the like.

Speed matters a lot but would sacrifice for:

  • Elegant (clean and short) code
  • Flexibility + scalability

I'm mostly between a Lisp and OCaml but any other suggestion is welcomed.

Thanks!

A: 

Fortran or C, might want to look into NAG routines. C would be more flexible, and easier to understand, but Fortran is usually regarded as the best for numerics.

James
C's numerics model, especially with C99, is actually better than Fortran's. Fortran is used for this sort of thing because it's easy to write and people are familiar with it, not because it provides a better numerics environment.
Stephen Canon
@Stephen - please, do not give such "flame war starting" opinions without providing hard evidence to back it up.
ldigas
@Idigas: Ok, I'll back it up. One simple example: Fortran choses the "wrong" sign of zero for results when the input lies on the branch cut of several complex functions (especially square root and log). See Kahan's excellent paper "Branch cuts for complex elementary functions, or, Much ado about nothing's sign bit" for examples of where the Fortran choice is inferior to the C standard (which follows Kahan's recommendation). More generally, Fortran allows many performance optimizations that may have negative consequences for numerical stability that are disallowed by default in C.
Stephen Canon
I would also point out that your comment should really be directed at James for "Fortran is usually regarded as best for numerics" =)
Stephen Canon
@Stephen Canon - No. Although I don't agree with James completely either, this comment was directed at yours. But, let's leave that aside. Unfortunatelly, I cannot quite undestand your second sentence. I admit, my english may be lacking somewhat, so I ask, if you could elaborate on the "one simple..."
ldigas
As far as quoted papers go, I've unfortunatelly, nor the time nor the interest anymore to read either. In the past I've been convinced several times to read papers, recommended by others in here, on similar topics (usually, in discussions which almost always touch "fortran is wrong" subject), and almost every time, I found them highly biased. Yes, it is true, fortran's conventions are(!) usually different. But they're usually different only from what computer scientists are used to (C school with some others mixed in).
ldigas
I'm an engineer myself (not cs), with programming only as a second interest, and still today, I find myself suprised sometimes, how it's conventions are thinked through. While C in comparison, find extremelly confusing, having not (or at least, to me) a "common denuminator". I've used complex variables in fortran to some extent, but found no "wrong sign". Since fortran standard is made on the basis of customers in industry's requests on compiler developers (and then that goes through a committee ...), I disagree with stating it is
ldigas
inferior. Personally (this part is subjective) I generally disagree with some of *very respected figures here quoted*'s opinions - Knuth for example, on several issues, for whom I regularly see followed almost religiously, while so rarely critically thinked of.
ldigas
@Idigas: Responding to your comment that Fortran's conventions are "usually different only from what computer scientists are used to": I'm a mathematician by training, not a computer scientist. The author I cited, William Kahan (who won the Turing prize for his work on numerics), is also a mathematician. Fortran's conventions for the sign of zero in complex arithmetic, reassociation of arithmetic, and other optimizations that are rightly called "unsafe" in C are counterproductive to rigorous numerical programming. Great for performance, bad for reproducibility and correctness.
Stephen Canon
I'll admit, "best" wasn't the correct word to use. I was meaning fastest, as the OP was interested in speed. Of course, things change all the time, and even different compilers for the same language may create code which varies in performance. Given that it is a relatively simple algorithm to implement, it wouldn't be too time consuming to test in each language before extending the project. I personally would use C, as I find Fortran rather difficult to program in, especially the older flavours.
James
@Stephen Canon - unfortunatelly, what conventions mathematicians use are a complete mystery to me. I'm familiar with some, but not enough, that I can state facts. Therefore, you have me at a disadvantage here.Btw, what is "reproducibility and correctness" ?
ldigas
@James - I find the older flavours nowadays also somewhat hard to handle. But I also have trouble finding reasons why anyone would use them, since f90/95, has been here for what, *only* 20 years ?
ldigas
@Idigas - One would think no one would use them, but when cheap departments only have licences for the f77 NAG libraries, then your hand is forced. Unfortunately, having learnt f90, it took a long, long time debugging, only to realise that the first 7 characters of a line are comment characters in f77!
James
@James - Please, do not take words out of context and twirl them around. I haven't stated anywhere that no one uses them. I said, that they're harder to use then newer incardinations. If you're locked in, because of vendors or because of libraries, the that's it - we've nothing to talk about. But, compared to other languages today, yes f77 is somewhat archair - that's why I'm suggesting not to use it (my comment above). Instead use ("newer", only 20 years have they been here) versions of the language. I use a mixture of f95/f2003 (most of f2003
ldigas
features has now been implemented) and I don't know of any other language which is suited for the job, as it is (save matlab maybe, but it's rather slow and quirky for my taste ... not to mention that I don't feel like rewriting code with every new version). As far as last (7 columns) comment goes - that rule is usually on the first 2 pages of every fortran book/manual/handout. Willingless to learn is not an excuse. And I see you still haven't read it, since the first 7 characters are not comment characters. First 5 are line label
ldigas
characteres, while the 6th is reserved for line continuation characters. Comments go into a line which starts with an "C" (for comment). Also, every compiler I've seen lately (to remember it) has an option for handling fixed form and free form, as well as f77 feats. only. Your f77 code is also f90 code should you choose to compile it, since f77 is a complete subset of f90. You can also mix them (if you're having f77 libraries with f90 code). One of the things I like about fortran is that it was build in mind with the fact that some software lasts more than your usual OS lifetime.
ldigas
But listen, I'm getting tired here writing paragraphs of text, to correct every blunder someone makes in two sentences he writes. So I'm gonna stop now ... you use whatever you want. If you want to use newer features, get yourself a f95/2003 book and read it; if you want to keep a mindset that fortran is stuck at f77 - I've nothing against that. To some ignorance is bliss.
ldigas
@Idigas. I was not intending to twist your words, merely providing a personal example of having difficulty in Fortran. This thread is no longer productive, therefore this shall be my last comment in it
James
+1  A: 

Its hard to say which language would be easiest, there are lisp, C++, C#, etc libraries to accomplish this, so alot if it has to do with personal preference. I would speculate Matlab is the most tailored and elegant solution specifically for these types of tasks, and it has a lot of built in support for ODEs... Lisp may be on the slow side... and I can't speak for OCaml.

tbischel
* Thanks for suggesting GSLL! * Unfortunately, matlab would be waay too slow for my purposes
Eelvex
+2  A: 

RK4 is a very basic method, and there lots of excellent implementations that are already written. Use one of them, and spend your effort on other aspects of the project.

Stephen Canon
Actually, I've already implemented everything in C but now I want to expand the project so I'm weighing my options...
Eelvex
+5  A: 

Here's an implementation of RK in Common Lisp:

http://github.com/bld/bld-ode/blob/master/rk.lisp

The nice thing about Common Lisp is that you can start with simple and elegant code and then make the critical bits run fast (e.g. by switching from mostly functional to stateful computation, or by declaring types).

SBCL has an excellent native-code compiler.

skypher
I also like the CL native bignum features.
Paul Nathan
+1  A: 

I'm not familiar with Runge Kutta, but OCaml can provide good speed and readability in general, at least if you're a bit careful. You then have the advantage of a robust static type system for the rest of your application.

Michael E
I'm not familiar with OCaml ;), but Runge-Kutta (http://en.wikipedia.org/wiki/Runge_kutta) refers to a class of algorithms for numerically solving systems of differential equations. The 1st order Runge_Kutta algorithm (RK1) is also known as Euler's Method (http://en.wikipedia.org/wiki/Euler_method) but it tends to rapidly lose accuracy. For many applications the 4th order Runge-Kutta algorithm (RK4) offers good accuracy and time performance.
andand
+2  A: 

apart from anything else, you can write ocaml bindings to your existing C runge-kutta solver.

Martin DeMello
A: 

I would suggest using python+numpy+scipy, the general math and numeric support (superbe multidimensional arrays) is excellent. Anyway it depends on specific needs.

pygabriel
You are right, python is also very good for this but unfortunately, it's too slow.
Eelvex
A: 

numpy and scipy, especially the ODE part, are wrappers around fortran libs, so they aren't slow for these tasks

brazhe