less1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. COMMENT
  2. REDUCE INTERACTIVE LESSON NUMBER 1
  3. David R. Stoutemyer
  4. University of Hawaii
  5. COMMENT This is lesson 1 of 7 interactive lessons about the REDUCE
  6. system for computer symbolic mathematics. These lessons presume an
  7. acquaintance with elementary calculus, together with a previous
  8. exposure to some computer programming language.
  9. These lessons have been designed for use on a DECsystem 10 or 20.
  10. Apart from changes to the prompt and interrupt characters however
  11. they should work just as well with any REDUCE implementation.
  12. In REDUCE, any sequence of characters from the word "COMMENT" through
  13. the next semicolon or dollar-sign statement separator is an
  14. explanatory remark ignored by the system. In general, either
  15. separator signals the end of a statement, with the dollar sign
  16. suppressing any output that might otherwise automatically be produced
  17. by the statement. The typing of a carriage return initiates the
  18. immediate sequential execution of all statements which have been
  19. terminated on that line. When REDUCE is ready for more input, it will
  20. prompt you with an asterisk at the left margin.
  21. To terminate the lesson and return to the operating system, type an
  22. interrupt character (DEC: control-C ) at any time.
  23. Expressions can be formed using "**", "*", "/", "+", and "-" to
  24. indicate exponentiation, multiplication, division, addition, and
  25. subtraction or negation respectively. Assignments to variables can
  26. be done using the operator ":=". For example:;
  27. R2D2 := (987654321/15)**3;
  28. COMMENT The immediately preceding line, without a semicolon, is the
  29. computed output generated by the line with a semicolon which precedes
  30. it. Note that exact indefinite-precision rational arithmetic was
  31. used, in contrast to the limited-precision arithmetic of traditional
  32. programming languages.
  33. We can use the name R2D2 to represent its value in subsequent
  34. expressions such as;
  35. R2D2 := -R2D2/25 + 3*(13-5);
  36. COMMENT Now I will give you an opportunity to try some analogous
  37. computations. To do so, type the letter N followed by a carriage return
  38. in response to our question "CONT?" (You could type Y if you wish to
  39. relinquish this opportunity, but I strongly recommend reinforced
  40. learning through active participation.) After trying an example or two,
  41. type the command "CONT" terminated by a semicolon and carriage return
  42. when you wish to proceed with the rest of the lesson. To avoid
  43. interference with our examples, please don't assign anything to any
  44. variable names beginning with the letters E through I. To avoid lengthy
  45. delays, I recommend keeping all of your examples approximately as
  46. trivial as ours, saving your more ambitious experiments until after the
  47. lesson. If you happen to initiate a calculation requiring an undue
  48. amount of time to evaluate or to print, you can abort that computation
  49. with an interrupt to get back to the operating system. Restart REDUCE,
  50. followed by the statement "IN LESS1", followed by a semicolon and
  51. return, to restart the lesson at the beginning;
  52. PAUSE;
  53. COMMENT Now watch this example illustrating some more dramatic
  54. differences from traditional scientific programming systems:;
  55. E1 := 2*G + 3*G + H**3/H;
  56. COMMENT Note how we are allowed to use variables to which we have
  57. assigned no values! Note too how similar terms and similar factors
  58. are combined automatically. REDUCE also automatically expands
  59. products and powers of sums, together with placing expressions over
  60. common denominators, as illustrated by the examples:;
  61. E2 := E1*(F+G);
  62. E2 := E1**2;
  63. E1+1/E1;
  64. COMMENT Our last example also illustrates that there is no need to
  65. assign an expression if we do not plan to use its value later. Try
  66. some similar examples:;
  67. PAUSE;
  68. COMMENT It is not always desirable to expand expressions over a
  69. common denominator, and we can use the OFF statement to turn off
  70. either or both computational flags which control these
  71. transformations. The flag named EXP controls EXPansion, and the
  72. flag named MCD controls the Making of Common Denominators;
  73. OFF EXP, MCD;
  74. E2 := E1**2 $
  75. E2 := E2*(F+G) + 1/E1;
  76. COMMENT To turn these flags back on, we type:;
  77. ON EXP, MCD;
  78. COMMENT Try a few relevant examples with these flags turned off
  79. individually and jointly;
  80. PAUSE;
  81. COMMENT Now consider the example:;
  82. E2 := (2*(F*H)**2 - F**2*G*H - (F*G)**2 - F*H**3 + F*H*G**2 - H**4
  83. + G*H**3)/(F**2*H - F**2*G - F*H**2 + 2*F*G*H - F*G**2
  84. - G*H**2 + G**2*H);
  85. COMMENT It is not obvious, but the numerator and denominator of this
  86. expression share a nontrivial common divisor which can be cancelled.
  87. To make REDUCE automatically cancel greatest common divisors, we turn
  88. on the computational flag named GCD:;
  89. ON GCD;
  90. E2;
  91. COMMENT The flag is not on by default because
  92. 1. It can consume a lot of time.
  93. 2. Often we know in advance the few places where a nontrivial
  94. GCD can occur in our problem.
  95. 3. Even without GCD cancellation, expansion and common denomin-
  96. ators guarantee that any rational expression which is equiv-
  97. alent to zero simplifies to zero.
  98. 4. When the denominator is the greatest common divisor, such
  99. as for (X**2 - 2*X + 1)/(X-1), REDUCE cancels the
  100. greatest common divisor even when GCD is OFF.
  101. 5. GCD cancellation sometimes makes expressions more
  102. complicated, such as with (F**10 - G**10)/(F**2 - F*G).
  103. Try the examples mentioned in this comment, together with one
  104. or two other relevant ones;
  105. PAUSE;
  106. COMMENT Exact rational arithmetic can consume an alarming amount of
  107. computer time when the constituent integers have quite large
  108. magnitudes, and the results become awkward to interpret
  109. qualitatively. When this is the case and somewhat inexact numerical
  110. coefficients are acceptable, we can have the arithmetic done floating
  111. point by turning on the computational flag FLOAT. With this flag on,
  112. any non-integer rational numbers are approximated by floating-point
  113. numbers, and the result of any arithmetic operation is floating-point
  114. when any of its operands is floating point. For example:;
  115. ON FLOAT, EXP;
  116. E1:= (12.3456789E3 *F + 3*G)**2 + 1/2;
  117. COMMENT With FLOAT off, any floating-point constants are
  118. automatically approximated by rational numbers:;
  119. OFF FLOAT;
  120. E1 := 12.35*G;
  121. PAUSE;
  122. COMMENT A number of elementary functions, such as SIN, COS and LOG,
  123. are built into REDUCE. Moreover, the letter E represents the base of
  124. the natural logarithms, so the exponentiation operator enables us to
  125. represent the exponential function as well as fractional powers. For
  126. example:;
  127. E1:= SIN(-F*G) + LOG(E) + (3*G**2*COS(-1))**(1/2);
  128. COMMENT What automatic simplifications can you identify in this
  129. example?
  130. Note that most REDUCE implementations do not approximate the values
  131. of these functions for non-trivial numerical arguments, and exact
  132. computations are generally impossible for such cases.
  133. Experimentally determine some other built-in simplifications for
  134. these functions;
  135. PAUSE;
  136. COMMENT Later you will learn how to introduce additional
  137. simplifications and additional functions, including numerical
  138. approximations for examples such as COS(1).
  139. Differentiation is also built-into REDUCE. For example, to
  140. differentiate E1 with respect to F;
  141. E2 := DF(E1,F);
  142. COMMENT To compute the second derivative of E2 with respect to G, we
  143. can type either DF(E2,G,2) or DF(E1,F,1,G,2) or DF(E1,F,G,2) or
  144. DF(E1,G,2,F,1) or;
  145. DF(E1,G,2,F);
  146. COMMENT Surely you can't resist trying a few derivatives of your
  147. own! (Careful, High-order derivatives can be alarmingly complicated);
  148. PAUSE;
  149. COMMENT REDUCE uses the name I to represent (-1)**(1/2),
  150. incorporating some simplification rules such as replacing I**2 by -1.
  151. Here is an opportunity to experimentally determine other
  152. simplifications such as for I**3, 1/I**23, and (I**2-1)/(I-1);
  153. PAUSE;
  154. COMMENT Clearly it is inadvisable to use E or I as a variable. T is
  155. also inadvisable for reasons that will become clear later.
  156. The value of a variable is said to be "bound" to the variable. Any
  157. variable to which we have assigned a value is called a bound variable,
  158. and any variable to which we have not assigned a value is called an
  159. indeterminate. Occasionally it is desirable to make a bound variable
  160. into an indeterminate, and this can be done using the CLEAR command.
  161. For example:;
  162. CLEAR R2D2, E1, E2;
  163. E2;
  164. COMMENT If you suspect that a degenerate assignment, such as E1:=E1,
  165. would suffice to clear a bound variable, try it on one of your own
  166. bound variables:;
  167. PAUSE;
  168. COMMENT REDUCE also supports matrix algebra, as illustrated by the
  169. following sequence:;
  170. MATRIX E1(4,1), F, H;
  171. COMMENT This declaration establishes E1 as a matrix with 4 rows and 1
  172. column, while establishing F and H as matrices of unspecified size.
  173. To establish element values (and sizes if not already established in
  174. the MATRIX declaration), we can use the MAT function, as illustrated
  175. by the following example:;
  176. H := MAT((LOG(G), G+3), (G, 5/7));
  177. COMMENT Only after establishing the size and establishing the element
  178. values of a declared matrix by executing a matrix assignment can we
  179. refer to an individual element or to the matrix as a whole. For
  180. example to increase the last element of H by 1 then form twice the
  181. transpose of H, we can type;
  182. H(2,2) := H(2,2) + 1;
  183. 2*TP(H);
  184. COMMENT To compute the determinant of H:;
  185. DET(H);
  186. COMMENT To compute the trace of H:;
  187. TRACE(H);
  188. COMMENT To compute the inverse of H, we can type H**(-1) or 1/H. To
  189. compute the solution to the equation H*F = MAT((G),(2)), we can
  190. left-multiply the right-hand side by the inverse of H:;
  191. F := 1/H*MAT((G),(2));
  192. COMMENT Notes:
  193. 1. MAT((G),(2))/H would denote right-multiplication by the
  194. inverse, which is not what we want.
  195. 2. Solutions for a set of right-hand-side vectors are most
  196. efficiently computed simultaneously by collecting the right-
  197. hand sides together as the columns of a single multiple-column
  198. matrix.
  199. 3. Subexpressions of the form 1/H*... or H**(-1)*... are computed
  200. more efficiently than if the inverse is computed separately in
  201. a previous statement, so separate computation of the inverse
  202. is advisable only if several solutions are desired and if
  203. they cannot be computed simultaneously.
  204. 4. MAT must have parentheses around each row of elements even if
  205. there is only one row or only one element per row.
  206. 5. References to individual matrix elements must have exactly two
  207. subscripts, even if the matrix has only one row or one column.
  208. Congratulations on completing lesson 1! I urge you to try a sequence of
  209. more ambitious examples for the various features that have been
  210. introduced, in order to gain some familiarity with the relationship
  211. between problem size and computing time for various operations. (In most
  212. implementations, the command "ON TIME" causes computing time to be
  213. printed.) I also urge you to bring to the next lesson appropriate
  214. examples from textbooks, articles, or elsewhere, in order to experience
  215. the decisive learning reinforcement afforded by meaningful personal
  216. examples that are not arbitrarily contrived.
  217. To avoid the possibility of interference from assignments and declar-
  218. ations in lesson 1, it is wise to execute lesson 2 in a fresh REDUCE
  219. job, when you are ready.
  220. ;END;