codegen,C - Maple Help

# Online Help

###### All Products    Maple    MapleSim

codegen

 C
 generate C code

 Calling Sequence C(s) C(s, options)

Parameters

 s - expression, array of expressions, list of equations, or procedure

Description

 • Important: The codegen[C] command has been deprecated.  Use the superseding command CodeGeneration[C] instead. See CodeGeneration for information on translation to other languages.
 • The codegen[C] function generates C code for evaluating the input. The input s must be one of the following: a single algebraic expression, an array of algebraic expressions, a list of equations of the form name = algebraic (which is understood to mean a sequence of assignment statements), or a Maple procedure.  If the array is not a named array, the name unknown is used. The remaining arguments are optional; they are described below.
 • For help on translating Maple procedures into C, see codegen/C/procedure. Note: Type declarations are only given for Maple procedures. The C code that is otherwise generated will have to be placed inside a C subroutine or main program and the type declarations will have to be supplied by the user.
 • The filename option: By default, the output is sent to standard output. If you are using the C command interactively, the output will appear on your terminal screen. An additional argument of the form filename = "f.c" can be used to direct the output to the file f.c.
 • The optimized option: if the keyword optimized is specified as an additional argument, common subexpression optimization is performed. The result is a sequence of assignment statements in which temporary values are stored in local variables beginning with the letter t. The global names t0, t1, t2, ... are reserved for use by C for this purpose.  The input to the optimizer must satisfy certain conditions.  See codegen/optimize for more information.
 • The precision option: the optional argument precision=single or precision=double specifies whether single precision or double precision is to be used in the generation of floating-point variables and constants. The default is single precision if the mode=single option is provided and double precision otherwise.
 • The mode option: the optional argument mode=single or mode=double specifies whether single precision or double precision math function names are generated.  The default is double precision (resulting in the standard math library names), even if the precision=single option is provided.
 • The digits option: non-floating point Maple constants such as integers, fractions, and symbols such as Pi, are converted using evalf to floating-point constants where necessary, for example as arguments to real functions such as sqrt. By default, the number of digits used is 7 for single precision, 16 for double precision. This can be set to n digits by specifying an optional argument digits = n.
 • The ansi option: If the input is a Maple procedure, by default, parameter declarations follow the old (Kernighan and Ritchie) C compiler syntax. If this option is given, parameter declarations follow the ANSI C syntax. See the last example in the Examples section.
 • The declarations option: If the optional argument declarations = x::t is given, this specifies that the variable x is to be given the type t during translation. For more help on translating Maple procedures, see codegen/C/procedure.
 • The parameters, locals, and globals options: if s is a computation sequence (a list of equations) then, by default, C assumes that all variables on the left-hand-side of the equations are global variables which cannot be removed from the computation sequence. These options are used to specify otherwise. Local variables may be removed by the C translator from a computation sequence.
 • In translating arrays (Maple vectors and matrices and other arrays), the C function will reindex array subscripts to 0-based indexing which C requires. If the array contains unassigned entries, the value output in the C code is the string undefined.
 • The C translator will translate certain Maple functions into their C equivalents.  The known functions are listed below. For example, sin(x) + sec(x) will be translated into sin(x)+1/cos(x) in double precision mode and sinf(x)+1/cosf(x) in single precision mode. If a function is not handled by the C translator, the user will be informed and the function will be translated as is.
 • The Maple floating-point functions understood by the C translator are: abs(x), signum(x), min(x,y), max(x,y), sqrt(x), ceil(x), floor(x), round(x), trunc(x), ln(x), exp(x), erf(x), and the trigonometric and hyperbolic functions, and their inverses. The Maple integer functions understood are: abs(a), min(a,b), max(a,b), signum(a), irem(a,b), iquo(b,b), and modp(a,p).
 • The function C produces C code as a side-effect and returns NULL as the function value. Therefore, the ditto commands (% and %%) will not recall the output from the C command.
 • The command with(codegen,C) allows the use of the abbreviated form of this command.

Examples

