Generate FORTRAN formatted string
This functionality does not run in MATLAB.
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.
The NoWarning option lets you suppress warnings. See Example 5.
The code generator converts a list of equations to a sequence of assignments:
generate::fortran([x = y[2 + i]^2*(y + sin(z)), x = tan(x^4)]): print(Unquoted,%)
x(1) = (sin(z)+y(1))*y(i+2)**2 x(2) = tan(x(1)**4)
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
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
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
By default, the generate::fortran function can issue warnings:
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)
generate::fortran returns a string containing FORTRAN code.