Most of the following examples use the CodeGeneration[C] function, but the options are available with all the other CodeGeneration functions, unless it is stated otherwise in the Options section above.
>
|
|
The 'coercetypes' Option
In the first call below, the variable y is coerced to a floating-point type. This coercion is not generated in the second call.
>
|
|
>
|
|
The 'declare' Option
In the following example, is assigned a floating-point number because it is declared to have type float. Otherwise, the translator would have deduced that it has type integer and generated an assignment to the integer 1.
>
|
|
The 'deducereturn' Option
The following Maple procedure returns the value 10 when invoked, but translation with the option deducereturn=false creates a C function with a void return type.
>
|
f := proc(x::Vector(4), n::integer) local i; for i to 4 do x[i] := 10 end do; end proc:
|
>
|
|
void f (int x[4], int n)
{
int i;
for (i = 1; i <= 4; i++)
x[i - 1] = 10;
}
| |
The 'deducetypes' Option
In the following example, is given the default type, numeric, and so it is assigned a floating-point number. Otherwise, the translator would have deduced that it has type integer and generated an assignment to the integer 1.
>
|
|
The 'defaulttype' Option
In the following example, the untyped variables are given the type integer instead of numeric. Thus the translation produces C declarations with type int rather than double.
>
|
f := proc(x) return x; end proc:
|
>
|
|
int f (int x)
{
return(x);
}
| |
The 'digits' Option
The following call shows how the digits option affects the translation of known constants.
cg = 0.31415926535897932385e1;
| |
The 'expandarrays' Option
In the following example, the variable is automatically created to hold the return values. In the first call, statements to perform element-wise copying are generated. In the second call, the assignment involves only the array names. In this case, you must replace the assignments and in the output with appropriate C statements (for example, calls to a user-defined C function).
>
|
f := proc(x::Vector(2), y::Vector(2), z) if z > 0 then x else y end if; end proc:
|
void f (
double x[2],
double y[2],
int z,
double cgret[2])
{
if (0 < z)
{
cgret[0] = x[0];
cgret[1] = x[1];
}
else
{
cgret[0] = y[0];
cgret[1] = y[1];
}
}
| |
>
|
|
void f (
double x[2],
double y[2],
int z,
double cgret[2])
{
if (0 < z)
cgret = x;
else
cgret = y;
}
| |
The 'functionprecision' Option
The following example specifies that double precision Fortran function names are to be used instead of generic function names.
>
|
|
The 'optimize' Option
In the following example, the computation sequence is optimized before it is translated.
>
|
|
x = y * z;
w = 0.50e1 * x;
| |
The 'output' Option
In the following example, the result is put into a string and returned.
>
|
|
The 'precision' Option
The following example specifies that single precision variables and constants are to be generated.
>
|
f := proc(x) return x+2.0; end proc:
|
>
|
|
float f (float x)
{
return(x + 0.20e1f);
}
| |
The 'reduceanalysis' Option
In the first call below, full analysis is applied. The translator recognizes, in analyzing the second assignment, that the variable has been given an integer type and includes the necessary type coercion. In the second call, each assignment is analyzed and translated on its own, with no knowledge retained from analysis of previous assignments.
>
|
|
x = 1;
y = (double) x + 0.20e1;
| |
>
|
|
The 'resultname' Option
In the following example, the result is assigned to a variable with the specified name instead of an automatically generated name.
>
|
|
The 'returnvariablename' Option
In the following example, the return variable is given the specified name instead of an automatically generated name.
>
|
f := proc(n) local i; for i to n do i*n end do end proc:
|
>
|
|
int f (int n)
{
int i;
int myreturn;
for (i = 1; i <= n; i++)
myreturn = i * n;
return(myreturn);
}
| |
The 'strict' Option
The following example illustrates the use of the strict setting.
>
|
|
cg0 = unknownFunction(x);
| |
>
|
|