% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.

%
%
%
%
%

%
\newcommand{\ugInOutTitle}{Input Files and Output Styles}
\newcommand{\ugInOutNumber}{4.}
%
% =====================================================================
\begin{page}{ugInOutPage}{4. Input Files and Output Styles}
% =====================================================================
\beginscroll

In this chapter we discuss how to collect \Language{} statements
and commands into files and then read the contents into the
workspace.
We also show how to display the results of your computations in
several different styles including \TeX{}, FORTRAN and
monospace two-dimensional format.\footnote{\TeX{} is a
trademark of the American Mathematical Society.}

The printed version of this book uses the \Language{}
\TeX{} output formatter.
When we demonstrate a particular output style, we will need to
turn \TeX{} formatting off and the output style on so
that the correct output is shown in the text.

\beginmenu
    \menudownlink{{4.1. Input Files}}{ugInOutInPage}
    \menudownlink{{4.2. The .fricas.input File}}{ugInOutSpadprofPage}
    \menudownlink{{4.3. Common Features of Using Output Formats}}{ugInOutOutPage}
    \menudownlink{{4.4. Monospace Two-Dimensional Mathematical Format}}{ugInOutAlgebraPage}
    \menudownlink{{4.5. TeX Format}}{ugInOutTeXPage}
    \menudownlink{{4.6. Math ML Format}}{ugInOutMathMLPage}
    \menudownlink{{4.7. Texmacs Format}}{ugInOutTexmacsPage}
    \menudownlink{{4.8. FORTRAN Format}}{ugInOutFortranPage}
    \menudownlink{{4.9. General Fortran-generation utilities in \Language{}}}{ugGeneralFortranPage}
\endmenu
\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugInOutInTitle}{Input Files}
\newcommand{\ugInOutInNumber}{4.1.}
%
% =====================================================================
\begin{page}{ugInOutInPage}{4.1. Input Files}
% =====================================================================
\beginscroll
%
In this section we explain what an {\it input file} is and
%-% \HDindex{file!input}{ugInOutInPage}{4.1.}{Input Files}
why you would want to know about it.
We discuss where \Language{} looks for input files and how you can
direct it to look elsewhere.
We also show how to read the contents of an input file into the
\spadgloss{workspace} and how to use the \spadgloss{history}
facility to generate an input file from the statements you have
entered directly into the workspace.

An {\it input} file contains \Language{} expressions and system
commands.
Anything that you can enter directly to \Language{} can be put
into an input file.
This is how you save input functions and expressions that you wish
to read into \Language{} more than one time.

To read an input file into \Language{}, use the \spadsys{)read}
system command.
%-% \HDsyscmdindex{read}{ugInOutInPage}{4.1.}{Input Files}
For example, you can read a file in a particular directory by issuing
\begin{verbatim}
)read /spad/src/input/matrix.input
\end{verbatim}
The ``{\bf .input}'' is optional; this also works:
\begin{verbatim}
)read /spad/src/input/matrix
\end{verbatim}
What happens if you just enter
\spadsys{)read matrix.input} or even \spadsys{)read matrix}?
\Language{} looks in your current working directory for input files
that are not qualified by a directory name.
Typically, this directory is the directory from which you invoked
\Language{}.
To change the current working directory, use the \spadsys{)cd} system command.
The command \spadsys{)cd} by itself shows the current
working
%-% \HDindex{directory!default for searching}{ugInOutInPage}{4.1.}{Input Files}
directory.
%-% \HDsyscmdindex{cd}{ugInOutInPage}{4.1.}{Input Files}
To change it to
%-% \HDindex{file!input!where found}{ugInOutInPage}{4.1.}{Input Files}
the \spadsys{src/input} subdirectory for user ``babar'',
issue
\begin{verbatim}
)cd /u/babar/src/input
\end{verbatim}
\Language{} looks first in this directory for an input file.
If it is not found, it looks in the system's directories, assuming
you meant some input file that was provided with \Language{}.

\beginImportant
If you have the \Language{} history facility turned on (which it is
by default), you can save all the lines you have entered into the
workspace by entering
\begin{verbatim}
)history )write
\end{verbatim}
%-% \HDsyscmdindex{history )write}{ugInOutInPage}{4.1.}{Input Files}

\Language{} tells you what input file to edit to see your
statements.
The file is in your home directory or in the directory you
specified with \spadsys{)cd}.
%-% \HDsyscmdindex{cd}{ugInOutInPage}{4.1.}{Input Files}
\endImportant

In \downlink{``\ugLangBlocksTitle''}{ugLangBlocksPage} in Section \ugLangBlocksNumber\ignore{ugLangBlocks}
we discuss using indentation in input files to group statements
into {\it blocks.}

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugInOutSpadprofTitle}{The .fricas.input File}
\newcommand{\ugInOutSpadprofNumber}{4.2.}
%
% =====================================================================
\begin{page}{ugInOutSpadprofPage}{4.2. The .fricas.input File}
% =====================================================================
\beginscroll

When \Language{} starts up, it tries to read the input file
{\bf .fricas.input} from your home
%-% \HDindex{start-up profile file}{ugInOutSpadprofPage}{4.2.}{The .fricas.input File}
directory.
%-% \HDindex{file!start-up profile}{ugInOutSpadprofPage}{4.2.}{The .fricas.input File}
It there is no {\bf .fricas.input} in your home directory, it reads the copy
located in its own {\bf src/input} directory.
%-% \HDindex{file!.fricas.input @{\bf .fricas.input}}{ugInOutSpadprofPage}{4.2.}{The .fricas.input File}
The file usually contains
system commands to personalize your \Language{} environment.
In the remainder of this section we mention a few things
that users frequently place in their
{\bf .fricas.input} files.

