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

Online Help

All Products    Maple    MapleSim





translate Maple code to Fortran code


Calling Sequence




Calling Sequence

Fortran(x, cgopts)




expression, list, rtable, procedure, or module



(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.


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


      w = -2 * x * z + y * z + x

Translate a list and assign to an array with name "w" in the target code.


      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.



      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 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:


      integer function f (x, y, z)
        integer x
        integer y
        integer z
        f = y * x - y * z + x * z

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:


      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

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)

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:


      doubleprecision function q (x)
        doubleprecision x
        q = sin(x) ** 2

      integer function p (x, y)
        doubleprecision x
        integer y
        if (0 .lt. y) then
          p = int(aint(x))
          p = int(ceil(x))
        end if

      program m

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.'

Use names longer than 6 characters with the limitvariablelength option.

p:=proc() local longvar: end proc;

pproclocallongvar;end proc



      subroutine p
        doubleprecision cg


      subroutine p
        doubleprecision longvar

See Also




Translation Details