REDUCE

8.3 Output of Expressions

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.

8.3.1 LINELENGTH Operator

This operator is used with the syntax

        LINELENGTH(NUM:integer):integer

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

8.3.2 Output Declarations

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:

ORDER Declaration

The declaration ORDER may be used to order variables on output. The syntax is:

        order v1,...vn;

where the vi are kernels. Thus,

        order x,y,z;

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,

        order x,y,z;  
        order y,x;

would order Z ahead of Y and X. The default ordering is usually alphabetic.

FACTOR Declaration

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

        factor x;

the polynomial (x + y + 1)2 will be printed as

         2                  2  
        X  + 2*X*(Y + 1) + Y  + 2*Y + 1

All expressions involving a given prefix operator may also be factored by putting the operator name in the list of factored identifiers. For example:

        factor x,cos,sin(x);

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.

8.3.3 Output Control Switches

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

        x^2*(y^2+2*y)+x*(y^2+z)/(2*a) .

The relevant switches are as follows:

ALLFAC Switch

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

            2  2        2          2  
        (2*X *Y *A + 4*X *Y*A + X*Y  + X*Z)/(2*A)

and with ALLFAC on as

                2                2  
        X*(2*X*Y *A + 4*X*Y*A + Y  + Z)/(2*A) .

ALLFAC is normally on, and is on in the following examples, except where otherwise stated.

DIV Switch

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

              2                2  (-1)        (-1)  
        X*(X*Y  + 2*X*Y + 1/2*Y *A     + 1/2*A    *Z) .

DIV is normally off.

LIST Switch

This switch causes the system to print each term in any sum on a separate line. With LIST on, our expression prints as

                2  
        X*(2*X*Y *A  
 
            + 4*X*Y*A  
 
               2  
            + Y  
 
            + Z)/(2*A) .

LIST is normally off.

NOSPLIT Switch

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.

RAT Switch

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:

            2                   2  
        (2*X *Y*A*(Y + 2) + X*(Y  + Z))/(2*A) .

With RAT on the output becomes:

         2                 2  
        X *Y*(Y + 2) + X*(Y  + Z)/(2*A) .

RAT is normally off.

Next, if we leave X factored, and turn on both DIV and RAT, the result becomes

         2                    (-1)   2  
        X *Y*(Y + 2) + 1/2*X*A    *(Y  + Z) .

Finally, with X factored, RAT on and ALLFAC off we retrieve the original structure

         2   2              2  
        X *(Y  + 2*Y) + X*(Y  + Z)/(2*A) .

RATPRI Switch

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

        A + B  
        -----  
          2

Turning this switch off causes such expressions to be output in a linear form.

REVPRI Switch

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

         2              2  
        X *Y + 2*X*Y + Y  + 7*Y + 9

whereas with REVPRI on, it will print as

                   2            2  
        9 + 7*Y + Y  + 2*X*Y + X *Y.

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:

  1. Expressions (including variables and constants). The expression is evaluated, and the result is printed out.
  2. Assignments. The expression on the right side of the := operator is evaluated, and is assigned to the variable on the left; then the symbol on the left is printed, followed by a “:=”, followed by the value of the expression on the right – almost exactly the way an assignment followed by a semicolon prints out normally. (The difference is that if the WRITE is in a FOR statement and the left-hand side of the assignment is an array position or something similar containing the variable of the FOR iteration, then the value of that variable is inserted in the printout.)
  3. Arbitrary strings of characters, preceded and followed by double-quote marks (e.g., ~string~).

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.

Examples:

  1. If A is X+5, B is itself, C is 123, M is an array, and Q=3, then
            write m(q):=a,~ ~,b/c,~ THANK YOU~;

    will set M(3) to x+5 and print

            M(Q) := X + 5 B/123 THANK YOU

    The blanks between the 5 and B, and the 3 and T, come from the blanks in the quoted strings.

  2. To print a table of the squares of the integers from 1 to 20:
            for i:=1:20 do write i,~ ~,i^2;

  3. To print a table of the squares of the integers from 1 to 20, and at the same time store them in positions 1 to 20 of an array A:
            for i:=1:20 do <<a(i):=i^2; write i,~ ~,a(i)>>;

    This will give us two columns of numbers. If we had used

            for i:=1:20 do write i,~ ~,a(i):=i^2;

    we would also get A(i) := repeated on each line.

  4. The following more complete example calculates the famous f and g series, first reported in Sconzo, P., LeSchack, A. R., and Tobey, R., “Symbolic Computation of f and g Series by Computer”, Astronomical Journal 70 (May 1965).
     x1:= -sig*(mu+2*eps)$  
     x2:= eps - 2*sig^2$  
     x3:= -3*mu*sig$  
     f:= 1$  
     g:= 0$  
     for i:= 1 step 1 until 10 do begin  
        f1:= -mu*g+x1*df(f,eps)+x2*df(f,sig)+x3*df(f,mu);  
        write ~f(~,i,~) := ~,f1;  
        g1:= f+x1*df(g,eps)+x2*df(g,sig)+x3*df(g,mu);  
        write ~g(~,i,~) := ~,g1;  
        f:=f1$  
        g:=g1$  
       end;

    A portion of the output, to illustrate the printout from the WRITE command, is as follows:

                    ... <prior output> ...  
     
                               2  
     F(4) := MU*(3*EPS - 15*SIG  + MU)  
     
     G(4) := 6*SIG*MU  
     
                                        2  
     F(5) := 15*SIG*MU*( - 3*EPS + 7*SIG  - MU)  
     
                               2  
     G(5) := MU*(9*EPS - 45*SIG  + MU)  
     
                    ... <more output> ...  

