UCP-19 March 1973 R E D U C E 2 U S E R ' S M A N U A L* by Anthony C. Hearn University of Utah Salt Lake City, Utah 84112 USA Second Edition *Work supported in part by the National Science Foundation under Grant No. GJ-32181. Computer time supported by the Advanced Research Projects Agency of the Office of the Department of Defense under Contract No. F30602-70-C-0300 at the University of Utah. i ABSTRACT This manual provides the user with a description of the algebraic programming system REDUCE 2. The capabilities of this system include: 1) expansion and ordering of polynomials and rational functions, 2) symbolic differentiation, 3) substitutions and pattern matching in a wide variety of forms, 4) calculation of the greatest common divisor of two polynomials, 5) automatic and user controlled simplification of expressions, 6) calculations with symbolic matrices, 7) a complete language for symbolic calculations, in which the REDUCE program itself is written, 8) calculations of interest to high energy physicists including spin 1/2 and spin 1 algebra, 9) tensor operations. ii UPDATES TO MANUAL This copy of the REDUCE 2 User's Manual includes all updates through March 15, 1973. iii TABLE OF CONTENTS 1. INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1 2. STRUCTURE OF PROGRAMS. . . . . . . . . . . . . . . . . . . . . . 2-1 2.1 Preliminary . . . . . . . . . . . . . . . . . . . . . . 2-1 2.2 The REDUCE Standard Character Set . . . . . . . . . . . 2-1 2.3 Numbers . . . . . . . . . . . . . . . . . . . . . . . . 2-1 2.4 Identifiers . . . . . . . . . . . . . . . . . . . . . . 2-2 2.5 Variables . . . . . . . . . . . . . . . . . . . . . . . 2-2 2.5.1 Reserved Variables. . . . . . . . . . . . . . . . . . 2-2 2.6 Operators . . . . . . . . . . . . . . . . . . . . . . . 2-2 2.6.1 DF. . . . . . . . . . . . . . . . . . . . . . . . . . 2-4 2.6.2 COS. LOG. SIN . . . . . . . . . . . . . . . . . . . . 2-4 2.6.3 SUB . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 2.7 Strings . . . . . . . . . . . . . . . . . . . . . . . . 2-5 2.8 Comments . . . . . . . . . . . . . . . . . . . . . . . 2-5 2.9 Expressions . . . . . . . . . . . . . . . . . . . . . . 2-5 2.9.1 Numerical Expressions . . . . . . . . . . . . . . . . 2-6 2.9.2 Scalar Expressions. . . . . . . . . . . . . . . . . . 2-6 2.9.3 Boolean Expressions . . . . . . . . . . . . . . . . . 2-6 2.9.4 Equations . . . . . . . . . . . . . . . . . . . . . . 2-6 2.10 Reserved Words . . . . . . . . . . . . . . . . . . . . 2-7 2.11 Statements . . . . . . . . . . . . . . . . . . . . . . 2-7 2.11.1 Assignment Statements. . . . . . . . . . . . . . . . 2-7 2.11.2 Conditional Statements . . . . . . . . . . . . . . . 2-8 2.11.3 FOR Statements . . . . . . . . . . . . . . . . . . . 2-8 2.11.4 GO TO Statements . . . . . . . . . . . . . . . . . . 2-9 2.11.5 Compound Statements. . . . . . . . . . . . . . . . .2-10 2.11.6 RETURN Statements. . . . . . . . . . . . . . . . . .2-10 2.12 Declarations . . . . . . . . . . . . . . . . . . . . .2-10 2.12.1 Variable Type Declarations . . . . . . . . . . . . .2-11 2.12.2 Array Declarations . . . . . . . . . . . . . . . . .2-11 2.12.3 Mode Handling Declarations . . . . . . . . . . . . .2-11 2.13 Commands . . . . . . . . . . . . . . . . . . . . . . .2-11 2.14 File Handling Commands . . . . . . . . . . . . . . . .2-12 2.14.1 IN . . . . . . . . . . . . . . . . . . . . . . . . .2-12 2.14.2 OUT. . . . . . . . . . . . . . . . . . . . . . . . .2-12 2.14.3 SHUT . . . . . . . . . . . . . . . . . . . . . . . .2-12 2.15 Substitution Commands. . . . . . . . . . . . . . . . .2-13 2.16 Removing Assignments and Substitution Rules from Expressions. . . . . . . . . . . . . . . . . . . . .2-14 2.17 Adding Rules for Differentiation of User-defined Operators. . . . . . . . . . . . . . . . . . . . . .2-14 2.18 Procedures . . . . . . . . . . . . . . . . . . . . . .2-15 2.19 Commands Used in Interactive Systems . . . . . . . . .2-16 2.20 DEFINE . . . . . . . . . . . . . . . . . . . . . . . .2-17 2.21 END. . . . . . . . . . . . . . . . . . . . . . . . . .2-17 iv 3. MANIPULATION OF ALGEBRAIC EXPRESSIONS. . . . . . . . . . . . . . 3-1 3.1 The Expression Workspace . . . . . . . . . . . . . . . 3-1 3.2 Output of Expressions . . . . . . . . . . . . . . . . . 3-2 3.2.1 ORDER . . . . . . . . . . . . . . . . . . . . . . . . 3-2 3.2.2 FACTOR . . . . . . . . . . . . . . . . . . . . . . . 3-2 3.2.3 Output Control Flags . . . . . . . . . . . . . . . . 3-3 3.2.4 WRITE Command . . . . . . . . . . . . . . . . . . . . 3-4 3.2.5 Suppression of Zeros . . . . . . . . . . . . . . . . 3-5 3.3 User Control of the Evaluation Process. . . . . . . . . 3-6 3.4 Cancellation of Common Factors. . . . . . . . . . . . . 3-6 3.5 Numerical Evaluation of Expressions . . . . . . . . . . 3-6 3.6 Saving Expressions for Later Use as Input . . . . . . . 3-8 3.7 Partitioning Expressions . . . . . . . . . . . . . . . 3-8 4. MATRIX CALCULATIONS. . . . . . . . . . . . . . . . . . . . . . . 4-1 4.1 Preliminary . . . . . . . . . . . . . . . . . . . . . . 4-1 4.2 MAT . . . . . . . . . . . . . . . . . . . . . . . . . . 4-1 4.3 Matrix Variables. . . . . . . . . . . . . . . . . . . . 4-1 4.4 Matrix Expressions. . . . . . . . . . . . . . . . . . . 4-1 4.5 Operators with Matrix Arguments . . . . . . . . . . . . 4-2 4.5.1 DET . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 4.5.3 TP. . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 4.5.3 TRACE . . . . . . . . . . . . . . . . . . . . . . . . 4-2 4.6 Matrix Assignments. . . . . . . . . . . . . . . . . . . 4-3 4.7 Evaluating Matrix Elements. . . . . . . . . . . . . . . 4-3 5. ADVANCED COMMANDS. . . . . . . . . . . . . . . . . . . . . . . . 5-1 5.1 Kernels . . . . . . . . . . . . . . . . . . . . . . . . 5-1 5.2 Substitutions for General Expressions . . . . . . . . . 5-2 5.3 Asymptotic Commands . . . . . . . . . . . . . . . . . . 5-4 6. SYMBOLIC MODE . . . . . . . . . . . . . . . . . . . . . . . . . 6-1 6.1 Preliminary . . . . . . . . . . . . . . . . . . . . . . 6-1 6.2 General Identifiers . . . . . . . . . . . . . . . . . . 6-2 6.3 Symbolic Infix Operators. . . . . . . . . . . . . . . . 6-2 6.4 Symbolic Expressions. . . . . . . . . . . . . . . . . . 6-2 6.5 Quoted Expressions. . . . . . . . . . . . . . . . . . . 6-2 6.6 LAMBDA Expressions. . . . . . . . . . . . . . . . . . . 6-3 6.7 Symbolic Assignment Statements. . . . . . . . . . . . . 6-3 6.8 Communication with Algebraic Mode . . . . . . . . . . . 6-4 6.9 Obtaining the LISP Equivalent of REDUCE Input . . . . . 6-4 APPENDIX A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-1 A.1 Reserved Identifiers. . . . . . . . . . . . . . . . . . A-1 A.2 Commands Normally Available in REDUCE . . . . . . . . . A-2 A.3 Mode Flags in REDUCE. . . . . . . . . . . . . . . . . . A-5 A.4 Diagnostic and Error Messages in REDUCE . . . . . . . . A-6 A.4.1 Error Messages. . . . . . . . . . . . . . . . . . . . A-6 A.4.2 Diagnostic Messages . . . . . . . . . . . . . . . . . A-8 v APPENDIX B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-1 B.1 Running REDUCE on the Stanford PDP-10 . . . . . . . . . B-1 B.2 Running REDUCE on the Stanford IBM 360/67 . . . . . . . B-3 APPENDIX C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C-1 Calculations in High Energy Physics . . . . . . . . . . . . . . C-1 C.1 Preliminary . . . . . . . . . . . . . . . . . . . . . . C-1 C.2 Operators Used in High Energy Physics Calculations. . . C-1 C.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . C-1 C.2.2 G . . . . . . . . . . . . . . . . . . . . . . . . . . C-2 C.2.3 EPS . . . . . . . . . . . . . . . . . . . . . . . . . C-2 C.3 Vector Variables. . . . . . . . . . . . . . . . . . . . C-3 C.4 Additional Expression Types . . . . . . . . . . . . . . C-3 C.4.1 Vector Expressions. . . . . . . . . . . . . . . . . . C-3 C.4.2 Dirac Expressions . . . . . . . . . . . . . . . . . . C-4 C.5 Trace Calculations . . . . . . . . . . . . . . . . . . C-4 C.6 Mass Declarations . . . . . . . . . . . . . . . . . . . C-5 C.7 Example . . . . . . . . . . . . . . . . . . . . . . . . C-5 REFERENCES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . R-1 1-1 1. INTRODUCTION REDUCE is a program designed for general algebraic computations of interest to mathematicians, physicists and engineers. Its capabilities include: 1) expansion and ordering of polynomials and rational functions, 2) symbolic differentiation, 3) substitutions and pattern matching in a wide variety of forms, 4) calculation of the greatest common divisor of two polynomials, 5) automatic and user controlled simplification of expressions, 6) calculations with symbolic matrices, 7) a complete language for symbolic calculations, in which the REDUCE program itself is written, 8) calculations of interest to high energy physicists including spin 1/2 and spin 1 algebra, 9) tensor operations. There are several levels at which REDUCE may be used and understood. The beginning user can acquire an operating knowledge of the system by studying Section 2 of this manual, which contains details of the basic structure of programs. Having mastered this, he should then study Section 3, which describes the facilities available for manipulating algebraic expressions. A more advanced user must understand Sections 4 and 5, which describe the matrix handling routines and some advanced commands. Finally, to become a complete expert, the user will have to learn LISP 1.5 and study the material on the symbolic mode of operation in Section 6. Additional material of interest may be found in the Appendices. Appendix A gives the user a useful summary of the system commands and other properties of the system. Appendix B gives instructions for using REDUCE at various computer installations. This information may of course need modification for your computer. Finally, Appendix C contains details of the high energy physics commands for those interested. The author would appreciate hearing from any users who experience trouble with the system (please include copies of relevant input and output). Acknowledgment of the use of REDUCE in any published calculations would also be appreciated. The author would also be grateful for a copy of any such publication. 2-1 2. STRUCTURE OF PROGRAMS 2.1 Preliminary A REDUCE program consists of a set of functional commands which are evaluated sequentially by the computer. These commands are built up from declarations, statements and expressions. Such entities are composed of sequences of numbers, variables, operators, strings, reserved words and delimiters (such as commas and parentheses), which in turn are sequences of basic characters. 2.2 The REDUCE Standard Character Set The basic characters which are used to build up REDUCE symbols are the following: i) The 26 upper case letters A through Z ii) The 10 decimal digits 0 through 9 iii) The special characters ! " $ % ' ( ) * + , - . / : ; < > Programs composed from this standard set of characters will run in any available REDUCE system. In addition, several implementations of REDUCE (for example, on the PDP-10) use additional characters to represent some of the operators in the system. The local operating instructions for the given computer such as those in Section B of this manual should be consulted for the meaning of these special characters. However, for generality in exposition we shall limit ourselves to the standard character set in the body of this manual. 2.3 Numbers Numbers in REDUCE may be of two types; integer and real. Integers consist of a signed or unsigned sequence of decimal digits written without a decimal point. e. g. -2, 5396, +32 There is no practical limit on the number of digits permitted as arbitrary precision arithmetic is used. Real numbers may be written in two ways; i) as a signed or unsigned sequence of 1-9 decimal digits with an embedded decimal point. ii) as in i) followed by a decimal exponent which is written as the letter E followed by a signed or unsigned integer. e. g. 32. +32.0 0.32E2 and 320.E-1 are all representations of 32. 2-2 Restriction: The unsigned part of any number may NOT begin with a decimal point. i. e. NOT ALLOWED .5 -.23 +.12 2.4 Identifiers Identifiers in REDUCE consist of one to twenty-four alphanumeric characters (i.e. upper case alphabetic letters or decimal digits) the first of which must be alphabetic. e. g. A AZ P1 Q23P AVERYLONGVARIABLE Identifiers are used as variables, labels and to name arrays, operators and procedures. Restrictions: Reserved words in REDUCE (see Section 2.10) may not be used as identifiers. No spaces may appear within an identifier, and an identifier may not extend over a line of text. 2.5 Variables Variables are a particular type of identifier, and are specified by name and type. Their names must be allowed identifiers. There are several variable types allowed, and these are discussed later, beginning in Section 2.12.1. 2.5.1 Reserved Variables Several variables in REDUCE have a particular value which cannot easily be changed by the user. These variables are as follows: I square root of -1. All powers of I are automatically replaced by the appropriate combination of -1 and I. E base of natural logarithms 2.6 Operators Operators in REDUCE are also specified by name and type. There are two types, infix and prefix. Infix operators occur between their arguments. 2-3 e. g. A + B - C X = Y AND W MEMBER Z The following infix operators are built into the system. ::= |OR|AND|NOT|MEMBER|=|NEQ|EQ|>=|>|<=|<|+|-|*|/|**|. The use of the EQ operator is explained in Section 6 and the . operator in Section 6 and Appendix C. These operators may be further divided into the following sub classes ::= |OR|AND|NOT|MEMBER ::= =|EQ|NEQ|>=|>|<=|< ::= +|-|*|/|** ::= . For compatibility with the intermediate language used by REDUCE, each special character infix operator has an additional alphanumeric identifier associated with it. These identifiers may be used interchangably with the corresponding infix character(s) on input. This correspondence is as follows: := SETQ = EQUAL >= GEQ > GREATERP <= LEQ < LESSP + PLUS - DIFFERENCE (unary MINUS) * TIMES / QUOTIENT (unary RECIP) ** EXPT . CONS The above operators are assumed to be binary,, except NOT which is unary and + and * which are nary. In addition, - and / may be used in a unary position. Any other operator is parsed as a binary operator using a left procedure grouping. Thus A/B/C is interpreted as (A/B)/C. There are two exceptions to this latter rule, namely ** and . which have a right precedence grouping. Thus A**B**C is interpreted as A**(B**C). Parentheses may be used to specify the order of combination. If parentheses are omitted then this order is by the precedence ordering given by the above list (from outermost to innermost operations). Prefix operators occur at the head of their arguments, which are written as a list enclosed in parentheses and separated by commas, as with normal mathematical functions. 2-4 e. g. COS(U) DF(X**2,X) Parentheses may be omitted if the operator is unary. e. g. COS Y and COS(Y) are equivalent. Such unary prefix operators have a precedence higher than any infix operator. Infix operators may also be used in a prefix format on input. On output, however, they will always be printed in infix form. Some prefix operators are also built into the system. These are as follows: 2.6.1 DF The operator DF is used to represent partial differentiation with respect to one or more variables. The first argument is the scalar expression to be differentiated. The remaining arguments specify the differentiation variables and the number of times they are applied by the following syntax: DF(,,,...,,) The may be omitted if it is 1. e. g. DF(Y,X) = dY/dX 2 2 DF(Y,X,2) = d Y/dX 5 2 2 DF(Y,X1,2,X2,X3,2)= d Y/dX dX dX 1 2 3 2.6.2 COS, LOG, SIN These elementary functions are included in the system with the following properties: COS (-X) = COS (X) SIN (-X) = - SIN (X) COS (0) = 1 SIN (0) = 0 LOG (E) = 1 LOG (1) = 0 Their derivatives are also known to the system. The user can also add further rules for the reduction of expressions involving these operators by the methods described in Sections 2.15 and 5.2. 2-5 2.6.3 SUB This operator is described in Section 2.15. The user may add new prefix operators to the system by the declaration OPERATOR. e. g. OPERATOR H,G1,ARCTAN; adds the prefix operators H, G1 and ARCTAN to the system. 2.7 Strings Strings are used only in output statements. A string consists of any number of characters enclosed in double quotes. e. g. "A STRING". 2.8 Comments Comments are useful for including explanatory material at various points in a program. They may be used in the following form: COMMENT where ::= ;|$ e. g. COMMENT THIS IS A COMMENT; Such comments are equivalent on input to an space. In addition, the sequence of symbols END is equivalent to the reserved word END. 2.9 Expressions REDUCE expressions may be of several types and consist of syntactically allowed sequences of numbers, variables, operators, left and right parentheses and commas. The most common types are as follows: 2-6 2.9.1 Numerical Expressions These consist of syntactically allowed combinations of integer or real variables, arithmetic operators and parentheses. They evaluate to numbers. Examples: 2 I + J - 2 * I**2 are numerical expressions if I and J are integers. 2.9.2 Scalar Expressions These consist of scalar variables and arithmetic operators and follow the normal rules of scalar algebra. Examples: X X**3 - 2*Y/(2*Z**2 - DF(X,Z)) (P**2 + M**2)**(1/2)*LOG (Y/M) 2.9.3 Boolean Expressions These are expressions which return a truth value. In REDUCE, the reserved word NIL represent the truth value 'false'. Any other expression represents 'true'. So in a sense any expression is a boolean expression because all expressions return a value. However, a proper boolean expression has the syntactical form: or They are used mainly in the IF and FOR statements described in Sections 2.11.2 and 2.11.3. Examples: J<1 X>0 or X=-2 2.9.4 Equations In the remainder of this manual, we shall refer to the expression = 2-7 as an equation. 2.10 Reserved Words Certain words are reserved in REDUCE. They may only be used in the manner described in this manual. These words are as follows: ::= BEGIN|DO|ELSE|END|FOR|FUNCTION|GO|GOTO|IF|LAMBDA| NIL|PRODUCT|RETURN|STEP|SUM|TO|UNTIL|WHILE| 2.11 Statements A statement is any allowed combination of reserved words and expressions, and has the syntax ::= | The following sub-sections describe some proper statements in REDUCE. 2.11.1 Assignment Statements These statements have the following syntax ::= := e. g. A1:=B+C H(X,Y):=X-2*Y By analogy with numerical assignments in ALGOL, an assignment statement sets the left hand side of the statement to the algebraic value of the right hand side. Unfortunately, the algebraic evaluation of an expression is not as unambiguous as the corresponding numerical evaluation. This algebraic evaluation process is generally referred to as 'simplification' in the sense that the evaluation usually but not always produces a simplified form for the expression. In REDUCE, the default evaluation of an expression involves expansion of the expression and collection of like terms, ordering of the terms, evaluation of derivatives and other functions and substitution for any expressions which have values assigned or declared (Sections 2.15 and 5.2). In many cases, this is all that the user needs. However, the user can exercise some control over the way in which the evaluation is performed by various declarations available to him. These declarations are explained in Section 3.3. 2-8 If a real (floating point) number is encountered during evaluation, the system will normally convert it into a ratio of two integers, and print a message informing the user of the conversion. If the user wants to use real arithmetic, he can inhibit this conversion by the command ON FLOAT;. This use of the ON declaration is explained in Section 2.12.3. The results of the evaluation of an expression are printed if a semicolon is used as a delimiter. Because it is not usually possible to know in advance how large an expression will be, no explicit format statements are offered to the user. However, a variety of output declarations are available so that the output can be produced in a variety of forms. These output declarations are explained in Section 3.4. It is also possible to write an assignment in the form := := ... := := In this form, each is set to the value of the . 2.11.2 Conditional Statements The conditional statement has the following syntax: ::= IF THEN ELSE Its use is obvious. The ELSE clause is optional. 2.11.3 FOR Statements The FOR statement is used to define a variety of program loops. Its general syntax is as follows: FOR := STEP (DO (UNTIL ) ( ( ) (SUM (WHILE ) ( (PRODUCT The DO version of the FOR statement is the normal ALGOL usage, and is similar to the FORTRAN DO statement. Its value is 0. The SUM and PRODUCT versions respectively form the sum and product of the relevant algebraic expression over the defined range. They return the value of the computed sum or product. 2-9 The within the FOR statement is assumed INTEGER. Its value during the calculation of the statement is independent of its value outside, so that I can be used in this context, even though I normally stands for the square root of -1. Examples: We assume that the declaration ARRAY A(10); has been made in the following examples. This declaration is explained in Section 2.12.2. (i) To set each element A(I) of the array to X**I we could write FOR I:=0 STEP 1 UNTIL 10 DO A(I):=X**I$ As a further convenience, the common construction STEP 1 UNTIL may be abbreviated by a colon. Thus we could write instead of the above FOR I:=0:10 DO A(I):=X**I$ Since the assignments in this calculation are not made at the top level, they are not printed. If the user desires this, he can use the WRITE statement for this purpose as in FOR I:=0:10 DO WRITE A(I):=X**I; Further uses of WRITE are described in Section 3.2.4. (ii) To sum the squares of the even positive integers through 50, we could write FOR I:=2 STEP 2 UNTIL 50 SUM I**2; (iii) To set X to the factorial of 10 we could write X := FOR I:=1:10 PRODUCT I; Alternatively, we could set the element A(I) to I! by the statements A(0):= 1$ FOR I:=1:10 DO A(I):=I*A(I-1)$ 2.11.4 GO TO Statements GO TO (or GOTO) statements are used to perform an unconditional transfer to a label in a compound statement (Section 2.11.5). They have the syntax: ::= GO TO