In order to have FORTRAN output always produced from your
computations, place the system command
\spadsys{)set output fortran on}
in {\bf .fricas.input}.
%-% \HDsyscmdindex{quit}{ugInOutSpadprofPage}{4.2.}{The .fricas.input File}
If you do want to be prompted for confirmation when you issue
the \spadsys{)quit} system command, place
\spadsys{)set quit protected}
in {\bf .fricas.input}.
%-% \HDsyscmdindex{set quit protected}{ugInOutSpadprofPage}{4.2.}{The .fricas.input File}
If you then decide that you do not want to be prompted, issue
\spadsys{)set quit unprotected}.
%-% \HDsyscmdindex{set quit unprotected}{ugInOutSpadprofPage}{4.2.}{The .fricas.input File}
This is the default setting
\footnote{The
system command \spadsys{)pquit} always prompts you for
confirmation.}

To see the other system variables you can set, issue \spadsys{)set}
or use the \HyperName{} {\bf Settings} facility to view and change
\Language{} system variables.

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugInOutOutTitle}{Common Features of Using Output Formats}
\newcommand{\ugInOutOutNumber}{4.3.}
%
% =====================================================================
\begin{page}{ugInOutOutPage}{4.3. Common Features of Using Output Formats}
% =====================================================================
\beginscroll

In this section we discuss how to start and stop the display
%-% \HDindex{output formats!common features}{ugInOutOutPage}{4.3.}{Common Features of Using Output Formats}
of the different output formats and how to send the output to the
screen or to a file.
%-% \HDindex{file!sending output to}{ugInOutOutPage}{4.3.}{Common Features of Using Output Formats}
To fix ideas, we use FORTRAN output format for most of the
examples.

You can use the \spadsys{)set output}
system
%-% \HDindex{output formats!starting}{ugInOutOutPage}{4.3.}{Common Features of Using Output Formats}
command to
%-% \HDindex{output formats!stopping}{ugInOutOutPage}{4.3.}{Common Features of Using Output Formats}
toggle or redirect the different kinds of output.
%-% \HDsyscmdindex{set output}{ugInOutOutPage}{4.3.}{Common Features of Using Output Formats}
The name of the kind of output follows ``output'' in the command.
The names are

\indent{0}
\beginitems
\item[fortran]  for FORTRAN output.
\item[algebra]  for monospace two-dimensional mathematical output.
\item[tex]      for \TeX{} output.
\item[mathml]   for Math ML output.
\item[texmacs]  for Texmacs output.
\enditems
\indent{0}

For example, issue \spadsys{)set output fortran on} to turn on
FORTRAN format and
issue \spadsys{)set output fortran off} to turn it off.
By default, {\tt algebra} is {\tt on} and all others are {\tt off}.
%-% \HDsyscmdindex{set output fortran}{ugInOutOutPage}{4.3.}{Common Features of Using Output Formats}
When output is started, it is sent to the screen.
To send the output to a file, give the file name without
%-% \HDindex{output formats!sending to file}{ugInOutOutPage}{4.3.}{Common Features of Using Output Formats}
directory or extension.
\Language{} appends a file extension depending on the kind of
output being produced.
\xtc{
Issue this to redirect FORTRAN output to, for example, the file
{\bf linalg.sfort}.
}{
\spadpaste{)set output fortran linalg}
}
\nullXtc{
You must {\it also} turn on the creation of FORTRAN output.
The above just says where it goes if it is created.
}{
\spadpaste{)set output fortran on}
}
In what directory is this output placed?
It goes into the directory from which you started \Language{},
or if you have used the \spadsys{)cd} system command, the one
that you specified with \spadsys{)cd}.
%-% \HDsyscmdindex{cd}{ugInOutOutPage}{4.3.}{Common Features of Using Output Formats}
You should use \spadsys{)cd} before you send the output to the file.

\nullXtc{
You can always direct output back to the screen by issuing this.
%-% \HDindex{output formats!sending to screen}{ugInOutOutPage}{4.3.}{Common Features of Using Output Formats}
}{
\spadpaste{)set output fortran console}
}
\nullXtc{
Let's make sure FORTRAN formatting is off so that nothing we
do from now on produces FORTRAN output.
}{
\spadpaste{)set output fortran off}
}
\nullXtc{
We also delete the demonstrated output file we created.
}{
\spadpaste{)system rm linalg.sfort}
}

You can abbreviate the words ``\spad{on},'' ``\spad{off}'' and
``\spad{console}'' to the minimal number
of characters needed to distinguish them.
Because of this, you cannot send output to files called
{\bf on.sfort, off.sfort, of.sfort,
console.sfort, consol.sfort} and so on.

The width of the output on the page is set by
%-% \HDindex{output formats!line length}{ugInOutOutPage}{4.3.}{Common Features of Using Output Formats}
\spadsys{)set output length}
for all formats except FORTRAN.
%-% \HDsyscmdindex{set output length}{ugInOutOutPage}{4.3.}{Common Features of Using Output Formats}
Use \spadsys{)set fortran fortlength} to
change the FORTRAN line length from its default value of \spad{72}.

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugInOutAlgebraTitle}{Monospace Two-Dimensional Mathematical Format}
\newcommand{\ugInOutAlgebraNumber}{4.4.}
%
% =====================================================================
\begin{page}{ugInOutAlgebraPage}{4.4. Monospace Two-Dimensional Mathematical Format}
% =====================================================================
\beginscroll

