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 DEC system 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 switches which control these
  71. transformations. The switch named EXP controls EXPansion, and the
  72. switch 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 switches back on, we type:;
  77. ON EXP, MCD;
  78. COMMENT Try a few relevant examples with these switches turned off
  79. individually and jointly;
  80. PAUSE;
  81. ON EXP; % Just in case you turned it off.
  82. COMMENT Now consider the example:;
  83. E2 := (2*(F*H)**2 - F**2*G*H - (F*G)**2 - F*H**3 + F*H*G**2 - H**4
  84. + G*H**3)/(F**2*H - F**2*G - F*H**2 + 2*F*G*H - F*G**2
  85. - G*H**2 + G**2*H);
  86. COMMENT It is not obvious, but the numerator and denominator of this
  87. expression share a nontrivial common divisor which can be canceled.
  88. To make REDUCE automatically cancel greatest common divisors, we turn
  89. on the computational switch named GCD:;
  90. ON GCD;
  91. E2;
  92. COMMENT The switch is not on by default because
  93. 1. It can consume a lot of time.
  94. 2. Often we know in advance the few places where a nontrivial
  95. GCD can occur in our problem.
  96. 3. Even without GCD cancellation, expansion and common denomin-
  97. ators guarantee that any rational expression which is equiv-
  98. alent to zero simplifies to zero.
  99. 4. When the denominator is the greatest common divisor, such
  100. as for (X**2 - 2*X + 1)/(X-1), REDUCE cancels the
  101. greatest common divisor even when GCD is OFF.
  102. 5. GCD cancellation sometimes makes expressions more
  103. complicated, such as with (F**10 - G**10)/(F**2 + F*G -2*G**2).
  104. Try the examples mentioned in this comment, together with one
  105. or two other relevant ones;
  106. PAUSE;
  107. COMMENT Exact rational arithmetic can consume an alarming amount of
  108. computer time when the constituent integers have quite large
  109. magnitudes, and the results become awkward to interpret
  110. qualitatively. When this is the case and somewhat inexact numerical
  111. coefficients are acceptable, we can have the arithmetic done floating
  112. point by turning on the computational switch ROUNDED. With this switch
  113. on, any non-integer rational numbers are approximated by floating-point
  114. numbers, and the result of any arithmetic operation is floating-point
  115. when any of its operands is floating point. For example:;
  116. ON ROUNDED;
  117. E1:= (12.3456789E3 *F + 3*G)**2 + 1/2;
  118. COMMENT With ROUNDED off, any floating-point constants are
  119. automatically approximated by rational numbers:;
  121. E1 := 12.35*G;
  122. PAUSE;
  123. COMMENT A number of elementary functions, such as SIN, COS and LOG,
  124. are built into REDUCE. Moreover, the letter E represents the base of
  125. the natural logarithms, so the exponentiation operator enables us to
  126. represent the exponential function as well as fractional powers. For
  127. example:;
  128. E1:= SIN(-F*G) + LOG(E) + (3*G**2*COS(-1))**(1/2);
  129. COMMENT What automatic simplifications can you identify in this
  130. example?
  131. Note that most REDUCE implementations do not approximate the values
  132. of these functions for non-trivial numerical arguments, and exact
  133. computations are generally impossible for such cases.
  134. Experimentally determine some other built-in simplifications for
  135. these functions;
  136. PAUSE;
  137. COMMENT Later you will learn how to introduce additional
  138. simplifications and additional functions, including numerical
  139. approximations for examples such as COS(1).
  140. Differentiation is also built-into REDUCE. For example, to
  141. differentiate E1 with respect to F;
  142. E2 := DF(E1,F);
  143. COMMENT To compute the second derivative of E2 with respect to G, we
  144. can type either DF(E2,G,2) or DF(E1,F,1,G,2) or DF(E1,F,G,2) or
  145. DF(E1,G,2,F,1) or;
  146. DF(E1,G,2,F);
  147. COMMENT Surely you can't resist trying a few derivatives of your
  148. own! (Careful, High-order derivatives can be alarmingly complicated);
  149. PAUSE;
  150. COMMENT REDUCE uses the name I to represent (-1)**(1/2),
  151. incorporating some simplification rules such as replacing I**2 by -1.
  152. Here is an opportunity to experimentally determine other
  153. simplifications such as for I**3, 1/I**23, and (I**2-1)/(I-1);
  154. PAUSE;
  155. COMMENT Clearly it is inadvisable to use E or I as a variable. T is
  156. also inadvisable for reasons that will become clear later.
  157. The value of a variable is said to be "bound" to the variable. Any
  158. variable to which we have assigned a value is called a bound variable,
  159. and any variable to which we have not assigned a value is called an
  160. indeterminate. Occasionally it is desirable to make a bound variable
  161. into an indeterminate, and this can be done using the CLEAR command.
  162. For example:;
  163. CLEAR R2D2, E1, E2;
  164. E2;
  165. COMMENT If you suspect that a degenerate assignment, such as E1:=E1,
  166. would suffice to clear a bound variable, try it on one of your own
  167. bound variables:;
  168. PAUSE;
  169. COMMENT REDUCE also supports matrix algebra, as illustrated by the
  170. following sequence:;
  171. MATRIX E1(4,1), F, H;
  172. COMMENT This declaration establishes E1 as a matrix with 4 rows and 1
  173. column, while establishing F and H as matrices of unspecified size.
  174. To establish element values (and sizes if not already established in
  175. the MATRIX declaration), we can use the MAT function, as illustrated
  176. by the following example:;
  177. H := MAT((LOG(G), G+3), (G, 5/7));
  178. COMMENT Only after establishing the size and establishing the element
  179. values of a declared matrix by executing a matrix assignment can we
  180. refer to an individual element or to the matrix as a whole. For
  181. example to increase the last element of H by 1 then form twice the
  182. transpose of H, we can type;
  183. H(2,2) := H(2,2) + 1;
  184. 2*TP(H);
  185. COMMENT To compute the determinant of H:;
  186. DET(H);
  187. COMMENT To compute the trace of H:;
  188. TRACE(H);
  189. COMMENT To compute the inverse of H, we can type H**(-1) or 1/H. To
  190. compute the solution to the equation H*F = MAT((G),(2)), we can
  191. left-multiply the right-hand side by the inverse of H:;
  192. F := 1/H*MAT((G),(2));
  193. COMMENT Notes:
  194. 1. MAT((G),(2))/H would denote right-multiplication by the
  195. inverse, which is not what we want.
  196. 2. Solutions for a set of right-hand-side vectors are most
  197. efficiently computed simultaneously by collecting the right-
  198. hand sides together as the columns of a single multiple-column
  199. matrix.
  200. 3. Subexpressions of the form 1/H*... or H**(-1)*... are computed
  201. more efficiently than if the inverse is computed separately in
  202. a previous statement, so separate computation of the inverse
  203. is advisable only if several solutions are desired and if
  204. they cannot be computed simultaneously.
  205. 4. MAT must have parentheses around each row of elements even if
  206. there is only one row or only one element per row.
  207. 5. References to individual matrix elements must have exactly two
  208. subscripts, even if the matrix has only one row or one column.
  209. Congratulations on completing lesson 1! I urge you to try a sequence of
  210. more ambitious examples for the various features that have been
  211. introduced, in order to gain some familiarity with the relationship
  212. between problem size and computing time for various operations. (In most
  213. implementations, the command "ON TIME" causes computing time to be
  214. printed.) I also urge you to bring to the next lesson appropriate
  215. examples from textbooks, articles, or elsewhere, in order to experience
  216. the decisive learning reinforcement afforded by meaningful personal
  217. examples that are not arbitrarily contrived.
  218. To avoid the possibility of interference from assignments and declar-
  219. ations in lesson 1, it is wise to execute lesson 2 in a fresh REDUCE
  220. job, when you are ready.
  221. ;END;