Origin for each line in Lesson_5.red from check-in f2c04ccdad:

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

iCAS Bundled REDUCE Scripts
Homepage | GitHub Mirror | SourceHut Mirror | NotABug Mirror | Chisel Mirror | Chisel RSS ]