This is the default output format for \Language{}.
%-% \HDsyscmdindex{set output algebra}{ugInOutAlgebraPage}{4.4.}{Monospace Two-Dimensional Mathematical Format}
It is usually on when you start the system.
%-% \HDindex{output formats!monospace 2D}{ugInOutAlgebraPage}{4.4.}{Monospace Two-Dimensional Mathematical Format}
%-% \HDindex{monospace 2D output format}{ugInOutAlgebraPage}{4.4.}{Monospace Two-Dimensional Mathematical Format}

\nullXtc{
If it is not, issue this.
}{
\spadpaste{)set output algebra on \bound{algon}}
}
\nullXtc{
Since the printed version of this book
(as opposed to the \HyperName{} version)
shows output produced by the
\TeX{} output formatter,
let us temporarily turn off
\TeX{} output.
}{
\spadpaste{)set output tex off \bound{texoff}}
}
\nullXtc{
Here is an example of what it looks like.
}{
\spadpaste{matrix [[i*x^i + j*\%i*y^j for i in 1..2] for j in 3..4] \free{algon texoff}}
}
\begin{verbatim}
+     3           3     2+
|3%i y  + x  3%i y  + 2x |
|                        |
|     4           4     2|
+4%i y  + x  4%i y  + 2x +
\end{verbatim}

\nullXtc{
Issue this to turn off this kind of formatting.
}{
\spadpaste{)set output algebra off}
}
\nullXtc{
Turn \TeX{} output on again.
}{
\spadpaste{)set output tex on}
}

The characters used for the matrix brackets above are rather ugly.
You get this character set when you issue
%-% \HDindex{character set}{ugInOutAlgebraPage}{4.4.}{Monospace Two-Dimensional Mathematical Format}
\spadsys{)set output characters plain}.
%-% \HDsyscmdindex{set output characters}{ugInOutAlgebraPage}{4.4.}{Monospace Two-Dimensional Mathematical Format}
This character set should be used when your machine or your
version of \Language{}
does not support Unicode character set.
If your machine and your version of \Language{} support Unicode, issue
\spadsys{)set output characters default}
to get better looking output.

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugInOutTeXTitle}{TeX Format}
\newcommand{\ugInOutTeXNumber}{4.5.}
%
% =====================================================================
\begin{page}{ugInOutTeXPage}{4.5. TeX Format}
% =====================================================================
\beginscroll

\Language{} can produce \TeX{} output for your
%-% \HDindex{output formats!TeX @{\TeX}}{ugInOutTeXPage}{4.5.}{TeX Format}
expressions.
%-% \HDindex{TeX output format @{\TeX} output format}{ugInOutTeXPage}{4.5.}{TeX Format}
The output is produced using macros from the
\LaTeX{} document preparation system by
Leslie Lamport.\footnote{See Leslie Lamport, {\it LaTeX: A Document
Preparation System,} Reading, Massachusetts: Addison-Wesley
Publishing Company, Inc., 1986.}
The printed version of this book was produced using this formatter.

\noOutputXtc{
To turn on \TeX{} output formatting, issue this.
%-% \HDsyscmdindex{set output tex}{ugInOutTeXPage}{4.5.}{TeX Format}
}{
\spadpaste{)set output tex on \bound{texon}}
}
Here is an example of its output.
\begin{verbatim}
matrix [[i*x^i + j*%i*y^j for i in 1..2] for j in 3..4]

\begin{fricasmath}{1}
\begin{MATRIX}{2}3\TIMES \ImaginaryI \TIMES \SUPER{\SYMBOL{y}}{3}+\SYMBOL{x}&%
3\TIMES \ImaginaryI \TIMES \SUPER{\SYMBOL{y}}{3}+2\TIMES \SUPER{\SYMBOL{x}}{2%
}\\4\TIMES \ImaginaryI \TIMES \SUPER{\SYMBOL{y}}{4}+\SYMBOL{x}&4\TIMES %
\ImaginaryI \TIMES \SUPER{\SYMBOL{y}}{4}+2\TIMES \SUPER{\SYMBOL{x}}{2}%
\end{MATRIX}%
\end{fricasmath}
\end{verbatim}
To turn \TeX{} output formatting off, issue
\spadsys{)set output tex off}.
The \LaTeX{} macros in the output generated by \Language{}
are generic. See the source file of \spadtype{TexFormat} for
appropriate definitions of these commands.

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugInOutMathMLTitle}{Math ML Format}
\newcommand{\ugInOutMathMLNumber}{4.6.}
%
% =====================================================================
\begin{page}{ugInOutMathMLPage}{4.6. Math ML Format}
% =====================================================================
\beginscroll

\Language{} can
%-% \HDindex{output formats!Math ML Format}{ugInOutMathMLPage}{4.6.}{Math ML Format}
produce Math ML format output for your
%-% \HDindex{Math ML Format}{ugInOutMathMLPage}{4.6.}{Math ML Format}
expressions.

\noOutputXtc{
To turn Math ML Format on, issue this.
%-% \HDsyscmdindex{set output mathml}{ugInOutMathMLPage}{4.6.}{Math ML Format}
}{
\spadpaste{)set output mathml on}
}
Here is an example of its output.
\begin{verbatim}
x+sqrt(2)

