I have to put this project on hold for a little bit, but since what's there 
is cool already I thought I'd mention it here.

"FoxySheep" is the Python portion of Robert Jacobson's Mathematica parser, 
with a crude translator to Python.

It also include translation from InputForm to FullForm and can show a parse 
tree for either. These are interesting in their own right if you are 
wondering how Mathematica will interpret something typed. 

However below I'll limit myself to just the InputForm to Python translation.

*Examples: *

foxy-sheep -o python
Enter a Mathematica expression. Enter either an empty line, Ctrl-C, or 
Ctrl-D to exit.

in:=* 2 + 2*
(2 + 2) 

Out[0]=4

in:= *1 + 2 + 3*
(1 + 2 + 3) 

Out[1]=6 
in:= *% + 4 *
(Out() + 4)

Out[2]=10 
in:= *((5 - 3) ^ (1 + 2))/ 4 *
((5 - 3) ** (1 + 2) / 4) 

Out[4]=2.0 
in:= *GCD[12,15]* 
math.gcd(12, 15) 

Out[5]=3 
in:= *{1, 2, 3} *
[1, 2, 3] 

Out[6]=[1, 2, 3]

And more examples: 

167.5 -> decimal.Decimal(167.5) 
15^^8 -> int(15, 8)
132` -> (132) 
1 / 10 3 -> (1 / 10 * 3) 
x^2 + y^2 -> (x ** 2 + y ** 2) 
Range[10] -> range(10) 
{a, b, c, d}[[3]] -> [a, b, c, d][2] # Handles 0-1 conversion 
{a, b, c, d, e, f}[[2 ;; 4]] -> [a, b, c, d, e, f][1:3] # ditto


Some comments about the above. Range[x] gets translated to range(x) 
although an argument could be made that it should be list(range(x)). Stuff 
like this is easily handled, but I'd probably wait until a use case where 
this messes up before addressing.

This project has been use-case driven: if no one (including myself) uses, I 
don' t feel compelled to fix. Right now I'm more interested in having the 
overall framework better covered.

Even though this handles the "toy" subset of Mathematica, you know the 
stuff that one presumes an undergraduate in a compiler class *should * be 
able to whip off as a homework excercise, just this subset may be useful. 
Just handling numbers in their full glory (with mantissa, exponent, base, 
numeric precision, complex parts, fractional parts) is useful. 

In fact that is what this StackExchange question convert mathematica math 
expression form to python math expression 
<https://mathematica.stackexchange.com/questions/85445/convert-mathematica-math-expression-form-to-python-math-expression>
  
is asking. And in order to handle that I needed added a conversion of E to 
math.e. I was tempted to add math.pi as well, but I am holding off on that 
until I come across a specific need for that. 

There is a lot of low hanging fruit of this sort that can be added. Dozens 
of functions and constants could be added. 

I will mention that one technique that I use that I find a win for anyone 
else who wants to extend this project or do the same thing in another 
project: go through the Mathematica tutorials and handle the simple 
examples there. For example the subscript examples come from Fast 
Introduction for Programmers 
<https://www.wolfram.com/language/fast-introduction-for-programmers/en/lists/>. 
The tutorial Fast Introduction for Math Students 
<https://www.wolfram.com/language/fast-introduction-for-math-students/en/entering-input/>
 I've 
gone over as well. 

I learned about the 1 indexing versus 0 indexing problem by having the 
program answer the tests that were posed in the first tutorial.

Conversion to Python is done via transforming “InputForm” input to an 
“OutputForm” parse tree, and then using that to convert to a Python AST. 
Finally a Python module, astor <https://pypi.org/project/astor/>is used to 
dump this to text. And that is why some of the examples above have the 
outer parenthesis. 

Why do we go through this more complicated for translating one string to 
another?

By keeping the structure as an AST we can contemplate more powerful 
transformations and make use of routines that exist for working with Python 
AST’s.

For example, translating {1,2,3} + 2 into Python, while not handled now, 
can be done by looking at the types of the operands of plus, and noticing 
one is a scalar while the other is a list.

The plan for handling this is basically to extend pytype  
<https://pypi.org/project/pytype/>to give the types of the two expressions 
on either side of "+". 

But as I said, right now I have to put this project on hold. It is  BSD-2Class 
on github  <https://github.com/rocky/FoxySheep2>so ohters can feel free to 
extend that or fork off of this. 

Enjoy!

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/5be6a69f-fe55-4476-9f8d-467a74fbada9n%40googlegroups.com.

Reply via email to