Generate Fortran formatted string

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.


generate::fortran(e, <NoWarning>, <Version = "versionName">)


generate::fortran(e) generates Fortran code for the MuPAD® expression e.

generate::fortran returns a Fortran formatted string representing an expression, equation, list of equations, or a matrix.

An equation represents an assignment in Fortran code. The type of the assignment is double.

When generating Fortran code for a matrix, the generator assigns only nonzero elements. See Example 2.

To print an output string to a file, use the fprint function. To remove quotation marks and to expand special characters like line breaks and tabs, use the printing option Unquoted.

Use the generate::optimize function to optimize the MuPAD code before converting it to Fortran code. See Example 4.

The NoWarning option lets you suppress warnings. See Example 5.

The Version option specifies the target version of the Fortran compiler that generate::fortran uses to generate code. The options are Fortran77 (default), Fortran90, and Fortran95. See Example 6.


Example 1

The code generator converts a list of equations to a sequence of assignments.

generate::fortran([x[1] = y[2 + i]^2*(y[1] + sin(z)),
                   x[2] = tan(x[1]^4)]):
      x(1) = (sin(z)+y(1))*y(i+2)**2
      x(2) = tan(x(1)**4)

Example 2

Generated Fortran code does not include assignments for zero elements of a matrix.

A:= matrix([[1, 0, 0],[0, 0, 1]]):
print(Unquoted, generate::fortran(A))
      A(1,1) = 1.0D0
      A(2,3) = 1.0D0

Example 3

If the first index of an array is not 1, then the generate::fortran function issues a warning.

A:= array(1..2, 2..3, [[1,2],[3,4]]):
print(Unquoted, generate::fortran(A))
Warning: The array index 'A[1..2, 2..3]' is out of range 1..n. [DOM_ARRAY::CF]
      A(1,2) = 1.0D0
      A(1,3) = 2.0D0
      A(2,2) = 3.0D0
      A(2,3) = 4.0D0

Example 4

The generate::fortran function does not optimize your code.

      generate::fortran([x = a + b, y = (a + b)^2])):
      x = a+b
      y = (a+b)**2

You can use the generate::optimize function before converting your MuPAD expression to Fortran code. For example, this function can reduce the number of operations by finding common subexpressions.

          generate::optimize([x = a + b, y = (a + b)^2])
      x = a+b
      y = x**2

Example 5

By default, the generate::fortran function can issue warnings.

print(Unquoted, generate::fortran(gamma(x)))
Warning: Function 'gamma' requires a Fortran2008 compiler.
      t0 = gamma(x)

Warnings help identify potential issues in converted code. To suppress warnings, use the NoWarning option.

print(Unquoted, generate::fortran(gamma(x), NoWarning))
      t0 = gamma(x)

If the warning specifies that the compiler required is either Fortran90 or Fortran95, then you can suppress the warning by specifying the correct compiler version using Version. For example, the ceiling function requires Fortran90 instead of the default Fortran77.

Warning: Function 'ceiling' requires a Fortran90 compiler.

Specify Version as Fortran90. The generate::fortran function does not issue a warning.

generate::fortran(ceil(x), Version = "Fortran90")

Example 6

By default, the generate::fortran function uses the target Fortran version Fortran77 to generate code. To specify Fortran90 or Fortran95 as the target version, use the Version option.

Generate output for the Fortran90 compiler by specifying the Version option as Fortran90.

f := expand((x+1)^20):
fcode90 := generate::fortran(f, Version = "Fortran90"):
print(Unquoted, fcode90)
      t0 = x*2.0D1+x**2*1.9D2+x**3*1.14D3+x**4*4.845D3+x**5*1.5504D4+x**&

The code formatting for multiline statements in Fortran90 differs from the formatting in the default target of Fortran77.



An expression, equation, list of equations, or a matrix



Suppress warnings.


Specify the Fortran compiler version. The default version is Fortran77. The Version values are Fortran77, Fortran90, and Fortran95. For example, generate::fortran(..., Version = "Fortran90") uses compiler version Fortran90.

Return Values

generate::fortran returns a string containing Fortran code.

See Also

MuPAD Functions

Was this topic helpful?