CodeGeneration - Maple Programming Help

Home : Support : Online Help : Programming : Input and Output : Translation : CodeGeneration/Fortran

CodeGeneration

 Fortran
 translate Maple code to Fortran code

 Calling Sequence Fortran(x, cgopts)

Parameters

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

Description

 • The Fortran command translates Maple code to Fortran 77 code.
 - If the parameter x is an algebraic expression, then a Fortran statement assigning the expression to a variable is generated.
 - If x is a list, rtable or Maple Array of algebraic expressions, then a sequence of Fortran statements assigning the elements to a Fortran 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 Fortran assignment statements is generated.
 - If x is a procedure, then either a Fortran function or a subroutine is generated.
 - If x is a module, then a Fortran program is generated, as described on the FortranDetails help page.
 • The parameter cgopts may include one or more CodeGeneration options, as described in CodeGenerationOptions. The limitvariablelength=false option, available for this command only, allows you to use names longer than 6 characters.  By default, such names are automatically replaced because they do not comply with the Fortran 77 standard.

Examples

See CodeGenerationOptions for a description of the options used in the following examples.

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

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

 > $\mathrm{Fortran}\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.

 > $\mathrm{Fortran}\left(\left[\left[x,2y\right],\left[5,z\right]\right],\mathrm{resultname}="w"\right)$
 w(1,1) = x       w(1,2) = 2 * y       w(2,1) = 5       w(2,2) = 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]:$
 > $\mathrm{Fortran}\left(\mathrm{cs},\mathrm{optimize}\right)$
 s = 0.10D1 + 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≔\mathrm{Fortran}\left(x+y+1,\mathrm{declare}=\left[x::\mathrm{float},y::\mathrm{integer}\right],\mathrm{output}=\mathrm{string}\right)$
 ${s}{≔}{"cg = x + dble\left(y\right) + 0.1D1"}$ (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:
 > $\mathrm{Fortran}\left(f,\mathrm{defaulttype}=\mathrm{integer}\right)$
 integer function f (x, y, z)         integer x         integer y         integer z         f = y * x - y * z + x * z         return       end

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:
 > $\mathrm{Fortran}\left(f\right)$
 doubleprecision function f (n)         integer n         doubleprecision x         integer i         doubleprecision cgret         x = 0.0D0         do 100, i = 1, n, 1           x = x + dble(i)           cgret = x 100     continue         f = cgret         return       end

Translate a procedure accepting an Array as a parameter.

 > f := proc(x::Array(numeric, 5..7))     return x+x+x; end proc:
 > $\mathrm{Fortran}\left(f\right)$
 doubleprecision function f (x)         doubleprecision x(5:7)         f = x(5) + x(6) + x(7)         return       end

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:
 > $\mathrm{Fortran}\left(m,\mathrm{resultname}=\mathrm{t0}\right)$
 Warning, the function names {ceil} are not recognized in the target language       doubleprecision function q (x)         doubleprecision x         q = sin(x) ** 2         return       end       integer function p (x, y)         doubleprecision x         integer y         if (0 .lt. y) then           p = int(aint(x))           return         else           p = int(ceil(x))           return         end if       end       program m       end

Translate a procedure with no return value, containing an output statement.

 > f := proc(N)     printf("%d is a Number.\n", N); end proc:
 > $\mathrm{Fortran}\left(f\right)$
 subroutine f (N)         doubleprecision N         print *, N, ' is a Number.'       end

Use names longer than 6 characters with the limitvariablelength option.

 > p:=proc() local longvar: end proc;
 ${p}{:=}{\mathbf{proc}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{longvar}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2)
 > $\mathrm{Fortran}\left(p\right)$
 Warning, the following variable name replacements were made: longvar -> cg       subroutine p         doubleprecision cg       end
 > $\mathrm{Fortran}\left(p,\mathrm{limitvariablelength}=\mathrm{false}\right)$
 subroutine p         doubleprecision longvar       end