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.
>
|
|
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));
| |
|
|
|
|
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
>
|
|
Now generate C code for the two procedures
>
|
|
#include <stdlib.h>
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);
}
| |
>
|
|
#include <math.h>
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));
}
| |
|