MaplePortal/CodeGeneration - Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : MaplePortal/CodeGeneration

 

Code Generation

Back to Portal

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.

θarctanl3z22l2z+x2+y2+l22+l4+l5+l3l4l5+l32x2+y2l4+l5z22l2z+x2+y2+l5+l2l4+l3l5+l2+l4l3z22l2z+x2+y2+l5+l2+l4+l3l5+l2l4l3l32l4+l52+z22l2z+x2+y2+l22+l4+l5+l3l4l5+l32l2zz22l2z+x2+y2+l22+l4+l5+l3l4l5+l3l3l222l2z+x2+y2+z2,l3l4+l5l2zz22l2z+x2+y2+l5+l2l4+l3l5+l2+l4l3z22l2z+x2+y2+l5+l2+l4+l3l5+l2l4l3l32l4+l52+z22l2z+x2+y2+l22+l4+l5+l3l4l5+l32x2+y2l222l2z+x2+y2+z2l3:

 

This equation is now converted to optimized C code.

CodeGenerationCθ,optimize, deducetypes=false

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

Robot Arm Code Generation

 

 

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

bisectionColebrookfriction&comma;0.000001&comma;1.0&comma;0.001&comma;0.1&comma;10000

0.04312610947

(1)

Now generate C code for the two procedures

CodeGenerationCbisectionColebrook

#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);
}

CodeGenerationCfriction

#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));
}