9

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!

7 Answers 7

5

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.

2
  • I also like the CL native bignum features. Jun 3, 2010 at 0:12
  • Isn't CL non-IEEE 754 compliant?
    – J D
    Dec 21, 2010 at 15:45
2

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.

1
  • Actually, I've already implemented everything in C but now I want to expand the project so I'm weighing my options...
    – Eelvex
    May 25, 2010 at 23:42
2

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.

1
  • 1
    I'm not familiar with OCaml ;), but Runge-Kutta (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 (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
    May 28, 2010 at 14:12
2

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

1

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.

1
  • * Thanks for suggesting GSLL! * Unfortunately, matlab would be waay too slow for my purposes
    – Eelvex
    May 25, 2010 at 23:37
1

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

2
  • You are right, python is also very good for this but unfortunately, it's too slow.
    – Eelvex
    Jun 3, 2010 at 19:46
  • Looking into PyDSTool might be worthwhile. Although it's a more general tool, it generates C solvers from a more math-oriented specification of ODEs on the fly. Added bonus: Poincare sections (Events ;) ).
    – mbudisic
    Apr 21, 2011 at 6:17
0

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.

18
  • 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. May 25, 2010 at 20:12
  • 3
    @Stephen - please, do not give such "flame war starting" opinions without providing hard evidence to back it up.
    – Rook
    May 25, 2010 at 20:24
  • 3
    @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. May 25, 2010 at 20:48
  • I would also point out that your comment should really be directed at James for "Fortran is usually regarded as best for numerics" =) May 25, 2010 at 20:49
  • 5
    @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. May 26, 2010 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.