 MaplePortal/CodeGeneration - Maple Help

 Code Generation

 Introduction

Maple will generate optimized code from your equations and procedures. Targets include C, C#, Fortran, Java, MATLAB®, Python and more.

This code is fast and efficient and eliminates unnecessary numeric operations.

 Generate Code from Expressions

This equation, for example, represents one of the joint angles of a robot arm, derived from a symbolic analysis in Maple.

 > $\mathrm{θ}≔\mathrm{arctan}{}\left(\frac{-\mathrm{l3}\sqrt{{\left({z}^{2}-2\mathrm{l2}z+{x}^{2}+{y}^{2}+{\mathrm{l2}}^{2}+\left(\mathrm{l4}+\mathrm{l5}+\mathrm{l3}\right)\left(-\mathrm{l4}-\mathrm{l5}+\mathrm{l3}\right)\right)}^{2}\left({x}^{2}+{y}^{2}\right)}\left(\mathrm{l4}+\mathrm{l5}\right)\sqrt{-\frac{\left({z}^{2}-2\mathrm{l2}z+{x}^{2}+{y}^{2}+\left(-\mathrm{l5}+\mathrm{l2}-\mathrm{l4}+\mathrm{l3}\right)\left(\mathrm{l5}+\mathrm{l2}+\mathrm{l4}-\mathrm{l3}\right)\right)\left({z}^{2}-2\mathrm{l2}z+{x}^{2}+{y}^{2}+\left(\mathrm{l5}+\mathrm{l2}+\mathrm{l4}+\mathrm{l3}\right)\left(-\mathrm{l5}+\mathrm{l2}-\mathrm{l4}-\mathrm{l3}\right)\right)}{{\mathrm{l3}}^{2}{\left(\mathrm{l4}+\mathrm{l5}\right)}^{2}}}+{\left({z}^{2}-2\mathrm{l2}z+{x}^{2}+{y}^{2}+{\mathrm{l2}}^{2}+\left(\mathrm{l4}+\mathrm{l5}+\mathrm{l3}\right)\left(-\mathrm{l4}-\mathrm{l5}+\mathrm{l3}\right)\right)}^{2}\left(\mathrm{l2}-z\right)}{\left({z}^{2}-2\mathrm{l2}z+{x}^{2}+{y}^{2}+{\mathrm{l2}}^{2}+\left(\mathrm{l4}+\mathrm{l5}+\mathrm{l3}\right)\left(-\mathrm{l4}-\mathrm{l5}+\mathrm{l3}\right)\right)\mathrm{l3}\left({\mathrm{l2}}^{2}-2\mathrm{l2}z+{x}^{2}+{y}^{2}+{z}^{2}\right)},\frac{\mathrm{l3}\left(\mathrm{l4}+\mathrm{l5}\right)\left(\mathrm{l2}-z\right)\sqrt{-\frac{\left({z}^{2}-2\mathrm{l2}z+{x}^{2}+{y}^{2}+\left(-\mathrm{l5}+\mathrm{l2}-\mathrm{l4}+\mathrm{l3}\right)\left(\mathrm{l5}+\mathrm{l2}+\mathrm{l4}-\mathrm{l3}\right)\right)\left({z}^{2}-2\mathrm{l2}z+{x}^{2}+{y}^{2}+\left(\mathrm{l5}+\mathrm{l2}+\mathrm{l4}+\mathrm{l3}\right)\left(-\mathrm{l5}+\mathrm{l2}-\mathrm{l4}-\mathrm{l3}\right)\right)}{{\mathrm{l3}}^{2}{\left(\mathrm{l4}+\mathrm{l5}\right)}^{2}}}+\sqrt{{\left({z}^{2}-2\mathrm{l2}z+{x}^{2}+{y}^{2}+{\mathrm{l2}}^{2}+\left(\mathrm{l4}+\mathrm{l5}+\mathrm{l3}\right)\left(-\mathrm{l4}-\mathrm{l5}+\mathrm{l3}\right)\right)}^{2}\left({x}^{2}+{y}^{2}\right)}}{\left({\mathrm{l2}}^{2}-2\mathrm{l2}z+{x}^{2}+{y}^{2}+{z}^{2}\right)\mathrm{l3}}\right):$

