Mathematica2Lisp



Actually, I wrote a program a few years ago to convert
Mathematica into lisp, in Mathematica:

LispFormat[x_,s_:$Output]:=
            If [Depth[Unevaluated[x]]==1,
                      WriteString[s,ToString[Unevaluated[InputForm[x]]]],
                          WriteString[s,"("];
                 LispFormat[Head[Unevaluated[x]]];
          Scan[Function[e, WriteString[s," "];
                   LispFormat[Unevaluated[e], s],
                HoldAllComplete],
   Unevaluated[x]];
              WriteString[s,")"]]

example:
LispFormat[x^2*Sin[x] + Sqrt[a x - b]]

displays as

(Plus (Power (Plus (Times -1 b) (Times a x)) 1/2) (Times (Power x 2) 
(Sin x)))

converting this to the correct internal macsyma form would require 
substituting
($sin simp)   for Sin
(mplus simp)  for Plus
(mexpt simp)  for Power

and a few other odds and ends.  The common lisp 1/2 might have
to be changed to ((rat) 1 2).

If you want to convert this to the correct external macsyma form, you
would have a much longer program, at least if you wanted to have
the minimum number of parentheses, and not ((x^2)*(sin(x)))+sqrt((a*x)-b)).
You would have to teach it about precedence.

RJF





Richard Fateman wrote:
> You do not have to do any parsing if you have a computer
> algebra system or two.
> 
> 1. To convert Mathematica to Macsyma, read the expression
> into Mathematica.
> In Mathematica you write a program to print out the expression
> in Macsyma form.  For example, change the formatting program
> so that Sin[x]  is printed sin(x).  etc.
> 
> 2. To convert Macsyma to Mathematica, read the expression into
> Macsyma.
> In Macsyma (actually in Lisp would be easier)  write a program
> that will take data like
> 
> (($f  simp) a b c) and print it like  f[a,b,c]
> 
> You also have to translate sin to Sin, etc.
> 
> Note that in Mathematica,  Plus[x,y,z] is the same as x+y+z.
> 
> I think that using MathML as an intermediate language is
> a really bad idea unless someone else has already done it.
> (This is possible, and should be looked into.)
> RJF
> 
> 
> kovzol wrote:
> 
>> Richard Fateman wrote:
>>
>>
>>> Your project is impossible except in a trivial sense.
>>
>>
>>
>> Yes, I know. :-)
>>
>>
>>> In the trivial sense, you can convert a tiny subset of each of
>>> these languages to the other. Or you could convert such a subset
>>> to MathML.
>>
>>
>>
>> This tiny subset should be enough first. I only need expression for the
>> first time.
>>
>>
>>> Such a "translation" could be accomplished in a few weeks with
>>> any parser generator tool if the languages were LL(1) or LALR(1).
>>> Which they are not.
>>
>>
>>
>> That's what I'd like to do. All of the expressions --- I mean here
>> algebraic expression, e.g. sin(exp(x^2-ln(a/b)*1/cos(c))) --- are "easy"
>> languages.
>>
>>
>>> For a larger class of expressions in any of the languages, your
>>> task may be as hard as translating between Hungarian and English,
>>> but much less useful.
>>
>>
>>
>> :-)
>>
>>
>>>> We'd like to use this parser/converter in Webmathematics Interactive,
>>>> however it is a common problem to convert formulae in general.
>>>
>>>
>>> It happens when someone wants to convert an already written
>>> program from one system to another, but at least some of this
>>> is inevitably done by hand.  I don't think this is a common
>>> problem requiring an automatic solution.  I suggest you read
>>> about mathml.
>>
>>
>>
>> There *are* programs which really need formula conversions. I mean word
>> processors (AbiWord, KWord, OpenOffice.org) or graphical, function
>> plotting software which may have export possibilities (MathPlot, 
>> GtkPlot).
>> However, this task seems to be very simple if one is an expert in using
>> parser generators (I'm not --- I have to learn it first). I also think
>> about an internal format, maybe this should be MathML. Thank you for your
>> suggestion. :-)
>>
>> Regards, Zoltan
>>
>> _______________________________________________
>> Maxima mailing list
>> Maxima@www.math.utexas.edu
>> http://www.math.utexas.edu/mailman/listinfo/maxima
> 
> 
> 
> _______________________________________________
> Maxima mailing list
> Maxima@www.math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima