Overview of the codegen Package - Maple Programming Help

Home : Support : Online Help : Programming : codegen Package : codegen

Overview of the codegen Package

 Calling Sequence codegen[command](arguments)

Description

 • The code generation (codegen) package contains tools for creating, manipulating, and translating Maple procedures into other languages. This includes tools for automatic differentiation of Maple procedures, code optimization, translation into C, Fortran and MathML, and an operation count of a Maple procedure.
 • Each command in the codegen package can be accessed by using either the long form or the short form of the command name in the command calling sequence.
 • Important: The newer CodeGeneration package also offers translation of Maple code to other languages. The codegen[C] and codegen[fortran] commands have been deprecated, and the superseding commands CodeGeneration[C] and CodeGeneration[Fortran] should be used instead.  Additionally, codegen[maple2intrep] and codegen[intrep2maple] have been superseded by ToInert and FromInert.

List of codegen Package Commands

 • The following is a list of available commands.

 To display the help page for a particular codegen command, see Getting Help with a Command in a Package.

Examples

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

The first example shows how the package can be used to create a Fortran subroutine to compute a vector valued function.

 > $f≔1-{ⅇ}^{-t}xy:$
 > $g≔x-y{ⅇ}^{-t}{x}^{2}:$
 > $v≔\left[\begin{array}{cc}f& g\end{array}\right]$
 ${v}{≔}\left[\begin{array}{cc}{1}{-}{{ⅇ}}^{{-}{t}}{}{x}{}{y}& {x}{-}{y}{}{{ⅇ}}^{{-}{t}}{}{{x}}^{{2}}\end{array}\right]$ (1)
 > fg := makeproc(v,parameters=[t,x,y]);
 ${\mathrm{fg}}{≔}{\mathbf{proc}}\left({t}{,}{x}{,}{y}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{v}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{v}{≔}{\mathrm{array}}{}\left({1}{..}{2}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{v}{[}{1}{]}{≔}{1}{-}{\mathrm{exp}}{}\left({−}{t}\right){*}{x}{*}{y}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{v}{[}{2}{]}{≔}{x}{-}{y}{*}{\mathrm{exp}}{}\left({−}{t}\right){*}{x}{^}{2}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{v}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2)
 > $\mathrm{fortran}\left(\mathrm{fg},\mathrm{optimized}\right)$
 subroutine fg(t,x,y,crea_par)       doubleprecision t       doubleprecision x       doubleprecision y       doubleprecision crea_par(2)       doubleprecision t1       doubleprecision t5       doubleprecision v(2)         t1 = exp(-t)         v(1) = -t1*x*y+1         t5 = x**2         v(2) = -y*t1*t5+x         crea_par(1) = v(1)         crea_par(2) = v(2)         return         return       end

The function f below computes the square of the distance from a point (x,y) to a circle of radius r with centre (h,k) under translation by (C,D) followed by rotation of theta radians. We use automatic differentiation to compute the gradient of the function f with respect to C,D,theta and generate C code for the result.  Finally we output the computational cost of the optimized code.

 > f := proc(C,D,theta,x,y,h,k,r) local s,c,xbar,ybar,d1,d2,d;   s := sin(theta);   c := cos(theta);   xbar := (x+C)*c + (y+D)*s;   ybar := (y+D)*c - (x+C)*s;   d1 := (h-xbar)^2;   d2 := (k-ybar)^2;   d := sqrt( d1+d2 ) - r;   d^2 end proc:
 > $G≔\mathrm{GRADIENT}\left(f,\left[C,\mathrm{D},\mathrm{θ}\right],\mathrm{result_type}=\mathrm{array}\right)$
 ${G}{≔}{\mathbf{proc}}\left({C}{,}{\mathrm{D}}{,}{\mathrm{θ}}{,}{x}{,}{y}{,}{h}{,}{k}{,}{r}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{c}{,}{d}{,}{\mathrm{d1}}{,}{\mathrm{d2}}{,}{\mathrm{dfr0}}{,}{\mathrm{grd}}{,}{s}{,}{\mathrm{xbar}}{,}{\mathrm{ybar}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{s}{≔}{\mathrm{sin}}{}\left({\mathrm{θ}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{c}{≔}{\mathrm{cos}}{}\left({\mathrm{θ}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{xbar}}{≔}\left({x}{+}{C}\right){*}{c}{+}\left({y}{+}{\mathrm{D}}\right){*}{s}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{ybar}}{≔}\left({y}{+}{\mathrm{D}}\right){*}{c}{-}\left({x}{+}{C}\right){*}{s}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{d1}}{≔}\left({h}{-}{\mathrm{xbar}}\right){^}{2}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{d2}}{≔}\left({k}{-}{\mathrm{ybar}}\right){^}{2}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{d}{≔}{\mathrm{sqrt}}{}\left({\mathrm{d1}}{+}{\mathrm{d2}}\right){-}{r}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{dfr0}}{≔}{\mathrm{array}}{}\left({1}{..}{7}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{dfr0}}{[}{7}{]}{≔}{2}{*}{d}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{dfr0}}{[}{6}{]}{≔}{1}{/}{2}{*}{\mathrm{dfr0}}{[}{7}{]}{/}\left({\mathrm{d1}}{+}{\mathrm{d2}}\right){^}\left({1}{/}{2}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{dfr0}}{[}{5}{]}{≔}{1}{/}{2}{*}{\mathrm{dfr0}}{[}{7}{]}{/}\left({\mathrm{d1}}{+}{\mathrm{d2}}\right){^}\left({1}{/}{2}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{dfr0}}{[}{4}{]}{≔}{\mathrm{dfr0}}{[}{6}{]}{*}\left({−}{2}{*}{k}{+}{2}{*}{\mathrm{ybar}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{dfr0}}{[}{3}{]}{≔}{\mathrm{dfr0}}{[}{5}{]}{*}\left({−}{2}{*}{h}{+}{2}{*}{\mathrm{xbar}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{dfr0}}{[}{2}{]}{≔}{\mathrm{dfr0}}{[}{4}{]}{*}\left({y}{+}{\mathrm{D}}\right){+}{\mathrm{dfr0}}{[}{3}{]}{*}\left({x}{+}{C}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{dfr0}}{[}{1}{]}{≔}{\mathrm{dfr0}}{[}{4}{]}{*}\left({−}{x}{-}{C}\right){+}{\mathrm{dfr0}}{[}{3}{]}{*}\left({y}{+}{\mathrm{D}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{≔}{\mathrm{array}}{}\left({1}{..}{3}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{[}{1}{]}{≔}{0}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{[}{2}{]}{≔}{c}{*}{\mathrm{dfr0}}{[}{4}{]}{+}{s}{*}{\mathrm{dfr0}}{[}{3}{]}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{[}{3}{]}{≔}{−}{\mathrm{dfr0}}{[}{2}{]}{*}{\mathrm{sin}}{}\left({\mathrm{θ}}\right){+}{\mathrm{dfr0}}{[}{1}{]}{*}{\mathrm{cos}}{}\left({\mathrm{θ}}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (3)
 > $C\left(G,\mathrm{optimized},\mathrm{precision}=\mathrm{single}\right)$
 #include void G(C,D,theta,x,y,h,k,r,grd) float C; float D; float theta; float x; float y; float h; float k; float r; float grd[3]; {   float c;   float d1;   float d2;   float dfr0[7];   float s;   float t1;   float t10;   float t12;   float t20;   float t22;   float t3;   float t7;   float t8;   float t9;   {     s = sin(theta);     c = cos(theta);     t1 = x+C;     t3 = y+D;     t7 = -t1*c-t3*s+h;     d1 = t7*t7;     t8 = -t3*c+t1*s+k;     d2 = t8*t8;     t9 = d1+d2;     t10 = sqrt(t9);     dfr0[6] = 2.0*t10-2.0*r;     t12 = sqrt(t9);     dfr0[5] = dfr0[6]/t12/2.0;     dfr0[4] = dfr0[5];     dfr0[3] = -2.0*dfr0[4]*t8;     dfr0[2] = -2.0*dfr0[4]*t7;     t20 = dfr0[3];     t22 = dfr0[2];     dfr0[1] = t22*t1+t20*t3;     dfr0[0] = -t20*t1+t22*t3;     grd[0] = 0.0;     grd[1] = t20*c+t22*s;     grd[2] = dfr0[0]*c-dfr0[1]*s;     return;   } }
 > $\mathrm{cost}\left(\mathrm{optimize}\left(G\right)\right)$
 ${23}{}{\mathrm{storage}}{+}{25}{}{\mathrm{assignments}}{+}{5}{}{\mathrm{functions}}{+}{12}{}{\mathrm{additions}}{+}{22}{}{\mathrm{multiplications}}{+}{18}{}{\mathrm{subscripts}}{+}{\mathrm{divisions}}$ (4)

In this example, we compute 1+x+x^2/2+x^3/6+...+x^n/n! We do this in two ways.  Firstly, we use a symbolic sum. To obtain C code we explicitly convert the symbolic sum to a for loop.

 > $f≔{\sum }_{i=0}^{n}\phantom{\rule[-0.0ex]{5.0px}{0.0ex}}\frac{{x}^{i}}{i!}$
 ${f}{≔}{\sum }_{{i}{=}{0}}^{{n}}\phantom{\rule[-0.0ex]{5.0px}{0.0ex}}\frac{{{x}}^{{i}}}{{i}{!}}$ (5)
 > F := makeproc(f,parameters=[n,x],locals=[i]);
 ${F}{≔}{\mathbf{proc}}\left({n}{,}{x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Sum}}{}\left({x}{^}{i}{/}{\mathrm{factorial}}{}\left({i}\right){,}{i}{=}{0}{..}{n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (6)
 > $F≔\mathrm{prep2trans}\left(F\right)$
 ${F}{≔}{\mathbf{proc}}\left({n}{,}{x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{,}{\mathrm{i1}}{,}{\mathrm{s1}}{,}{\mathrm{t1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{0}{<}{−}{n}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}{≔}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{else}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{t1}}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{t1}}{≔}{x}{*}{\mathrm{t1}}{/}{\mathrm{i1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}{≔}{\mathrm{s1}}{+}{\mathrm{t1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (7)
 > $F≔\mathrm{declare}\left(n::\mathrm{integer},F\right)$
 ${F}{≔}{\mathbf{proc}}\left({n}{::}{\mathrm{integer}}{,}{x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{,}{\mathrm{i1}}{,}{\mathrm{s1}}{,}{\mathrm{t1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{0}{<}{−}{n}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}{≔}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{else}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{t1}}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{t1}}{≔}{x}{*}{\mathrm{t1}}{/}{\mathrm{i1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}{≔}{\mathrm{s1}}{+}{\mathrm{t1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (8)
 > $C\left(F\right)$
 double F(n,x) int n; double x; {   double i;   int i1;   double s1;   double t1;   {     if( 0.0 < -n )       s1 = 0.0;     else       {         t1 = 1.0;         s1 = 1.0;         for(i1 = 1;i1 <= n;i1++)         {           t1 = x/i1*t1;           s1 += t1;         }       }     return(s1);   } }

In the second approach, our program uses a loop to compute the finite sum. We use the intermediate representation for programs provided by the codegen package.  This representation is an expression tree.  It can be converted into a Maple procedure using the intrep2maple command which can then be converted into Fortran or C code if desired.

 > $f≔\mathrm{Proc}\left(\mathrm{Parameters}\left(n::\mathrm{integer},x::\mathrm{float}\right),\mathrm{Locals}\left(i,s,t\right),\mathrm{StatSeq}\left(\mathrm{Assign}\left(s,1.0\right),\mathrm{Assign}\left(t,1.0\right),\mathrm{For}\left(i,1,1,n,\mathrm{true},\mathrm{StatSeq}\left(\mathrm{Assign}\left(t,\frac{xt}{i}\right),\mathrm{Assign}\left(s,s+t\right)\right)\right),s\right)\right)$
 ${f}{≔}{\mathrm{Proc}}{}\left({\mathrm{Parameters}}{}\left({n}{::}{\mathrm{integer}}{,}{x}{::}{\mathrm{float}}\right){,}{\mathrm{Locals}}{}\left({i}{,}{s}{,}{t}\right){,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({s}{,}{1.0}\right){,}{\mathrm{Assign}}{}\left({t}{,}{1.0}\right){,}{\mathrm{For}}{}\left({i}{,}{1}{,}{1}{,}{n}{,}{\mathrm{true}}{,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({t}{,}\frac{{x}{}{t}}{{i}}\right){,}{\mathrm{Assign}}{}\left({s}{,}{s}{+}{t}\right)\right)\right){,}{s}\right)\right)$ (9)
 > $F≔\mathrm{intrep2maple}\left(f\right)$
 ${F}{≔}{\mathbf{proc}}\left({n}{::}{\mathrm{integer}}{,}{x}{::}{\mathrm{float}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{,}{s}{,}{t}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{s}{≔}{1.0}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{t}{≔}{1.0}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{t}{≔}{x}{*}{t}{/}{i}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{s}{≔}{s}{+}{t}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{s}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (10)

In this example, we compute the gradient and hessian of a simple function, manipulate the procedures then join the two together.

 > $f≔1-{ⅇ}^{-t}xy$
 ${f}{≔}{1}{-}{{ⅇ}}^{{-}{t}}{}{x}{}{y}$ (11)
 > F := makeproc(f,[x,y,t]);
 ${F}{≔}{\mathbf{proc}}\left({x}{,}{y}{,}{t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{1}{-}{\mathrm{exp}}{}\left({−}{t}\right){*}{x}{*}{y}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (12)
 > $G≔\mathrm{GRADIENT}\left(F,\left[x,y\right],\mathrm{result_type}=\mathrm{array}\right)$
 ${G}{≔}{\mathbf{proc}}\left({x}{,}{y}{,}{t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{≔}{\mathrm{array}}{}\left({1}{..}{2}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{[}{1}{]}{≔}{−}{\mathrm{exp}}{}\left({−}{t}\right){*}{y}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{[}{2}{]}{≔}{−}{\mathrm{exp}}{}\left({−}{t}\right){*}{x}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (13)
 > $G≔\mathrm{dontreturn}\left(\mathrm{grd},\mathrm{makeparam}\left(\mathrm{grd},G\right)\right)$
 ${G}{≔}{\mathbf{proc}}\left({x}{,}{y}{,}{t}{,}{\mathrm{grd}}{::}\left({\mathrm{array}}{}\left({1}{..}{2}\right)\right)\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{[}{1}{]}{≔}{−}{\mathrm{exp}}{}\left({−}{t}\right){*}{y}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{[}{2}{]}{≔}{−}{\mathrm{exp}}{}\left({−}{t}\right){*}{x}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (14)
 > $H≔\mathrm{HESSIAN}\left(F,\left[x,y\right],\mathrm{result_type}=\mathrm{array}\right):$
 > $H≔\mathrm{renamevar}\left(\mathrm{grd}=\mathrm{hes},\mathrm{dontreturn}\left(\mathrm{grd},\mathrm{makeparam}\left(\mathrm{grd},H\right)\right)\right)$
 ${H}{≔}{\mathbf{proc}}\left({x}{,}{y}{,}{t}{,}{\mathrm{hes}}{::}\left({\mathrm{array}}{}\left({1}{..}{2}{,}{1}{..}{2}\right)\right)\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{df}}{,}{\mathrm{dfr0}}{,}{\mathrm{grd1}}{,}{\mathrm{grd2}}{,}{\mathrm{t1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{t1}}{≔}{\mathrm{exp}}{}\left({−}{t}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd1}}{≔}{−}{\mathrm{t1}}{*}{y}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd2}}{≔}{−}{\mathrm{t1}}{*}{x}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{df}}{≔}{\mathrm{array}}{}\left({1}{..}{2}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{dfr0}}{≔}{\mathrm{array}}{}\left({1}{..}{2}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{df}}{[}{1}{]}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{dfr0}}{[}{2}{]}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{hes}}{[}{1}{,}{1}{]}{≔}{0}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{hes}}{[}{1}{,}{2}{]}{≔}{−}{\mathrm{df}}{[}{1}{]}{*}{\mathrm{t1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{hes}}{[}{2}{,}{1}{]}{≔}{−}{\mathrm{dfr0}}{[}{2}{]}{*}{\mathrm{t1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{hes}}{[}{2}{,}{2}{]}{≔}{0}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (15)
 > $\mathrm{GH}≔\mathrm{optimize}\left(\mathrm{joinprocs}\left(\left[G,H\right]\right)\right)$
 ${\mathrm{GH}}{≔}{\mathbf{proc}}\left({x}{,}{y}{,}{t}{,}{\mathrm{grd}}{,}{\mathrm{hes}}{::}\left({\mathrm{array}}{}\left({1}{..}{2}{,}{1}{..}{2}\right)\right)\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{t1}}{,}{\mathrm{t2}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{t2}}{≔}{\mathrm{exp}}{}\left({−}{t}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{[}{1}{]}{≔}{−}{\mathrm{t2}}{*}{y}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{grd}}{[}{2}{]}{≔}{−}{\mathrm{t2}}{*}{x}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{t1}}{≔}{\mathrm{t2}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{hes}}{[}{1}{,}{1}{]}{≔}{0}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{hes}}{[}{1}{,}{2}{]}{≔}{−}{\mathrm{t1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{hes}}{[}{2}{,}{1}{]}{≔}{\mathrm{hes}}{[}{1}{,}{2}{]}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{hes}}{[}{2}{,}{2}{]}{≔}{0}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (16)

This final example shows the functionality of the maple2intrep and intrep2maple commands. The maple2intrep command converts a Maple procedure into an intermediate representation which is suitable for manipulation. One must be careful about how one evaluates this representation because the functions in the code will evaluate.

 > f := proc(x,n) local A,i;   A := array(0..n);   A[0] := 1;   for i to n do A[i] := x*A[i-1] end do;   A end proc:
 > $\mathrm{IR}≔\mathrm{maple2intrep}\left(f\right)$
 ${\mathrm{IR}}{≔}{\mathrm{Proc}}{}\left({\mathrm{Name}}{}\left({f}{..}{\mathrm{List}}{}\left({0}{..}{n}{,}{\mathrm{float}}\right)\right){,}{\mathrm{Parameters}}{}\left({x}{..}{\mathrm{float}}{,}{n}{..}{\mathrm{integer}}\right){,}{\mathrm{Options}}{}\left({}\right){,}{\mathrm{Description}}{}\left({}\right){,}{\mathrm{Locals}}{}\left({A}{..}{\mathrm{List}}{}\left({0}{..}{n}{,}{\mathrm{float}}\right){,}{i}{..}{\mathrm{integer}}\right){,}{\mathrm{Globals}}{}\left({}\right){,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({A}{,}{\mathrm{array}}{}\left({0}{..}{n}\right)\right){,}{\mathrm{Assign}}{}\left({{A}}_{{0}}{,}{1}\right){,}{\mathrm{For}}{}\left({i}{,}{1}{,}{1}{,}{n}{,}{\mathrm{true}}{,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({{A}}_{{i}}{,}{x}{}{{A}}_{{-}{1}{+}{i}}\right)\right)\right){,}{A}\right)\right)$ (17)
 > $\mathrm{intrep2maple}\left(\mathrm{IR}\right)$
 > $\mathrm{intrep2maple}\left(\mathrm{eval}\left(\mathrm{IR},1\right)\right)$
 ${\mathbf{proc}}\left({x}{,}{n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}{,}{i}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}{≔}{\mathrm{array}}{}\left({0}{..}{n}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}{[}{0}{]}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}{[}{i}{]}{≔}{x}{*}{A}{[}{i}{-}{1}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (18)