# 4.7 FORTRAN Format¶

In addition to turning FORTRAN output on and off and stating where the output formats:FORTRAN output should be placed, there are many options that control the FORTRAN output format appearance of the generated code. In this section we describe some of the basic options. Issue )set fortran to see a full list with their current settings.

The output FORTRAN expression usually begins in column 7. If the expression needs more than one line, the ampersand character & is used in column 6. Since some versions of FORTRAN have restrictions on the number of lines per statement, FriCAS breaks long expressions into segments with a maximum of 1320 characters (20 lines of 66 characters) per segment. set fortran If you want to change this, say, to 660 characters, issue the system command set fortran explength )set fortran explength 660. FORTRAN output format:breaking into multiple statements You can turn off the line breaking by issuing )set fortran segment off. set fortran segment Various code optimization levels are available.

FORTRAN output is produced after you issue this. set output fortran

```
)set output fortran on
```

For the initial examples, we set the optimization level to 0, which is the lowest level. set fortran optlevel

```
)set fortran optlevel 0
```

The output is usually in columns 7 through 72, although fewer columns are used in the following examples so that the output FORTRAN output format:line length fits nicely on the page.

```
)set fortran fortlength 60
```

By default, the output goes to the screen and is displayed before the standard FriCAS two-dimensional output. In this example, an assignment to the variable R1 was generated because this is the result of step 1.

```
(x+y)^3
```

```
R1=y^3+3*x*y*y+3*x*x*y+x^3
```

y3+3xy2+3x2y+x3 |

_{Type: Polynomial Integer}

Here is an example that illustrates the line breaking.

```
(x+y+z)^3
```

```
R2=z^3+(3*y+3*x)*z*z+(3*y*y+6*x*y+3*x*x)*z+y^3+3*x*y
&*y+3*x*x*y+x^3
```

z3+(3y+3x)z2+(3y2+6xy+3x2)z+y3+3xy2+3x2y+x3 |

_{Type: Polynomial Integer}

Note in the above examples that integers are generally converted to FORTRAN output format:integers vs. floats floating point numbers, except in exponents. This is the default behavior but can be turned off by issuing )set fortran ints2floats off. set fortran ints2floats The rules governing when the conversion is done are:

- If an integer is an exponent, convert it to a floating point number if it is greater than 32767 in absolute value, otherwise leave it as an integer.
- Convert all other integers in an expression to floating point numbers.

These rules only govern integers in expressions. Numbers generated by FriCAS for DIMENSION statements are also integers.

To set the type of generated FORTRAN data, FORTRAN output format:data types use one of the following:

```
)set fortran defaulttype REAL
)set fortran defaulttype INTEGER
)set fortran defaulttype COMPLEX
)set fortran defaulttype LOGICAL
)set fortran defaulttype CHARACTER
```

When temporaries are created, they are given a default type of REAL. Also, the REAL versions of functions are used by default.

```
sin(x)
```

```
R3=DSIN(x)
```

sin(x) |

_{Type: Expression Integer}

At optimization level 1, FriCAS removes common subexpressions. FORTRAN output format:optimization level set fortran optlevel

```
)set fortran optlevel 1
```

```
(x+y+z)^3
```

```
T2=y*y
T3=x*x
R4=z^3+(3*y+3*x)*z*z+(3*T2+6*x*y+3*T3)*z+y^3+3*x*T2+
&3*T3*y+x^3
```

z3+(3y+3x)z2+(3y2+6xy+3x2)z+y3+3xy2+3x2y+x3 |

_{Type: Polynomial Integer}

This changes the precision to DOUBLE. set fortran precision double Substitute single for double FORTRAN output format:precision to return to single precision. set fortran precision single

```
)set fortran precision double
```

Complex constants display the precision.

```
2.3 + 5.6*%i
```

```
R5=(2.3D0,5.6D0)
```

2.3+5.6i |

_{Type: Complex Float}

The function names that FriCAS generates depend on the chosen precision.

```
sin %e
```

```
R6=DSIN(DEXP(1))
```

sin(e) |

_{Type: Expression Integer}

Reset the precision to single and look at these two examples again.

```
)set fortran precision single
```

```
2.3 + 5.6*%i
```

```
R7=(2.3,5.6)
```

2.3+5.6i |

_{Type: Complex Float}

```
sin %e
```

```
R8=SIN(EXP(1))
```

sin(e) |

_{Type: Expression Integer}

Expressions that look like lists, streams, sets or matrices cause array code to be generated.

```
[x+1,y+1,z+1]
```

```
T1(1)=x+1
T1(2)=y+1
T1(3)=z+1
R9=T1
```

[x+1,y+1,z+1] |

_{Type: List Polynomial Integer}

A temporary variable is generated to be the name of the array. FORTRAN output format:arrays This may have to be changed in your particular application.

```
set[2,3,4,3,5]
```

```
T1(1)=2
T1(2)=3
T1(3)=4
T1(4)=5
R10=T1
```

{2,3,4,5} |

_{Type: Set PositiveInteger}

By default, the starting index for generated FORTRAN arrays is 0.

```
matrix [ [2.3,9.7],[0.0,18.778] ]
```

```
T1(0,0)=2.3
T1(0,1)=9.7
T1(1,0)=0.0
T1(1,1)=18.778
T1
```

[2.39.70.018.778] |

_{Type: Matrix Float}

To change the starting index for generated FORTRAN arrays to be 1, set fortran startindex issue this. This value can only be 0 or 1.

```
)set fortran startindex 1
```

Look at the code generated for the matrix again.

```
matrix [ [2.3,9.7],[0.0,18.778] ]
```

```
T1(1,1)=2.3
T1(1,2)=9.7
T1(2,1)=0.0
T1(2,2)=18.778
T1
```

[2.39.70.018.778] |

_{Type: Matrix Float}