<math xmlns="http://www.w3.org/1998/Math/MathML" mathsize="big" display="block">
<mrow><mi>x</mi><mo>+</mo><msqrt><mrow><mn>2</mn></mrow></msqrt></mrow>
</math>
\end{verbatim}
\noOutputXtc{
To turn Math ML Format output formatting off, issue this.
}{
\spadpaste{)set output mathml off}
}
%
\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugInOutTexmacsTitle}{Texmacs Format}
\newcommand{\ugInOutTexmacsNumber}{4.7.}
%
% =====================================================================
\begin{page}{ugInOutTexmacsPage}{4.7. Texmacs Format}
% =====================================================================
\beginscroll

\Language{} can
%-% \HDindex{output formats!Texmacs Format}{ugInOutTexmacsPage}{4.7.}{Texmacs Format}
produce Texmacs Scheme format output for your
%-% \HDindex{Texmacs Format}{ugInOutTexmacsPage}{4.7.}{Texmacs Format}
expressions.  This is mostly useful for interfacing with Texmacs.

\noOutputXtc{
To turn Texmacs Format on, issue this.
%-% \HDsyscmdindex{set output texmacs}{ugInOutTexmacsPage}{4.7.}{Texmacs Format}
}{
\spadpaste{)set output texmacs on}
}
Here is an example of its output.
\begin{verbatim}
x+sqrt(2)

scheme: (with "mode" "math"
(concat (concat  "x" ) "+" (sqrt (concat  "2" )))
)
\end{verbatim}
\noOutputXtc{
To turn Texmacs Format output formatting off, issue this.
}{
\spadpaste{)set output texmacs off}
}

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugInOutFortranTitle}{FORTRAN Format}
\newcommand{\ugInOutFortranNumber}{4.8.}
%
% =====================================================================
\begin{page}{ugInOutFortranPage}{4.8. FORTRAN Format}
% =====================================================================
\beginscroll

In addition to turning FORTRAN output on and off and stating where the
%-% \HDindex{output formats!FORTRAN}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
output should be placed, there are many options that control the
%-% \HDindex{FORTRAN output format}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
appearance of the generated code.
In this section we describe some of the basic options.
Issue \spadsys{)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
\spadSyntax{\&} is used in column 6.
Since some versions of FORTRAN have restrictions on the number of lines
per statement, \Language{} breaks long expressions into segments with
a maximum of 1320 characters (20 lines of 66 characters) per segment.
%-% \HDsyscmdindex{set fortran}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
If you want to change this, say, to 660 characters,
issue the system command
%-% \HDsyscmdindex{set fortran explength}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
\spadsys{)set fortran explength 660}.
%-% \HDindex{FORTRAN output format!breaking into multiple statements}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
You can turn off the line breaking by issuing
\spadsys{)set fortran segment off}.
%-% \HDsyscmdindex{set fortran segment}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
Various code optimization levels are available.
%
\noOutputXtc{
FORTRAN output is produced after you issue this.
%-% \HDsyscmdindex{set output fortran}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
}{
\spadpaste{)set output fortran on \bound{forton}}
}
\noOutputXtc{
For the initial examples, we set the optimization level to 0, which is the
lowest level.
%-% \HDsyscmdindex{set fortran optlevel}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
}{
\spadpaste{)set fortran optlevel 0 \bound{opt0}\free{forton}}
}
\noOutputXtc{
The output is usually in columns 7 through 72, although fewer columns
are used in the following examples so that the output
%-% \HDindex{FORTRAN output format!line length}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
fits nicely on the page.
}{
\spadpaste{)set fortran fortlength 60}
}
\xtc{
By default, the output goes to the screen and is displayed
before the standard \Language{} two-dimensional output.
In this example, an
assignment to the variable \spad{R1} was generated because this is
the result of step 1.
}{
\spadpaste{(x+y)^3 \free{opt0}}
}
\xtc{
Here is an example that illustrates the line breaking.
}{
\spadpaste{(x+y+z)^3 \free{opt0}}
}

Note in the above examples that integers are generally converted to
%-% \HDindex{FORTRAN output format!integers vs. floats}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
floating point numbers, except in exponents.
This is the default behavior but can be turned off by issuing
\spadsys{)set fortran ints2floats off}.
%-% \HDsyscmdindex{set fortran ints2floats}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
The rules governing when the conversion is done are:
\indent{4}
\beginitems
\item[1. ] 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.
\item[2. ] Convert all other integers in an expression to floating
point numbers.
\enditems
\indent{0}
These rules only govern integers in expressions.
Numbers generated by \Language{} for \spad{DIMENSION} statements are also
integers.

To set the type of generated FORTRAN data,
%-% \HDindex{FORTRAN output format!data types}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
use one of the following:
\begin{verbatim}
)set fortran defaulttype REAL
)set fortran defaulttype INTEGER
)set fortran defaulttype COMPLEX
)set fortran defaulttype LOGICAL
)set fortran defaulttype CHARACTER
\end{verbatim}

