 C - Maple Help

CodeGeneration

 C
 translate Maple code to C code Calling Sequence C(x, cgopts) Parameters

 x - expression, list, rtable, procedure or module cgopts - (optional) one or more CodeGeneration options Description

 • The C command translates Maple code to ANSI C code.
 - If the parameter x is an algebraic expression, then a C statement assigning the expression to a variable is generated.
 - If x is a list, rtable or Maple Array of algebraic values, then a sequence of C statements assigning the elements to a C array is produced.  Only the initialized elements of the rtable or Maple Array are translated.
 - If x is a list of equations $\mathrm{nm}=\mathrm{expr}$ where $\mathrm{nm}$ is a name and $\mathrm{expr}$ is an algebraic expression, this is understood to mean a sequence of assignment statements.  In this case, the equivalent sequence of C assignment statements is generated.
 - If x is a procedure, then a C function is generated, along with any necessary directives for library inclusion.
 - If x is a module, then the module members are translated as described on the CDetails help page.
 • The parameter cgopts may include one or more CodeGeneration options, as described in CodeGenerationOptions. Examples

For a description of the options used in the following examples, see CodeGenerationOptions.

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

Translate a simple expression and assign to the name w'' in the target code.

 > $C\left(x+yz-2xz,\mathrm{resultname}="w"\right)$
 w = -2 * x * z + y * z + x;

Translate a list and assign to an array with name w'' in the target code.

 > $C\left(\left[\left[x,2y\right],\left[5,z\right]\right],\mathrm{resultname}="w"\right)$
 w = x; w = 2 * y; w = 5; w = z;

Translate a computation sequence.  Optimize the input first.

 > $\mathrm{cs}≔\left[s=1.0+x,t=\mathrm{ln}\left(s\right){ⅇ}^{-x},r={ⅇ}^{-x}+xt\right]:$
 > $C\left(\mathrm{cs},\mathrm{optimize}\right)$
 s = 0.10e1 + x; t1 = log(s); t2 = exp(-x); t = t2 * t1; r = x * t + t2;

Declare that x is a float and y is an integer. Return the result in a string.

 > $s≔C\left(x+y+1,\mathrm{declare}=\left[x::\mathrm{float},y::\mathrm{integer}\right],\mathrm{output}=\mathrm{string}\right)$
 ${s}{≔}{"cg = x + \left(double\right) y + 0.1e1;"}$ (1)

Translate a procedure.  Assume that all untyped variables have type integer.

 > f := proc(x, y, z) return x*y-y*z+x*z; end proc:
 > $C\left(f,\mathrm{defaulttype}=\mathrm{integer}\right)$
 int f (int x, int y, int z) {   return(y * x - y * z + x * z); }

Translate a procedure containing an implicit return.  A new variable is created to hold the return value.

 > f := proc(n)   local x, i;   x := 0.0;   for i to n do     x := x + i;   end do; end proc:
 > $C\left(f\right)$
 double f (int n) {   double x;   int i;   double cgret;   x = 0.0e0;   for (i = 1; i <= n; i++)   {     x = x + (double) i;     cgret = x;   }   return(cgret); }

Translate a procedure accepting an Array as a parameter.  Note that the indices are renumbered so that the C array starts at index 0.

 > f := proc(x::Array(numeric, 5..7))   return x+x+x; end proc:
 > $C\left(f\right)$
 double f (double x) {   return(x + x + x); }

Translate a module with one exported and one local procedure.

 > m := module() export p; local q;     p := proc(x,y) if y>0 then trunc(x); else ceil(x); end if; end proc:     q := proc(x) sin(x)^2; end proc: end module:
 > $C\left(m,\mathrm{resultname}=\mathrm{t0}\right)$
 #include double q; double p (double x, int y); double q (double x) {   return(pow(sin(x), 0.2e1)); } double p (double x, int y) {   if (0 < y)     return((double) (int)(x));   else     return(ceil(x)); }

Translate an expression involving the max function.

 > $C\left(\mathrm{max}\left(\frac{1}{\left|x\right|},{x}^{2}\right)\right)$
 cg0 = (x * x >= 1 / abs(x) ? x * x : 1 / abs(x));