5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: REDUCE INTERACTIVE LESSON NUMBER 5
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: David R. Stoutemyer
5f892713c3 2021-03-03 trnsz@pobox.c: University of Hawaii
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT This is lesson 5 of 7 REDUCE lessons.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: There are at least two good reasons for wanting to save REDUCE
5f892713c3 2021-03-03 trnsz@pobox.c: expression assignments to a file:
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: 1. So that one can logout, then resume computation at a later
5f892713c3 2021-03-03 trnsz@pobox.c: time.
5f892713c3 2021-03-03 trnsz@pobox.c: 2. So that needed main memory space can be cleared without
5f892713c3 2021-03-03 trnsz@pobox.c: irrecoverably losing the values of variables which are not
5f892713c3 2021-03-03 trnsz@pobox.c: needed in the next expression but will be needed later.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: Using trivial small expressions, the following statement sequence
5f892713c3 2021-03-03 trnsz@pobox.c: illustrates how this could be done:
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: OFF NAT,
5f892713c3 2021-03-03 trnsz@pobox.c: OUT TEMP,
5f892713c3 2021-03-03 trnsz@pobox.c: F1 := (F + G)^2,
5f892713c3 2021-03-03 trnsz@pobox.c: G1 := G*F1,
5f892713c3 2021-03-03 trnsz@pobox.c: OUT T,
5f892713c3 2021-03-03 trnsz@pobox.c: CLEAR F1,
5f892713c3 2021-03-03 trnsz@pobox.c: H1 := H*G1,
5f892713c3 2021-03-03 trnsz@pobox.c: OUT TEMP,
5f892713c3 2021-03-03 trnsz@pobox.c: CLEAR G1,
5f892713c3 2021-03-03 trnsz@pobox.c: H2 := F*H1,
5f892713c3 2021-03-03 trnsz@pobox.c: CLEAR H1,
5f892713c3 2021-03-03 trnsz@pobox.c: SHUT TEMP,
5f892713c3 2021-03-03 trnsz@pobox.c: IN TEMP,
5f892713c3 2021-03-03 trnsz@pobox.c: F1,
5f892713c3 2021-03-03 trnsz@pobox.c: ON NAT,
5f892713c3 2021-03-03 trnsz@pobox.c: F1.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: ON NAT yields the natural output style with raised exponents, which is
5f892713c3 2021-03-03 trnsz@pobox.c: unsuitable for subsequent input.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: The OUT-statement causes subsequent output to be directed to the file
5f892713c3 2021-03-03 trnsz@pobox.c: named in the statement, until overridden by a different OUT-statement
5f892713c3 2021-03-03 trnsz@pobox.c: or until the file is closed by a SHUT-statement. File T is the
5f892713c3 2021-03-03 trnsz@pobox.c: terminal, and any other name designates a normal file. Such names
5f892713c3 2021-03-03 trnsz@pobox.c: must comply with the local file-naming conventions as well as with the
5f892713c3 2021-03-03 trnsz@pobox.c: REDUCE syntax. If the output is not of lasting importance, I find
5f892713c3 2021-03-03 trnsz@pobox.c: that including something like "TEMPORARY" or "SCRATCH" in the name
5f892713c3 2021-03-03 trnsz@pobox.c: helps remind me to delete it later.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: Successive OUT-statements to the same file will append rather than
5f892713c3 2021-03-03 trnsz@pobox.c: overwrite output if and only if there is no intervening SHUT-
5f892713c3 2021-03-03 trnsz@pobox.c: statement for that file. The SHUT-statement also has the effect of an
5f892713c3 2021-03-03 trnsz@pobox.c: implied OUT T.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: Note:
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: 1. The generated output is the simplified expression rather than
5f892713c3 2021-03-03 trnsz@pobox.c: the raw form entered at the terminal.
5f892713c3 2021-03-03 trnsz@pobox.c: 2. Each output assignment automatically has a dollar-sign appended
5f892713c3 2021-03-03 trnsz@pobox.c: so that it is legal input and so that (perhaps lengthy) output
5f892713c3 2021-03-03 trnsz@pobox.c: will not unavoidably be generated at the terminal when the file
5f892713c3 2021-03-03 trnsz@pobox.c: is read in later.
5f892713c3 2021-03-03 trnsz@pobox.c: 3. Output cannot be sent simultaneously to 2 or more files.
5f892713c3 2021-03-03 trnsz@pobox.c: 4. Statements entered at the terminal which do not generate output
5f892713c3 2021-03-03 trnsz@pobox.c: -- such as declarations, LET rules, and procedure definitions
5f892713c3 2021-03-03 trnsz@pobox.c: -- do not appear in the output file.
5f892713c3 2021-03-03 trnsz@pobox.c: 5. One could get declarations, procedure definitions, rules, etc.
5f892713c3 2021-03-03 trnsz@pobox.c: written to a file from the terminal by typing statements such
5f892713c3 2021-03-03 trnsz@pobox.c: as
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: WRITE "
5f892713c3 2021-03-03 trnsz@pobox.c: ALGEBRAIC PROCEDURE ...
5f892713c3 2021-03-03 trnsz@pobox.c: ... ".
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: This could serve as a means of generating permanent copies of
5f892713c3 2021-03-03 trnsz@pobox.c: LET rules, procedures, etc., but it is quite awkward compared
5f892713c3 2021-03-03 trnsz@pobox.c: with the usual way, which is to generate a file containing the
5f892713c3 2021-03-03 trnsz@pobox.c: REDUCE program by using a text editor, then input the program
5f892713c3 2021-03-03 trnsz@pobox.c: by using the IN-statement. If you have refrained from learning
5f892713c3 2021-03-03 trnsz@pobox.c: to use a basic text editor (such as Windows Notepad), hesitate
5f892713c3 2021-03-03 trnsz@pobox.c: no longer. I suggest that your first text-editing exercise be
5f892713c3 2021-03-03 trnsz@pobox.c: to create an IN file for (re)defining the function
5f892713c3 2021-03-03 trnsz@pobox.c: FACTORIAL(n).
5f892713c3 2021-03-03 trnsz@pobox.c: 6. The reason I didn't actually execute the above statement
5f892713c3 2021-03-03 trnsz@pobox.c: sequence is that when the input to REDUCE comes from a file,
5f892713c3 2021-03-03 trnsz@pobox.c: both the input and output are sent to the output file (which is
5f892713c3 2021-03-03 trnsz@pobox.c: convenient for producing a file containing both the input and
5f892713c3 2021-03-03 trnsz@pobox.c: output of a demonstration.) Consequently, you would have seen
5f892713c3 2021-03-03 trnsz@pobox.c: none of the statements between the "OUT TEMP" and "OUT T" as
5f892713c3 2021-03-03 trnsz@pobox.c: well as between the second "OUT TEMP" and the "SHUT TEMP",
5f892713c3 2021-03-03 trnsz@pobox.c: until the IN-statement was executed. The example is confusing
5f892713c3 2021-03-03 trnsz@pobox.c: enough without having things scrambled from the order you would
5f892713c3 2021-03-03 trnsz@pobox.c: type them. To clarify all of this, I encourage you to actually
5f892713c3 2021-03-03 trnsz@pobox.c: execute the above statement sequence with an appropriately
5f892713c3 2021-03-03 trnsz@pobox.c: chosen file name and using semicolons instead of the commas and
5f892713c3 2021-03-03 trnsz@pobox.c: final period. Afterwards, to return to the lesson, type CONT.
5f892713c3 2021-03-03 trnsz@pobox.c: 7. It is often more natural to use a string (e.g. "less5.red")
5f892713c3 2021-03-03 trnsz@pobox.c: rather than an identifier (e.g. less5!.red) for a filename.
5f892713c3 2021-03-03 trnsz@pobox.c: REDUCE accepts both.;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: pause;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT Suppose you and your colleagues developed or obtained a set of
5f892713c3 2021-03-03 trnsz@pobox.c: REDUCE files containing supplementary packages such as trigonometric
5f892713c3 2021-03-03 trnsz@pobox.c: simplification, Laplace transforms, etc. It would be a waste of time
5f892713c3 2021-03-03 trnsz@pobox.c: (and distracting) to have these files displayed every time they were
5f892713c3 2021-03-03 trnsz@pobox.c: input, so this output can be suppressed by inserting the statement
5f892713c3 2021-03-03 trnsz@pobox.c: "OFF ECHO" at the beginning of the file. (But don't forget to include
5f892713c3 2021-03-03 trnsz@pobox.c: the statement "ON ECHO" at the end of the file.)
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: The lessons have amply demonstrated the PAUSE-statement, which is
5f892713c3 2021-03-03 trnsz@pobox.c: useful for insertion in input files at the top-level or within
5f892713c3 2021-03-03 trnsz@pobox.c: functions when input from the user is necessary or desired.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: It often happens that after generating an expression, one decides that
5f892713c3 2021-03-03 trnsz@pobox.c: it would be convenient to use it as the body of a function definition,
5f892713c3 2021-03-03 trnsz@pobox.c: with one or more of the indeterminates therein as parameters, which
5f892713c3 2021-03-03 trnsz@pobox.c: can be done as follows. (If you input code like this directly rather
5f892713c3 2021-03-03 trnsz@pobox.c: than from a file, you will need to agree to let REDUCE declare F to be
5f892713c3 2021-03-03 trnsz@pobox.c: an operator.);
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: (1-(v/c)^2)^(1/2);
5f892713c3 2021-03-03 trnsz@pobox.c: for all v saveas f(v);
5f892713c3 2021-03-03 trnsz@pobox.c: f(5);
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT Here the indeterminate V became a parameter of F.
5f892713c3 2021-03-03 trnsz@pobox.c: Alternatively, we can save the previous expression to a variable.;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: saveas fof5;
5f892713c3 2021-03-03 trnsz@pobox.c: fof5;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT I find this technique more convenient than referring to the
5f892713c3 2021-03-03 trnsz@pobox.c: special variable WS.;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: pause;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT The FOR-loop provides a convenient way to form finite sums or
5f892713c3 2021-03-03 trnsz@pobox.c: products with specific integer index limits. However, this need is so
5f892713c3 2021-03-03 trnsz@pobox.c: ubiquitous that REDUCE provides even more convenient syntax of the
5f892713c3 2021-03-03 trnsz@pobox.c: forms
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: FOR index := initial STEP increment UNTIL final SUM expression,
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: FOR index := initial STEP increment UNTIL final PRODUCT expression.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: As before, ":" is an acceptable abbreviation for "STEP 1 UNTIL". As
5f892713c3 2021-03-03 trnsz@pobox.c: an example of their use, here is a very concise definition of a
5f892713c3 2021-03-03 trnsz@pobox.c: function which computes Taylor-series expansions of symbolic
5f892713c3 2021-03-03 trnsz@pobox.c: expressions:;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: algebraic procedure taylor(ex, x, pt, n);
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT This function returns the degree-N Taylor-series
5f892713c3 2021-03-03 trnsz@pobox.c: expansion of expression EX with respect to indeterminate X,
5f892713c3 2021-03-03 trnsz@pobox.c: expanded about expression PT. For a series-like appearance,
5f892713c3 2021-03-03 trnsz@pobox.c: display the answer under the influence of FACTOR X, ON RAT,
5f892713c3 2021-03-03 trnsz@pobox.c: and perhaps also ON DIV;
5f892713c3 2021-03-03 trnsz@pobox.c: sub(x=pt, ex) + for k:=1:n sum(sub(x=pt, df(ex,x,k))*(x-pt)^k
5f892713c3 2021-03-03 trnsz@pobox.c: / for j:=1:k product j);
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: clear a, x; factor x; on rat, div;
5f892713c3 2021-03-03 trnsz@pobox.c: g1 := taylor(e^x, x, 0, 4);
5f892713c3 2021-03-03 trnsz@pobox.c: g2 := taylor(e^cos(x)*cos(sin(x)), x, 0, 3);
5f892713c3 2021-03-03 trnsz@pobox.c: % This illustrates the zero denominator limitation; continue anyway:
5f892713c3 2021-03-03 trnsz@pobox.c: taylor(log(x), x, 0, 4);
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT It would, of course, be more efficient to compute each
5f892713c3 2021-03-03 trnsz@pobox.c: derivative and factorial from the preceding one. (Similarly for
5f892713c3 2021-03-03 trnsz@pobox.c: (X-PT)^K if and only if PT NEQ 0).
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: The Fourier series expansion of our example E^COS(X)*COS(SIN(X))
5f892713c3 2021-03-03 trnsz@pobox.c: is 1 + cos(x) + cos(2*x)/2 + cos(3*x)/(3*2) + ... .
5f892713c3 2021-03-03 trnsz@pobox.c: Use the above SUM and PRODUCT features to generate the partial sum of
5f892713c3 2021-03-03 trnsz@pobox.c: this series through terms of order COS(6*X);
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: pause;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT Closed-form solutions are often unobtainable for nontrivial
5f892713c3 2021-03-03 trnsz@pobox.c: problems, even using computer algebra. When this is the case,
5f892713c3 2021-03-03 trnsz@pobox.c: truncated symbolic series solutions are often worth trying before
5f892713c3 2021-03-03 trnsz@pobox.c: resorting to approximate numerical solutions.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: When we combine truncated series it is pointless (and worse yet,
5f892713c3 2021-03-03 trnsz@pobox.c: misleading) to retain terms of higher order than is justified by the
5f892713c3 2021-03-03 trnsz@pobox.c: constituents. For example, if we wish to multiply together the
5f892713c3 2021-03-03 trnsz@pobox.c: truncated series G1 and G2 generated above, there is no point in
5f892713c3 2021-03-03 trnsz@pobox.c: retaining terms higher than third degree in X. We can avoid even
5f892713c3 2021-03-03 trnsz@pobox.c: generating such terms as follows:;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: let x^4 = 0;
5f892713c3 2021-03-03 trnsz@pobox.c: g3 := g1*g2;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT Replacing X^4 with 0 has the effect of also replacing all
5f892713c3 2021-03-03 trnsz@pobox.c: higher powers of X with 0. We could, of course, use our TAYLOR
5f892713c3 2021-03-03 trnsz@pobox.c: function to compute G3 directly, but differentiation is time consuming
5f892713c3 2021-03-03 trnsz@pobox.c: compared to truncated polynomial algebra. Moreover, our TAYLOR
5f892713c3 2021-03-03 trnsz@pobox.c: function requires a closed-form expression to begin with, whereas
5f892713c3 2021-03-03 trnsz@pobox.c: iterative techniques often permit us to construct symbolic series
5f892713c3 2021-03-03 trnsz@pobox.c: solutions even when we have no such closed form.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: Now consider the truncated series:;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: clear y; factor y;
5f892713c3 2021-03-03 trnsz@pobox.c: h1 := taylor(cos y, y, 0, 6);
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT Suppose we regard terms of order X^N in G1 as being comparable
5f892713c3 2021-03-03 trnsz@pobox.c: to terms of order Y^(2*N) in H1, and we want to form (G1*H1)^2. This
5f892713c3 2021-03-03 trnsz@pobox.c: can be done as follows:;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: let y^7 = 0;
5f892713c3 2021-03-03 trnsz@pobox.c: f1 := (g1*h1)^2;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT Note however that any terms of the form C*X^M*Y^N with
5f892713c3 2021-03-03 trnsz@pobox.c: 2*M+N > 6 are inconsistent with the accuracy of the constituent
5f892713c3 2021-03-03 trnsz@pobox.c: series, and we have generated several such misleading terms by
5f892713c3 2021-03-03 trnsz@pobox.c: independently truncating powers of X and Y. To avoid generating such
5f892713c3 2021-03-03 trnsz@pobox.c: junk, we can specify that a term be replaced by 0 whenever a weighted
5f892713c3 2021-03-03 trnsz@pobox.c: sum of exponents of specified indeterminates and functional forms
5f892713c3 2021-03-03 trnsz@pobox.c: exceeds a specified weight level. In our example this is done as
5f892713c3 2021-03-03 trnsz@pobox.c: follows:;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: weight x=2, y=1;
5f892713c3 2021-03-03 trnsz@pobox.c: wtlevel 6;
5f892713c3 2021-03-03 trnsz@pobox.c: f1 := f1;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT Variables not mentioned in a WEIGHT declaration have a weight
5f892713c3 2021-03-03 trnsz@pobox.c: of 0, and the default weight-level is 2.;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: pause;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT Here is an example of how one might compute numerical
5f892713c3 2021-03-03 trnsz@pobox.c: approximations to the cosine function. (But note that REDUCE can
5f892713c3 2021-03-03 trnsz@pobox.c: already do this automatically!) One way is to provide a supplementary
5f892713c3 2021-03-03 trnsz@pobox.c: LET rule for numerical arguments. For example, since our TAYLOR
5f892713c3 2021-03-03 trnsz@pobox.c: function would reveal that the Taylor series for cos x is
5f892713c3 2021-03-03 trnsz@pobox.c: 1 - x^2/2! + x^4/4! - ...;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: for all x such that numberp x let abs(x) = x, abs(-x) = x;
5f892713c3 2021-03-03 trnsz@pobox.c: epsrecip := 1024$
5f892713c3 2021-03-03 trnsz@pobox.c: on rounded;
5f892713c3 2021-03-03 trnsz@pobox.c: while 1.0 + 1.0/epsrecip neq 1.0 do
5f892713c3 2021-03-03 trnsz@pobox.c: epsrecip := epsrecip + epsrecip;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: for all x such that numberp num x and numberp den x let cos x =
5f892713c3 2021-03-03 trnsz@pobox.c: begin COMMENT X is integer, real, or a rational number. This rule
5f892713c3 2021-03-03 trnsz@pobox.c: returns the Taylor-series approximation to COS X, truncated when
5f892713c3 2021-03-03 trnsz@pobox.c: the last included term is less than (1/EPSRECIP) of the returned
5f892713c3 2021-03-03 trnsz@pobox.c: answer. EPSRECIP is a global variable initialized to a value
5f892713c3 2021-03-03 trnsz@pobox.c: that is appropriate to the local floating-point precision.
5f892713c3 2021-03-03 trnsz@pobox.c: Arbitrarily larger values are justifiable when X is exact and
5f892713c3 2021-03-03 trnsz@pobox.c: ROUNDED is off. No angle reduction is performed, so this
5f892713c3 2021-03-03 trnsz@pobox.c: function is not recommended for ABS(X) >= about PI/2;
5f892713c3 2021-03-03 trnsz@pobox.c: integer k; scalar mxsq, term, ans;
5f892713c3 2021-03-03 trnsz@pobox.c: k := 1;
5f892713c3 2021-03-03 trnsz@pobox.c: mxsq := -x*x;
5f892713c3 2021-03-03 trnsz@pobox.c: term := mxsq/2;
5f892713c3 2021-03-03 trnsz@pobox.c: ans := term + 1;
5f892713c3 2021-03-03 trnsz@pobox.c: while abs(num term)*epsrecip*den(ans) - abs(num ans)*den(term) > 0 do
5f892713c3 2021-03-03 trnsz@pobox.c: << term := term*mxsq/k/(k+1);
5f892713c3 2021-03-03 trnsz@pobox.c: ans := term + ans;
5f892713c3 2021-03-03 trnsz@pobox.c: k := k+2 >>;
5f892713c3 2021-03-03 trnsz@pobox.c: return ans
5f892713c3 2021-03-03 trnsz@pobox.c: end;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: cos(f) + cos(1/2);
5f892713c3 2021-03-03 trnsz@pobox.c: off rounded;
5f892713c3 2021-03-03 trnsz@pobox.c: cos(1/2);
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT As an exercise, write a similar rule for the SIN or LOG, or
5f892713c3 2021-03-03 trnsz@pobox.c: replace the COS rule with an improved one which uses angle reduction
5f892713c3 2021-03-03 trnsz@pobox.c: so that angles outside a modest range are represented as equivalent
5f892713c3 2021-03-03 trnsz@pobox.c: angles within the range, before computing the Taylor series.;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: pause;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT There is a REDUCE compiler, and you may wish to learn how to
5f892713c3 2021-03-03 trnsz@pobox.c: use it. However, even if rules such as the above ones are compiled,
5f892713c3 2021-03-03 trnsz@pobox.c: they will be slow compared to the implementation-dependent hand-coded
5f892713c3 2021-03-03 trnsz@pobox.c: ones used by most FORTRAN-like systems, so REDUCE provides a way to
5f892713c3 2021-03-03 trnsz@pobox.c: generate FORTRAN programs which can then be compiled and executed in a
5f892713c3 2021-03-03 trnsz@pobox.c: subsequent job step. This is useful when there is a lot of
5f892713c3 2021-03-03 trnsz@pobox.c: floating-point computation or when we wish to exploit an existing
5f892713c3 2021-03-03 trnsz@pobox.c: FORTRAN program. Suppose, for example, that we wish to utilize an
5f892713c3 2021-03-03 trnsz@pobox.c: existing FORTRAN subroutine which uses the Newton-Raphson iteration
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: Xnew := Xold - SUB(X=Xold, F(X)/DF(F(X),X))
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: to attempt an approximate solution to the equation F(X)=0. Most such
5f892713c3 2021-03-03 trnsz@pobox.c: subroutines require the user to provide a FORTRAN function or
5f892713c3 2021-03-03 trnsz@pobox.c: subroutine which, given Xold, returns F(X)/DF(F(X),X) evaluated at
5f892713c3 2021-03-03 trnsz@pobox.c: X=Xold. If F(X) is complicated, manual symbolic derivation of
5f892713c3 2021-03-03 trnsz@pobox.c: DF(F(X),X) is a tedious and error-prone process. We can get REDUCE to
5f892713c3 2021-03-03 trnsz@pobox.c: relieve us of this responsibility as is illustrated below for the
5f892713c3 2021-03-03 trnsz@pobox.c: trivial example F(X) = X*E^X - 1:
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: ON FORT, ROUNDED,
5f892713c3 2021-03-03 trnsz@pobox.c: OUT FONDFFILE,
5f892713c3 2021-03-03 trnsz@pobox.c: WRITE " REAL FUNCTION FONDF(XOLD)",
5f892713c3 2021-03-03 trnsz@pobox.c: WRITE " REAL XOLD, F",
5f892713c3 2021-03-03 trnsz@pobox.c: F := XOLD*E^XOLD - 1.0,
5f892713c3 2021-03-03 trnsz@pobox.c: FONDF := F/DF(F,XOLD),
5f892713c3 2021-03-03 trnsz@pobox.c: WRITE " RETURN",
5f892713c3 2021-03-03 trnsz@pobox.c: WRITE " END",
5f892713c3 2021-03-03 trnsz@pobox.c: SHUT FONDFFILE.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT Under the influence of ON FORT, the output generated by
5f892713c3 2021-03-03 trnsz@pobox.c: assignments is printed as valid FORTRAN assignment statements, using
5f892713c3 2021-03-03 trnsz@pobox.c: as many continuation lines as necessary up to the number specified by
5f892713c3 2021-03-03 trnsz@pobox.c: the global variable !*CARDNO, which is initially set to 20. The
5f892713c3 2021-03-03 trnsz@pobox.c: output generated by an expression which is not an assignment is a
5f892713c3 2021-03-03 trnsz@pobox.c: corresponding assignment to a variable named ANS. In either case,
5f892713c3 2021-03-03 trnsz@pobox.c: expressions which would otherwise exceed !*CARDNO continuation lines
5f892713c3 2021-03-03 trnsz@pobox.c: are evaluated piecewise, using ANS as an intermediate variable.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: Try executing the above statement sequence, using an appropriate
5f892713c3 2021-03-03 trnsz@pobox.c: filename and using semicolons instead of the commas and period at the
5f892713c3 2021-03-03 trnsz@pobox.c: end of the lines (only), then view the file after the lesson to see
5f892713c3 2021-03-03 trnsz@pobox.c: how it worked.;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: pause;
5f892713c3 2021-03-03 trnsz@pobox.c: off fort, rounded;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT To make this technique usable by non-REDUCE programmers, we
5f892713c3 2021-03-03 trnsz@pobox.c: could write a more general REDUCE program which given merely the
5f892713c3 2021-03-03 trnsz@pobox.c: expression F by the user, outputs not only the function FONDF, but
5f892713c3 2021-03-03 trnsz@pobox.c: also any necessary job-control commands and an appropriate main
5f892713c3 2021-03-03 trnsz@pobox.c: program for calling the Newton-Raphson subroutine and printing the
5f892713c3 2021-03-03 trnsz@pobox.c: results.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: Sometimes it is desirable to modify or supplement the syntax of
5f892713c3 2021-03-03 trnsz@pobox.c: REDUCE. For example:
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: 1. Electrical engineers may prefer to input J as the
5f892713c3 2021-03-03 trnsz@pobox.c: representation of (-1)^(1/2).
5f892713c3 2021-03-03 trnsz@pobox.c: 2. A user might prefer to input LOGE to denote natural
5f892713c3 2021-03-03 trnsz@pobox.c: logarithms. (Note that LN is already defined as a synonym for
5f892713c3 2021-03-03 trnsz@pobox.c: LOG.)
5f892713c3 2021-03-03 trnsz@pobox.c: 3. A user might prefer to use DERIV instead of DF to request
5f892713c3 2021-03-03 trnsz@pobox.c: differentiation.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: Such lexical macros can be established by the DEFINE declaration:;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: clear x, j;
5f892713c3 2021-03-03 trnsz@pobox.c: define j=i, loge=log, deriv=df;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT Now watch!;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: g1 := sub(x=loge(j^3*x), deriv(x^2,x));
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT Each "equation" in a DEFINE declaration must be of the form
5f892713c3 2021-03-03 trnsz@pobox.c: "name = item", where each item is an expression, an operator, or a
5f892713c3 2021-03-03 trnsz@pobox.c: REDUCE-reserved word such as "FOR". Such replacements take place
5f892713c3 2021-03-03 trnsz@pobox.c: during the lexical scanning, before any evaluation, LET rules, or
5f892713c3 2021-03-03 trnsz@pobox.c: built-in simplification. Think of a good application for this
5f892713c3 2021-03-03 trnsz@pobox.c: facility, then try it.;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: pause;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT When REDUCE is being run non-interactively, with input from a
5f892713c3 2021-03-03 trnsz@pobox.c: file rather than a terminal, it is preferable to have REDUCE make
5f892713c3 2021-03-03 trnsz@pobox.c: reasonable decisions and proceed when it encounters apparently
5f892713c3 2021-03-03 trnsz@pobox.c: undeclared operators, divisions by zero, etc. In interactive mode, it
5f892713c3 2021-03-03 trnsz@pobox.c: is preferable to pause and query the user. ON INT specifies the
5f892713c3 2021-03-03 trnsz@pobox.c: latter style, and OFF INT specifies the former. Under the influence
5f892713c3 2021-03-03 trnsz@pobox.c: of OFF INT, we can also have most error messages suppressed by
5f892713c3 2021-03-03 trnsz@pobox.c: specifying OFF MSG. This is sometimes useful when we expect abnormal
5f892713c3 2021-03-03 trnsz@pobox.c: conditions and do not want our output marred by the associated
5f892713c3 2021-03-03 trnsz@pobox.c: messages. INT is automatically turned off during input from a file in
5f892713c3 2021-03-03 trnsz@pobox.c: response to an IN-command from a terminal.;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: pause;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT REDUCE provides a trace command for debugging, which employs
5f892713c3 2021-03-03 trnsz@pobox.c: the syntax
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: TR functionname1, functionname2, ..., functionnameN.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: An analogous command named UNTR removes function names from trace
5f892713c3 2021-03-03 trnsz@pobox.c: status;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: pause;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT REDUCE also provides an assignment-tracing command for
5f892713c3 2021-03-03 trnsz@pobox.c: debugging, which employs the syntax
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: TRST functionname1, functionname2, ..., functionnameN.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: An analogous command named UNTRST removes functionnames from this
5f892713c3 2021-03-03 trnsz@pobox.c: status. All assignments in the designated functions are reported,
5f892713c3 2021-03-03 trnsz@pobox.c: except for assignments to array elements. Such functions must be
5f892713c3 2021-03-03 trnsz@pobox.c: uncompiled and must have a top-level BEGIN-block. To apply both TRST
5f892713c3 2021-03-03 trnsz@pobox.c: and TR to a function simultaneously, it is crucial to request them in
5f892713c3 2021-03-03 trnsz@pobox.c: that order, and it is necessary to relinquish the two kinds of tracing
5f892713c3 2021-03-03 trnsz@pobox.c: in the opposite order.;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: pause;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: COMMENT The REDUCE algebraic algorithms are written in a subset of
5f892713c3 2021-03-03 trnsz@pobox.c: REDUCE called RLISP. In turn, the more sophisticated features of
5f892713c3 2021-03-03 trnsz@pobox.c: RLISP are written in a small subset of RLISP, which is itself written
5f892713c3 2021-03-03 trnsz@pobox.c: in a simple dialect of LISP called Standard LISP.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: RLISP is ideal for implementing algebraic algorithms, but the RLISP
5f892713c3 2021-03-03 trnsz@pobox.c: environment is not most suitable for the routine use of these
5f892713c3 2021-03-03 trnsz@pobox.c: algorithms in the natural mathematical style of the preceding lessons.
5f892713c3 2021-03-03 trnsz@pobox.c: Accordingly, REDUCE is initially in a mode called ALGEBRAIC, which
5f892713c3 2021-03-03 trnsz@pobox.c: provides the user with the environment illustrated in the preceding
5f892713c3 2021-03-03 trnsz@pobox.c: lessons, while insulating him from accidental interaction with the
5f892713c3 2021-03-03 trnsz@pobox.c: numerous functions, global variables, etc. necessary for implementing
5f892713c3 2021-03-03 trnsz@pobox.c: the built-in algebra. In contrast, the underlying RLISP system
5f892713c3 2021-03-03 trnsz@pobox.c: together with all of the algebraic simplification algorithms written
5f892713c3 2021-03-03 trnsz@pobox.c: therein is called SYMBOLIC mode.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: As we have seen, algebraic-mode rules and procedures can be used to
5f892713c3 2021-03-03 trnsz@pobox.c: extend the built-in algebraic capabilities. However, some extensions
5f892713c3 2021-03-03 trnsz@pobox.c: can be accomplished most easily or efficiently by descending to
5f892713c3 2021-03-03 trnsz@pobox.c: SYMBOLIC mode.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: To make REDUCE operate in symbolic mode, we merely execute the top-
5f892713c3 2021-03-03 trnsz@pobox.c: level mode-declaration statement consisting of the word SYMBOLIC. We
5f892713c3 2021-03-03 trnsz@pobox.c: can subsequently switch back by executing the statement consisting of
5f892713c3 2021-03-03 trnsz@pobox.c: the word ALGEBRAIC.
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: RLISP has the semantics of LISP with the syntax of our by-now-familiar
5f892713c3 2021-03-03 trnsz@pobox.c: algebraic-mode REDUCE, so RLISP provides a natural tool for many
5f892713c3 2021-03-03 trnsz@pobox.c: applications besides computer algebra, such as games, theorem-proving,
5f892713c3 2021-03-03 trnsz@pobox.c: natural-language translation, computer-aided instruction, and
5f892713c3 2021-03-03 trnsz@pobox.c: artificial intelligence in general. For this reason, it is possible
5f892713c3 2021-03-03 trnsz@pobox.c: to run RLISP without any of the symbolic-mode algebraic algorithms
5f892713c3 2021-03-03 trnsz@pobox.c: that are written in RLISP, and it is advisable to thus save space when
5f892713c3 2021-03-03 trnsz@pobox.c: the application does not involve computer algebra. (An RLISP system
5f892713c3 2021-03-03 trnsz@pobox.c: is a step in the process of building a complete REDUCE system, but is
5f892713c3 2021-03-03 trnsz@pobox.c: not distributed as an independent application, although one can be
5f892713c3 2021-03-03 trnsz@pobox.c: built from the source code available.)
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: We have now discussed virtually every feature that is available in
5f892713c3 2021-03-03 trnsz@pobox.c: algebraic mode, so lesson 6 will deal solely with RLISP, and lesson 7
5f892713c3 2021-03-03 trnsz@pobox.c: will deal with communication between ALGEBRAIC and SYMBOLIC mode for
5f892713c3 2021-03-03 trnsz@pobox.c: mathematical purposes. However, I suggest that you proceed to those
5f892713c3 2021-03-03 trnsz@pobox.c: lessons only if and when:
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: 1. You have consolidated and fully absorbed the information in
5f892713c3 2021-03-03 trnsz@pobox.c: lessons 1 through 5 by considerable practice beyond the
5f892713c3 2021-03-03 trnsz@pobox.c: exercises therein. (The exercises were intended to also
5f892713c3 2021-03-03 trnsz@pobox.c: suggest good related project ideas.)
5f892713c3 2021-03-03 trnsz@pobox.c: 2. You feel the need for a facility which you believe is
5f892713c3 2021-03-03 trnsz@pobox.c: impossible or quite awkward to implement solely in ALGEBRAIC
5f892713c3 2021-03-03 trnsz@pobox.c: mode.
5f892713c3 2021-03-03 trnsz@pobox.c: 3. You have read an introductory text about LISP, such as "A
5f892713c3 2021-03-03 trnsz@pobox.c: Concise Introduction to LISP" by David L. Matuszek, which is
5f892713c3 2021-03-03 trnsz@pobox.c: freely available at
5f892713c3 2021-03-03 trnsz@pobox.c: https://www.cis.upenn.edu/~matuszek/LispText/lisp.html.
5f892713c3 2021-03-03 trnsz@pobox.c: 4. You are familiar with the definition of Standard LISP, as
5f892713c3 2021-03-03 trnsz@pobox.c: described in the "Standard LISP Report" which was published in
5f892713c3 2021-03-03 trnsz@pobox.c: the October 1979 SIGPLAN Notices. [A copy is freely available
5f892713c3 2021-03-03 trnsz@pobox.c: via http://reduce-algebra.sourceforge.net/documentation.php.]
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: Don't forget to view or print your newly generated FORTRAN file and to
5f892713c3 2021-03-03 trnsz@pobox.c: delete any temporary files created by this lesson.;
5f892713c3 2021-03-03 trnsz@pobox.c:
5f892713c3 2021-03-03 trnsz@pobox.c: ;end;