Thursday, 15 September 2011

Adventures in MathLink

Recently I became interested in MathLink, a feature in Mathematica allowing for external calls to C programs.  There is also a J/Link project allowing Java to interface with Mathematica.

In simple terms, MathLink allows Mathematica to run pass some arguments to a C program, run the program and return the results.  An example use of such a feature might be a numerical project.  Suppose one is interested in a numerical problem and would like to develop tools to perform simulations.  Typically Mathematica is slow at large scale numerical work, or, perhaps it is that a particular C library would be very useful.  Either way, one decides C code would be useful.  Using MathLink one could develop a toolkit of functions combining the efficiency of C code with the analytic tools and user interface of Mathematica.  Clearly this would be very useful to some people.

It is also possible to use MathLink in reverse, that is to run Mathematica as a subroutine inside a C program.  I did not explore this option.

To learn about MathLink I set myself three projects.  Firstly writing a simple proof-of-concept program in C that Mathematica could call.  Secondly writing a C program that itself calls a FORTRAN program.  Mathematica would then be able to call the C program, which would use a FORTRAN program to perform a computation, and return the results to Mathematica.  This may be useful to remaining FORTRAN users and it is a simple language for C code to call.  Thirdly, and most ambitiously, I wanted to produce a MathLink solution that allowed Mathematica to call Lisp code, again through a C program.

Each of the three tasks will make up further blog entries.

No comments:

Post a Comment