LESS2 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 Space.
  20. As with all REDUCE expressions, these can also be used to create new
  21. expressions:
  22. (INPUT(n)/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
  35. the 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
  51. order 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
  56. before those mentioned in the later declaration. Try to determine
  57. the default ordering algorithm used in your REDUCE implementation, and
  58. try to achieve some delicate rearrangements using the ORDER
  59. declaration.;
  60. PAUSE;
  61. COMMENT You may have also noticed that REDUCE factors out any
  62. number, indeterminate, functional form, or the largest integer power
  63. thereof which exactly divides every term of a result or every term of
  64. a parenthesized subexpression of a result. For example:;
  65. ON EXP, MCD;
  66. G1:= F**2*(G**2 + 2*G) + F*(G**2+H)/(2*F1);
  67. COMMENT This process usually leads to more compact expressions and
  68. reveals important structural information. However, the process can
  69. yield results which are difficult to interpret if the resulting
  70. parentheses are nested more than about two levels, and it is often
  71. desirable to see a fully expanded result to facilitate direct
  72. comparison of all terms. To suppress this monomial factoring, we can
  73. turn off an output control switch named ALLFAC;
  74. OFF ALLFAC;
  75. G1;
  76. PAUSE;
  77. COMMENT The ALLFAC monomial-factorization process is strongly
  78. dependent upon the ordering. We can achieve a more selective monomial
  79. factorization by using the FACTOR declaration, which declares a
  80. variable to have FACTOR status. If any indeterminates or functional
  81. forms occurring in an expression are in FACTOR status when the
  82. expression is printed, terms having the same powers of the
  83. indeterminates or functional forms are collected together, and the
  84. power is factored out. Terms containing two or more indeterminates or
  85. functional forms under FACTOR status are not included in this monomial
  86. factorization process. For example:;
  87. OFF ALLFAC; FACTOR F; G1;
  88. FACTOR G; G1; 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 subexpressions 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
  104. for RATional-function coefficients.) For example:;
  105. ON RAT;
  106. G1;
  107. PAUSE;
  108. COMMENT RAT has no effect on expressions which have no
  109. indeterminates or functional forms under the influence of FACTOR.
  110. The related but different DIV switch permits us to distribute numerical
  111. and monomial factors of the denominator over every term of the
  112. numerator, expressing these distributed portions as rational-number
  113. coefficients and negative power factors respectively. (A mnemonic
  114. aid: DIV DIVides by monomials.) The overall effect can also depend
  115. strongly on whether the RAT switch is on or off. Series and
  116. polynomials are often most 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
  144. variable, say G1, which was previously assigned an expression
  145. containing F1. The answer is:
  146. 1. If F1 was a bound variable in the expression when it was set
  147. to G1, then subsequent changes to the value of F1 have no
  148. effect on G1 because all traces of F1 in G1 disappeared after
  149. F1 contributed its value to the formation of G1.
  150. 2. If F1 was an indeterminate in an expression previously
  151. assigned to G1, then for each subsequent use of G1, F1
  152. contributes its 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. On some
  175. machines (Vax/Unix, IBM) this will cause REDUCE to terminate abnormally.
  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 by
  181. 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
  187. is also valuable for achieving the effect of a local assignment
  188. within a subexpression, 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 =
  192. expression", separated by commas, before the expression which is its
  193. last argument. Try devising a set of examples which reveals whether
  194. such multiple substitutions are done left to right, right to left, in
  195. parallel, or unpredictably.
  196. This is the end of lesson 2. To execute lesson 3, start a fresh
  197. REDUCE job.
  198. ;END;