5f892713c3 2021-03-03 1: COMMENT
5f892713c3 2021-03-03 2:
5f892713c3 2021-03-03 3: REDUCE INTERACTIVE LESSON NUMBER 1
5f892713c3 2021-03-03 4:
5f892713c3 2021-03-03 5: David R. Stoutemyer
5f892713c3 2021-03-03 6: University of Hawaii
5f892713c3 2021-03-03 7:
5f892713c3 2021-03-03 8:
5f892713c3 2021-03-03 9: COMMENT This is lesson 1 of 7 interactive lessons about the REDUCE
5f892713c3 2021-03-03 10: system for computer symbolic mathematics. These lessons presume an
5f892713c3 2021-03-03 11: acquaintance with elementary calculus, together with a previous
5f892713c3 2021-03-03 12: exposure to some computer programming language.
5f892713c3 2021-03-03 13:
5f892713c3 2021-03-03 14: In REDUCE, any sequence of characters from the word "COMMENT" through
5f892713c3 2021-03-03 15: the next semicolon or dollar-sign statement separator is an
5f892713c3 2021-03-03 16: explanatory remark ignored by the system. In general, either
5f892713c3 2021-03-03 17: separator signals the end of a statement, with the dollar sign
5f892713c3 2021-03-03 18: suppressing any output that might otherwise automatically be produced
5f892713c3 2021-03-03 19: by the statement. The typing of a carriage return initiates the
5f892713c3 2021-03-03 20: immediate sequential execution of all statements which have been
5f892713c3 2021-03-03 21: terminated on that line. When REDUCE is ready for more input, it will
5f892713c3 2021-03-03 22: prompt you with a prompt number followed by a colon and a space at the
5f892713c3 2021-03-03 23: left margin.
5f892713c3 2021-03-03 24:
5f892713c3 2021-03-03 25: Expressions can be formed using "^", "*", "/", "+", and "-" to
5f892713c3 2021-03-03 26: indicate exponentiation, multiplication, division, addition, and
5f892713c3 2021-03-03 27: subtraction or negation respectively. Assignments to variables can be
5f892713c3 2021-03-03 28: done using the operator ":=". For example:;
5f892713c3 2021-03-03 29:
5f892713c3 2021-03-03 30: r2d2 := (987654321/15)^3;
5f892713c3 2021-03-03 31:
5f892713c3 2021-03-03 32: COMMENT The immediately preceding line, without a semicolon, is the
5f892713c3 2021-03-03 33: computed output generated by the line with a semicolon which precedes
5f892713c3 2021-03-03 34: it. Note that exact indefinite-precision rational arithmetic was
5f892713c3 2021-03-03 35: used, in contrast to the limited-precision arithmetic of traditional
5f892713c3 2021-03-03 36: programming languages.
5f892713c3 2021-03-03 37:
5f892713c3 2021-03-03 38: We can use the name R2D2 to represent its value in subsequent
5f892713c3 2021-03-03 39: expressions such as;
5f892713c3 2021-03-03 40:
5f892713c3 2021-03-03 41: r2d2 := -r2d2/25 + 3*(13-5);
5f892713c3 2021-03-03 42:
5f892713c3 2021-03-03 43: COMMENT We could equally well write the name of this variable as R2D2,
5f892713c3 2021-03-03 44: r2D2 or R2d2 since REDUCE is case-insensitive. Using this
5f892713c3 2021-03-03 45: flexibility, in these lessons code discussed within the text will be
5f892713c3 2021-03-03 46: written in upper case to make it stand out, but code to be actually
5f892713c3 2021-03-03 47: executed by REDUCE will be written in lower case.
5f892713c3 2021-03-03 48:
5f892713c3 2021-03-03 49: Now I will give you an opportunity to try some analogous computations.
5f892713c3 2021-03-03 50: To do so, type the letter n or N followed by a carriage return in
5f892713c3 2021-03-03 51: response to the question "Cont?". (You could type y or Y if you wish
5f892713c3 2021-03-03 52: to relinquish this opportunity, but I strongly recommend reinforced
5f892713c3 2021-03-03 53: learning through active participation.) After trying an example or
5f892713c3 2021-03-03 54: two, type the command "cont" (with any capitalization) terminated by a
5f892713c3 2021-03-03 55: semicolon and carriage return when you wish to proceed with the rest
5f892713c3 2021-03-03 56: of the lesson. To avoid interference with our examples, please don't
5f892713c3 2021-03-03 57: assign anything to any variable names beginning with the letters E
5f892713c3 2021-03-03 58: through I. To avoid lengthy delays, I recommend keeping all of your
5f892713c3 2021-03-03 59: examples approximately as trivial as ours, saving your more ambitious
5f892713c3 2021-03-03 60: experiments until after the lesson. If you happen to initiate a
5f892713c3 2021-03-03 61: calculation requiring an undue amount of time to evaluate or display,
5f892713c3 2021-03-03 62: you can abort that computation with an interrupt (Ctrl-C) to terminate
5f892713c3 2021-03-03 63: REDUCE. Restart REDUCE, followed by the statement "in less1.red",
5f892713c3 2021-03-03 64: followed by a semicolon and return, to restart the lesson at the
5f892713c3 2021-03-03 65: beginning.;
5f892713c3 2021-03-03 66:
5f892713c3 2021-03-03 67: pause;
5f892713c3 2021-03-03 68:
5f892713c3 2021-03-03 69: COMMENT Now watch this example illustrating some more dramatic
5f892713c3 2021-03-03 70: differences from traditional scientific programming systems:;
5f892713c3 2021-03-03 71:
5f892713c3 2021-03-03 72: e1 := 2*g + 3*g + h^3/h;
5f892713c3 2021-03-03 73:
5f892713c3 2021-03-03 74: COMMENT Note how we are allowed to use variables to which we have
5f892713c3 2021-03-03 75: assigned no values! Note too how similar terms and similar factors
5f892713c3 2021-03-03 76: are combined automatically. REDUCE also automatically expands
5f892713c3 2021-03-03 77: products and powers of sums, together with placing expressions over
5f892713c3 2021-03-03 78: common denominators, as illustrated by the examples:;
5f892713c3 2021-03-03 79:
5f892713c3 2021-03-03 80: e2 := e1*(f+g);
5f892713c3 2021-03-03 81: e2 := e1^2;
5f892713c3 2021-03-03 82: e1+1/e1;
5f892713c3 2021-03-03 83:
5f892713c3 2021-03-03 84: COMMENT Our last example also illustrates that there is no need to
5f892713c3 2021-03-03 85: assign an expression if we do not plan to use its value later. Try
5f892713c3 2021-03-03 86: some similar examples.;
5f892713c3 2021-03-03 87:
5f892713c3 2021-03-03 88: pause;
5f892713c3 2021-03-03 89:
5f892713c3 2021-03-03 90: COMMENT It is not always desirable to expand expressions over a common
5f892713c3 2021-03-03 91: denominator, and we can use the OFF statement to turn off either or
5f892713c3 2021-03-03 92: both computational switches which control these transformations. The
5f892713c3 2021-03-03 93: switch named EXP controls EXPansion, and the switch named MCD controls
5f892713c3 2021-03-03 94: the Making of Common Denominators;
5f892713c3 2021-03-03 95:
5f892713c3 2021-03-03 96: off exp, mcd;
5f892713c3 2021-03-03 97: e2 := e1^2$
5f892713c3 2021-03-03 98: e2 := e2*(f+g) + 1/e1;
5f892713c3 2021-03-03 99:
5f892713c3 2021-03-03 100: COMMENT To turn these switches back on, we type:;
5f892713c3 2021-03-03 101:
5f892713c3 2021-03-03 102: on exp, mcd;
5f892713c3 2021-03-03 103:
5f892713c3 2021-03-03 104: COMMENT Try a few relevant examples with these switches turned off
5f892713c3 2021-03-03 105: individually and jointly;
5f892713c3 2021-03-03 106:
5f892713c3 2021-03-03 107: pause;
5f892713c3 2021-03-03 108:
5f892713c3 2021-03-03 109: on exp; % Just in case you turned it off!
5f892713c3 2021-03-03 110:
5f892713c3 2021-03-03 111: COMMENT The "%" character is another way to introduce a comment that
5f892713c3 2021-03-03 112: extends as far as the end of the line.
5f892713c3 2021-03-03 113:
5f892713c3 2021-03-03 114: Now consider the example:;
5f892713c3 2021-03-03 115:
5f892713c3 2021-03-03 116: e2 := (2*(f*h)^2 - f^2*g*h - (f*g)^2 - f*h^3 + f*h*g^2 - h^4
5f892713c3 2021-03-03 117: + g*h^3)/(f^2*h - f^2*g - f*h^2 + 2*f*g*h - f*g^2
5f892713c3 2021-03-03 118: - g*h^2 + g^2*h);
5f892713c3 2021-03-03 119:
5f892713c3 2021-03-03 120: COMMENT It is not obvious, but the numerator and denominator of this
5f892713c3 2021-03-03 121: expression share a nontrivial common divisor which can be canceled.
5f892713c3 2021-03-03 122: To make REDUCE automatically cancel greatest common divisors, we turn
5f892713c3 2021-03-03 123: on the computational switch named GCD:;
5f892713c3 2021-03-03 124:
5f892713c3 2021-03-03 125: on gcd;
5f892713c3 2021-03-03 126: e2;
5f892713c3 2021-03-03 127:
5f892713c3 2021-03-03 128: COMMENT The switch is not on by default because
5f892713c3 2021-03-03 129:
5f892713c3 2021-03-03 130: 1. It can consume a lot of time.
5f892713c3 2021-03-03 131: 2. Often we know in advance the few places where a nontrivial
5f892713c3 2021-03-03 132: GCD can occur in our problem.
5f892713c3 2021-03-03 133: 3. Even without GCD cancellation, expansion and common
5f892713c3 2021-03-03 134: denominators guarantee that any rational expression which is
5f892713c3 2021-03-03 135: equivalent to zero simplifies to zero.
5f892713c3 2021-03-03 136: 4. When the denominator is the greatest common divisor, such
5f892713c3 2021-03-03 137: as for (X^2 - 2*X + 1)/(X - 1), REDUCE cancels the
5f892713c3 2021-03-03 138: greatest common divisor even when GCD is OFF.
5f892713c3 2021-03-03 139: 5. GCD cancellation sometimes makes expressions more
5f892713c3 2021-03-03 140: complicated, such as with (F^10 - G^10)/(F^2 + F*G - 2*G^2).
5f892713c3 2021-03-03 141:
5f892713c3 2021-03-03 142: Try the examples mentioned in this comment, together with one or two
5f892713c3 2021-03-03 143: other relevant ones.;
5f892713c3 2021-03-03 144:
5f892713c3 2021-03-03 145: pause;
5f892713c3 2021-03-03 146:
5f892713c3 2021-03-03 147: COMMENT Exact rational arithmetic can consume an alarming amount of
5f892713c3 2021-03-03 148: computer time when the constituent integers have quite large
5f892713c3 2021-03-03 149: magnitudes, and the results become awkward to interpret qualitatively.
5f892713c3 2021-03-03 150: When this is the case and somewhat inexact numerical coefficients are
5f892713c3 2021-03-03 151: acceptable, we can have the arithmetic done using floating point by
5f892713c3 2021-03-03 152: turning on the computational switch ROUNDED. With this switch on, any
5f892713c3 2021-03-03 153: non-integer rational numbers are approximated by floating-point
5f892713c3 2021-03-03 154: numbers, and the result of any arithmetic operation is floating-point
5f892713c3 2021-03-03 155: when any of its operands is floating point. For example:;
5f892713c3 2021-03-03 156:
5f892713c3 2021-03-03 157: on rounded;
5f892713c3 2021-03-03 158: e1 := (12.3456789e3*f + 3*g)^2 + 1/2;
5f892713c3 2021-03-03 159:
5f892713c3 2021-03-03 160: COMMENT With ROUNDED off, any floating-point constants are
5f892713c3 2021-03-03 161: automatically represented as explicit rational numbers:;
5f892713c3 2021-03-03 162:
5f892713c3 2021-03-03 163: off rounded;
5f892713c3 2021-03-03 164: e1 := 12.35*g;
5f892713c3 2021-03-03 165: pause;
5f892713c3 2021-03-03 166:
5f892713c3 2021-03-03 167: COMMENT A number of elementary functions, such as SIN, COS and LOG,
5f892713c3 2021-03-03 168: are built into REDUCE. Moreover, the letter E represents the base of
5f892713c3 2021-03-03 169: the natural logarithms, so the exponentiation operator enables us to
5f892713c3 2021-03-03 170: represent the exponential function as well as fractional powers. For
5f892713c3 2021-03-03 171: example:;
5f892713c3 2021-03-03 172:
5f892713c3 2021-03-03 173: e1:= sin(-f*g) + log(e) + (3*g^2*cos(-1))^(1/2);
5f892713c3 2021-03-03 174:
5f892713c3 2021-03-03 175: COMMENT What automatic simplifications can you identify in this
5f892713c3 2021-03-03 176: example?
5f892713c3 2021-03-03 177:
5f892713c3 2021-03-03 178: Note that REDUCE never approximates the values of these functions,
5f892713c3 2021-03-03 179: even for numerical arguments, and exact computations are generally
5f892713c3 2021-03-03 180: impossible for non-trivial numerical arguments.
5f892713c3 2021-03-03 181:
5f892713c3 2021-03-03 182: Experimentally determine some other built-in simplifications for these
5f892713c3 2021-03-03 183: functions.;
5f892713c3 2021-03-03 184:
5f892713c3 2021-03-03 185: pause;
5f892713c3 2021-03-03 186:
5f892713c3 2021-03-03 187: COMMENT Later you will learn how to introduce additional
5f892713c3 2021-03-03 188: simplifications and additional functions. To compute numerical
5f892713c3 2021-03-03 189: approximations for examples such as COS(1) simply turn on ROUNDED.
5f892713c3 2021-03-03 190:
5f892713c3 2021-03-03 191: Differentiation is also built-into REDUCE. For example, to
5f892713c3 2021-03-03 192: differentiate E1 with respect to F:;
5f892713c3 2021-03-03 193:
5f892713c3 2021-03-03 194: e2 := df(e1,f);
5f892713c3 2021-03-03 195:
5f892713c3 2021-03-03 196: COMMENT To compute the second derivative of E2 with respect to G, we
5f892713c3 2021-03-03 197: can type either DF(E2,G,2) or DF(E1,F,1,G,2) or DF(E1,F,G,2) or
5f892713c3 2021-03-03 198: DF(E1,G,2,F,1) or;
5f892713c3 2021-03-03 199:
5f892713c3 2021-03-03 200: df(e1,g,2,f);
5f892713c3 2021-03-03 201:
5f892713c3 2021-03-03 202: COMMENT Surely you can't resist trying a few derivatives of your own!
5f892713c3 2021-03-03 203: (Careful, high-order derivatives can be alarmingly complicated.);
5f892713c3 2021-03-03 204:
5f892713c3 2021-03-03 205: pause;
5f892713c3 2021-03-03 206:
5f892713c3 2021-03-03 207: COMMENT REDUCE uses the name I to represent (-1)^(1/2), incorporating
5f892713c3 2021-03-03 208: some simplification rules such as replacing I^2 by -1. Here is an
5f892713c3 2021-03-03 209: opportunity to experimentally determine other simplifications such as
5f892713c3 2021-03-03 210: for I^3, 1/I^23, and (I^2-1)/(I-1).;
5f892713c3 2021-03-03 211:
5f892713c3 2021-03-03 212: pause;
5f892713c3 2021-03-03 213:
5f892713c3 2021-03-03 214: COMMENT Clearly it is inadvisable to use E or I as a variable. T is
5f892713c3 2021-03-03 215: also inadvisable for reasons that will become clear later.
5f892713c3 2021-03-03 216:
5f892713c3 2021-03-03 217: The value of a variable is said to be "bound" to the variable. Any
5f892713c3 2021-03-03 218: variable to which we have assigned a value is called a bound variable,
5f892713c3 2021-03-03 219: and any variable to which we have not assigned a value is called an
5f892713c3 2021-03-03 220: indeterminate. Occasionally it is desirable to make a bound variable
5f892713c3 2021-03-03 221: into an indeterminate, and this can be done using the CLEAR command.
5f892713c3 2021-03-03 222: For example:;
5f892713c3 2021-03-03 223:
5f892713c3 2021-03-03 224: clear r2d2, e1, e2;
5f892713c3 2021-03-03 225: e2;
5f892713c3 2021-03-03 226:
5f892713c3 2021-03-03 227: COMMENT If you suspect that a degenerate assignment, such as E1:=E1,
5f892713c3 2021-03-03 228: would suffice to clear a bound variable, try it on one of your own
5f892713c3 2021-03-03 229: bound variables.;
5f892713c3 2021-03-03 230:
5f892713c3 2021-03-03 231: pause;
5f892713c3 2021-03-03 232:
5f892713c3 2021-03-03 233: COMMENT REDUCE also supports matrix algebra, as illustrated by the
5f892713c3 2021-03-03 234: following:;
5f892713c3 2021-03-03 235:
5f892713c3 2021-03-03 236: matrix e1(4,1), f;
5f892713c3 2021-03-03 237: e1; f;
5f892713c3 2021-03-03 238:
5f892713c3 2021-03-03 239: COMMENT This declaration establishes E1 as a matrix with 4 rows and 1
5f892713c3 2021-03-03 240: column, while establishing F as a matrix of unspecified size. To
5f892713c3 2021-03-03 241: establish element values (and sizes if not already established in the
5f892713c3 2021-03-03 242: MATRIX declaration), we can use the MAT function, as illustrated by
5f892713c3 2021-03-03 243: the following example:;
5f892713c3 2021-03-03 244:
5f892713c3 2021-03-03 245: h := mat((log(g), g+3), (g, 5/7));
5f892713c3 2021-03-03 246:
5f892713c3 2021-03-03 247: COMMENT Only after establishing the size of a matrix by declaring it
5f892713c3 2021-03-03 248: or executing a matrix assignment can we refer to an individual element
5f892713c3 2021-03-03 249: or to the matrix as a whole. For example to increase the last element
5f892713c3 2021-03-03 250: of H by 1 then form twice the transpose of H, we can type:;
5f892713c3 2021-03-03 251:
5f892713c3 2021-03-03 252: h(2,2) := h(2,2) + 1;
5f892713c3 2021-03-03 253: 2*tp(h);
5f892713c3 2021-03-03 254:
5f892713c3 2021-03-03 255: COMMENT To compute the determinant of H:;
5f892713c3 2021-03-03 256:
5f892713c3 2021-03-03 257: det(h);
5f892713c3 2021-03-03 258:
5f892713c3 2021-03-03 259: COMMENT To compute the trace of H:;
5f892713c3 2021-03-03 260:
5f892713c3 2021-03-03 261: trace(h);
5f892713c3 2021-03-03 262:
5f892713c3 2021-03-03 263: COMMENT To compute the inverse of H, we can use H^(-1) or 1/H. To
5f892713c3 2021-03-03 264: compute the solution to the equation H*F = MAT((G),(2)), we can
5f892713c3 2021-03-03 265: left-multiply the right-hand side by the inverse of H:;
5f892713c3 2021-03-03 266:
5f892713c3 2021-03-03 267: f := 1/h*mat((g),(2));
5f892713c3 2021-03-03 268:
5f892713c3 2021-03-03 269: COMMENT Notes:
5f892713c3 2021-03-03 270:
5f892713c3 2021-03-03 271: 1. MAT((G),(2))/H would denote right-multiplication by the
5f892713c3 2021-03-03 272: inverse, which is not what we want.
5f892713c3 2021-03-03 273: 2. Solutions for a set of right-hand-side vectors are most
5f892713c3 2021-03-03 274: efficiently computed simultaneously by collecting the right-
5f892713c3 2021-03-03 275: hand sides together as the columns of a single multiple-column
5f892713c3 2021-03-03 276: matrix.
5f892713c3 2021-03-03 277: 3. Sub-expressions of the form 1/H*... or H^(-1)*... are computed
5f892713c3 2021-03-03 278: more efficiently than if the inverse is computed separately in
5f892713c3 2021-03-03 279: a previous statement, so separate computation of the inverse
5f892713c3 2021-03-03 280: is advisable only if several solutions are desired and if
5f892713c3 2021-03-03 281: they cannot be computed simultaneously.
5f892713c3 2021-03-03 282: 4. MAT must have parentheses around each row of elements even if
5f892713c3 2021-03-03 283: there is only one row or only one element per row.
5f892713c3 2021-03-03 284: 5. References to individual matrix elements must have exactly two
5f892713c3 2021-03-03 285: subscripts, even if the matrix has only one row or one column.
5f892713c3 2021-03-03 286:
5f892713c3 2021-03-03 287: Congratulations on completing lesson 1! I urge you to try a sequence
5f892713c3 2021-03-03 288: of more ambitious examples for the various features that have been
5f892713c3 2021-03-03 289: introduced, in order to gain some familiarity with the relationship
5f892713c3 2021-03-03 290: between problem size and computing time for various operations. (The
5f892713c3 2021-03-03 291: command "ON TIME" causes computing time to be output.) I also urge
5f892713c3 2021-03-03 292: you to bring to the next lesson appropriate examples from textbooks,
5f892713c3 2021-03-03 293: articles, or elsewhere, in order to experience the decisive learning
5f892713c3 2021-03-03 294: reinforcement afforded by meaningful personal examples that are not
5f892713c3 2021-03-03 295: arbitrarily contrived.
5f892713c3 2021-03-03 296:
5f892713c3 2021-03-03 297: To avoid the possibility of interference from assignments and
5f892713c3 2021-03-03 298: declarations in lesson 1, it is wise to execute lesson 2 in a fresh
5f892713c3 2021-03-03 299: REDUCE session, when you are ready.
5f892713c3 2021-03-03 300:
5f892713c3 2021-03-03 301: ;end;