codegen,C - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


codegen

  

C

  

generate C code

 

Calling Sequence

Parameters

Description

Examples

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.

withcodegen,C:

f1x2+3x2x3+x4

f112x+3x2x3+x4

(1)

Cf

      t0 = 1.0-x/2.0+3.0*x*x-x*x*x+x*x*x*x;

Cf,optimized

      t2 = x*x;
      t5 = t2*t2;
      t6 = 1.0-x/2.0+3.0*t2-t2*x+t5;

fπlnx2sqrt2lnx22

fπlnx22lnx22

(2)

Cf,optimized

      t1 = x*x;
      t2 = log(t1);
      t4 = sqrt(2.0);
      t5 = t2*t2;
      t7 = t2*0.3141592653589793E1-t5*t4;

css=1+x,t=lnsexpx,r=expx+xt

css=1+x,t=lnsⅇx,r=ⅇx+xt

(3)

Ccs,optimized

      s = 1.0+x;
      t1 = log(s);
      t2 = exp(-x);
      t = t2*t1;
      r = x*t+t2;

Ccs,optimized,locals=s,t,r

      t1 = log(1.0+x);
      t2 = exp(-x);
      r = x*t2*t1+t2;

varrayexpxx,expxx2:

Cv,optimized

      t1 = exp(-x);
      t3 = x*x;
      v[0] = x*t1;
      v[1] = t3*t1;

A matrix with an undefined entry

Aarray1..2,1..2,symmetric:

A1,1logx:A1,21logx:

printA

lnx1lnx1lnx`?`2,2

(4)

CA,mode=single

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

CA,optimized

      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

fconvert13x22x3+x4,horner

f1+3+2+xxx2

(5)

funapplyf,x

fx1+3+2+xxx2

(6)

Cf

/* The options were    : operatorarrow */
double f(x)
double x;
{
  {
    return(1.0+(-3.0+(-2.0+x)*x)*x*x);
  }
}

Cf,ansi

/* The options were    : operatorarrow */
double f(double x)
{
  {
    return(1.0+(-3.0+(-2.0+x)*x)*x*x);
  }
}

See Also

codegen

codegen,C,procedure(deprecated)

CodeGeneration

CodeGeneration[C]

Writing Efficient Maple Code