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