Python - Maple Help

CodeGeneration

 Python
 translate Maple code to Python code

 Calling Sequence Python(x, cgopts)

Parameters

 x - expression, list, rtable, procedure, or module cgopts - (optional) one or more CodeGeneration options

Description

 • The Python(x, cgopts) calling sequence translates Maple code to Python code.
 - If the parameter x is an algebraic expression, then a Python statement assigning the expression to a variable is generated.
 - If x is a list, Maple Array, or rtable of algebraic expressions, then a sequence of Python statements assigning the elements to a Python array is produced.  Only the initialized elements of the rtable or Maple Array are translated.
 - If x is a list of equations $\mathrm{nm}=\mathrm{expr}$, where $\mathrm{nm}$ is a name and $\mathrm{expr}$ is an algebraic expression, then this is understood as a sequence of assignment statements.  In this case, the equivalent sequence of Python assignment statements is generated.
 - If x is a procedure, then a Python class is generated containing a function equivalent to the procedure, along with any necessary import statements.
 - If x is a module, then a Python class is generated, as described on the PythonDetails help page.
 • The parameter cgopts may include one or more CodeGeneration options, as described in CodeGenerationOptions.

Examples

For a description of the options used in the following examples, see CodeGenerationOptions.

 > $\mathrm{with}\left(\mathrm{CodeGeneration}\right):$

Translate a simple expression and assign it to the name $w$ in the target code.

 > $\mathrm{Python}\left(x+yz-2xz,\mathrm{resultname}="w"\right)$
 w = -2 * x * z + y * z + x

Translate a list and assign it to an array with the name $w$ in the target code.

 > $\mathrm{Python}\left(\left[\left[x,2y\right],\left[5,z\right]\right],\mathrm{resultname}="w"\right)$
 w = [[x,2 * y],[5,z]]

Translate a computation sequence.  Optimize the input first.

 > $\mathrm{cs}≔\left[s=1.0+x,t=\mathrm{ln}\left(s\right)\mathrm{exp}\left(-x\right),r=\mathrm{exp}\left(-x\right)+xt\right]:$
 > $\mathrm{Python}\left(\mathrm{cs},\mathrm{optimize}\right)$
 s = 0.10e1 + x t1 = math.log(s) t2 = math.exp(-x) t = t2 * t1 r = x * t + t2

Declare that $x$ is a float and $y$ is an integer. Return the result in a string.

 > $s≔\mathrm{Python}\left(x+y+1,\mathrm{declare}=\left[x::\mathrm{float},y::\mathrm{integer}\right],\mathrm{output}=\mathrm{string}\right)$
 ${s}{≔}{"cg = x + y + 1"}$ (1)

Translate a procedure.  Assume that all untyped variables have type integer.

 > f := proc(x, y, z) return x*y-y*z+x*z; end proc:
 > $\mathrm{Python}\left(f,\mathrm{defaulttype}=\mathrm{integer}\right)$
 def f (x, y, z):     return(y * x - y * z + x * z)

Translate a procedure containing an implicit return.  A new variable is created to hold the return value.

 > f := proc(n)   local x, i;   x := 0.0;   for i to n do     x := x + i;   end do; end proc:
 > $\mathrm{Python}\left(f\right)$
 def f (n):     x = 0.0e0     for i in range(1, n + 1):         x = x + i         cgret = x     return(cgret)

Translate a linear combination of hyperbolic trigonometric functions.

 > $\mathrm{Python}\left(2\mathrm{cosh}\left(x\right)-7\mathrm{tanh}\left(x\right)\right)$
 cg0 = 2 * math.cosh(x) - 7 * math.tanh(x)

Translate a procedure with no return value containing a printf statement.

 > f := proc(a::integer, p::integer)   printf("The integer remainder of %d divided by %d is: %d\n", a, p, irem(a, p)); end proc:
 > $\mathrm{Python}\left(f\right)$
 def f (a, p):     printf("The integer remainder of %d divided by %d is: %d\n", a, p, a % p)

Translate a procedure involving linear algebra.

 > detHilbert := proc(n :: posint)    uses LinearAlgebra;    return Determinant( HilbertMatrix( n ) ); end proc:
 > $\mathrm{Python}\left(\mathrm{detHilbert}\right)$
 import numpy.linalg import scipy.linalg def detHilbert (n):     return(numpy.linalg.det(scipy.linalg.hilbert(n)))

Notice that by default CodeGeneration translates LinearAlgebra[Determinant] to the Python function numpy.linalg.det.  However as the Python scipy.linalg library is already being used in the above example and also contains a implementation of the determinant, it may be simpler to use that translation.  This may be achieved using the libraryorder option to indicate that scipy.linalg is preferred over numpy.linalg.

 > $\mathrm{Python}\left(\mathrm{detHilbert},\mathrm{libraryorder}=\left["scipy.linalg","numpy.linalg"\right]\right)$
 import scipy.linalg def detHilbert (n):     return(scipy.linalg.det(scipy.linalg.hilbert(n)))

Compatibility

 • The CodeGeneration[Python] command was introduced in Maple 18.