CodeGeneration
Fortran
translate Maple code to Fortran code
Calling Sequence
Parameters
Description
Examples
Fortran(x, cgopts)
x
-
expression, list, rtable, procedure, or module
cgopts
(optional) one or more CodeGeneration options
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 nm=expr where nm is a name and 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.
For more information about how the CodeGeneration package translates Maple code to other languages, see Translation Details. For more information about translation to Fortran in particular, see FortranDetails.
See CodeGenerationOptions for a description of the options used in the following examples.
with⁡CodeGeneration:
Translate a simple expression and assign to the name "w" in the target code.
Fortran⁡x+y⁢z−2⁢x⁢z,resultname=w
w = -2 * x * z + y * z + x
Translate a list and assign to an array with name "w" in the target code.
Fortran⁡x,2⁢y,5,z,resultname=w
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.
cs≔s=1.0+x,t=ln⁡s⁢exp⁡−x,r=exp⁡−x+x⁢t:
Fortran⁡cs,optimize
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≔Fortran⁡x+y+1,declare=x::float,y::integer,output=string
s≔ cg = x + dble(y) + 0.1D1
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:
Fortran⁡f,defaulttype=integer
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:
Fortran⁡f
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[5]+x[6]+x[7]; end proc:
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:
Fortran⁡m,resultname=t0
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:
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 ≔ proclocallongvar;end proc
Fortran⁡p
subroutine p doubleprecision cg end
Fortran⁡p,limitvariablelength=false
subroutine p doubleprecision longvar end
See Also
CodeGenerationOptions
FortranDetails
Translation Details
Download Help Document