A considerable degree of flexibility is available in REDUCE in the printing of expressions generated during calculations. No explicit format statements are supplied, as these are in most cases of little use in algebraic calculations, where the size of output or its composition is not generally known in advance. Instead, REDUCE provides a series of mode options to the user that should enable him to produce his output in a comprehensible and possibly pleasing form.
The most extreme option offered is to suppress the output entirely from any top level evaluation. This is accomplished by turning off the switch OUTPUT which is normally on. It is useful for limiting output when loading large files or producing “clean” output from the prettyprint programs.
In most circumstances, however, we wish to view the output, so we need to know how to format it appropriately. As we mentioned earlier, an algebraic expression is normally printed in an expanded form, filling the whole output line with terms. Certain output declarations, however, can be used to affect this format. To begin with, we look at an operator for changing the length of the output line.
This operator is used with the syntax
and sets the output line length to the integer NUM. It returns the previous output line length (so that it can be stored for later resetting of the output line if needed).
We now describe a number of switches and declarations that are available for controlling output formats. It should be noted, however, that the transformation of large expressions to produce these varied output formats can take a lot of computing time and space. If a user wishes to speed up the printing of the output in such cases, he can turn off the switch PRI. If this is done, then output is produced in one fixed format, which basically reflects the internal form of the expression, and none of the options below apply. PRI is normally on.
With PRI on, the output declarations and switches available are as follows:
The declaration ORDER may be used to order variables on output. The syntax is:
where the vi are kernels. Thus,
orders X ahead of Y, Y ahead of Z and all three ahead of other variables not given an order. order nil; resets the output order to the system default. The order of variables may be changed by further calls of ORDER, but then the reordered variables would have an order lower than those in earlier ORDER calls. Thus,
would order Z ahead of Y and X. The default ordering is usually alphabetic.
This declaration takes a list of identifiers or kernels as argument. FACTOR is not a factoring command (use FACTORIZE or the FACTOR switch for this purpose); rather it is a separation command. All terms involving fixed powers of the declared expressions are printed as a product of the fixed powers and a sum of the rest of the terms.
For example, after the declaration
the polynomial (x + y + 1)2 will be printed as
All expressions involving a given prefix operator may also be factored by putting the operator name in the list of factored identifiers. For example:
causes all powers of X and SIN(X) and all functions of COS to be factored.
Note that FACTOR does not affect the order of its arguments. You should also use ORDER if this is important.
The declaration remfac v1,...,vn; removes the factoring flag from the expressions v1 through vn.
In addition to these declarations, the form of the output can be modified by switching various output control switches using the declarations ON and OFF. We shall illustrate the use of these switches by an example, namely the printing of the expression
The relevant switches are as follows:
This switch will cause the system to search the whole expression, or any sub-expression enclosed in parentheses, for simple multiplicative factors and print them outside the parentheses. Thus our expression with ALLFAC off will print as
and with ALLFAC on as
ALLFAC is normally on, and is on in the following examples, except where otherwise stated.
This switch makes the system search the denominator of an expression for simple factors that it divides into the numerator, so that rational fractions and negative powers appear in the output. With DIV on, our expression would print as
DIV is normally off.
This switch causes the system to print each term in any sum on a separate line. With LIST on, our expression prints as
LIST is normally off.
Under normal circumstances, the printing routines try to break an expression across lines at a natural point. This is a fairly expensive process. If you are not overly concerned about where the end-of-line breaks come, you can speed up the printing of expressions by turning off the switch NOSPLIT. This switch is normally on.
This switch is only useful with expressions in which variables are factored with FACTOR. With this mode, the overall denominator of the expression is printed with each factored sub-expression. We assume a prior declaration factor x; in the following output. We first print the expression with RAT set to off:
With RAT on the output becomes:
RAT is normally off.
Next, if we leave X factored, and turn on both DIV and RAT, the result becomes
Finally, with X factored, RAT on and ALLFAC off we retrieve the original structure
If the numerator and denominator of an expression can each be printed in one line, the output routines will print them in a two dimensional notation, with numerator and denominator on separate lines and a line of dashes in between. For example, (a+b)/2 will print as
Turning this switch off causes such expressions to be output in a linear form.
The normal ordering of terms in output is from highest to lowest power. In some situations (e.g., when a power series is output), the opposite ordering is more convenient. The switch REVPRI if on causes such a reverse ordering of terms. For example, the expression y*(x+1)^2+(y+3)^2 will normally print as
whereas with REVPRI on, it will print as
8.3.4 WRITE Command
In simple cases no explicit output command is necessary in REDUCE, since the value of any expression is automatically printed if a semicolon is used as a delimiter. There are, however, several situations in which such a command is useful.
In a FOR, WHILE, or REPEAT statement it may be desired to output something each time the statement within the loop construct is repeated.
It may be desired for a procedure to output intermediate results or other information while it is running. It may be desired to have results labeled in special ways, especially if the output is directed to a file or device other than the terminal.
The WRITE command consists of the word WRITE followed by one or more items separated by commas, and followed by a terminator. There are three kinds of items that can be used:
The items specified by a single WRITE statement print side by side on one line. (The line is broken automatically if it is too long.) Strings print exactly as quoted. The WRITE command itself however does not return a value.
The print line is closed at the end of a WRITE command evaluation. Therefore the command WRITE ~~; (specifying nothing to be printed except the empty string) causes a line to be skipped.
will set M(3) to x+5 and print
The blanks between the 5 and B, and the 3 and T, come from the blanks in the quoted strings.
This will give us two columns of numbers. If we had used
we would also get A(i) := repeated on each line.
A portion of the output, to illustrate the printout from the WRITE command, is as follows:
It is sometimes annoying to have zero assignments (i.e. assignments of the form <expression> := 0) printed, especially in printing large arrays with many zero elements. The output from such assignments can be suppressed by turning on the switch NERO.
It is naturally possible to evaluate expressions numerically in REDUCE by giving all variables and sub-expressions numerical values. However, as we pointed out elsewhere the user must declare real arithmetical operation by turning on the switch ROUNDED. However, it should be remembered that arithmetic in REDUCE is not particularly fast, since results are interpreted rather than evaluated in a compiled form. The user with a large amount of numerical computation after all necessary algebraic manipulations have been performed is therefore well advised to perform these calculations in a FORTRAN or similar system. For this purpose, REDUCE offers facilities for users to produce FORTRAN compatible files for numerical processing.
First, when the switch FORT is on, the system will print expressions in a FORTRAN notation. Expressions begin in column seven. If an expression extends over one line, a continuation mark (.) followed by a blank appears on subsequent cards. After a certain number of lines have been produced (according to the value of the variable CARD_NO), a new expression is started. If the expression printed arises from an assignment to a variable, the variable is printed as the name of the expression. Otherwise the expression is given the default name ANS. An error occurs if identifiers or numbers are outside the bounds permitted by FORTRAN.
A second option is to use the WRITE command to produce other programs.
The following REDUCE statements
will generate a file forfil that contains:
If the arguments of a WRITE statement include an expression that requires continuation records, the output will need editing, since the output routine prints the arguments of WRITE sequentially, and the continuation mechanism therefore generates its auxiliary variables after the preceding expression has been printed.
Finally, since there is no direct analog of list in FORTRAN, a comment line of the form
will be printed if you try to print a list with FORT on.
There are a number of methods available to change the default format of the FORTRAN output.
The breakup of the expression into subparts is such that the number of continuation lines produced is less than a given number. This number can be modified by the assignment
card_no := ⟨number⟩;
where ⟨number⟩ is the total number of cards allowed in a statement. The default value of CARD_NO is 20.
The width of the output expression is also adjustable by the assignment
fort_width := ⟨integer⟩;
FORT_WIDTH which sets the total width of a given line to ⟨integer⟩. The initial FORTRAN output width is 70.
REDUCE automatically inserts a decimal point after each isolated integer coefficient in a FORTRAN expression (so that, for example, 4 becomes 4. ). To prevent this, set the PERIOD mode switch to OFF.
FORTRAN output is normally produced in lower case. If upper case is desired, the switch FORTUPPER should be turned on.
Finally, the default name ANS assigned to an unnamed expression and its subparts can be changed by the operator VARNAME. This takes a single identifier as argument, which then replaces ANS as the expression name. The value of VARNAME is its argument.
Further facilities for the production of FORTRAN and other language output are provided by the SCOPE and GENTRAN packagesdescribed in chapters 16.26 and 16.60.
It is often useful to save an expression on an external file for use later as input in further calculations. The commands for opening and closing output files are explained elsewhere. However, we see in the examples on output of expressions that the standard “natural” method of printing expressions is not compatible with the input syntax. So to print the expression in an input compatible form we must inhibit this natural style by turning off the switch NAT. If this is done, a dollar sign will also be printed at the end of the expression.
The following sequence of commands
will generate a file out that contains
In those cases where the final result has a complicated form, it is often convenient to display the skeletal structure of the answer. The operator STRUCTR, that takes a single expression as argument, will do this for you. Its syntax is:
The structure is printed effectively as a tree, in which the subparts are laid out with auxiliary names. If the optional ID1 is absent, the auxiliary names are prefixed by the root ANS. This root may be changed by the operator VARNAME. If the optional ID1 is present, and is an array name, the subparts are named as elements of that array, otherwise ID1 is used as the root prefix. (The second optional argument ID2 is explained later.)
The EXPRN can be either a scalar or a matrix expression. Use of any other will result in an error.
Let us suppose that the workspace contains ((A+B)^2+C)^3+D. Then the input STRUCTR WS; will (with EXP off) result in the output:
The workspace remains unchanged after this operation, since STRUCTR in the default situation returns no value (if STRUCTR is used as a sub-expression, its value is taken to be 0). In addition, the sub-expressions are normally only displayed and not retained. If you wish to access the sub-expressions with their displayed names, the switch SAVESTRUCTR should be turned on. In this case, STRUCTR returns a list whose first element is a representation for the expression, and subsequent elements are the sub-expression relations. Thus, with SAVESTRUCTR on, STRUCTR WS in the above example would return
The PART operator can be used to retrieve the required parts of the expression. For example, to get the value of ANS2 in the above, one could say:
If FORT is on, then the results are printed in the reverse order; the algorithm in fact guaranteeing that no sub-expression will be referenced before it is defined. The second optional argument ID2 may also be used in this case to name the actual expression (or expressions in the case of a matrix argument).
Let us suppose that M, a 2 by 1 matrix, contains the elements ((a+b)^2 + c)^3 + d and (a + b)*(c + d) respectively, and that V has been declared to be an array. With EXP off and FORT on, the statement structr(2*m,v,k); will result in the output