\xtc{
When temporaries are created, they are given a default type of
{\tt REAL.}
Also, the {\tt REAL} versions of functions are used by default.
}{
\spadpaste{sin(x) \free{opt1}}
}
\noOutputXtc{
At optimization level 1, \Language{} removes common subexpressions.
%-% \HDindex{FORTRAN output format!optimization level}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
%-% \HDsyscmdindex{set fortran optlevel}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
}{
\spadpaste{)set fortran optlevel 1 \bound{opt1}\free{forton}}
}
\xtc{
}{
\spadpaste{(x+y+z)^3 \free{opt1}}
}
\noOutputXtc{
This changes the precision to {\tt DOUBLE}.
%-% \HDsyscmdindex{set fortran precision double}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
Substitute \spad{single} for \spad{double}
%-% \HDindex{FORTRAN output format!precision}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
to return to single precision.
%-% \HDsyscmdindex{set fortran precision single}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
}{
\spadpaste{)set fortran precision double \free{opt1}\bound{double1}}
}
\xtc{
Complex constants display the precision.
}{
\spadpaste{2.3 + 5.6*\%i  \free{double1}}
}
\xtc{
The function names that \Language{} generates depend on the chosen
precision.
}{
\spadpaste{sin \%e  \free{double1}}
}
\noOutputXtc{
Reset the precision to \spad{single} and look at these two
examples again.
}{
\spadpaste{)set fortran precision single \free{opt1}\bound{single1}}
}
\xtc{
}{
\spadpaste{2.3 + 5.6*\%i  \free{single1}}
}
\xtc{
}{
\spadpaste{sin \%e  \free{single1}}
}
\xtc{
Expressions that look like lists, streams, sets or matrices cause
array code to be generated.
}{
\spadpaste{[x+1,y+1,z+1] \free{opt1}}
}
\xtc{
A temporary variable is generated to be the name of the array.
%-% \HDindex{FORTRAN output format!arrays}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
This may have to be changed in your particular application.
}{
\spadpaste{set[2,3,4,3,5] \free{opt1}}
}
\xtc{
By default, the starting index for generated FORTRAN arrays is \spad{0}.
}{
\spadpaste{matrix [[2.3,9.7],[0.0,18.778]] \free{opt1}}
}
\noOutputXtc{
To change the starting index for generated FORTRAN arrays to be \spad{1},
%-% \HDsyscmdindex{set fortran startindex}{ugInOutFortranPage}{4.8.}{FORTRAN Format}
issue this.
This value can only be \spad{0} or \spad{1}.
}{
\spadpaste{)set fortran startindex 1 \free{opt1}\bound{start1}}
}
\xtc{
Look at the code generated for the matrix again.
}{
\spadpaste{matrix [[2.3,9.7],[0.0,18.778]] \free{start1}}
}

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGeneralFortranTitle}{General Fortran-generation utilities in \Language{}}
\newcommand{\ugGeneralFortranNumber}{4.9.}
%
% =====================================================================
\begin{page}{ugGeneralFortranPage}{4.9. General Fortran-generation utilities in \Language{}}
% =====================================================================
\beginscroll

This section describes more advanced facilities which are available to users
who wish to generate Fortran code from within \Language{}.  There are
facilities to manipulate templates, store type information, and generate
code fragments or complete programs.

\beginmenu
    \menudownlink{{4.9.1. Template Manipulation}}{ugGenForTemplatePage}
    \menudownlink{{4.9.2. Manipulating the Fortran Output Stream}}{ugGneForManipulatingPage}
    \menudownlink{{4.9.3. Fortran Types}}{ugGenForTypesPage}
    \menudownlink{{4.9.4. FortranScalarType}}{ugGenForScalarTypePage}
    \menudownlink{{4.9.5. FortranType}}{ugGenForTypePage}
    \menudownlink{{4.9.6. SymbolTable}}{ugGenForSymbolTablePage}
    \menudownlink{{4.9.7. TheSymbolTable}}{ugGenForTheSymbolTablePage}
    \menudownlink{{4.9.8. Advanced Fortran Code Generation}}{ugGenForAdvancedPage}
    \menudownlink{{4.9.9. Switch}}{ugGenForSwitchPage}
    \menudownlink{{4.9.10. FortranCode}}{ugGenForCodePage}
    \menudownlink{{4.9.11. FortranProgram}}{ugGenForProgramPage}
\endmenu
\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGenForTemplateTitle}{Template Manipulation}
\newcommand{\ugGenForTemplateNumber}{4.9.1.}
%
% =====================================================================
\begin{page}{ugGenForTemplatePage}{4.9.1. Template Manipulation}
% =====================================================================
\beginscroll

A template is a skeletal program which is ``fleshed out'' with data when
it is processed.  It is a sequence of {\em active} and {\em passive} parts:
active parts are sequences of \Language{} commands which are processed as if they
had been typed into the interpreter; passive parts are simply echoed
verbatim on the Fortran output stream.

Suppose, for example, that we have the following template, stored in
the file ``test.tem'':
\begin{verbatim}
-- A simple template
beginVerbatim
      DOUBLE PRECISION FUNCTION F(X)
      DOUBLE PRECISION X
endVerbatim
outputAsFortran("F",f)
beginVerbatim
      RETURN
      END
endVerbatim
\end{verbatim}
The passive parts lie between the two
tokens {\tt beginVerbatim} and  {\tt endVerbatim}.  There
are two active statements: one which is simply a \Language{} (
{\tt --})
comment, and one which produces an assignment to the current value
of {\tt f}.  We could use it as follows:
\begin{verbatim}
(4) ->f := 4.0/(1+X^2)

           4
   (4)   ------
          2
         X  + 1

(5) ->processTemplate "test.tem"
      DOUBLE PRECISION FUNCTION F(X)
      DOUBLE PRECISION X
      F=4.0D0/(X*X+1.0D0)
      RETURN
      END

   (5)  "CONSOLE"
\end{verbatim}

(A more reliable method of specifying the filename will be introduced
below.)  Note that the Fortran assignment {\tt F=4.0D0/(X*X+1.0D0)}
automatically converted 4.0 and 1 into DOUBLE PRECISION numbers; in
general, the \Language{} Fortran generation facility will convert
anything which should be a floating point object into either
a Fortran REAL or DOUBLE PRECISION object.
\noOutputXtc{
Which alternative is used is determined by the command
}{
\spadpaste{)set fortran precision}
}

