5f892713c3 2021-03-03 1: COMMENT
5f892713c3 2021-03-03 2:
5f892713c3 2021-03-03 3: REDUCE INTERACTIVE LESSON NUMBER 5
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 5 of 7 REDUCE lessons.
5f892713c3 2021-03-03 10:
5f892713c3 2021-03-03 11: There are at least two good reasons for wanting to save REDUCE
5f892713c3 2021-03-03 12: expression assignments to a file:
5f892713c3 2021-03-03 13:
5f892713c3 2021-03-03 14: 1. So that one can logout, then resume computation at a later
5f892713c3 2021-03-03 15: time.
5f892713c3 2021-03-03 16: 2. So that needed main memory space can be cleared without
5f892713c3 2021-03-03 17: irrecoverably losing the values of variables which are not
5f892713c3 2021-03-03 18: needed in the next expression but will be needed later.
5f892713c3 2021-03-03 19:
5f892713c3 2021-03-03 20: Using trivial small expressions, the following statement sequence
5f892713c3 2021-03-03 21: illustrates how this could be done:
5f892713c3 2021-03-03 22:
5f892713c3 2021-03-03 23: OFF NAT,
5f892713c3 2021-03-03 24: OUT TEMP,
5f892713c3 2021-03-03 25: F1 := (F + G)^2,
5f892713c3 2021-03-03 26: G1 := G*F1,
5f892713c3 2021-03-03 27: OUT T,
5f892713c3 2021-03-03 28: CLEAR F1,
5f892713c3 2021-03-03 29: H1 := H*G1,
5f892713c3 2021-03-03 30: OUT TEMP,
5f892713c3 2021-03-03 31: CLEAR G1,
5f892713c3 2021-03-03 32: H2 := F*H1,
5f892713c3 2021-03-03 33: CLEAR H1,
5f892713c3 2021-03-03 34: SHUT TEMP,
5f892713c3 2021-03-03 35: IN TEMP,
5f892713c3 2021-03-03 36: F1,
5f892713c3 2021-03-03 37: ON NAT,
5f892713c3 2021-03-03 38: F1.
5f892713c3 2021-03-03 39:
5f892713c3 2021-03-03 40: ON NAT yields the natural output style with raised exponents, which is
5f892713c3 2021-03-03 41: unsuitable for subsequent input.
5f892713c3 2021-03-03 42:
5f892713c3 2021-03-03 43: The OUT-statement causes subsequent output to be directed to the file
5f892713c3 2021-03-03 44: named in the statement, until overridden by a different OUT-statement
5f892713c3 2021-03-03 45: or until the file is closed by a SHUT-statement. File T is the
5f892713c3 2021-03-03 46: terminal, and any other name designates a normal file. Such names
5f892713c3 2021-03-03 47: must comply with the local file-naming conventions as well as with the
5f892713c3 2021-03-03 48: REDUCE syntax. If the output is not of lasting importance, I find
5f892713c3 2021-03-03 49: that including something like "TEMPORARY" or "SCRATCH" in the name
5f892713c3 2021-03-03 50: helps remind me to delete it later.
5f892713c3 2021-03-03 51:
5f892713c3 2021-03-03 52: Successive OUT-statements to the same file will append rather than
5f892713c3 2021-03-03 53: overwrite output if and only if there is no intervening SHUT-
5f892713c3 2021-03-03 54: statement for that file. The SHUT-statement also has the effect of an
5f892713c3 2021-03-03 55: implied OUT T.
5f892713c3 2021-03-03 56:
5f892713c3 2021-03-03 57: Note:
5f892713c3 2021-03-03 58:
5f892713c3 2021-03-03 59: 1. The generated output is the simplified expression rather than
5f892713c3 2021-03-03 60: the raw form entered at the terminal.
5f892713c3 2021-03-03 61: 2. Each output assignment automatically has a dollar-sign appended
5f892713c3 2021-03-03 62: so that it is legal input and so that (perhaps lengthy) output
5f892713c3 2021-03-03 63: will not unavoidably be generated at the terminal when the file
5f892713c3 2021-03-03 64: is read in later.
5f892713c3 2021-03-03 65: 3. Output cannot be sent simultaneously to 2 or more files.
5f892713c3 2021-03-03 66: 4. Statements entered at the terminal which do not generate output
5f892713c3 2021-03-03 67: -- such as declarations, LET rules, and procedure definitions
5f892713c3 2021-03-03 68: -- do not appear in the output file.
5f892713c3 2021-03-03 69: 5. One could get declarations, procedure definitions, rules, etc.
5f892713c3 2021-03-03 70: written to a file from the terminal by typing statements such
5f892713c3 2021-03-03 71: as
5f892713c3 2021-03-03 72:
5f892713c3 2021-03-03 73: WRITE "
5f892713c3 2021-03-03 74: ALGEBRAIC PROCEDURE ...
5f892713c3 2021-03-03 75: ... ".
5f892713c3 2021-03-03 76:
5f892713c3 2021-03-03 77: This could serve as a means of generating permanent copies of
5f892713c3 2021-03-03 78: LET rules, procedures, etc., but it is quite awkward compared
5f892713c3 2021-03-03 79: with the usual way, which is to generate a file containing the
5f892713c3 2021-03-03 80: REDUCE program by using a text editor, then input the program
5f892713c3 2021-03-03 81: by using the IN-statement. If you have refrained from learning
5f892713c3 2021-03-03 82: to use a basic text editor (such as Windows Notepad), hesitate
5f892713c3 2021-03-03 83: no longer. I suggest that your first text-editing exercise be
5f892713c3 2021-03-03 84: to create an IN file for (re)defining the function
5f892713c3 2021-03-03 85: FACTORIAL(n).
5f892713c3 2021-03-03 86: 6. The reason I didn't actually execute the above statement
5f892713c3 2021-03-03 87: sequence is that when the input to REDUCE comes from a file,
5f892713c3 2021-03-03 88: both the input and output are sent to the output file (which is
5f892713c3 2021-03-03 89: convenient for producing a file containing both the input and
5f892713c3 2021-03-03 90: output of a demonstration.) Consequently, you would have seen
5f892713c3 2021-03-03 91: none of the statements between the "OUT TEMP" and "OUT T" as
5f892713c3 2021-03-03 92: well as between the second "OUT TEMP" and the "SHUT TEMP",
5f892713c3 2021-03-03 93: until the IN-statement was executed. The example is confusing
5f892713c3 2021-03-03 94: enough without having things scrambled from the order you would
5f892713c3 2021-03-03 95: type them. To clarify all of this, I encourage you to actually
5f892713c3 2021-03-03 96: execute the above statement sequence with an appropriately
5f892713c3 2021-03-03 97: chosen file name and using semicolons instead of the commas and
5f892713c3 2021-03-03 98: final period. Afterwards, to return to the lesson, type CONT.
5f892713c3 2021-03-03 99: 7. It is often more natural to use a string (e.g. "less5.red")
5f892713c3 2021-03-03 100: rather than an identifier (e.g. less5!.red) for a filename.
5f892713c3 2021-03-03 101: REDUCE accepts both.;
5f892713c3 2021-03-03 102:
5f892713c3 2021-03-03 103: pause;
5f892713c3 2021-03-03 104:
5f892713c3 2021-03-03 105: COMMENT Suppose you and your colleagues developed or obtained a set of
5f892713c3 2021-03-03 106: REDUCE files containing supplementary packages such as trigonometric
5f892713c3 2021-03-03 107: simplification, Laplace transforms, etc. It would be a waste of time
5f892713c3 2021-03-03 108: (and distracting) to have these files displayed every time they were
5f892713c3 2021-03-03 109: input, so this output can be suppressed by inserting the statement
5f892713c3 2021-03-03 110: "OFF ECHO" at the beginning of the file. (But don't forget to include
5f892713c3 2021-03-03 111: the statement "ON ECHO" at the end of the file.)
5f892713c3 2021-03-03 112:
5f892713c3 2021-03-03 113: The lessons have amply demonstrated the PAUSE-statement, which is
5f892713c3 2021-03-03 114: useful for insertion in input files at the top-level or within
5f892713c3 2021-03-03 115: functions when input from the user is necessary or desired.
5f892713c3 2021-03-03 116:
5f892713c3 2021-03-03 117: It often happens that after generating an expression, one decides that
5f892713c3 2021-03-03 118: it would be convenient to use it as the body of a function definition,
5f892713c3 2021-03-03 119: with one or more of the indeterminates therein as parameters, which
5f892713c3 2021-03-03 120: can be done as follows. (If you input code like this directly rather
5f892713c3 2021-03-03 121: than from a file, you will need to agree to let REDUCE declare F to be
5f892713c3 2021-03-03 122: an operator.);
5f892713c3 2021-03-03 123:
5f892713c3 2021-03-03 124: (1-(v/c)^2)^(1/2);
5f892713c3 2021-03-03 125: for all v saveas f(v);
5f892713c3 2021-03-03 126: f(5);
5f892713c3 2021-03-03 127:
5f892713c3 2021-03-03 128: COMMENT Here the indeterminate V became a parameter of F.
5f892713c3 2021-03-03 129: Alternatively, we can save the previous expression to a variable.;
5f892713c3 2021-03-03 130:
5f892713c3 2021-03-03 131: saveas fof5;
5f892713c3 2021-03-03 132: fof5;
5f892713c3 2021-03-03 133:
5f892713c3 2021-03-03 134: COMMENT I find this technique more convenient than referring to the
5f892713c3 2021-03-03 135: special variable WS.;
5f892713c3 2021-03-03 136:
5f892713c3 2021-03-03 137: pause;
5f892713c3 2021-03-03 138:
5f892713c3 2021-03-03 139: COMMENT The FOR-loop provides a convenient way to form finite sums or
5f892713c3 2021-03-03 140: products with specific integer index limits. However, this need is so
5f892713c3 2021-03-03 141: ubiquitous that REDUCE provides even more convenient syntax of the
5f892713c3 2021-03-03 142: forms
5f892713c3 2021-03-03 143:
5f892713c3 2021-03-03 144: FOR index := initial STEP increment UNTIL final SUM expression,
5f892713c3 2021-03-03 145:
5f892713c3 2021-03-03 146: FOR index := initial STEP increment UNTIL final PRODUCT expression.
5f892713c3 2021-03-03 147:
5f892713c3 2021-03-03 148: As before, ":" is an acceptable abbreviation for "STEP 1 UNTIL". As
5f892713c3 2021-03-03 149: an example of their use, here is a very concise definition of a
5f892713c3 2021-03-03 150: function which computes Taylor-series expansions of symbolic
5f892713c3 2021-03-03 151: expressions:;
5f892713c3 2021-03-03 152:
5f892713c3 2021-03-03 153: algebraic procedure taylor(ex, x, pt, n);
5f892713c3 2021-03-03 154: COMMENT This function returns the degree-N Taylor-series
5f892713c3 2021-03-03 155: expansion of expression EX with respect to indeterminate X,
5f892713c3 2021-03-03 156: expanded about expression PT. For a series-like appearance,
5f892713c3 2021-03-03 157: display the answer under the influence of FACTOR X, ON RAT,
5f892713c3 2021-03-03 158: and perhaps also ON DIV;
5f892713c3 2021-03-03 159: sub(x=pt, ex) + for k:=1:n sum(sub(x=pt, df(ex,x,k))*(x-pt)^k
5f892713c3 2021-03-03 160: / for j:=1:k product j);
5f892713c3 2021-03-03 161:
5f892713c3 2021-03-03 162: clear a, x; factor x; on rat, div;
5f892713c3 2021-03-03 163: g1 := taylor(e^x, x, 0, 4);
5f892713c3 2021-03-03 164: g2 := taylor(e^cos(x)*cos(sin(x)), x, 0, 3);
5f892713c3 2021-03-03 165: % This illustrates the zero denominator limitation; continue anyway:
5f892713c3 2021-03-03 166: taylor(log(x), x, 0, 4);
5f892713c3 2021-03-03 167:
5f892713c3 2021-03-03 168: COMMENT It would, of course, be more efficient to compute each
5f892713c3 2021-03-03 169: derivative and factorial from the preceding one. (Similarly for
5f892713c3 2021-03-03 170: (X-PT)^K if and only if PT NEQ 0).
5f892713c3 2021-03-03 171:
5f892713c3 2021-03-03 172: The Fourier series expansion of our example E^COS(X)*COS(SIN(X))
5f892713c3 2021-03-03 173: is 1 + cos(x) + cos(2*x)/2 + cos(3*x)/(3*2) + ... .
5f892713c3 2021-03-03 174: Use the above SUM and PRODUCT features to generate the partial sum of
5f892713c3 2021-03-03 175: this series through terms of order COS(6*X);
5f892713c3 2021-03-03 176:
5f892713c3 2021-03-03 177: pause;
5f892713c3 2021-03-03 178:
5f892713c3 2021-03-03 179: COMMENT Closed-form solutions are often unobtainable for nontrivial
5f892713c3 2021-03-03 180: problems, even using computer algebra. When this is the case,
5f892713c3 2021-03-03 181: truncated symbolic series solutions are often worth trying before
5f892713c3 2021-03-03 182: resorting to approximate numerical solutions.
5f892713c3 2021-03-03 183:
5f892713c3 2021-03-03 184: When we combine truncated series it is pointless (and worse yet,
5f892713c3 2021-03-03 185: misleading) to retain terms of higher order than is justified by the
5f892713c3 2021-03-03 186: constituents. For example, if we wish to multiply together the
5f892713c3 2021-03-03 187: truncated series G1 and G2 generated above, there is no point in
5f892713c3 2021-03-03 188: retaining terms higher than third degree in X. We can avoid even
5f892713c3 2021-03-03 189: generating such terms as follows:;
5f892713c3 2021-03-03 190:
5f892713c3 2021-03-03 191: let x^4 = 0;
5f892713c3 2021-03-03 192: g3 := g1*g2;
5f892713c3 2021-03-03 193:
5f892713c3 2021-03-03 194: COMMENT Replacing X^4 with 0 has the effect of also replacing all
5f892713c3 2021-03-03 195: higher powers of X with 0. We could, of course, use our TAYLOR
5f892713c3 2021-03-03 196: function to compute G3 directly, but differentiation is time consuming
5f892713c3 2021-03-03 197: compared to truncated polynomial algebra. Moreover, our TAYLOR
5f892713c3 2021-03-03 198: function requires a closed-form expression to begin with, whereas
5f892713c3 2021-03-03 199: iterative techniques often permit us to construct symbolic series
5f892713c3 2021-03-03 200: solutions even when we have no such closed form.
5f892713c3 2021-03-03 201:
5f892713c3 2021-03-03 202: Now consider the truncated series:;
5f892713c3 2021-03-03 203:
5f892713c3 2021-03-03 204: clear y; factor y;
5f892713c3 2021-03-03 205: h1 := taylor(cos y, y, 0, 6);
5f892713c3 2021-03-03 206:
5f892713c3 2021-03-03 207: COMMENT Suppose we regard terms of order X^N in G1 as being comparable
5f892713c3 2021-03-03 208: to terms of order Y^(2*N) in H1, and we want to form (G1*H1)^2. This
5f892713c3 2021-03-03 209: can be done as follows:;
5f892713c3 2021-03-03 210:
5f892713c3 2021-03-03 211: let y^7 = 0;
5f892713c3 2021-03-03 212: f1 := (g1*h1)^2;
5f892713c3 2021-03-03 213:
5f892713c3 2021-03-03 214: COMMENT Note however that any terms of the form C*X^M*Y^N with
5f892713c3 2021-03-03 215: 2*M+N > 6 are inconsistent with the accuracy of the constituent
5f892713c3 2021-03-03 216: series, and we have generated several such misleading terms by
5f892713c3 2021-03-03 217: independently truncating powers of X and Y. To avoid generating such
5f892713c3 2021-03-03 218: junk, we can specify that a term be replaced by 0 whenever a weighted
5f892713c3 2021-03-03 219: sum of exponents of specified indeterminates and functional forms
5f892713c3 2021-03-03 220: exceeds a specified weight level. In our example this is done as
5f892713c3 2021-03-03 221: follows:;
5f892713c3 2021-03-03 222:
5f892713c3 2021-03-03 223: weight x=2, y=1;
5f892713c3 2021-03-03 224: wtlevel 6;
5f892713c3 2021-03-03 225: f1 := f1;
5f892713c3 2021-03-03 226:
5f892713c3 2021-03-03 227: COMMENT Variables not mentioned in a WEIGHT declaration have a weight
5f892713c3 2021-03-03 228: of 0, and the default weight-level is 2.;
5f892713c3 2021-03-03 229:
5f892713c3 2021-03-03 230: pause;
5f892713c3 2021-03-03 231:
5f892713c3 2021-03-03 232: COMMENT Here is an example of how one might compute numerical
5f892713c3 2021-03-03 233: approximations to the cosine function. (But note that REDUCE can
5f892713c3 2021-03-03 234: already do this automatically!) One way is to provide a supplementary
5f892713c3 2021-03-03 235: LET rule for numerical arguments. For example, since our TAYLOR
5f892713c3 2021-03-03 236: function would reveal that the Taylor series for cos x is
5f892713c3 2021-03-03 237: 1 - x^2/2! + x^4/4! - ...;
5f892713c3 2021-03-03 238:
5f892713c3 2021-03-03 239: for all x such that numberp x let abs(x) = x, abs(-x) = x;
5f892713c3 2021-03-03 240: epsrecip := 1024$
5f892713c3 2021-03-03 241: on rounded;
5f892713c3 2021-03-03 242: while 1.0 + 1.0/epsrecip neq 1.0 do
5f892713c3 2021-03-03 243: epsrecip := epsrecip + epsrecip;
5f892713c3 2021-03-03 244:
5f892713c3 2021-03-03 245: for all x such that numberp num x and numberp den x let cos x =
5f892713c3 2021-03-03 246: begin COMMENT X is integer, real, or a rational number. This rule
5f892713c3 2021-03-03 247: returns the Taylor-series approximation to COS X, truncated when
5f892713c3 2021-03-03 248: the last included term is less than (1/EPSRECIP) of the returned
5f892713c3 2021-03-03 249: answer. EPSRECIP is a global variable initialized to a value
5f892713c3 2021-03-03 250: that is appropriate to the local floating-point precision.
5f892713c3 2021-03-03 251: Arbitrarily larger values are justifiable when X is exact and
5f892713c3 2021-03-03 252: ROUNDED is off. No angle reduction is performed, so this
5f892713c3 2021-03-03 253: function is not recommended for ABS(X) >= about PI/2;
5f892713c3 2021-03-03 254: integer k; scalar mxsq, term, ans;
5f892713c3 2021-03-03 255: k := 1;
5f892713c3 2021-03-03 256: mxsq := -x*x;
5f892713c3 2021-03-03 257: term := mxsq/2;
5f892713c3 2021-03-03 258: ans := term + 1;
5f892713c3 2021-03-03 259: while abs(num term)*epsrecip*den(ans) - abs(num ans)*den(term) > 0 do
5f892713c3 2021-03-03 260: << term := term*mxsq/k/(k+1);
5f892713c3 2021-03-03 261: ans := term + ans;
5f892713c3 2021-03-03 262: k := k+2 >>;
5f892713c3 2021-03-03 263: return ans
5f892713c3 2021-03-03 264: end;
5f892713c3 2021-03-03 265:
5f892713c3 2021-03-03 266: cos(f) + cos(1/2);
5f892713c3 2021-03-03 267: off rounded;
5f892713c3 2021-03-03 268: cos(1/2);
5f892713c3 2021-03-03 269:
5f892713c3 2021-03-03 270: COMMENT As an exercise, write a similar rule for the SIN or LOG, or
5f892713c3 2021-03-03 271: replace the COS rule with an improved one which uses angle reduction
5f892713c3 2021-03-03 272: so that angles outside a modest range are represented as equivalent
5f892713c3 2021-03-03 273: angles within the range, before computing the Taylor series.;
5f892713c3 2021-03-03 274:
5f892713c3 2021-03-03 275: pause;
5f892713c3 2021-03-03 276:
5f892713c3 2021-03-03 277: COMMENT There is a REDUCE compiler, and you may wish to learn how to
5f892713c3 2021-03-03 278: use it. However, even if rules such as the above ones are compiled,
5f892713c3 2021-03-03 279: they will be slow compared to the implementation-dependent hand-coded
5f892713c3 2021-03-03 280: ones used by most FORTRAN-like systems, so REDUCE provides a way to
5f892713c3 2021-03-03 281: generate FORTRAN programs which can then be compiled and executed in a
5f892713c3 2021-03-03 282: subsequent job step. This is useful when there is a lot of
5f892713c3 2021-03-03 283: floating-point computation or when we wish to exploit an existing
5f892713c3 2021-03-03 284: FORTRAN program. Suppose, for example, that we wish to utilize an
5f892713c3 2021-03-03 285: existing FORTRAN subroutine which uses the Newton-Raphson iteration
5f892713c3 2021-03-03 286:
5f892713c3 2021-03-03 287: Xnew := Xold - SUB(X=Xold, F(X)/DF(F(X),X))
5f892713c3 2021-03-03 288:
5f892713c3 2021-03-03 289: to attempt an approximate solution to the equation F(X)=0. Most such
5f892713c3 2021-03-03 290: subroutines require the user to provide a FORTRAN function or
5f892713c3 2021-03-03 291: subroutine which, given Xold, returns F(X)/DF(F(X),X) evaluated at
5f892713c3 2021-03-03 292: X=Xold. If F(X) is complicated, manual symbolic derivation of
5f892713c3 2021-03-03 293: DF(F(X),X) is a tedious and error-prone process. We can get REDUCE to
5f892713c3 2021-03-03 294: relieve us of this responsibility as is illustrated below for the
5f892713c3 2021-03-03 295: trivial example F(X) = X*E^X - 1:
5f892713c3 2021-03-03 296:
5f892713c3 2021-03-03 297: ON FORT, ROUNDED,
5f892713c3 2021-03-03 298: OUT FONDFFILE,
5f892713c3 2021-03-03 299: WRITE " REAL FUNCTION FONDF(XOLD)",
5f892713c3 2021-03-03 300: WRITE " REAL XOLD, F",
5f892713c3 2021-03-03 301: F := XOLD*E^XOLD - 1.0,
5f892713c3 2021-03-03 302: FONDF := F/DF(F,XOLD),
5f892713c3 2021-03-03 303: WRITE " RETURN",
5f892713c3 2021-03-03 304: WRITE " END",
5f892713c3 2021-03-03 305: SHUT FONDFFILE.
5f892713c3 2021-03-03 306:
5f892713c3 2021-03-03 307: COMMENT Under the influence of ON FORT, the output generated by
5f892713c3 2021-03-03 308: assignments is printed as valid FORTRAN assignment statements, using
5f892713c3 2021-03-03 309: as many continuation lines as necessary up to the number specified by
5f892713c3 2021-03-03 310: the global variable !*CARDNO, which is initially set to 20. The
5f892713c3 2021-03-03 311: output generated by an expression which is not an assignment is a
5f892713c3 2021-03-03 312: corresponding assignment to a variable named ANS. In either case,
5f892713c3 2021-03-03 313: expressions which would otherwise exceed !*CARDNO continuation lines
5f892713c3 2021-03-03 314: are evaluated piecewise, using ANS as an intermediate variable.
5f892713c3 2021-03-03 315:
5f892713c3 2021-03-03 316: Try executing the above statement sequence, using an appropriate
5f892713c3 2021-03-03 317: filename and using semicolons instead of the commas and period at the
5f892713c3 2021-03-03 318: end of the lines (only), then view the file after the lesson to see
5f892713c3 2021-03-03 319: how it worked.;
5f892713c3 2021-03-03 320:
5f892713c3 2021-03-03 321: pause;
5f892713c3 2021-03-03 322: off fort, rounded;
5f892713c3 2021-03-03 323:
5f892713c3 2021-03-03 324: COMMENT To make this technique usable by non-REDUCE programmers, we
5f892713c3 2021-03-03 325: could write a more general REDUCE program which given merely the
5f892713c3 2021-03-03 326: expression F by the user, outputs not only the function FONDF, but
5f892713c3 2021-03-03 327: also any necessary job-control commands and an appropriate main
5f892713c3 2021-03-03 328: program for calling the Newton-Raphson subroutine and printing the
5f892713c3 2021-03-03 329: results.
5f892713c3 2021-03-03 330:
5f892713c3 2021-03-03 331: Sometimes it is desirable to modify or supplement the syntax of
5f892713c3 2021-03-03 332: REDUCE. For example:
5f892713c3 2021-03-03 333:
5f892713c3 2021-03-03 334: 1. Electrical engineers may prefer to input J as the
5f892713c3 2021-03-03 335: representation of (-1)^(1/2).
5f892713c3 2021-03-03 336: 2. A user might prefer to input LOGE to denote natural
5f892713c3 2021-03-03 337: logarithms. (Note that LN is already defined as a synonym for
5f892713c3 2021-03-03 338: LOG.)
5f892713c3 2021-03-03 339: 3. A user might prefer to use DERIV instead of DF to request
5f892713c3 2021-03-03 340: differentiation.
5f892713c3 2021-03-03 341:
5f892713c3 2021-03-03 342: Such lexical macros can be established by the DEFINE declaration:;
5f892713c3 2021-03-03 343:
5f892713c3 2021-03-03 344: clear x, j;
5f892713c3 2021-03-03 345: define j=i, loge=log, deriv=df;
5f892713c3 2021-03-03 346:
5f892713c3 2021-03-03 347: COMMENT Now watch!;
5f892713c3 2021-03-03 348:
5f892713c3 2021-03-03 349: g1 := sub(x=loge(j^3*x), deriv(x^2,x));
5f892713c3 2021-03-03 350:
5f892713c3 2021-03-03 351: COMMENT Each "equation" in a DEFINE declaration must be of the form
5f892713c3 2021-03-03 352: "name = item", where each item is an expression, an operator, or a
5f892713c3 2021-03-03 353: REDUCE-reserved word such as "FOR". Such replacements take place
5f892713c3 2021-03-03 354: during the lexical scanning, before any evaluation, LET rules, or
5f892713c3 2021-03-03 355: built-in simplification. Think of a good application for this
5f892713c3 2021-03-03 356: facility, then try it.;
5f892713c3 2021-03-03 357:
5f892713c3 2021-03-03 358: pause;
5f892713c3 2021-03-03 359:
5f892713c3 2021-03-03 360: COMMENT When REDUCE is being run non-interactively, with input from a
5f892713c3 2021-03-03 361: file rather than a terminal, it is preferable to have REDUCE make
5f892713c3 2021-03-03 362: reasonable decisions and proceed when it encounters apparently
5f892713c3 2021-03-03 363: undeclared operators, divisions by zero, etc. In interactive mode, it
5f892713c3 2021-03-03 364: is preferable to pause and query the user. ON INT specifies the
5f892713c3 2021-03-03 365: latter style, and OFF INT specifies the former. Under the influence
5f892713c3 2021-03-03 366: of OFF INT, we can also have most error messages suppressed by
5f892713c3 2021-03-03 367: specifying OFF MSG. This is sometimes useful when we expect abnormal
5f892713c3 2021-03-03 368: conditions and do not want our output marred by the associated
5f892713c3 2021-03-03 369: messages. INT is automatically turned off during input from a file in
5f892713c3 2021-03-03 370: response to an IN-command from a terminal.;
5f892713c3 2021-03-03 371:
5f892713c3 2021-03-03 372: pause;
5f892713c3 2021-03-03 373:
5f892713c3 2021-03-03 374: COMMENT REDUCE provides a trace command for debugging, which employs
5f892713c3 2021-03-03 375: the syntax
5f892713c3 2021-03-03 376:
5f892713c3 2021-03-03 377: TR functionname1, functionname2, ..., functionnameN.
5f892713c3 2021-03-03 378:
5f892713c3 2021-03-03 379: An analogous command named UNTR removes function names from trace
5f892713c3 2021-03-03 380: status;
5f892713c3 2021-03-03 381:
5f892713c3 2021-03-03 382: pause;
5f892713c3 2021-03-03 383:
5f892713c3 2021-03-03 384: COMMENT REDUCE also provides an assignment-tracing command for
5f892713c3 2021-03-03 385: debugging, which employs the syntax
5f892713c3 2021-03-03 386:
5f892713c3 2021-03-03 387: TRST functionname1, functionname2, ..., functionnameN.
5f892713c3 2021-03-03 388:
5f892713c3 2021-03-03 389: An analogous command named UNTRST removes functionnames from this
5f892713c3 2021-03-03 390: status. All assignments in the designated functions are reported,
5f892713c3 2021-03-03 391: except for assignments to array elements. Such functions must be
5f892713c3 2021-03-03 392: uncompiled and must have a top-level BEGIN-block. To apply both TRST
5f892713c3 2021-03-03 393: and TR to a function simultaneously, it is crucial to request them in
5f892713c3 2021-03-03 394: that order, and it is necessary to relinquish the two kinds of tracing
5f892713c3 2021-03-03 395: in the opposite order.;
5f892713c3 2021-03-03 396:
5f892713c3 2021-03-03 397: pause;
5f892713c3 2021-03-03 398:
5f892713c3 2021-03-03 399: COMMENT The REDUCE algebraic algorithms are written in a subset of
5f892713c3 2021-03-03 400: REDUCE called RLISP. In turn, the more sophisticated features of
5f892713c3 2021-03-03 401: RLISP are written in a small subset of RLISP, which is itself written
5f892713c3 2021-03-03 402: in a simple dialect of LISP called Standard LISP.
5f892713c3 2021-03-03 403:
5f892713c3 2021-03-03 404: RLISP is ideal for implementing algebraic algorithms, but the RLISP
5f892713c3 2021-03-03 405: environment is not most suitable for the routine use of these
5f892713c3 2021-03-03 406: algorithms in the natural mathematical style of the preceding lessons.
5f892713c3 2021-03-03 407: Accordingly, REDUCE is initially in a mode called ALGEBRAIC, which
5f892713c3 2021-03-03 408: provides the user with the environment illustrated in the preceding
5f892713c3 2021-03-03 409: lessons, while insulating him from accidental interaction with the
5f892713c3 2021-03-03 410: numerous functions, global variables, etc. necessary for implementing
5f892713c3 2021-03-03 411: the built-in algebra. In contrast, the underlying RLISP system
5f892713c3 2021-03-03 412: together with all of the algebraic simplification algorithms written
5f892713c3 2021-03-03 413: therein is called SYMBOLIC mode.
5f892713c3 2021-03-03 414:
5f892713c3 2021-03-03 415: As we have seen, algebraic-mode rules and procedures can be used to
5f892713c3 2021-03-03 416: extend the built-in algebraic capabilities. However, some extensions
5f892713c3 2021-03-03 417: can be accomplished most easily or efficiently by descending to
5f892713c3 2021-03-03 418: SYMBOLIC mode.
5f892713c3 2021-03-03 419:
5f892713c3 2021-03-03 420: To make REDUCE operate in symbolic mode, we merely execute the top-
5f892713c3 2021-03-03 421: level mode-declaration statement consisting of the word SYMBOLIC. We
5f892713c3 2021-03-03 422: can subsequently switch back by executing the statement consisting of
5f892713c3 2021-03-03 423: the word ALGEBRAIC.
5f892713c3 2021-03-03 424:
5f892713c3 2021-03-03 425: RLISP has the semantics of LISP with the syntax of our by-now-familiar
5f892713c3 2021-03-03 426: algebraic-mode REDUCE, so RLISP provides a natural tool for many
5f892713c3 2021-03-03 427: applications besides computer algebra, such as games, theorem-proving,
5f892713c3 2021-03-03 428: natural-language translation, computer-aided instruction, and
5f892713c3 2021-03-03 429: artificial intelligence in general. For this reason, it is possible
5f892713c3 2021-03-03 430: to run RLISP without any of the symbolic-mode algebraic algorithms
5f892713c3 2021-03-03 431: that are written in RLISP, and it is advisable to thus save space when
5f892713c3 2021-03-03 432: the application does not involve computer algebra. (An RLISP system
5f892713c3 2021-03-03 433: is a step in the process of building a complete REDUCE system, but is
5f892713c3 2021-03-03 434: not distributed as an independent application, although one can be
5f892713c3 2021-03-03 435: built from the source code available.)
5f892713c3 2021-03-03 436:
5f892713c3 2021-03-03 437: We have now discussed virtually every feature that is available in
5f892713c3 2021-03-03 438: algebraic mode, so lesson 6 will deal solely with RLISP, and lesson 7
5f892713c3 2021-03-03 439: will deal with communication between ALGEBRAIC and SYMBOLIC mode for
5f892713c3 2021-03-03 440: mathematical purposes. However, I suggest that you proceed to those
5f892713c3 2021-03-03 441: lessons only if and when:
5f892713c3 2021-03-03 442:
5f892713c3 2021-03-03 443: 1. You have consolidated and fully absorbed the information in
5f892713c3 2021-03-03 444: lessons 1 through 5 by considerable practice beyond the
5f892713c3 2021-03-03 445: exercises therein. (The exercises were intended to also
5f892713c3 2021-03-03 446: suggest good related project ideas.)
5f892713c3 2021-03-03 447: 2. You feel the need for a facility which you believe is
5f892713c3 2021-03-03 448: impossible or quite awkward to implement solely in ALGEBRAIC
5f892713c3 2021-03-03 449: mode.
5f892713c3 2021-03-03 450: 3. You have read an introductory text about LISP, such as "A
5f892713c3 2021-03-03 451: Concise Introduction to LISP" by David L. Matuszek, which is
5f892713c3 2021-03-03 452: freely available at
5f892713c3 2021-03-03 453: https://www.cis.upenn.edu/~matuszek/LispText/lisp.html.
5f892713c3 2021-03-03 454: 4. You are familiar with the definition of Standard LISP, as
5f892713c3 2021-03-03 455: described in the "Standard LISP Report" which was published in
5f892713c3 2021-03-03 456: the October 1979 SIGPLAN Notices. [A copy is freely available
5f892713c3 2021-03-03 457: via http://reduce-algebra.sourceforge.net/documentation.php.]
5f892713c3 2021-03-03 458:
5f892713c3 2021-03-03 459: Don't forget to view or print your newly generated FORTRAN file and to
5f892713c3 2021-03-03 460: delete any temporary files created by this lesson.;
5f892713c3 2021-03-03 461:
5f892713c3 2021-03-03 462: ;end;