prep2trans - Maple Help

codegen

 prep2trans
 prepare a Maple procedure for translation
 split
 prepare a Maple procedure for automatic differentiation
 horner
 convert formulae in a procedure to horner form

 Calling Sequence prep2trans(f) horner(f, x) split(f) split(f, x)

Parameters

 f - Maple procedure x - list or set of symbols

Description

 • The prep2trans function is used to transform certain symbolic expressions into forms suitable for translation into a target language such as C or Fortran.  For example, piecewise expressions are translated into if statements, symbolic sums are translated into for loops.
 • The horner function takes as input a Maple procedure and a variable or list or set of variables, and converts all formulae in the procedure to Horner form in x.
 • The split function is used to break up certain symbolic expressions into computation sequences suitable for automatic differentiation. Long products and complicated compositions are broken up into computation sequences.  If the second argument x is specified, it specifies the independent variables, the variables that the function f will be differentiated in.
 • The command with(codegen,prep2trans) allows the use of the abbreviated form of this command.
 • The command with(codegen,split) allows the use of the abbreviated form of this command.
 • The command with(codegen,horner) allows the use of the abbreviated form of this command.

Examples

 > $\mathrm{with}\left(\mathrm{codegen}\right):$
 > f := proc(x) piecewise(x<0,0,x<1,x,x>1,2-x,0) end proc;
 ${f}{≔}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{piecewise}}{}\left({x}{<}{0}{,}{0}{,}{x}{<}{1}{,}{x}{,}{1}{<}{x}{,}{2}{-}{x}{,}{0}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (1)
 > $\mathrm{prep2trans}\left(f\right)$
 ${\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{<}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{<}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{1}{<}{x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{2}{-}{x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{else}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{0}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2)
 > f := proc(x) local s; sum(x^i/i!,i=0..n) end proc;
 ${f}{≔}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{s}{;}\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}}$ (3)
 > $\mathrm{prep2trans}\left(f\right)$
 ${\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i1}}{,}{s}{,}{\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}}$ (4)
 > f := proc(n,A) local i,j; sum(sum(A[i,j],i=1..n),j=1..n) end proc;
 ${f}{≔}{\mathbf{proc}}\left({n}{,}{A}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{,}{j}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{sum}}{}\left({\mathrm{sum}}{}\left({A}{[}{i}{,}{j}{]}{,}{i}{=}{1}{..}{n}\right){,}{j}{=}{1}{..}{n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (5)
 > $\mathrm{prep2trans}\left(f\right)$
 ${\mathbf{proc}}\left({n}{,}{A}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{,}{\mathrm{i1}}{,}{\mathrm{i2}}{,}{j}{,}{\mathrm{s1}}{,}{\mathrm{s2}}{,}{\mathrm{t1}}{,}{\mathrm{t2}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}{≔}{0}{;}\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{s2}}{≔}{0}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{i2}}\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{s2}}{≔}{\mathrm{s2}}{+}{A}{[}{\mathrm{i2}}{,}{\mathrm{i1}}{]}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}{≔}{\mathrm{s1}}{+}{\mathrm{s2}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (6)
 > h := proc(x,y,z) 1-2*x*y-x*y^2*z*(1-x) end proc;
 ${h}{≔}{\mathbf{proc}}\left({x}{,}{y}{,}{z}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{1}{-}{2}{*}{x}{*}{y}{-}{x}{*}{y}{^}{2}{*}{z}{*}\left({1}{-}{x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (7)
 > $\mathrm{horner}\left(h,x\right)$
 ${\mathbf{proc}}\left({x}{,}{y}{,}{z}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{1}{+}\left({x}{*}{y}{^}{2}{*}{z}{-}{y}{^}{2}{*}{z}{-}{2}{*}{y}\right){*}{x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (8)
 > $\mathrm{horner}\left(h,\left[x,y\right]\right)$
 ${\mathbf{proc}}\left({x}{,}{y}{,}{z}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{1}{+}\left(\left({−}{y}{*}{z}{-}{2}\right){*}{y}{+}{x}{*}{y}{^}{2}{*}{z}\right){*}{x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (9)
 > $\mathrm{split}\left(h\right)$
 ${\mathbf{proc}}\left({x}{,}{y}{,}{z}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s0}}{,}{\mathrm{s1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s0}}{≔}{x}{*}{z}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}{≔}{y}{^}{2}{*}\left({1}{-}{x}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{−}{\mathrm{s0}}{*}{\mathrm{s1}}{-}{2}{*}{x}{*}{y}{+}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (10)
 > g := proc(x,y,t) 2*sin(x^2*y)*exp(-t^2) end proc;
 ${g}{≔}{\mathbf{proc}}\left({x}{,}{y}{,}{t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{2}{*}{\mathrm{sin}}{}\left({x}{^}{2}{*}{y}\right){*}{\mathrm{exp}}{}\left({−}{t}{^}{2}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (11)
 > $\mathrm{split}\left(g\right)$
 ${\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{s0}}{,}{\mathrm{s1}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s0}}{≔}{x}{^}{2}{*}{y}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s1}}{≔}{−}{t}{^}{2}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{2}{*}{\mathrm{exp}}{}\left({\mathrm{s1}}\right){*}{\mathrm{sin}}{}\left({\mathrm{s0}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (12)
 > $\mathrm{split}\left(g,\left[x,y\right]\right)$
 ${\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{s0}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{s0}}{≔}{x}{^}{2}{*}{y}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{2}{*}{\mathrm{exp}}{}\left({−}{t}{^}{2}\right){*}{\mathrm{sin}}{}\left({\mathrm{s0}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (13)