Important: The codegen[C] command has been deprecated.  Use the superseding command CodeGeneration[C] instead.

 > $\mathrm{with}\left(\mathrm{codegen},C\right):$
 > $f≔1-\frac{x}{2}+3{x}^{2}-{x}^{3}+{x}^{4}$
 ${f}{≔}{1}{-}\frac{{1}}{{2}}{}{x}{+}{3}{}{{x}}^{{2}}{-}{{x}}^{{3}}{+}{{x}}^{{4}}$ (1)
 > $C\left(f\right)$
 t0 = 1.0-x/2.0+3.0*x*x-x*x*x+x*x*x*x;
 > $C\left(f,\mathrm{optimized}\right)$
 t2 = x*x;       t5 = t2*t2;       t6 = 1.0-x/2.0+3.0*t2-t2*x+t5;
 > $f≔\mathrm{\pi }\mathrm{ln}\left({x}^{2}\right)-\mathrm{sqrt}\left(2\right){\mathrm{ln}\left({x}^{2}\right)}^{2}$
 ${f}{≔}{\mathrm{\pi }}{}{\mathrm{ln}}{}\left({{x}}^{{2}}\right){-}\sqrt{{2}}{}{{\mathrm{ln}}{}\left({{x}}^{{2}}\right)}^{{2}}$ (2)
 > $C\left(f,\mathrm{optimized}\right)$
 t1 = x*x;       t2 = log(t1);       t4 = sqrt(2.0);       t5 = t2*t2;       t7 = 0.3141592653589793E1*t2-t4*t5;
 > $\mathrm{cs}≔\left[s=1+x,t=\mathrm{ln}\left(s\right)\mathrm{exp}\left(-x\right),r=\mathrm{exp}\left(-x\right)+xt\right]$
 ${\mathrm{cs}}{≔}\left[{s}{=}{1}{+}{x}{,}{t}{=}{\mathrm{ln}}{}\left({s}\right){}{{ⅇ}}^{{-}{x}}{,}{r}{=}{{ⅇ}}^{{-}{x}}{+}{x}{}{t}\right]$ (3)
 > $C\left(\mathrm{cs},\mathrm{optimized}\right)$
 s = 1.0+x;       t1 = log(s);       t2 = exp(-x);       t = t2*t1;       r = x*t+t2;
 > $C\left(\mathrm{cs},\mathrm{optimized},\mathrm{locals}=\left[s,t,r\right]\right)$
 t1 = log(1.0+x);       t2 = exp(-x);       r = x*t2*t1+t2;
 > $v≔\mathrm{array}\left(\left[\mathrm{exp}\left(-x\right)x,\mathrm{exp}\left(-x\right){x}^{2}\right]\right):$
 > $C\left(v,\mathrm{optimized}\right)$
 t1 = exp(-x);       t3 = x*x;       v[0] = t1*x;       v[1] = t1*t3;

A matrix with an undefined entry

 > $A≔\mathrm{array}\left(1..2,1..2,\mathrm{symmetric}\right):$
 > $A\left[1,1\right]≔\mathrm{log}\left(x\right):$$A\left[1,2\right]≔1-\mathrm{log}\left(x\right):$
 > $\mathrm{print}\left(A\right)$
 $\left[\begin{array}{cc}{\mathrm{ln}}{}\left({x}\right)& {1}{-}{\mathrm{ln}}{}\left({x}\right)\\ {1}{-}{\mathrm{ln}}{}\left({x}\right)& {{\mathrm{?}}}_{{2}{,}{2}}\end{array}\right]$ (4)
 > $C\left(A,\mathrm{mode}=\mathrm{single}\right)$
 A[0][0] = logf(x);       A[0][1] = 1.0-logf(x);       A[1][0] = 1.0-logf(x);       A[1][1] = (0.0/0.0);
 > $C\left(A,\mathrm{optimized}\right)$
 t1 = log(x);       t2 = 1.0-t1;       A[0][0] = t1;       A[0][1] = t2;       A[1][0] = t2;       A[1][1] = (0.0/0.0);

A simple procedure with declarations

 > $f≔\mathrm{convert}\left(1-3{x}^{2}-2{x}^{3}+{x}^{4},\mathrm{horner}\right)$
 ${f}{≔}{1}{+}\left({-}{3}{+}\left({-}{2}{+}{x}\right){}{x}\right){}{{x}}^{{2}}$ (5)
 > $f≔\mathrm{unapply}\left(f,x\right)$
 ${f}{≔}{x}{↦}{1}{+}\left({-}{3}{+}\left({-}{2}{+}{x}\right){\cdot }{x}\right){\cdot }{{x}}^{{2}}$ (6)
 > $C\left(f\right)$
 /* The options were    : operatorarrow */ double f(x) double x; {   {     return(1.0+(-3.0+(-2.0+x)*x)*x*x);   } }
 > $C\left(f,\mathrm{ansi}\right)$
 /* The options were    : operatorarrow */ double f(double x) {   {     return(1.0+(-3.0+(-2.0+x)*x)*x*x);   } }

 See Also