# Mathematica + Numerical Recipes

the idea of combining Mathematica with the new routines in the 3rd edition of Numerical Recipes (NR) is very interesting. In fact, there is a NR library to Matlab which works very well because not only it allows one to create NR code (in C++) and install it as a function in Matlab, but it also cleverly handles the input and output of data.

I know MathLink can be used to link C code to Mathematica, but seeing the coding level of it's Matlab counterpart, I wouldn't even dare to try and do this myself.

In my view, the true power of scientific computing is in combining different libraries/platforms, efficiently exploiting their merits and deficits.

-
As far as I know there is also NR for Java available. Reading the answer to this post will make it very easy to call those from MMa. – Matariki Jul 17 '12 at 23:05
"... the true power of scientific computing is in combining different libraries/platforms, efficiently exploiting their merits and deficits." Yeah, but only what's valuable. NR contains bad algorithms, I've benchmarked several of them myself. And google a bit for the NR controversy, and have a look at the Criticism section in the wikipedia article. And as a Mathematica user you'll find plenty of NR stuff that's already included in Mathematica. – Andreas Lauschke Jul 17 '12 at 23:06
Andreas, I agree. No platform is perfect, and the key is in exploiting the good routines of each one. Here is an example: a while ago I was solving the following somewhat general problem: find the local minima of a function starting at some specified point; then change the function slightly and find a new minima starting from the previous point; do this sequel toy a bunch of times. Since we are always close to the min, this Should require only a few function evaluations and thus be quite fast. – Gabriel Landi Jul 18 '12 at 0:29
@Gabriel: Then show your problem here. Pick the brilliant minds that are in this group. Maybe your Mathematica implementation was wrong or too simplistic, so from the solutions of others you could even learn how to do it better. The minimizers in Mathematica have several different method options, and there's many ways to use them in clever ways you've never seen before. Besides, there's also a few 3rd party optimization packages for Mathematica. – Andreas Lauschke Jul 18 '12 at 0:39
Sorry Andreas. That is not the point. I am sure you understand that different languages/platforms are better at different types of problems. By combining NR with Mathematica, my goal is to have a unified access to both of them. I am sorry, I don't want to turn this into a programming lenguage's discussion; my question was technical and I appreciate everyone's feedback. – Gabriel Landi Jul 18 '12 at 1:50

Here are a couple of thoughts:

1) If you want to link to NR, I suggest using LibraryLink since the overhead of MathLink may be too much for some applications. (In version 5, I used MathLink to link against BLAS libraries but an overhead was noticeable.)

2) A lot of quite sophisticated, high performance algorithms are already implemented in Mathematica. I'd suggest you try them first. As an arbitrary example (but there are many more), numerical integration is well implemented in Mathematica.

3) If your application area is not represented to your satisfaction (I'd like to hear about that), you might make a question from that here and there may be alternatives. So it might be good if ask for a specific numerical issue you'd like to solve and then see what is already available.

4) Additionally, I am not sure if you are aware of this, there are links to say, BLAS readily available. The libraries that come with Mathematica are quite optimized. (e.g. Intel's MKL for BLAS)

-
+1. Nail on the head. Don't try to fix what's not broken. – Andreas Lauschke Jul 17 '12 at 23:40
Hi Ruebenko, I appreciate the reply. I have the habit of comparing Mathematica and NR whenever possible and I know some Mathematica libraries are quite optimized. However, the habilitation if easily switching between one and the other is what I ultimately strive. For instance, sometimes I write code using NR and call it from Mathematica using terminal syntax + ReadList, which I know is not very efficient. – Gabriel Landi Jul 18 '12 at 0:24
@GabrielLandi, you mention comparison, then using ReadList is not going to be efficient in speed and likely memory, Have a look at LibraryLink that will improve the interface. Also, I am curious to hear what comes out of your comparison. – user21 Jul 18 '12 at 14:08
I recently compared BinaryReadList to Java FileInputStream on large files and found that they are speed-equivalent, and MathLink/JLink data transfer overhead was a minor factor since most of the time was spent on the system I/O calls. Based on this, and the fact that modern Java is speed-equivalent to C, I may expect not much performance boost from using LibraryLink vs BinaryReadList, except when you have to call the latter in a loop (having loops in C rather than Mathematica can of course make a difference for lightweight records). The usual ReadList is much slower, of course. – Leonid Shifrin Jul 18 '12 at 19:32
Yes, good point BinaryReadList should do much better than ReadList – user21 Jul 18 '12 at 19:34