5f892713c3 2021-03-03 1: COMMENT
5f892713c3 2021-03-03 2:
5f892713c3 2021-03-03 3: REDUCE INTERACTIVE LESSON NUMBER 2
5f892713c3 2021-03-03 4:
5f892713c3 2021-03-03 5: David R. Stoutemyer
5f892713c3 2021-03-03 6: University of Hawaii
5f892713c3 2021-03-03 7:
5f892713c3 2021-03-03 8:
5f892713c3 2021-03-03 9: COMMENT This is lesson 2 of 7 REDUCE lessons. Please refrain from
5f892713c3 2021-03-03 10: using variables beginning with the letters F through H during the
5f892713c3 2021-03-03 11: lesson.
5f892713c3 2021-03-03 12:
5f892713c3 2021-03-03 13: By now you have probably had the experience of generating an
5f892713c3 2021-03-03 14: expression, and then having to repeat the calculation because you
5f892713c3 2021-03-03 15: forgot to assign it to a variable or because you did not expect to
5f892713c3 2021-03-03 16: want to use it later. REDUCE maintains a history of all inputs and
5f892713c3 2021-03-03 17: computation during an interactive session. (Note, this is only for
5f892713c3 2021-03-03 18: interactive sessions.) To use an input expression in a new
5f892713c3 2021-03-03 19: computation, you can say
5f892713c3 2021-03-03 20:
5f892713c3 2021-03-03 21: INPUT(n)
5f892713c3 2021-03-03 22:
5f892713c3 2021-03-03 23: where n is the appropriate command number. The evaluated computations
5f892713c3 2021-03-03 24: can be accessed through
5f892713c3 2021-03-03 25:
5f892713c3 2021-03-03 26: WS(n) or simply WS
5f892713c3 2021-03-03 27:
5f892713c3 2021-03-03 28: if you wish to refer to the last computation. WS stands for Work
5f892713c3 2021-03-03 29: Space. As with all REDUCE expressions, these can also be used to
5f892713c3 2021-03-03 30: create new expressions, e.g.:
5f892713c3 2021-03-03 31:
5f892713c3 2021-03-03 32: (INPUT(n1)/WS(n2))^2
5f892713c3 2021-03-03 33:
5f892713c3 2021-03-03 34: Special characters can be used to make unique REDUCE variable names
5f892713c3 2021-03-03 35: that reduce the chance of accidental interference with any other
5f892713c3 2021-03-03 36: variables. In general, whenever you want to include an otherwise
5f892713c3 2021-03-03 37: forbidden character such as * in a name, merely precede it by an
5f892713c3 2021-03-03 38: exclamation point, which is called the escape character. However,
5f892713c3 2021-03-03 39: pick a character other than "*", which is used for many internal
5f892713c3 2021-03-03 40: REDUCE names. Otherwise, if most of us use "*" the purpose will be
5f892713c3 2021-03-03 41: defeated.;
5f892713c3 2021-03-03 42:
5f892713c3 2021-03-03 43: g + !%h;
5f892713c3 2021-03-03 44: ws;
5f892713c3 2021-03-03 45: pause;
5f892713c3 2021-03-03 46:
5f892713c3 2021-03-03 47: COMMENT You can also name the expression in the workspace by using the
5f892713c3 2021-03-03 48: command SAVEAS, for example:;
5f892713c3 2021-03-03 49:
5f892713c3 2021-03-03 50: saveas gplush;
5f892713c3 2021-03-03 51: gplush;
5f892713c3 2021-03-03 52: pause;
5f892713c3 2021-03-03 53:
5f892713c3 2021-03-03 54: COMMENT You may have noticed that REDUCE imposes its own order on the
5f892713c3 2021-03-03 55: indeterminates and functional forms that appear in results, and that
5f892713c3 2021-03-03 56: this ordering can strongly affect the intelligibility of the results.
5f892713c3 2021-03-03 57: For example:;
5f892713c3 2021-03-03 58:
5f892713c3 2021-03-03 59: g1 := 2*h*g + e + f1 + f + f^2 + f2 + 5 + log(f1) + sin(f1);
5f892713c3 2021-03-03 60:
5f892713c3 2021-03-03 61: COMMENT The ORDER declaration permits us to order indeterminates and
5f892713c3 2021-03-03 62: functional forms as we choose. For example, to order F2 before F1,
5f892713c3 2021-03-03 63: and to order F1 before all remaining variables:;
5f892713c3 2021-03-03 64:
5f892713c3 2021-03-03 65: order f2, f1;
5f892713c3 2021-03-03 66: g1;
5f892713c3 2021-03-03 67: pause;
5f892713c3 2021-03-03 68:
5f892713c3 2021-03-03 69: COMMENT Now suppose we partially change our mind and decide to order
5f892713c3 2021-03-03 70: LOG(F1) ahead of F1;
5f892713c3 2021-03-03 71:
5f892713c3 2021-03-03 72: order log(f1), f1;
5f892713c3 2021-03-03 73: g1;
5f892713c3 2021-03-03 74:
5f892713c3 2021-03-03 75: COMMENT Note that any other indeterminates or functional forms under
5f892713c3 2021-03-03 76: the influence of a previous ORDER declaration, such as F2, rank before
5f892713c3 2021-03-03 77: those mentioned in the later declaration. Try to determine the
5f892713c3 2021-03-03 78: default ordering algorithm used in your REDUCE implementation, and try
5f892713c3 2021-03-03 79: to achieve some delicate rearrangements using the ORDER declaration.;
5f892713c3 2021-03-03 80:
5f892713c3 2021-03-03 81: pause;
5f892713c3 2021-03-03 82:
5f892713c3 2021-03-03 83: COMMENT You may have also noticed that REDUCE factors out any number,
5f892713c3 2021-03-03 84: indeterminate, functional form, or the largest integer power thereof,
5f892713c3 2021-03-03 85: which exactly divides every term of a result or every term of a
5f892713c3 2021-03-03 86: parenthesized sub-expression of a result. For example:;
5f892713c3 2021-03-03 87:
5f892713c3 2021-03-03 88: on exp, mcd;
5f892713c3 2021-03-03 89: g1 := f^2*(g^2 + 2*g) + f*(g^2 + h)/(2*f1);
5f892713c3 2021-03-03 90:
5f892713c3 2021-03-03 91: COMMENT This process usually leads to more compact expressions and
5f892713c3 2021-03-03 92: reveals important structural information. However, the process can
5f892713c3 2021-03-03 93: yield results which are difficult to interpret if the resulting
5f892713c3 2021-03-03 94: parentheses are nested more than about two levels, and it is often
5f892713c3 2021-03-03 95: desirable to see a fully expanded result to facilitate direct
5f892713c3 2021-03-03 96: comparison of all terms. To suppress this monomial factoring, we can
5f892713c3 2021-03-03 97: turn off an output control switch named ALLFAC;
5f892713c3 2021-03-03 98:
5f892713c3 2021-03-03 99: off allfac;
5f892713c3 2021-03-03 100: g1;
5f892713c3 2021-03-03 101: pause;
5f892713c3 2021-03-03 102:
5f892713c3 2021-03-03 103: COMMENT The ALLFAC monomial-factorization process is strongly
5f892713c3 2021-03-03 104: dependent upon the ordering. We can achieve a more selective monomial
5f892713c3 2021-03-03 105: factorization by using the FACTOR declaration, which declares a
5f892713c3 2021-03-03 106: variable to have FACTOR status. If any indeterminates or functional
5f892713c3 2021-03-03 107: forms occurring in an expression are in FACTOR status when the
5f892713c3 2021-03-03 108: expression is output, terms having the same powers of the
5f892713c3 2021-03-03 109: indeterminates or functional forms are collected together, and the
5f892713c3 2021-03-03 110: power is factored out. Terms containing two or more indeterminates or
5f892713c3 2021-03-03 111: functional forms under FACTOR status are not included in this monomial
5f892713c3 2021-03-03 112: factorization process. For example:;
5f892713c3 2021-03-03 113:
5f892713c3 2021-03-03 114: off allfac; factor f; g1;
5f892713c3 2021-03-03 115: factor g; g1;
5f892713c3 2021-03-03 116: pause;
5f892713c3 2021-03-03 117:
5f892713c3 2021-03-03 118: COMMENT We can use the REMFAC command to remove items from factor
5f892713c3 2021-03-03 119: status:;
5f892713c3 2021-03-03 120:
5f892713c3 2021-03-03 121: remfac f;
5f892713c3 2021-03-03 122: g1;
5f892713c3 2021-03-03 123:
5f892713c3 2021-03-03 124: COMMENT ALLFAC can still have an effect on the coefficients of the
5f892713c3 2021-03-03 125: monomials that have been factored out under the influence of FACTOR:;
5f892713c3 2021-03-03 126:
5f892713c3 2021-03-03 127: on allfac;
5f892713c3 2021-03-03 128: g1;
5f892713c3 2021-03-03 129: pause;
5f892713c3 2021-03-03 130:
5f892713c3 2021-03-03 131: COMMENT It is often desirable to distribute denominators over all
5f892713c3 2021-03-03 132: factored sub-expressions generated under the influence of a FACTOR
5f892713c3 2021-03-03 133: declaration, such as when we wish to view a result as a polynomial or
5f892713c3 2021-03-03 134: as a power series in the factored indeterminates or functional forms,
5f892713c3 2021-03-03 135: with coefficients which are rational functions of any other
5f892713c3 2021-03-03 136: indeterminates or functional forms. (A mnemonic aid is: think RAT for
5f892713c3 2021-03-03 137: RATional-function coefficients.) For example:;
5f892713c3 2021-03-03 138:
5f892713c3 2021-03-03 139: on rat;
5f892713c3 2021-03-03 140: g1;
5f892713c3 2021-03-03 141: pause;
5f892713c3 2021-03-03 142:
5f892713c3 2021-03-03 143: COMMENT RAT has no effect on expressions which have no indeterminates
5f892713c3 2021-03-03 144: or functional forms under the influence of FACTOR. The related but
5f892713c3 2021-03-03 145: different DIV switch permits us to distribute numerical and monomial
5f892713c3 2021-03-03 146: factors of the denominator over every term of the numerator,
5f892713c3 2021-03-03 147: expressing these distributed portions as rational-number coefficients
5f892713c3 2021-03-03 148: and negative power factors respectively. (A mnemonic aid: DIV DIVides
5f892713c3 2021-03-03 149: by monomials.) The overall effect can also depend strongly on whether
5f892713c3 2021-03-03 150: the RAT switch is on or off. Series and polynomials are often most
5f892713c3 2021-03-03 151: attractive with RAT and DIV both on;
5f892713c3 2021-03-03 152:
5f892713c3 2021-03-03 153: on div, rat;
5f892713c3 2021-03-03 154: g1;
5f892713c3 2021-03-03 155: off rat;
5f892713c3 2021-03-03 156: g1;
5f892713c3 2021-03-03 157: pause;
5f892713c3 2021-03-03 158:
5f892713c3 2021-03-03 159: remfac g;
5f892713c3 2021-03-03 160: g1;
5f892713c3 2021-03-03 161: pause;
5f892713c3 2021-03-03 162:
5f892713c3 2021-03-03 163: COMMENT With a very complicated result, detailed study of the result
5f892713c3 2021-03-03 164: is often facilitated by having each new term begin on a new line,
5f892713c3 2021-03-03 165: which can be accomplished using the LIST switch:;
5f892713c3 2021-03-03 166:
5f892713c3 2021-03-03 167: on list;
5f892713c3 2021-03-03 168: g1;
5f892713c3 2021-03-03 169: pause;
5f892713c3 2021-03-03 170:
5f892713c3 2021-03-03 171: COMMENT In various combinations, ORDER, FACTOR, the computational
5f892713c3 2021-03-03 172: switches EXP, MCD, GCD, and ROUNDED, together with the output control
5f892713c3 2021-03-03 173: switches ALLFAC, RAT, DIV, and LIST provide a variety of output
5f892713c3 2021-03-03 174: alternatives. With experience, it is usually possible to use these
5f892713c3 2021-03-03 175: tools to produce a result in the desired form, or at least in a form
5f892713c3 2021-03-03 176: which is far more acceptable than the one produced by the default
5f892713c3 2021-03-03 177: settings. I encourage you to experiment with various combinations
5f892713c3 2021-03-03 178: while this information is fresh in your mind;
5f892713c3 2021-03-03 179:
5f892713c3 2021-03-03 180: pause;
5f892713c3 2021-03-03 181: off list, rat, div, gcd, rounded;
5f892713c3 2021-03-03 182: on allfac, mcd, exp;
5f892713c3 2021-03-03 183:
5f892713c3 2021-03-03 184: COMMENT You may have wondered whether or not an assignment to a
5f892713c3 2021-03-03 185: variable, say F1, automatically updates the value of a bound variable,
5f892713c3 2021-03-03 186: say G1, which was previously assigned an expression containing F1.
5f892713c3 2021-03-03 187: The answer is:
5f892713c3 2021-03-03 188:
5f892713c3 2021-03-03 189: 1. If F1 was a bound variable in the expression when it was set to
5f892713c3 2021-03-03 190: G1, then subsequent changes to the value of F1 have no effect
5f892713c3 2021-03-03 191: on G1 because all traces of F1 in G1 disappeared after F1
5f892713c3 2021-03-03 192: contributed its value to the formation of G1.
5f892713c3 2021-03-03 193:
5f892713c3 2021-03-03 194: 2. If F1 was an indeterminate in an expression previously assigned
5f892713c3 2021-03-03 195: to G1, then for each subsequent use of G1, F1 contributes its
5f892713c3 2021-03-03 196: current value at the time of that use.
5f892713c3 2021-03-03 197:
5f892713c3 2021-03-03 198: These phenomena are illustrated by the following sequence:;
5f892713c3 2021-03-03 199:
5f892713c3 2021-03-03 200: pause;
5f892713c3 2021-03-03 201: f2 := f;
5f892713c3 2021-03-03 202: g1 := f1 + f2;
5f892713c3 2021-03-03 203: f2 := g;
5f892713c3 2021-03-03 204: g1;
5f892713c3 2021-03-03 205: f1 := g;
5f892713c3 2021-03-03 206: f1 := h;
5f892713c3 2021-03-03 207: g1;
5f892713c3 2021-03-03 208: f1 := g;
5f892713c3 2021-03-03 209: g1;
5f892713c3 2021-03-03 210:
5f892713c3 2021-03-03 211: COMMENT Experience indicates that it is well worth studying this
5f892713c3 2021-03-03 212: sequence and experimenting with others until these phenomena are
5f892713c3 2021-03-03 213: thoroughly understood. You might, for example, mimic the above
5f892713c3 2021-03-03 214: example, but with another level of evaluation included by inserting a
5f892713c3 2021-03-03 215: statement analogous to "Q9:=G1" after "F2:=G", and inserting an
5f892713c3 2021-03-03 216: expression analogous to "Q9" at the end, to compare with G1.;
5f892713c3 2021-03-03 217:
5f892713c3 2021-03-03 218: pause;
5f892713c3 2021-03-03 219:
5f892713c3 2021-03-03 220: COMMENT Note also, that if an indeterminate is used directly, or
5f892713c3 2021-03-03 221: indirectly through another expression, in evaluating itself, this will
5f892713c3 2021-03-03 222: lead to an infinite recursion. For example, the following expression
5f892713c3 2021-03-03 223: results in infinite recursion at the first evaluation of H1, so REDUCE
5f892713c3 2021-03-03 224: will detect it as an error.
5f892713c3 2021-03-03 225:
5f892713c3 2021-03-03 226: H1 := H1 + 1
5f892713c3 2021-03-03 227:
5f892713c3 2021-03-03 228: You may experiment with this problem later at your own risk.
5f892713c3 2021-03-03 229:
5f892713c3 2021-03-03 230: It is often desirable to make an assignment to an indeterminate in a
5f892713c3 2021-03-03 231: previously established expression have a permanent effect, as if the
5f892713c3 2021-03-03 232: assignment were done before forming the expression. This can be done
5f892713c3 2021-03-03 233: by using the substitute function, SUB.;
5f892713c3 2021-03-03 234:
5f892713c3 2021-03-03 235: g1 := f1 + f2;
5f892713c3 2021-03-03 236: h1 := sub(f1=h, g1);
5f892713c3 2021-03-03 237: f1 := g;
5f892713c3 2021-03-03 238: h1;
5f892713c3 2021-03-03 239:
5f892713c3 2021-03-03 240: COMMENT Note the use of "=" rather than ":=" in SUB. This function is
5f892713c3 2021-03-03 241: also valuable for achieving the effect of a local assignment within a
5f892713c3 2021-03-03 242: sub-expression, without binding the involved indeterminate or
5f892713c3 2021-03-03 243: functional form in the rest of the expression or wherever else it
5f892713c3 2021-03-03 244: occurs. More generally the SUB function can have any number of
5f892713c3 2021-03-03 245: equations of the form "indeterminate or functional form = expression",
5f892713c3 2021-03-03 246: separated by commas, before the expression which is its last argument.
5f892713c3 2021-03-03 247: Try devising a set of examples which reveals whether such multiple
5f892713c3 2021-03-03 248: substitutions are done left to right, right to left, in parallel, or
5f892713c3 2021-03-03 249: unpredictably.
5f892713c3 2021-03-03 250:
5f892713c3 2021-03-03 251: This is the end of lesson 2. To execute lesson 3, start a fresh
5f892713c3 2021-03-03 252: REDUCE session.
5f892713c3 2021-03-03 253:
5f892713c3 2021-03-03 254: ;end;