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