This equation is now converted to optimized C code.

 >
 t1 = l2 * l2; t3 = 0.2e1 * l2 * z; t4 = x * x; t5 = y * y; t6 = z * z; t10 = t1 - t3 + t4 + t5 + t6 + (l4 + l5 + l3) * (-l4 - l5 + l3); t11 = t10 * t10; t14 = sqrt((t4 + t5) * t11); t16 = l4 + l5; t26 = l3 * l3; t28 = t16 * t16; t32 = sqrt(-0.1e1 / t28 / t26 * (-t3 + t4 + t5 + t6 + (l5 + l2 + l4 + l3) * (-l5 + l2 - l4 - l3)) * (-t3 + t4 + t5 + t6 + (-l5 + l2 - l4 + l3) * (l5 + l2 + l4 - l3))); t35 = l2 - z; t40 = 0.1e1 / l3; t42 = 0.1e1 / (t1 - t3 + t4 + t5 + t6); t51 = atan2(t42 * t40 / t10 * (-t32 * t16 * t14 * l3 + t35 * t11), t40 * t42 * (t32 * t35 * t16 * l3 + t14));

 Applications Generate Code from Procedures

You can also convert Maple procedures to code.

These two procedures, for example, solve the Colebrook equation (an empirical equation that describes friction in pipe flow) using a bisection method.

 > friction := proc (f, e, Dia, Rey)       return -2*log10(e/(3.7*Dia)+2.51/((Rey*sqrt(f))))-1/sqrt(f): end proc:
 > bisectionColebrook := proc (friction, a, b, e, Dia, Rey) local epsilonABS, epsilonSTEP, c, atemp, btemp: epsilonABS := 0.1e-4: epsilonSTEP := 0.1e-4: atemp := a: btemp := b: while epsilonSTEP <= btemp-atemp or epsilonABS <= abs(friction(atemp, e, Dia, Rey))       and epsilonABS <= abs(friction(btemp, e, Dia, Rey)) do    c := atemp / 2 + btemp / 2:    if abs(friction(c, e, Dia, Rey)) <= 0 then       break    elif friction(atemp, e, Dia, Rey)*friction(c, e, Dia, Rey) < 0 then       btemp := c    else       atemp := c    end if end do: return atemp: end proc:

Test the bisection method

 > $\mathrm{bisectionColebrook}\left(\mathrm{friction},0.000001,1.0,0.001,0.1,10000\right)$
 ${0.04312610947}$ (1)

Now generate C code for the two procedures

 > $\mathrm{CodeGeneration}\left[C\right]\left(\mathrm{bisectionColebrook}\right)$
 #include double bisectionColebrook (   double friction,   double a,   double b,   double e,   double Dia,   double Rey) {   double epsilonABS;   double epsilonSTEP;   double c;   double atemp;   double btemp;   epsilonABS = 0.1e-4;   epsilonSTEP = 0.1e-4;   atemp = a;   btemp = b;   while (epsilonSTEP <= btemp - atemp || epsilonABS <= abs(friction(atemp, e, Dia, Rey)) && epsilonABS <= abs(friction(btemp, e, Dia, Rey)))   {     c = atemp / 2 + btemp / 2;     if (abs(friction(c, e, Dia, Rey)) <= 0)       break;     else if (friction(atemp, e, Dia, Rey) * friction(c, e, Dia, Rey) < 0)       btemp = c;     else       atemp = c;   }   return(atemp); }
 > $\mathrm{CodeGeneration}\left[C\right]\left(\mathrm{friction}\right)$
 #include double friction (   double f,   double e,   double Dia,   double Rey) {   return(-0.2e1 * log10(e / 0.37e1 / Dia + 0.251e1 / (Rey * sqrt(f))) - 0.1e1 / sqrt(f)); }
 >