12
$\begingroup$

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.

Does anyone know anything about this topic? I have asked the NR forum about this and received no answer.

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.

$\endgroup$
13
  • $\begingroup$ 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. $\endgroup$
    – Matariki
    Jul 17, 2012 at 23:05
  • 5
    $\begingroup$ "... 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. $\endgroup$ Jul 17, 2012 at 23:06
  • $\begingroup$ 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. $\endgroup$ Jul 18, 2012 at 0:29
  • 1
    $\begingroup$ @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. $\endgroup$ Jul 18, 2012 at 0:39
  • 1
    $\begingroup$ 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. $\endgroup$ Jul 18, 2012 at 1:50

1 Answer 1

15
$\begingroup$

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)

$\endgroup$
5
  • 5
    $\begingroup$ +1. Nail on the head. Don't try to fix what's not broken. $\endgroup$ Jul 17, 2012 at 23:40
  • $\begingroup$ 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. $\endgroup$ Jul 18, 2012 at 0:24
  • $\begingroup$ @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. $\endgroup$
    – user21
    Jul 18, 2012 at 14:08
  • $\begingroup$ 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. $\endgroup$ Jul 18, 2012 at 19:32
  • $\begingroup$ Yes, good point BinaryReadList should do much better than ReadList $\endgroup$
    – user21
    Jul 18, 2012 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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