f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: REDUCE INTERACTIVE LESSON NUMBER 2
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: David R. Stoutemyer
f2c04ccdad 2021-03-03 trnsz@pobox.c: University of Hawaii
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT This is lesson 2 of 7 REDUCE lessons. Please refrain from
f2c04ccdad 2021-03-03 trnsz@pobox.c: using variables beginning with the letters F through H during the
f2c04ccdad 2021-03-03 trnsz@pobox.c: lesson.
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: By now you have probably had the experience of generating an
f2c04ccdad 2021-03-03 trnsz@pobox.c: expression, and then having to repeat the calculation because you
f2c04ccdad 2021-03-03 trnsz@pobox.c: forgot to assign it to a variable or because you did not expect to
f2c04ccdad 2021-03-03 trnsz@pobox.c: want to use it later. REDUCE maintains a history of all inputs and
f2c04ccdad 2021-03-03 trnsz@pobox.c: computation during an interactive session. (Note, this is only for
f2c04ccdad 2021-03-03 trnsz@pobox.c: interactive sessions.) To use an input expression in a new
f2c04ccdad 2021-03-03 trnsz@pobox.c: computation, you can say
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: INPUT(n)
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: where n is the appropriate command number. The evaluated computations
f2c04ccdad 2021-03-03 trnsz@pobox.c: can be accessed through
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: WS(n) or simply WS
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: if you wish to refer to the last computation. WS stands for Work
f2c04ccdad 2021-03-03 trnsz@pobox.c: Space. As with all REDUCE expressions, these can also be used to
f2c04ccdad 2021-03-03 trnsz@pobox.c: create new expressions, e.g.:
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: (INPUT(n1)/WS(n2))^2
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: Special characters can be used to make unique REDUCE variable names
f2c04ccdad 2021-03-03 trnsz@pobox.c: that reduce the chance of accidental interference with any other
f2c04ccdad 2021-03-03 trnsz@pobox.c: variables. In general, whenever you want to include an otherwise
f2c04ccdad 2021-03-03 trnsz@pobox.c: forbidden character such as * in a name, merely precede it by an
f2c04ccdad 2021-03-03 trnsz@pobox.c: exclamation point, which is called the escape character. However,
f2c04ccdad 2021-03-03 trnsz@pobox.c: pick a character other than "*", which is used for many internal
f2c04ccdad 2021-03-03 trnsz@pobox.c: REDUCE names. Otherwise, if most of us use "*" the purpose will be
f2c04ccdad 2021-03-03 trnsz@pobox.c: defeated.;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: g + !%h;
f2c04ccdad 2021-03-03 trnsz@pobox.c: ws;
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT You can also name the expression in the workspace by using the
f2c04ccdad 2021-03-03 trnsz@pobox.c: command SAVEAS, for example:;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: saveas gplush;
f2c04ccdad 2021-03-03 trnsz@pobox.c: gplush;
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT You may have noticed that REDUCE imposes its own order on the
f2c04ccdad 2021-03-03 trnsz@pobox.c: indeterminates and functional forms that appear in results, and that
f2c04ccdad 2021-03-03 trnsz@pobox.c: this ordering can strongly affect the intelligibility of the results.
f2c04ccdad 2021-03-03 trnsz@pobox.c: For example:;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1 := 2*h*g + e + f1 + f + f^2 + f2 + 5 + log(f1) + sin(f1);
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT The ORDER declaration permits us to order indeterminates and
f2c04ccdad 2021-03-03 trnsz@pobox.c: functional forms as we choose. For example, to order F2 before F1,
f2c04ccdad 2021-03-03 trnsz@pobox.c: and to order F1 before all remaining variables:;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: order f2, f1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT Now suppose we partially change our mind and decide to order
f2c04ccdad 2021-03-03 trnsz@pobox.c: LOG(F1) ahead of F1;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: order log(f1), f1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT Note that any other indeterminates or functional forms under
f2c04ccdad 2021-03-03 trnsz@pobox.c: the influence of a previous ORDER declaration, such as F2, rank before
f2c04ccdad 2021-03-03 trnsz@pobox.c: those mentioned in the later declaration. Try to determine the
f2c04ccdad 2021-03-03 trnsz@pobox.c: default ordering algorithm used in your REDUCE implementation, and try
f2c04ccdad 2021-03-03 trnsz@pobox.c: to achieve some delicate rearrangements using the ORDER declaration.;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT You may have also noticed that REDUCE factors out any number,
f2c04ccdad 2021-03-03 trnsz@pobox.c: indeterminate, functional form, or the largest integer power thereof,
f2c04ccdad 2021-03-03 trnsz@pobox.c: which exactly divides every term of a result or every term of a
f2c04ccdad 2021-03-03 trnsz@pobox.c: parenthesized sub-expression of a result. For example:;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: on exp, mcd;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1 := f^2*(g^2 + 2*g) + f*(g^2 + h)/(2*f1);
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT This process usually leads to more compact expressions and
f2c04ccdad 2021-03-03 trnsz@pobox.c: reveals important structural information. However, the process can
f2c04ccdad 2021-03-03 trnsz@pobox.c: yield results which are difficult to interpret if the resulting
f2c04ccdad 2021-03-03 trnsz@pobox.c: parentheses are nested more than about two levels, and it is often
f2c04ccdad 2021-03-03 trnsz@pobox.c: desirable to see a fully expanded result to facilitate direct
f2c04ccdad 2021-03-03 trnsz@pobox.c: comparison of all terms. To suppress this monomial factoring, we can
f2c04ccdad 2021-03-03 trnsz@pobox.c: turn off an output control switch named ALLFAC;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: off allfac;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT The ALLFAC monomial-factorization process is strongly
f2c04ccdad 2021-03-03 trnsz@pobox.c: dependent upon the ordering. We can achieve a more selective monomial
f2c04ccdad 2021-03-03 trnsz@pobox.c: factorization by using the FACTOR declaration, which declares a
f2c04ccdad 2021-03-03 trnsz@pobox.c: variable to have FACTOR status. If any indeterminates or functional
f2c04ccdad 2021-03-03 trnsz@pobox.c: forms occurring in an expression are in FACTOR status when the
f2c04ccdad 2021-03-03 trnsz@pobox.c: expression is output, terms having the same powers of the
f2c04ccdad 2021-03-03 trnsz@pobox.c: indeterminates or functional forms are collected together, and the
f2c04ccdad 2021-03-03 trnsz@pobox.c: power is factored out. Terms containing two or more indeterminates or
f2c04ccdad 2021-03-03 trnsz@pobox.c: functional forms under FACTOR status are not included in this monomial
f2c04ccdad 2021-03-03 trnsz@pobox.c: factorization process. For example:;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: off allfac; factor f; g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: factor g; g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT We can use the REMFAC command to remove items from factor
f2c04ccdad 2021-03-03 trnsz@pobox.c: status:;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: remfac f;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT ALLFAC can still have an effect on the coefficients of the
f2c04ccdad 2021-03-03 trnsz@pobox.c: monomials that have been factored out under the influence of FACTOR:;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: on allfac;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT It is often desirable to distribute denominators over all
f2c04ccdad 2021-03-03 trnsz@pobox.c: factored sub-expressions generated under the influence of a FACTOR
f2c04ccdad 2021-03-03 trnsz@pobox.c: declaration, such as when we wish to view a result as a polynomial or
f2c04ccdad 2021-03-03 trnsz@pobox.c: as a power series in the factored indeterminates or functional forms,
f2c04ccdad 2021-03-03 trnsz@pobox.c: with coefficients which are rational functions of any other
f2c04ccdad 2021-03-03 trnsz@pobox.c: indeterminates or functional forms. (A mnemonic aid is: think RAT for
f2c04ccdad 2021-03-03 trnsz@pobox.c: RATional-function coefficients.) For example:;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: on rat;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT RAT has no effect on expressions which have no indeterminates
f2c04ccdad 2021-03-03 trnsz@pobox.c: or functional forms under the influence of FACTOR. The related but
f2c04ccdad 2021-03-03 trnsz@pobox.c: different DIV switch permits us to distribute numerical and monomial
f2c04ccdad 2021-03-03 trnsz@pobox.c: factors of the denominator over every term of the numerator,
f2c04ccdad 2021-03-03 trnsz@pobox.c: expressing these distributed portions as rational-number coefficients
f2c04ccdad 2021-03-03 trnsz@pobox.c: and negative power factors respectively. (A mnemonic aid: DIV DIVides
f2c04ccdad 2021-03-03 trnsz@pobox.c: by monomials.) The overall effect can also depend strongly on whether
f2c04ccdad 2021-03-03 trnsz@pobox.c: the RAT switch is on or off. Series and polynomials are often most
f2c04ccdad 2021-03-03 trnsz@pobox.c: attractive with RAT and DIV both on;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: on div, rat;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: off rat;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: remfac g;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT With a very complicated result, detailed study of the result
f2c04ccdad 2021-03-03 trnsz@pobox.c: is often facilitated by having each new term begin on a new line,
f2c04ccdad 2021-03-03 trnsz@pobox.c: which can be accomplished using the LIST switch:;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: on list;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT In various combinations, ORDER, FACTOR, the computational
f2c04ccdad 2021-03-03 trnsz@pobox.c: switches EXP, MCD, GCD, and ROUNDED, together with the output control
f2c04ccdad 2021-03-03 trnsz@pobox.c: switches ALLFAC, RAT, DIV, and LIST provide a variety of output
f2c04ccdad 2021-03-03 trnsz@pobox.c: alternatives. With experience, it is usually possible to use these
f2c04ccdad 2021-03-03 trnsz@pobox.c: tools to produce a result in the desired form, or at least in a form
f2c04ccdad 2021-03-03 trnsz@pobox.c: which is far more acceptable than the one produced by the default
f2c04ccdad 2021-03-03 trnsz@pobox.c: settings. I encourage you to experiment with various combinations
f2c04ccdad 2021-03-03 trnsz@pobox.c: while this information is fresh in your mind;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c: off list, rat, div, gcd, rounded;
f2c04ccdad 2021-03-03 trnsz@pobox.c: on allfac, mcd, exp;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT You may have wondered whether or not an assignment to a
f2c04ccdad 2021-03-03 trnsz@pobox.c: variable, say F1, automatically updates the value of a bound variable,
f2c04ccdad 2021-03-03 trnsz@pobox.c: say G1, which was previously assigned an expression containing F1.
f2c04ccdad 2021-03-03 trnsz@pobox.c: The answer is:
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: 1. If F1 was a bound variable in the expression when it was set to
f2c04ccdad 2021-03-03 trnsz@pobox.c: G1, then subsequent changes to the value of F1 have no effect
f2c04ccdad 2021-03-03 trnsz@pobox.c: on G1 because all traces of F1 in G1 disappeared after F1
f2c04ccdad 2021-03-03 trnsz@pobox.c: contributed its value to the formation of G1.
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: 2. If F1 was an indeterminate in an expression previously assigned
f2c04ccdad 2021-03-03 trnsz@pobox.c: to G1, then for each subsequent use of G1, F1 contributes its
f2c04ccdad 2021-03-03 trnsz@pobox.c: current value at the time of that use.
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: These phenomena are illustrated by the following sequence:;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c: f2 := f;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1 := f1 + f2;
f2c04ccdad 2021-03-03 trnsz@pobox.c: f2 := g;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: f1 := g;
f2c04ccdad 2021-03-03 trnsz@pobox.c: f1 := h;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c: f1 := g;
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT Experience indicates that it is well worth studying this
f2c04ccdad 2021-03-03 trnsz@pobox.c: sequence and experimenting with others until these phenomena are
f2c04ccdad 2021-03-03 trnsz@pobox.c: thoroughly understood. You might, for example, mimic the above
f2c04ccdad 2021-03-03 trnsz@pobox.c: example, but with another level of evaluation included by inserting a
f2c04ccdad 2021-03-03 trnsz@pobox.c: statement analogous to "Q9:=G1" after "F2:=G", and inserting an
f2c04ccdad 2021-03-03 trnsz@pobox.c: expression analogous to "Q9" at the end, to compare with G1.;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: pause;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT Note also, that if an indeterminate is used directly, or
f2c04ccdad 2021-03-03 trnsz@pobox.c: indirectly through another expression, in evaluating itself, this will
f2c04ccdad 2021-03-03 trnsz@pobox.c: lead to an infinite recursion. For example, the following expression
f2c04ccdad 2021-03-03 trnsz@pobox.c: results in infinite recursion at the first evaluation of H1, so REDUCE
f2c04ccdad 2021-03-03 trnsz@pobox.c: will detect it as an error.
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: H1 := H1 + 1
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: You may experiment with this problem later at your own risk.
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: It is often desirable to make an assignment to an indeterminate in a
f2c04ccdad 2021-03-03 trnsz@pobox.c: previously established expression have a permanent effect, as if the
f2c04ccdad 2021-03-03 trnsz@pobox.c: assignment were done before forming the expression. This can be done
f2c04ccdad 2021-03-03 trnsz@pobox.c: by using the substitute function, SUB.;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: g1 := f1 + f2;
f2c04ccdad 2021-03-03 trnsz@pobox.c: h1 := sub(f1=h, g1);
f2c04ccdad 2021-03-03 trnsz@pobox.c: f1 := g;
f2c04ccdad 2021-03-03 trnsz@pobox.c: h1;
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: COMMENT Note the use of "=" rather than ":=" in SUB. This function is
f2c04ccdad 2021-03-03 trnsz@pobox.c: also valuable for achieving the effect of a local assignment within a
f2c04ccdad 2021-03-03 trnsz@pobox.c: sub-expression, without binding the involved indeterminate or
f2c04ccdad 2021-03-03 trnsz@pobox.c: functional form in the rest of the expression or wherever else it
f2c04ccdad 2021-03-03 trnsz@pobox.c: occurs. More generally the SUB function can have any number of
f2c04ccdad 2021-03-03 trnsz@pobox.c: equations of the form "indeterminate or functional form = expression",
f2c04ccdad 2021-03-03 trnsz@pobox.c: separated by commas, before the expression which is its last argument.
f2c04ccdad 2021-03-03 trnsz@pobox.c: Try devising a set of examples which reveals whether such multiple
f2c04ccdad 2021-03-03 trnsz@pobox.c: substitutions are done left to right, right to left, in parallel, or
f2c04ccdad 2021-03-03 trnsz@pobox.c: unpredictably.
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: This is the end of lesson 2. To execute lesson 3, start a fresh
f2c04ccdad 2021-03-03 trnsz@pobox.c: REDUCE session.
f2c04ccdad 2021-03-03 trnsz@pobox.c:
f2c04ccdad 2021-03-03 trnsz@pobox.c: ;end;