Lesson_2.red 8.7 KB

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