# ToMatlab, from Mathematica

The classical image of a mathematician is that of someone who spends their days scribbling on paper or scratching at the blackboard, proving theorems by hand. However, these days pen and paper will only get you so far; eventually you will likely have to do some tedious algebra and, certainly in applied mathematics, some numerical work. For me, my go-to tool for algebraic manipulations is Mathematica. Mathematica is also pretty good for some light numerics or when it is necessary to do both numerical and symbolic work in the same routine. For purely numerical work, however, I still favour MATLAB both for its speed and simply because I am more familiar with it. On the downside, although MATLAB does have a symbolic toolbox, mixing both symbolic and numerical operations is still rather tedious in MATLAB, I find.

Recently, I came up against a problem that required me to using a combination of both Mathematica and MATLAB. The precise form of the problem is not important, but essentially I needed to find the roots of a high order polynomial and perform some singular value decompositions on a large matrix; this process needed to be repeated around a million times so, naturally, speed was important.

Using Mathematica, I was able to find the coefficients of the high order polynomial and the elements of my matrix in closed (if rather verbose) form. The problem then arose of transferring these expressions from Mathematica to MATLAB so that I could use MATLAB's roots routine to find the eigenvalues of the companion matrix, and hence the roots of the polynomial, and perform the singular value decomposition of my matrix. Sadly, Mathematica has no built-in routine for converting its code into MATLAB code (although Maple does, if I recall correctly) and the expressions were horrendously long, making the probability of creating typos whilst manually rekeying the coefficients almost one.

Expecting that this was a fairly common problem, I googled the relevant terms and was quickly rewarded with ToMatlab, a package written by Harri Ojanen, which converts Mathematica expressions to MATLAB code. After downloading the package, I simply copied the ToMatlab.m file to Mathematica's Applications directory. You can find the correct path by running the command

FileNameJoin[{$UserBaseDirectory, "Applications"}]

from within Mathematica. The package can then be loaded into the kernel by executing

<< ToMatlab`

in your notebook. Calling the function

ToMatlab[]

will then convert your expression to MATLAB code ready to be copied and pasted. Although the package does not seem to be maintained, everything appears to work correctly with the latest versions of MATLAB and Mathematica.

Whilst writing this, what was initially intended to be a brief, post I came across MATLink which seems to offer a richer interface between Mathematica and MATLAB. However, there appear to be some issues with running it on unix and so I have not had chance to try it out yet.