It is sometimes useful to end a template before the file itself ends (e.g. to
allow the template to be tested incrementally or so that a piece of text
describing how the template works can be included).  It is of course possible
to ``comment-out'' the remainder of the file.  Alternatively, the single token
{\tt endInput} as part of an active portion of the template will cause
processing to be ended prematurely at that point.

The \spadfun{processTemplate} command comes in two flavours.  In the first case,
illustrated above, it takes one argument of domain \spadtype{FileName},
the name of the template to be processed, and writes its output on the
current Fortran output stream.  In general, a filename can be generated
from {\em directory}, {\em name} and {\em extension} components, using
the operation \spadfun{filename}, as in
\begin{verbatim}
processTemplate filename("","test","tem")
\end{verbatim}
There is an alternative version of \spadfun{processTemplate}, which
takes two arguments (both of domain \spadtype{FileName}).  In this case the
first argument is the name of the template to be processed, and the
second is the file in which to write the results.  Both versions return
the location of the generated Fortran code as their result
({\tt "CONSOLE"} in the above example).

It is sometimes useful to be able to mix active and passive parts of a
line or statement.  For example you might want to generate a Fortran
Comment describing your data set.  For this kind of application we
provide three functions as follows:
\newline
\spadfun{fortranLiteral}\tab{25}writes a string on the Fortran output stream\newline
\spadfun{fortranCarriageReturn}\tab{25}writes a carriage return on the Fortran output stream\newline
\spadfun{fortranLiteralLine}\tab{25}writes a string followed by a return on the Fortran output stream\newline
\xtc{
So we could create our comment as follows:
}{
\spadpaste{m := matrix [[1,2,3],[4,5,6]]\bound{m}}
}
\xtc{
}{
\spadpaste{fortranLiteralLine(concat ["C\ \ \ \ \ \ The\ Matrix\ has\ ", nrows(m)::String, "\ rows\ and\ ", ncols(m)::String, "\ columns"])\$FortranTemplate \free{m}}
}
\xtc{
or, alternatively:
}{
\spadpaste{fortranLiteral("C\ \ \ \ \ \ The\ Matrix\ has\ ")\$FortranTemplate}
}
\xtc{
}{
\spadpaste{fortranLiteral(nrows(m)::String)\$FortranTemplate}
}
\xtc{
}{
\spadpaste{fortranLiteral("\ rows\ and\ ")\$FortranTemplate}
}
\xtc{
}{
\spadpaste{fortranLiteral(ncols(m)::String)\$FortranTemplate\free{m}}
}
\xtc{
}{
\spadpaste{fortranLiteral("\ columns")\$FortranTemplate}
}
\noOutputXtc{
}{
\spadpaste{fortranCarriageReturn()\$FortranTemplate}
}

We should stress that these functions, together with the \spadfun{outputAsFortran}
function are the {\em only} sure ways
of getting output to appear on the Fortran output stream.  Attempts to use
\Language{} commands such as \spadfun{output} or \spadfunX{writeline} may appear to give
the required result when displayed on the console, but will give the wrong
result when Fortran and algebraic output are sent to differing locations.  On
the other hand, these functions can be used to send helpful messages to the
user, without interfering with the generated Fortran.

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGneForManipulatingTitle}{Manipulating the Fortran Output Stream}
\newcommand{\ugGneForManipulatingNumber}{4.9.2.}
%
% =====================================================================
\begin{page}{ugGneForManipulatingPage}{4.9.2. Manipulating the Fortran Output Stream}
% =====================================================================
\beginscroll


Sometimes it is useful to manipulate the Fortran output stream in a program,
possibly without being aware of its current value.  The main use of this is
for gathering type declarations (see ``Fortran Types'' below) but it can be useful
in other contexts as well.  Thus we provide a set of commands to manipulate
a stack of (open) output streams.  Only one stream can be written to at
any given time.  The stack is never empty---its initial value is the
console or the current value of the Fortran output stream, and can be
determined using
\xtc{
}{
\spadpaste{topFortranOutputStack()\$FortranOutputStackPackage}
}
(see below).
The commands available to manipulate the stack are:
\newline
\spadfun{clearFortranOutputStack}\tab{25}resets the stack\newline
\spadfun{pushFortranOutputStack}\tab{25}pushes a \spadtype{FileName} onto the stack\newline
\spadfun{popFortranOutputStack}\tab{25}pops the stack\newline
\spadfun{showFortranOutputStack}\tab{25}returns the current stack\newline
\spadfun{topFortranOutputStack}\tab{25}returns the top element of the stack\newline
These commands are all part of \spadtype{FortranOutputStackPackage}.

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGenForTypesTitle}{Fortran Types}
\newcommand{\ugGenForTypesNumber}{4.9.3.}
%
% =====================================================================
\begin{page}{ugGenForTypesPage}{4.9.3. Fortran Types}
% =====================================================================
\beginscroll

When generating code it is important to keep track of the Fortran types of
the objects which we are generating.  This is useful for a number of reasons,
not least to ensure that we are actually generating legal Fortran code.  The
current type system is built up in several layers, and we shall describe each
in turn.

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGenForScalarTypeTitle}{FortranScalarType}
\newcommand{\ugGenForScalarTypeNumber}{4.9.4.}
%
% =====================================================================
\begin{page}{ugGenForScalarTypePage}{4.9.4. FortranScalarType}
% =====================================================================
\beginscroll


