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