Documentation

# `generate`::`fortran`

Generate Fortran formatted string

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

## Syntax

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

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

## 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, then the `generate::fortran` function issues a warning.

```A:= array(1..2, 2..3, [[1,2],[3,4]]): print(Unquoted, generate::fortran(A))```
```Warning: Array index 'A[1..2, 2..3]' out of range 1..n. ```
``` 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 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.

`generate::fortran(ceil(x))`
```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.0D+1+x**2*1.9D+2+x**3*1.14D+3+x**4*4.845D+3+x**5*1.5504D+& &4+x**6*3.876D+4+x**7*7.752D+4+x**8*1.2597D+5+x**9*1.6796D+5+x**10*& &1.84756D+5+x**11*1.6796D+5+x**12*1.2597D+5+x**13*7.752D+4+x**14*3.& &876D+4+x**15*1.5504D+4+x**16*4.845D+3+x**17*1.14D+3+x**18*1.9D+2+x& &**19*2.0D+1+x**20+1.0D0 ```

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

## Parameters

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

## Options

 `NoWarning` Suppress warnings. `Version` 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.