This domain represents the simple Fortran datatypes: REAL, DOUBLE PRECISION,
COMPLEX, LOGICAL, INTEGER, and CHARACTER.
It is possible to \spadfun{coerce} a \spadtype{String} or \spadtype{Symbol}
into the domain, test whether two objects are equal, and also apply
the predicate functions \spadfunFrom{real?}{FortranScalarType} etc.

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGenForTypeTitle}{FortranType}
\newcommand{\ugGenForTypeNumber}{4.9.5.}
%
% =====================================================================
\begin{page}{ugGenForTypePage}{4.9.5. FortranType}
% =====================================================================
\beginscroll


This domain represents ``full'' types: i.e., datatype plus array dimensions
(where appropriate) plus whether or not the parameter is an external
subprogram.  It is possible to \spadfun{coerce} an object of
\spadtype{FortranScalarType} into the domain or \spadfun{construct} one
from an element of \spadtype{FortranScalarType}, a list of
\spadtype{Polynomial Integer}s (which can of course be simple integers or
symbols) representing its dimensions, and
a \spadtype{Boolean} declaring whether it is external or not.  The list
of dimensions must be empty if the \spadtype{Boolean} is {\tt true}.
The functions \spadfun{scalarTypeOf}, \spadfun{dimensionsOf} and
\spadfun{external?} return the appropriate
parts, and it is possible to get the various basic Fortran Types via
functions like \spadfun{fortranReal}.
\xtc{
For example:
}{
\spadpaste{type:=construct(real,[i,10],false)\$FortranType}
}
\xtc{
or
}{
\spadpaste{type:=[real,[i,10],false]\$FortranType\bound{type}}
}
\xtc{
}{
\spadpaste{scalarTypeOf type\free{type}}
}
\xtc{
}{
\spadpaste{dimensionsOf type\free{type}}
}
\xtc{
}{
\spadpaste{external?  type\free{type}}
}
\xtc{
}{
\spadpaste{fortranLogical()\$FortranType}
}
\xtc{
}{
\spadpaste{construct(integer,[],true)\$FortranType}
}

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGenForSymbolTableTitle}{SymbolTable}
\newcommand{\ugGenForSymbolTableNumber}{4.9.6.}
%
% =====================================================================
\begin{page}{ugGenForSymbolTablePage}{4.9.6. SymbolTable}
% =====================================================================
\beginscroll


This domain creates and manipulates a symbol table for generated Fortran code.
This is used by \spadtype{FortranProgram} to represent the types of objects in
a subprogram.  The commands available are:\newline
\spadfun{empty}\tab{25}creates a new \spadtype{SymbolTable}\newline
\spadfunX{declare}\tab{25}creates a new entry in a table \newline
\spadfun{fortranTypeOf}\tab{25}returns the type of an object in a table \newline
\spadfun{parametersOf}\tab{25}returns a list of all the symbols in the table \newline
\spadfun{typeList}\tab{25}returns a list of all objects of a given type \newline
\spadfun{typeLists}\tab{25}returns a list of lists of all objects sorted by type \newline
\spadfun{externalList}\tab{25}returns a list of all {\tt EXTERNAL} objects \newline
\spadfun{printTypes}\tab{25}produces Fortran type declarations from a table\newline
\xtc{
}{
\spadpaste{symbols := empty()\$SymbolTable\bound{symbols}}
}
\xtc{
}{
\spadpaste{declare!(X, fortranReal()\$FortranType, symbols)\free{symbols}}
}
\xtc{
}{
\spadpaste{declare!(M,construct(real,[i,j],false)\$FortranType,symbols)\free{symbols}}
}
\xtc{
}{
\spadpaste{declare!([i,j], fortranInteger()\$FortranType, symbols)\free{symbols} \bound{i j} }
}
\xtc{
}{
\spadpaste{symbols\free{symbols}}
}
\xtc{
}{
\spadpaste{fortranTypeOf(i,symbols)\free{symbols i}}
}
\xtc{
}{
\spadpaste{typeList(real,symbols)\free{symbols}}
}
\xtc{
}{
\spadpaste{printTypes symbols\free{symbols}}
}

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGenForTheSymbolTableTitle}{TheSymbolTable}
\newcommand{\ugGenForTheSymbolTableNumber}{4.9.7.}
%
% =====================================================================
\begin{page}{ugGenForTheSymbolTablePage}{4.9.7. TheSymbolTable}
% =====================================================================
\beginscroll


