Next: , Previous:   [Contents][Index]

83 Package pytranslate


83.1 Introduction to pytranslate

pytranslate package provides Maxima to Python translation functionality. The package is experimental, and the specifications of the functions in this package might change. It was written as a Google Summer of Code project by Lakshya A Agrawal (Undergraduate Student, IIIT-Delhi) in 2019. A detailed project report is available as a GitHub Gist.

The package needs to be loaded in a Maxima instance for use, by executing load("pytranslate");
The statements are converted to python3 syntax. The file pytranslate.py must be imported for all translations to run, as shown in example.

Example:

(%i1) load ("pytranslate")$
/* Define an example function to calculate factorial */
(%i2) pytranslate(my_factorial(x) := if (x = 1 or x = 0) then 1
                  else x * my_factorial(x - 1));
(%o2) 
def my_factorial(x, v = v):
    v = Stack({}, v)
    v.ins({"x" : x})
    return((1 if ((v["x"] == 1) or (v["x"] == 0)) \
              else (v["x"] * my_factorial((v["x"] + (-1))))))
m["my_factorial"] = my_factorial
(%i3) my_factorial(5);
(%o3)                          120
>>> from pytranslate import *
>>> def my_factorial(x, v = v):
...     v = Stack({}, v)
...     v.ins({"x" : x})
...     return((1 if ((v["x"] == 1) or (v["x"] == 0)) \
...     else (v["x"] * my_factorial((v["x"] + (-1))))))
... 
>>> my_factorial(5)
120

The Maxima to Python Translator works in two stages:
1. Conversion of the internal Maxima representation to a defined Intermediate Representation, henceforth referred as IR(mapping is present in share/pytranslate/maxima-to-ir.html)
2. The conversion of IR to Python.

Supported Maxima forms:
1. Numbers(including complex numbers)
2. Assignment operators
3. Arithmetic operators(+, -, *, ^, /, !)
4. Logical operators(and, or, not)
5. Relational operators(>, <, >=, <=, !=, ==)
6. Lists
7. Arrays
8. block
9. Function and function calls
10. if-else converted to Python conditionals
11. for loops
12. lambda form

83.1.1 Tests for pytranslate

The tests for pytranslate are present at share/pytranslate/rtest_pytranslate.mac and can be run by executing batch(rtest_pytranslate, test);


83.2 Functions in pytranslate

Function: pytranslate (expr, [print-ir])

Translates the expression expr to equivalent python3 statements. Output is printed in the stdout.

Example:

(%i1) load ("pytranslate")$
(%i2) pytranslate('(for i:8 step -1 unless i<3 do (print(i))));
(%o2) 
v["i"] = 8
while not((v["i"] < 3)):
    m["print"](v["i"])
    v["i"] = (v["i"] + -1)
del v["i"]

expr is evaluated, and the return value is used for translation. Hence, for statements like assignment, it might be useful to quote the statement:

(%i1) load ("pytranslate")$
(%i2) pytranslate(x:20);
(%o2) 
20
(%i3) pytranslate('(x:20));
(%o3) 
v["x"] = 20

Passing the optional parameter (print-ir) to pytranslate as t, will print the internal IR representation of expr and return the translated python3 code.

(%i1) load("pytranslate");
(%o1) pytranslate
(%i2) pytranslate('(plot3d(lambda([x, y], x^2+y^(-1)), [x, 1, 10],
                   [y, 1, 10])), t);
(body
 (funcall (element-array "m" (string "plot3d"))
          (lambda
              ((symbol "x") (symbol "y")
               (op-no-bracket
                =
                (symbol "v")
                (funcall (symbol "stack") (dictionary) (symbol "v"))))
            (op +
                (funcall (element-array (symbol "m") (string "pow"))
                         (symbol "x") (num 2 0))
                (funcall (element-array (symbol "m") (string "pow"))
                         (symbol "y") (unary-op - (num 1 0)))))
          (struct-list (string "x") (num 1 0) (num 10 0))
          (struct-list (string "y") (num 1 0) (num 10 0))))
(%o2) 
m["plot3d"](lambda x, y, v = Stack({}, v): (m["pow"](x, 2) + m["\
pow"](y, (-1))), ["x", 1, 10], ["y", 1, 10])
Function: show_form (expr)

Displays the internal maxima form of expr

(%i4) show_form(a^b);
((mexpt) $a $b) 
(%o4) a^b

83.3 Extending pytranslate

Working of pytranslate:


Next: , Previous:   [Contents][Index]