For a description of the options used in the following examples, see CodeGenerationOptions.
Translate a simple expression and assign it to the name in the target code.
w = -2 * x * z + y * z + x
| |
Translate a list and assign it to an array with the name in the target code.
Translate a computation sequence. Optimize the input first.
s = 0.10e1 + x
t1 = math.log(s)
t2 = math.exp(-x)
t = t2 * t1
r = x * t + t2
| |
Declare that is a float and is an integer. Return the result in a string.
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:
|
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:
|
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.
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:
|
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:
|
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.
import scipy.linalg
def detHilbert (n):
return(scipy.linalg.det(scipy.linalg.hilbert(n)))
| |