8.3.5 Suppression of Zeros

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.

8.3.6 FORTRAN Style Output Of Expressions

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.

Example:

The following REDUCE statements

 on fort;  
 out ~forfil~;  
 write ~C     this is a fortran program~;  
 write ~ 1    format(e13.5)~;  
 write ~      u=1.23~;  
 write ~      v=2.17~;  
 write ~      w=5.2~;  
 x:=(u+v+w)^11;  
 write ~C     it was foolish to expand this expression~;  
 write ~      print 1,x~;  
 write ~      end~;  
 shut ~forfil~;  
 off fort;

will generate a file forfil that contains:

c this is a fortran program  
 1    format(e13.5)  
      u=1.23  
      v=2.17  
      w=5.2  
      ans1=1320.*u**3*v*w**7+165.*u**3*w**8+55.*u**2*v**9+495.*u  
     . **2*v**8*w+1980.*u**2*v**7*w**2+4620.*u**2*v**6*w**3+  
     . 6930.*u**2*v**5*w**4+6930.*u**2*v**4*w**5+4620.*u**2*v**3*  
     . w**6+1980.*u**2*v**2*w**7+495.*u**2*v*w**8+55.*u**2*w**9+  
     . 11.*u*v**10+110.*u*v**9*w+495.*u*v**8*w**2+1320.*u*v**7*w  
     . **3+2310.*u*v**6*w**4+2772.*u*v**5*w**5+2310.*u*v**4*w**6  
     . +1320.*u*v**3*w**7+495.*u*v**2*w**8+110.*u*v*w**9+11.*u*w  
     . **10+v**11+11.*v**10*w+55.*v**9*w**2+165.*v**8*w**3+330.*  
     . v**7*w**4+462.*v**6*w**5+462.*v**5*w**6+330.*v**4*w**7+  
     . 165.*v**3*w**8+55.*v**2*w**9+11.*v*w**10+w**11  
      x=u**11+11.*u**10*v+11.*u**10*w+55.*u**9*v**2+110.*u**9*v*  
     . w+55.*u**9*w**2+165.*u**8*v**3+495.*u**8*v**2*w+495.*u**8  
     . *v*w**2+165.*u**8*w**3+330.*u**7*v**4+1320.*u**7*v**3*w+  
     . 1980.*u**7*v**2*w**2+1320.*u**7*v*w**3+330.*u**7*w**4+462.  
     . *u**6*v**5+2310.*u**6*v**4*w+4620.*u**6*v**3*w**2+4620.*u  
     . **6*v**2*w**3+2310.*u**6*v*w**4+462.*u**6*w**5+462.*u**5*  
     . v**6+2772.*u**5*v**5*w+6930.*u**5*v**4*w**2+9240.*u**5*v  
     . **3*w**3+6930.*u**5*v**2*w**4+2772.*u**5*v*w**5+462.*u**5  
     . *w**6+330.*u**4*v**7+2310.*u**4*v**6*w+6930.*u**4*v**5*w  
     . **2+11550.*u**4*v**4*w**3+11550.*u**4*v**3*w**4+6930.*u**  
     . 4*v**2*w**5+2310.*u**4*v*w**6+330.*u**4*w**7+165.*u**3*v  
     . **8+1320.*u**3*v**7*w+4620.*u**3*v**6*w**2+9240.*u**3*v**  
     . 5*w**3+11550.*u**3*v**4*w**4+9240.*u**3*v**3*w**5+4620.*u  
     . **3*v**2*w**6+ans1  
c     it was foolish to expand this expression  
      print 1,x  
      end

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

        c ***** invalid fortran construct (list) not printed

will be printed if you try to print a list with FORT on.

FORTRAN Output Options

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.

8.3.7 Saving Expressions for Later Use as Input

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.

Example:

The following sequence of commands

        off nat; out ~out~; x := (y+z)^2; write ~end~;  
        shut ~out~; on nat;

will generate a file out that contains

        X := Y**2 + 2*Y*Z + Z**2$  
        END$

8.3.8 Displaying Expression Structure

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:

   STRUCTR(EXPRN:algebraic[,ID1:identifier[,ID2:identifier]]);

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.

Example:

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:

        ANS3  
 
           where  
 
                          3  
              ANS3 := ANS2  + D  
 
                          2  
              ANS2 := ANS1  + C  
 
              ANS1 := A + B

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

                       3              2  
        {ANS3,ANS3=ANS2  + D,ANS2=ANS1  + C,ANS1=A + B}

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:

        part(ws,3,2);

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

Example:

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

      V(1)=A+B  
      V(2)=V(1)**2+C  
      V(3)=V(2)**3+D  
      V(4)=C+D  
      K(1,1)=2.*V(3)  
      K(2,1)=2.*V(1)*V(4)