Documentation Center

  • Trial Software
  • Product Updates

generate::fortran

Generate FORTRAN formatted string

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

generate::fortran(e, <NoWarning>)

Description

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. Use the printing option Unquoted to remove quotation marks and to expand special characters like line breaks and tabs.

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.

Examples

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)]):
print(Unquoted,%)
      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, 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:

print(Unquoted,
      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:

print(Unquoted,
      generate::fortran(
          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 FORTRAN_2008 compiler.
      t0 = gamma(x)

If you started using generate::fortran recently, the warnings can help you identify the potential issues in the converted code. If you want to suppress warnings, use the NoWarning option:

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

Parameters

e

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

Options

NoWarning

Suppress warnings.

Return Values

generate::fortran returns a string containing FORTRAN code.

See Also

MuPAD Functions

Was this topic helpful?