Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Porting Mathematica to Octave

I have to port a lot of files from Mathematica to Octave. I found a Lisp Mathematica parser from ~1991 but I am not really familiar with Lisp, so I was wondering if anyone has any experience with porting in this direction. After researching and sending an email to WolframAlpha and no real results I would have to use Lex and Yacc to produce a cross compiler. This seems a bit excessive to me.

Any hints or pointers would be greatly appreciated.

Clarification:

I am starting with a lot of Mathematica files and their functionality has to be ported to Octave. I just want to achieve this goal in as less time as possible, as this is a task my boss gave me to do over the holidays. Thanks for your help, I will look at FullForm and examine the Mathematica file for non-portable content. If it is just possible to convert a certain amount of the files, I would have to do the rest by hand, which would take some time. So this is basically a one time thing to move from one program to another.

As mentioned in Leonid's post, the task seems excessive but I am a student research assistant and this is exactly the task I have to complete in my department.

like image 408
wikke Avatar asked Dec 14 '11 16:12

wikke


3 Answers

I work with both MATLAB (the $$$$ version of Octave) and Mathematica fairly regularly, and I'm pretty damn confident that it is impossible to implement an parser/porter that automatically converts from Mathematica to the other. For starters, MATLAB/Octave does not support pattern matching or term re-writing or functional programming, all of which are the most common and optimal coding styles in Mathematica.

For example, consider the pure function (Mathematica)

f = #^2&;

There really isn't an equivalent of this in MATLAB/Octave. Anonymous functions come close enough and you could write it as (MATLAB/Octave)

g=@(x)x.^2;

However, Mathematica will symbolically square anything you pass to the function, whereas MATLAB will only work on scalars/vectors/matrices. So even if you come across a pure function, you can't automatically rewrite it into an anonymous function without understanding what is being passed to the pure function.

Another concept that's missing in MATLAB/Octave is that of lazy evaluation/SetDelayed or := in mathematica. So if your pure function was

f := a #^2 &;

then, f uses the value of a at the time when f is called. However, writing it as an anonymous function:

g = @(x)a*x.^2;

will capture the value of a at the time of definition, forming a closure. I provide a better example in this answer of mine

If you have a code base that is written primarily in a procedural style in Mathematica, then it might be possible, in theory, to translate to octave. However, I have a hard time imagining a serious Mathematica project written entirely in a procedural manner. In short, it takes a human to translate the logic in a manner idiomatic to each language


Lastly, if you're trying to just convert expressions from Mathematica to MATLAB/Octave, then you might want to take a look at the ToMatlab package. I use this quite often, to convert expressions from computations in Mathematica for use in a different code base in MATLAB. A simple example would be:

expr = Sin[x + x^3] + ArcSin[y];
ToMatlab[expr]
Out[1]= sin(x + x.^3) + asin(y)
like image 200
abcd Avatar answered Nov 09 '22 00:11

abcd


You don't need Mathematica parser to port. You need to write your porting program in Mathematica and reuse the Mathematica's own parser. This is along the same direction as SymbolicC. If I were you, I would construct some Mathematica sub-language (call it SymbolicOctave), with completely inert heads, and then write a function called say ToOctaveCodeString. You can consult the implementation of SymbolicC to see how this was done for C - it is available within the Mathematica distribution.

The second, and most complicated part, would be to write a translator from the subset of Mathematica code to this inert SymbolicOctave representation. This part is really non-trivial, because (presumably) Mathematica language has a lot of features that Octave language does not natively support, and you will have to implement those in Octave. Here you will also need to use certain techniques to generate Mathematica code with Mathematica, one of which I described in this answer. This all assumes that you have access to Mathematica.

Having said all that, I'd give up on the general task and just use Mathematica, if there is such option. The task seems anywhere feasible only for a very narrow and speciic subset of Mathematica, and even then would be non-trivial. If there is an option to link Mathematica and use it from Octave instead of porting everything to Octave, I'd go that way (technically this should be easy to do). With the cost of even commercial license for full Mathematica being rather low these days (comparing to the competing products, particularly having in mind products similar to Octave), it may well be a better option also financially, given the development costs to create a converter which would be any good.

like image 32
Leonid Shifrin Avatar answered Nov 09 '22 00:11

Leonid Shifrin


An idiomatic way of generating a code for a less capable system from Mathematica is not to use a third party tool (which should be as powerful as Mathematica core), but to use Mathematica itself.

Some of the code generators of this kind are already available out of the box (for C and Fortran), take a look at how they're implemented and then retarget them for Octave/Matlab.

like image 2
SK-logic Avatar answered Nov 09 '22 00:11

SK-logic