This domain creates and manipulates one global symbol table to be used, for
example, during template processing. It is
also used when
linking to external Fortran routines. The
information stored for each subprogram (and the main program segment, where
relevant) is:
\indent{4}
\beginitems
\item[-] its name;
\item[-] its return type;
\item[-] its argument list;
\item[-] and its argument types.
\enditems
\indent{0}
Initially, any information provided is deemed to be for the main program
segment.
\xtc{
Issuing the following command indicates that from now on all information
refers to the subprogram \spad{F}.
}{
\spadpaste{newSubProgram(F)\$TheSymbolTable}
}
\xtc{
It is possible to return to processing the main program segment by issuing
the command:
}{
\spadpaste{endSubProgram()\$TheSymbolTable}
}
The following commands exist:
\newline
\spadfunX{returnType}\tab{25}declares the return type of the current subprogram \newline
\spadfun{returnTypeOf}\tab{25}returns the return type of a subprogram \newline
\spadfunX{argumentList}\tab{25}declares the argument list of the current subprogram \newline
\spadfun{argumentListOf}\tab{25}returns the argument list of a subprogram \newline
\spadfunX{declare}\tab{25}provides type declarations for parameters of the current subprogram \newline
\spadfun{symbolTableOf}\tab{25}returns the symbol table  of a subprogram \newline
\spadfun{printHeader}\tab{25}produce the Fortran header for the current subprogram \newline
In addition there are versions of these commands which are parameterised by
the name of a subprogram, and others parameterised by both the name of a
subprogram and by an instance of \spadtype{TheSymbolTable}.
\xtc{
}{
\spadpaste{newSubProgram(F)\$TheSymbolTable \bound{forPleasure}}
}
\xtc{
}{
\spadpaste{argumentList!(F, [X])\$TheSymbolTable\free{forPleasure}}
}
\xtc{
}{
\spadpaste{returnType!(F,real)\$TheSymbolTable \free{forPleasure}}
}
\xtc{
}{
\spadpaste{declare!(X,fortranReal(),F)\$TheSymbolTable \free{forPleasure}}
}
\xtc{
}{
\spadpaste{printHeader(F)\$TheSymbolTable\free{forPleasure}}
}

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGenForAdvancedTitle}{Advanced Fortran Code Generation}
\newcommand{\ugGenForAdvancedNumber}{4.9.8.}
%
% =====================================================================
\begin{page}{ugGenForAdvancedPage}{4.9.8. Advanced Fortran Code Generation}
% =====================================================================
\beginscroll

This section describes facilities for representing Fortran statements, and
building up complete subprograms from them.

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGenForSwitchTitle}{Switch}
\newcommand{\ugGenForSwitchNumber}{4.9.9.}
%
% =====================================================================
\begin{page}{ugGenForSwitchPage}{4.9.9. Switch}
% =====================================================================
\beginscroll


This domain is used to represent statements like {\tt x < y}.  Although
these can be represented directly in \Language{}, it is a little cumbersome.

Instead we have a set of operations, such as \spadfun{LT} to represent
\spad{<},
to let us build such statements.  The available constructors are:
\newline
\spadfun{LT}\tab{25}< \newline
\spadfun{GT}\tab{25}> \newline
\spadfun{LE}\tab{25}<= \newline
\spadfun{GE}\tab{25}>= \newline
\spadfun{EQ}\tab{25}= \newline
\spadfun{AND}\tab{25}{\tt and}\newline
\spadfun{OR}\tab{25}{\tt or} \newline
\spadfun{NOT}\tab{25}{\tt not} \newline
\xtc{
So for example:
}{
\spadpaste{LT(x,y)\$Switch}
}

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGenForCodeTitle}{FortranCode}
\newcommand{\ugGenForCodeNumber}{4.9.10.}
%
% =====================================================================
\begin{page}{ugGenForCodePage}{4.9.10. FortranCode}
% =====================================================================
\beginscroll

This domain represents code segments or operations: currently assignments,
conditionals, blocks, comments, gotos, continues, various kinds of loops,
and return statements.
\xtc{
For example we can create quite a complicated conditional statement using
assignments, and then turn it into Fortran code:
}{
\spadpaste{c := cond(LT(X,Y),assign(F,X),cond(GT(Y,Z),assign(F,Y),assign(F,Z))\$FortranCode)\$FortranCode \bound{c}}
}
\xtc{
}{
\spadpaste{printCode c\free{c}}
}

The Fortran code is printed
on the current Fortran output stream.

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugGenForProgramTitle}{FortranProgram}
\newcommand{\ugGenForProgramNumber}{4.9.11.}
%
% =====================================================================
\begin{page}{ugGenForProgramPage}{4.9.11. FortranProgram}
% =====================================================================
\beginscroll


This domain is used to construct complete Fortran subprograms out of
elements of \spadtype{FortranCode}.  It is parameterised by the name of the
target subprogram (a \spadtype{Symbol}), its return type (from
\spadtype{Union}(\spadtype{FortranScalarType},``void'')),
its arguments (from \spadtype{List Symbol}), and
its symbol table (from \spadtype{SymbolTable}).  One can
\spadfun{coerce} elements of either \spadtype{FortranCode}
or \spadtype{Expression} into it.

\xtc{
First of all we create a symbol table:
}{
\spadpaste{symbols := empty()\$SymbolTable\bound{symbols}}
}
\xtc{
Now put some type declarations into it:
}{
\spadpaste{declare!([X,Y],fortranReal()\$FortranType,symbols)\free{symbols}}
}
\xtc{
Then (for convenience)
we set up the particular instantiation of \spadtype{FortranProgram}
}{
\spadpaste{FP := FortranProgram(F,real,[X,Y],symbols)\free{symbols}\bound{FP}}
}
\xtc{
Create an object of type \spadtype{Expression(Integer)}:
}{
\spadpaste{asp := X*sin(Y)\bound{asp}}
}
\xtc{
Now \spadfun{coerce} it into \spadtype{FP}, and print its Fortran form:
}{
\spadpaste{outputAsFortran(asp::FP)\free{FP asp}}
}

We can generate a \spadtype{FortranProgram} using \spadtype{FortranCode}.
For example:
\xtc{
Augment our symbol table:
}{
\spadpaste{declare!(Z,fortranReal()\$FortranType,symbols)\free{symbols}\bound{Z}}
}
\xtc{
prepare conditional:
}{
\spadpaste{cc := cond(LT(X,Y),assign(F,X),assign(F,Y))\$FortranCode \bound{cc}}
}
\xtc{
and transform the conditional expression we prepared earlier:
}{
\spadpaste{outputAsFortran([cc,returns()\$FortranCode]::FP) \free{FP cc Z}}
}
\endscroll
\autobuttons
\end{page}
%
