subst.tex 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. \chapter{Substitution Commands}\index{Substitution}
  2. An important class of commands in {\REDUCE} define
  3. substitutions for variables and expressions to be made during the
  4. evaluation of expressions. Such substitutions use the prefix operator
  5. {\tt SUB}, various forms of the command {\tt LET}, and rule sets.
  6. \section{SUB Operator}\ttindex{SUB}
  7. Syntax:
  8. \begin{verbatim}
  9. SUB(<substitution_list>,EXPRN1:algebraic):algebraic
  10. \end{verbatim}
  11. where {\tt <substitution\_list>} is a list of one or more equations of the
  12. form
  13. \begin{verbatim}
  14. VAR:kernel=EXPRN:algebraic
  15. \end{verbatim}
  16. or a kernel that evaluates to such a list.
  17. The {\tt SUB} operator gives the algebraic result of replacing every
  18. occurrence of the variable {\tt VAR} in the expression {\tt EXPRN1} by the
  19. expression {\tt EXPRN}. Specifically, {\tt EXPRN1} is first evaluated
  20. using all available rules. Next the substitutions are made, and finally
  21. the substituted expression is reevaluated. When more than one variable
  22. occurs in the substitution list, the substitution is performed by
  23. recursively walking down the tree representing {\tt EXPRN1}, and replacing
  24. every {\tt VAR} found by the appropriate {\tt EXPRN}. The {\tt EXPRN} are
  25. not themselves searched for any occurrences of the various {\tt VAR}s.
  26. The trivial case {\tt SUB(EXPRN1)} returns the algebraic value of
  27. {\tt EXPRN1}.
  28. {\it Examples:}
  29. \begin{verbatim}
  30. 2 2
  31. sub({x=a+y,y=y+1},x^2+y^2) -> A + 2*A*Y + 2*Y + 2*Y + 1
  32. \end{verbatim}
  33. and with {\tt s := \{x=a+y,y=y+1\}},
  34. \begin{verbatim}
  35. 2 2
  36. sub(s,x^2+y^2) -> A + 2*A*Y + 2*Y + 2*Y + 1
  37. \end{verbatim}
  38. Note that the global assignments {\tt x:=a+y}, etc., do not take place.
  39. {\tt EXPRN1} can be any valid algebraic expression whose type is such that
  40. a substitution process is defined for it (e.g., scalar expressions, lists
  41. and matrices). An error will occur if an expression of an invalid type
  42. for substitution occurs either in {\tt EXPRN} or {\tt EXPRN1}.
  43. The braces around the substitution list may also be omitted, as in:
  44. \begin{verbatim}
  45. 2 2
  46. sub(x=a+y,y=y+1,x^2+y^2) -> A + 2*A*Y + 2*Y + 2*Y + 1
  47. \end{verbatim}
  48. \section{LET Rules}\ttindex{LET}
  49. Unlike substitutions introduced via {\tt SUB}, {\tt LET}
  50. rules are global in scope and stay in effect until replaced or {\tt CLEAR}ed.
  51. The simplest use of the {\tt LET} statement is in the form
  52. \begin{verbatim}
  53. LET <substitution list>
  54. \end{verbatim}
  55. where {\tt <substitution list>} is a list of rules separated by commas, each
  56. of the form:
  57. \begin{verbatim}
  58. <variable> = <expression>
  59. \end{verbatim}
  60. or
  61. \begin{verbatim}
  62. <prefix operator>(<argument>,...,<argument>) = <expression>
  63. \end{verbatim}
  64. or
  65. \begin{verbatim}
  66. <argument> <infix operator>,..., <argument> = <expression>
  67. \end{verbatim}
  68. For example,
  69. \begin{verbatim}
  70. let {x => y^2,
  71. h(u,v) => u - v,
  72. cos(pi/3) => 1/2,
  73. a*b => c,
  74. l+m => n,
  75. w^3 => 2*z - 3,
  76. z^10 => 0}
  77. \end{verbatim}
  78. The list brackets can be left out if preferred. The above rules could
  79. also have been entered as seven separate {\tt LET} statements.
  80. After such {\tt LET} rules have been input, {\tt X} will always be
  81. evaluated as the square of {\tt Y}, and so on. This is so even if at the
  82. time the {\tt LET} rule was input, the variable {\tt Y} had a value other
  83. than {\tt Y}. (In contrast, the assignment {\tt x:=y\verb|^|2} will set {\tt X}
  84. equal to the square of the current value of {\tt Y}, which could be quite
  85. different.)
  86. The rule {\tt let a*b=c} means that whenever {\tt A} and {\tt B} are both
  87. factors in an expression their product will be replaced by {\tt C}. For
  88. example, {\tt a\verb|^|5*b\verb|^|7*w} would be replaced by
  89. {\tt c\verb|^|5*b\verb|^|2*w}.
  90. The rule for {\tt l+m} will not only replace all occurrences of {\tt l+m}
  91. by {\tt N}, but will also normally replace {\tt L} by {\tt n-m}, but not
  92. {\tt M} by {\tt n-l}. A more complete description of this case is given
  93. in Section~\ref{sec-gensubs}.
  94. The rule pertaining to {\tt w\verb|^|3} will apply to any power of {\tt W}
  95. greater than or equal to the third.
  96. Note especially the last example, {\tt let z\verb|^|10=0}. This declaration
  97. means, in effect: ignore the tenth or any higher power of {\tt Z}. Such
  98. declarations, when appropriate, often speed up a computation to a
  99. considerable degree. (See\index{Asymptotic command}
  100. Section~\ref{sec-asymp} for more details.)
  101. Any new operators occurring in such {\tt LET} rules will be automatically
  102. declared {\tt OPERATOR} by the system, if the rules are being read from a
  103. file. If they are being entered interactively, the system will ask
  104. {\tt DECLARE} ... {\tt OPERATOR?} . Answer {\tt Y} or {\tt N} and hit
  105. \key{Return}.
  106. In each of these examples, substitutions are only made for the explicit
  107. expressions given; i.e., none of the variables may be considered arbitrary
  108. in any sense. For example, the command
  109. \begin{verbatim}
  110. let h(u,v) = u - v;
  111. \end{verbatim}
  112. will cause {\tt h(u,v)} to evaluate to {\tt U - V}, but will not affect
  113. {\tt h(u,z)} or {\tt H} with any arguments other than precisely the
  114. symbols {\tt U,V}.
  115. These simple {\tt LET} rules are on the same logical level as assignments
  116. made with the := operator. An assignment {\tt x := p+q} cancels a rule
  117. {\tt let x = y\verb|^|2} made earlier, and vice versa.
  118. {\it CAUTION:} A recursive rule such as
  119. \begin{verbatim}
  120. let x = x + 1;
  121. \end{verbatim}
  122. is erroneous, since any subsequent evaluation of {\tt X} would lead to a
  123. non-terminating chain of substitutions:
  124. \begin{verbatim}
  125. x -> x + 1 -> (x + 1) + 1 -> ((x + 1) + 1) + 1 -> ...
  126. \end{verbatim}
  127. Similarly, coupled substitutions such as
  128. \begin{verbatim}
  129. let l = m + n, n = l + r;
  130. \end{verbatim}
  131. would lead to the same error. As a result, if you try to evaluate an {\tt X},
  132. {\tt L} or {\tt N} defined as above, you will get an error such as
  133. \begin{verbatim}
  134. X improperly defined in terms of itself
  135. \end{verbatim}
  136. Array and matrix elements can appear on the left-hand side of a {\tt LET}
  137. statement. However, because of their {\em instant evaluation\/}
  138. \index{Instant evaluation} property, it is the value of the element that
  139. is substituted for, rather than the element itself. E.g.,
  140. \begin{verbatim}
  141. array a(5);
  142. a(2) := b;
  143. let a(2) = c;
  144. \end{verbatim}
  145. results in {\tt B} being substituted by {\tt C}; the assignment for
  146. {\tt a(2)} does not change.
  147. Finally, if an error occurs in any equation in a {\tt LET} statement
  148. (including generalized statements involving {\tt FOR ALL} and {\tt SUCH
  149. THAT)}, the remaining rules are not evaluated.
  150. \subsection{FOR ALL \ldots LET}\ttindex{FOR ALL}
  151. If a substitution for all possible values of a given argument of an
  152. operator is required, the declaration {\tt FOR ALL} may be used. The
  153. syntax of such a command is
  154. \begin{verbatim}
  155. FOR ALL <variable>,...,<variable>
  156. <LET statement> <terminator>
  157. \end{verbatim}
  158. e.g.,
  159. \begin{verbatim}
  160. for all x,y let h(x,y) = x-y;
  161. for all x let k(x,y) = x^y;
  162. \end{verbatim}
  163. The first of these declarations would cause {\tt h(a,b)} to be evaluated
  164. as {\tt A-B}, {\tt h(u+v,u+w)} to be {\tt V-W}, etc. If the operator
  165. symbol {\tt H} is used with more or fewer argument places, not two, the
  166. {\tt LET} would have no effect, and no error would result.
  167. The second declaration would cause {\tt k(a,y)} to be evaluated as
  168. {\tt a\verb|^|y}, but would have no effect on {\tt k(a,z)} since the rule
  169. didn't say {\tt FOR ALL Y} ... .
  170. Where we used {\tt X} and {\tt Y} in the examples, any variables could
  171. have been used. This use of a variable doesn't affect the value it may
  172. have outside the {\tt LET} statement. However, you should remember what
  173. variables you actually used. If you want to delete the rule subsequently,
  174. you must use the same variables in the {\tt CLEAR} command.
  175. It is possible to use more complicated expressions as a template for a
  176. {\tt LET} statement, as explained in the section on substitutions for
  177. general expressions. In nearly all cases, the rule will be accepted, and
  178. a consistent application made by the system. However, if there is a sole
  179. constant or a sole free variable on the left-hand side of a rule (e.g.,
  180. {\tt let 2=3} or {\tt for all x let x=2)}, then the system is unable to
  181. handle the rule, and the error message
  182. \begin{verbatim}
  183. Substitution for ... not allowed
  184. \end{verbatim}
  185. will be issued. Any variable listed in the {\tt FOR ALL} part will have
  186. its symbol preceded by an equal sign: {\tt X} in the above example will
  187. appear as {\tt =X}. An error will also occur if a variable in the
  188. {\tt FOR ALL} part is not properly matched on both sides of the {\tt LET}
  189. equation.
  190. \subsection{FOR ALL \ldots SUCH THAT \ldots LET}
  191. \ttindex{FOR ALL}\ttindex{SUCH THAT}
  192. If a substitution is desired for more than a single value of a variable in
  193. an operator or other expression, but not all values, a conditional form of
  194. the {\tt FOR ALL \ldots LET} declaration can be used.
  195. {\it Example:}
  196. \begin{verbatim}
  197. for all x such that numberp x and x<0 let h(x)=0;
  198. \end{verbatim}
  199. will cause {\tt h(-5)} to be evaluated as 0, but {\tt H} of a positive
  200. integer, or of an argument that is not an integer at all, would not be
  201. affected. Any boolean expression can follow the {\tt SUCH THAT} keywords.
  202. \subsection{Removing Assignments and Substitution Rules}\ttindex{CLEAR}
  203. The user may remove all assignments and substitution rules from any
  204. expression by the command {\tt CLEAR}, in the form
  205. \begin{verbatim}
  206. CLEAR <expression>,...,<expression><terminator>
  207. \end{verbatim}
  208. e.g.
  209. \begin{verbatim}
  210. clear x, h(x,y);
  211. \end{verbatim}
  212. Because of their {\em instant evaluation\/} property, array and matrix elements
  213. cannot be cleared with {\tt CLEAR}. For example, if {\tt A} is an array,
  214. you must say
  215. \begin{verbatim}
  216. a(3) := 0;
  217. \end{verbatim}
  218. rather than
  219. \begin{verbatim}
  220. clear a(3);
  221. \end{verbatim}
  222. to ``clear'' element {\tt a(3)}.
  223. On the other hand, a whole array (or matrix) {\tt A} can be cleared by the
  224. command {\tt clear a}; This means much more than resetting to 0 all the
  225. elements of {\tt A}. The fact that {\tt A} is an array, and what its
  226. dimensions are, are forgotten, so {\tt A} can be redefined as another type
  227. of object, for example an operator.
  228. The more general types of {\tt LET} declarations can also be deleted by
  229. using {\tt CLEAR}. Simply repeat the {\tt LET} rule to be deleted, using
  230. {\tt CLEAR} in place of {\tt LET}, and omitting the equal sign and
  231. right-hand part. The same dummy variables must be used in the {\tt FOR
  232. ALL} part, and the boolean expression in the {\tt SUCH THAT} part must be
  233. written the same way. (The placing of blanks doesn't have to be
  234. identical.)
  235. {\it Example:} The {\tt LET} rule
  236. \begin{verbatim}
  237. for all x such that numberp x and x<0 let h(x)=0;
  238. \end{verbatim}
  239. can be erased by the command
  240. \begin{verbatim}
  241. for all x such that numberp x and x<0 clear h(x);
  242. \end{verbatim}
  243. \subsection{Overlapping LET Rules}
  244. {\tt CLEAR} is not the only way to delete a {\tt LET} rule. A new {\tt
  245. LET} rule identical to the first, but with a different expression after
  246. the equal sign, replaces the first. Replacements are also made in other
  247. cases where the existing rule would be in conflict with the new rule. For
  248. example, a rule for {\tt x\verb|^|4} would replace a rule for {\tt x\verb|^|5}.
  249. The user should however be cautioned against having several {\tt LET}
  250. rules in effect that relate to the same expression. No guarantee can be
  251. given as to which rules will be applied by {\REDUCE} or in what order. It
  252. is best to {\tt CLEAR} an old rule before entering a new related {\tt LET}
  253. rule.
  254. \subsection{Substitutions for General Expressions}
  255. \label{sec-gensubs}
  256. The examples of substitutions discussed in other sections have involved
  257. very simple rules. However, the substitution mechanism used in {\REDUCE} is
  258. very general, and can handle arbitrarily complicated rules without
  259. difficulty.
  260. The general substitution mechanism used in {\REDUCE} is discussed in Hearn, A.
  261. C., ``{\REDUCE}, A User-Oriented Interactive System for Algebraic
  262. Simplification,'' Interactive Systems for Experimental Applied Mathematics,
  263. (edited by M. Klerer and J. Reinfelds), Academic Press, New York (1968),
  264. 79-90, and Hearn. A. C., ``The Problem of Substitution,'' Proc. 1968 Summer
  265. Institute on Symbolic Mathematical Computation, IBM Programming Laboratory
  266. Report FSC 69-0312 (1969). For the reasons given in these
  267. references, {\REDUCE} does not attempt to implement a general pattern
  268. matching algorithm. However, the present system uses far more sophisticated
  269. techniques than those discussed in the above papers. It is now possible for
  270. the rules appearing in arguments of {\tt LET} to have the form
  271. \begin{verbatim}
  272. <substitution expression> = <expression>
  273. \end{verbatim}
  274. where any rule to which a sensible meaning can be assigned is permitted.
  275. However, this meaning can vary according to the form of {\tt <substitution
  276. expression>}. The semantic rules associated with the application of the
  277. substitution are completely consistent, but somewhat complicated by the
  278. pragmatic need to perform such substitutions as efficiently as possible.
  279. The following rules explain how the majority of the cases are handled.
  280. To begin with, the {\tt <substitution expression>} is first partly
  281. simplified by collecting like terms and putting identifiers (and kernels)
  282. in the system order. However, no substitutions are performed on any part
  283. of the expression with the exception of expressions with the {\em instant
  284. evaluation\/} property, such as array and matrix elements, whose actual
  285. values are used. It should also be noted that the system order used is
  286. not changeable by the user, even with the {\tt KORDER} command. Specific
  287. cases are then handled as follows:
  288. \begin{enumerate}
  289. \item If the resulting simplified rule has a left-hand side that is an
  290. identifier, an expression with a top-level algebraic operator or a power,
  291. then the rule is added without further change to the appropriate table.
  292. \item If the operator * appears at the top level of the simplified left-hand
  293. side, then any constant arguments in that expression are moved to the
  294. right-hand side of the rule. The remaining left-hand side is then added
  295. to the appropriate table. For example,
  296. \begin{verbatim}
  297. let 2*x*y=3
  298. \end{verbatim}
  299. becomes
  300. \begin{verbatim}
  301. let x*y=3/2
  302. \end{verbatim}
  303. so that {\tt x*y} is added to the product substitution table, and when
  304. this rule is applied, the expression {\tt x*y} becomes 3/2, but {\tt X} or
  305. {\tt Y} by themselves are not replaced.
  306. \item If the operators {\tt +}, {\tt -} or {\tt /} appear at the top level
  307. of the simplified left-hand side, all but the first term is moved to the
  308. right-hand side of the rule. Thus the rules
  309. \begin{verbatim}
  310. let l+m=n, x/2=y, a-b=c
  311. \end{verbatim}
  312. become
  313. \begin{verbatim}
  314. let l=n-m, x=2*y, a=c+b.
  315. \end{verbatim}
  316. \end{enumerate}
  317. One problem that can occur in this case is that if a quantified expression
  318. is moved to the right-hand side, a given free variable might no longer
  319. appear on the left-hand side, resulting in an error because of the
  320. unmatched free variable. E.g.,
  321. \begin{verbatim}
  322. for all x,y let f(x)+f(y)=x*y
  323. \end{verbatim}
  324. would become
  325. \begin{verbatim}
  326. for all x,y let f(x)=x*y-f(y)
  327. \end{verbatim}
  328. which no longer has {\tt Y} on both sides.
  329. The fact that array and matrix elements are evaluated in the left-hand side
  330. of rules can lead to confusion at times. Consider for example the
  331. statements
  332. \begin{verbatim}
  333. array a(5); let x+a(2)=3; let a(3)=4;
  334. \end{verbatim}
  335. The left-hand side of the first rule will become {\tt X}, and the second
  336. 0. Thus the first rule will be instantiated as a substitution for
  337. {\tt X}, and the second will result in an error.
  338. The order in which a list of rules is applied is not easily understandable
  339. without a detailed knowledge of the system simplification protocol. It is
  340. also possible for this order to change from release to release, as improved
  341. substitution techniques are implemented. Users should therefore assume
  342. that the order of application of rules is arbitrary, and program
  343. accordingly.
  344. After a substitution has been made, the expression being evaluated is
  345. reexamined in case a new allowed substitution has been generated. This
  346. process is continued until no more substitutions can be made.
  347. As mentioned elsewhere, when a substitution expression appears in a
  348. product, the substitution is made if that expression divides the product.
  349. For example, the rule
  350. \begin{verbatim}
  351. let a^2*c = 3*z;
  352. \end{verbatim}
  353. would cause {\tt a\verb|^|2*c*x} to be replaced by {\tt 3*Z*X} and
  354. {\tt a\verb|^|2*c\verb|^|2} by {\tt 3*Z*C}. If the substitution is desired only
  355. when the substitution expression appears in a product with the explicit
  356. powers supplied in the rule, the command {\tt MATCH} should be used
  357. instead.\ttindex{MATCH}
  358. For example,
  359. \begin{verbatim}
  360. match a^2*c = 3*z;
  361. \end{verbatim}
  362. would cause {\tt a\verb|^|2*c*x} to be replaced by {\tt 3*Z*X}, but
  363. {\tt a\verb|^|2*c\verb|^|2} would not be replaced. {\tt MATCH} can also be used
  364. with the {\tt FOR ALL} constructions described above.
  365. To remove substitution rules of the type discussed in this section, the
  366. {\tt CLEAR}\ttindex{CLEAR} command can be used, combined, if necessary,
  367. with the same {\tt FOR ALL} clause with which the rule was defined, for
  368. example:
  369. \begin{verbatim}
  370. for all x clear log(e^x),e^log(x),cos(w*t+theta(x));
  371. \end{verbatim}
  372. Note, however, that the arbitrary variable names in this case {\em must\/}
  373. be the same as those used in defining the substitution.
  374. \section{Rule Lists} \index{Rule lists}
  375. Rule lists offer an alternative approach to defining substitutions that is
  376. different from either {\tt SUB} or {\tt LET}. In fact, they provide the
  377. best features of both, since they have all the capabilities of {\tt LET},
  378. but the rules can also be applied locally as is possible with {\tt SUB}.
  379. In time, they will be used more and more in {\REDUCE}. However, since they
  380. are relatively new, much of the {\REDUCE} code you see uses the older
  381. constructs.
  382. A rule list is a list of {\em rules\/} that have the syntax
  383. \begin{verbatim}
  384. <expression> => <expression> (WHEN <boolean expression>)
  385. \end{verbatim}
  386. For example,
  387. \begin{verbatim}
  388. {cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2,
  389. cos(~n*pi) => (-1)^n when remainder(n,2)=0}
  390. \end{verbatim}
  391. The tilde preceding a variable marks that variable as {\em free\/} for that
  392. rule, much as a variable in a {\tt FOR ALL} clause in a {\tt LET}
  393. statement. The first occurrence of that variable in each relevant rule
  394. must be so marked on input, otherwise inconsistent results can occur.
  395. For example, the rule list
  396. \begin{verbatim}
  397. {cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2,
  398. cos(x)^2 => (1+cos(2x))/2}
  399. \end{verbatim}
  400. designed to replace products of cosines, would not be correct, since the
  401. second rule would only apply to the explicit argument {\tt X}. Later
  402. occurrences in the same rule may also be marked, but this is optional
  403. (internally, all such rules are stored with each relevant variable
  404. explicitly marked). The optional {\tt WHEN}\ttindex{WHEN} clause allows
  405. constraints to be placed on the application of the rule, much as the {\tt
  406. SUCH THAT} clause in a {\tt LET} statement.
  407. A rule list may be named, for example
  408. \begin{verbatim}
  409. trig1 := {cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2,
  410. cos(~x)*sin(~y) => (sin(x+y)-sin(x-y))/2,
  411. sin(~x)*sin(~y) => (cos(x-y)-cos(x+y))/2,
  412. cos(~x)^2 => (1+cos(2*x))/2,
  413. sin(~x)^2 => (1-cos(2*x))/2};
  414. \end{verbatim}
  415. Such named rule lists may be inspected as needed. E.g., the command
  416. {\tt trig1;} would cause the above list to be printed.
  417. Rule lists may be used in two ways. They can be globally instantiated by
  418. means of the command {\tt LET}.\ttindex{LET} For example,
  419. \begin{verbatim}
  420. let trig1;
  421. \end{verbatim}
  422. would cause the above list of rules to be globally active from then on until
  423. cancelled by the command {\tt CLEARRULES},\ttindex{CLEARRULES} as in
  424. \begin{verbatim}
  425. clearrules trig1;
  426. \end{verbatim}
  427. {\tt CLEARRULES} has the syntax
  428. \begin{verbatim}
  429. CLEARRULES <rule list>|<name of rule list>(,...) .
  430. \end{verbatim}
  431. The second way to use rule lists is to invoke them locally by means of a
  432. {\tt WHERE}\ttindex{WHERE} clause. For example
  433. \begin{verbatim}
  434. cos(a)*cos(b+c)
  435. where {cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2};
  436. \end{verbatim}
  437. or
  438. \begin{verbatim}
  439. cos(a)*sin(b) where trigrules;
  440. \end{verbatim}
  441. The syntax of an expression with a {\tt WHERE} clause is:
  442. \begin{verbatim}
  443. <expression>
  444. WHERE <rule>|<rule list>(,<rule>|<rule list> ...)
  445. \end{verbatim}
  446. so the first example above could also be written
  447. \begin{verbatim}
  448. cos(a)*cos(b+c)
  449. where cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2;
  450. \end{verbatim}
  451. The effect of this construct is that the rule list(s) in the {\tt WHERE}
  452. clause only apply to the expression on the left of {\tt WHERE}. They have
  453. no effect outside the expression. In particular, they do not affect
  454. previously defined {\tt WHERE} clauses or {\tt LET} statements. For
  455. example, the sequence
  456. \begin{verbatim}
  457. let a=2;
  458. a where a=>4;
  459. a;
  460. \end{verbatim}
  461. would result in the output
  462. \begin{verbatim}
  463. 4
  464. 2
  465. \end{verbatim}
  466. Although {\tt WHERE} has a precedence less than any other infix operator,
  467. it still binds higher than keywords such as {\tt ELSE}, {\tt THEN},
  468. {\tt DO}, {\tt REPEAT} and so on. Thus the expression
  469. \begin{verbatim}
  470. if a=2 then 3 else a+2 where a=3
  471. \end{verbatim}
  472. will parse as
  473. \begin{verbatim}
  474. if a=2 then 3 else (a+2 where a=3)
  475. \end{verbatim}
  476. {\tt WHERE} may be used to introduce auxiliary variables in symbolic mode
  477. expressions, as described in Section~\ref{sec-lambda}. However, the
  478. symbolic mode use has different semantics, so expressions do not carry
  479. from one mode to the other.
  480. \COMPATNOTE In order to provide compatibility with older versions of rule
  481. lists released through the Network Library, it is currently possible to use
  482. an equal sign interchangeably with the replacement sign {\tt =>} in rules
  483. and {\tt LET} statements. However, since this will change in future
  484. versions, the replacement sign is preferable in rules and the equal sign
  485. in non-rule-based {\tt LET} statements.
  486. \subsection*{Advanced Use of Rule Lists}
  487. Some advanced features of the rule list mechanism make it possible to
  488. write more complicated rules than those discussed so far, and in many
  489. cases to write more compact rule lists. These features are:
  490. \begin{itemize}
  491. \item Free operators
  492. \item Double slash operator
  493. \item Double tilde variables.
  494. \end{itemize}
  495. A {\bf free operator} in the left hand side of a pattern will match any
  496. operator with the same number of arguments. The free operator is written
  497. in the same style as a variable. For example, the implementation of the
  498. product rule of differentiation can be written as:
  499. \begin{verbatim}
  500. operator diff, !~f, !~g;
  501. prule := {diff(~f(~x) * ~g(~x),x) =>
  502. diff(f(x),x) * g(x) + diff(g(x),x) * f(x)};
  503. let prule;
  504. diff(sin(z)*cos(z),z);
  505. cos(z)*diff(sin(z),z) + diff(cos(z),z)*sin(z)
  506. \end{verbatim}
  507. The {\bf double slash operator} may be used as an alternative to a single
  508. slash (quotient) in order to match quotients properly. E.g., in the
  509. example of the Gamma function above, one can use:
  510. \begin{verbatim}
  511. gammarule :=
  512. {gamma(~z)//(~c*gamma(~zz)) => gamma(z)/(c*gamma(zz-1)*zz)
  513. when fixp(zz -z) and (zz -z) >0,
  514. gamma(~z)//gamma(~zz) => gamma(z)/(gamma(zz-1)*zz)
  515. when fixp(zz -z) and (zz -z) >0};
  516. let gammarule;
  517. gamma(z)/gamma(z+3);
  518. 1
  519. ----------------------
  520. 3 2
  521. z + 6*z + 11*z + 6
  522. \end{verbatim}
  523. The above example suffers from the fact that two rules had to be
  524. written in order to perform the required operation. This can be simplified
  525. by the use of {\bf double tilde variables}. E.g. the rule list
  526. \begin{verbatim}
  527. GGrule := {
  528. gamma(~z)//(~~c*gamma(~zz)) => gamma(z)/(c*gamma(zz-1)*zz)
  529. when fixp(zz -z) and (zz -z) >0};
  530. \end{verbatim}
  531. will implement the same operation in a much more compact way.
  532. In general, double tilde variables are bound to the neutral element
  533. with respect to the operation in which they are used.
  534. \begin{tabular}{lll}
  535. Pattern given & Argument used & Binding \\
  536. \\
  537. \symbol{126}z + \symbol{126}\symbol{126}y & x & z=x; y=0 \\
  538. \symbol{126}z + \symbol{126}\symbol{126}y & x+3 & z=x; y=3 or z=3; y=x \\
  539. \\
  540. \symbol{126}z * \symbol{126}\symbol{126}y & x & z=x; y=1\\
  541. \symbol{126}z * \symbol{126}\symbol{126}y & x*3 & z=x; y=3 or z=3; y=x\\
  542. \\
  543. \symbol{126}z / \symbol{126}\symbol{126}y & x & z=x; y=1\\
  544. \symbol{126}z / \symbol{126}\symbol{126}y & x/3 & z=x; y=3 \\
  545. \\
  546. \end{tabular}
  547. Remarks: A double tilde variable as the numerator of a pattern is not allowed.
  548. Also, using double tilde variables may lead to recursion errors when the
  549. zero case is not handled properly.
  550. \begin{verbatim}
  551. let f(~~a * ~x,x) => a * f(x,x) when freeof (a,x);
  552. f(z,z);
  553. ***** f(z,z) improperly defined in terms of itself
  554. % BUT:
  555. let ff(~~a * ~x,x)
  556. => a * ff(x,x) when freeof (a,x) and a neq 1;
  557. ff(z,z);
  558. ff(z,z)
  559. ff(3*z,z);
  560. 3*ff(z,z)
  561. \end{verbatim}
  562. \subsection*{Displaying Rules Associated with an Operator}
  563. The operator {\tt SHOWRULES}\ttindex{SHOWRULES} takes a single identifier
  564. as argument, and returns in rule-list form the operator rules associated
  565. with that argument. For example:
  566. \begin{verbatim}
  567. showrules log;
  568. {LOG(E) => 1,
  569. LOG(1) => 0,
  570. ~X
  571. LOG(E ) => ~X,
  572. 1
  573. DF(LOG(~X),~X) => ----}
  574. ~X
  575. \end{verbatim}
  576. Such rules can then be manipulated further as with any list. For example
  577. {\tt rhs first ws;} has the value {\tt 1}. Note that an operator may
  578. have other properties that cannot be displayed in such a form, such as the
  579. fact it is an odd function, or has a definition defined as a procedure.
  580. \subsection*{Order of Application of Rules}
  581. If rules have overlapping domains, their order of application is
  582. important. In general, it is very difficult to specify this order
  583. precisely, so that it is best to assume that the order is arbitrary.
  584. However, if only one operator is involved, the order of application of the
  585. rules for this operator can be determined from the following:
  586. \begin{enumerate}
  587. \item Rules containing at least one free variable apply before all rules
  588. without free variables.
  589. \item Rules activated in the most recent {\tt LET}
  590. command are applied first.
  591. \item {\tt LET} with several entries generate
  592. the same order of application as a corresponding sequence of commands with
  593. one rule or rule set each.
  594. \item Within a rule set, the rules containing at least
  595. one free variable are applied in their given order.
  596. In other words, the first member of the list is applied first.
  597. \item Consistent with the first item, any rule in a rule list that
  598. contains no free variables is applied after all rules containing free
  599. variables.
  600. \end{enumerate}
  601. {\it Example:} The following rule set enables the computation of exact
  602. values of the Gamma function:
  603. \begin{verbatim}
  604. operator gamma,gamma_error;
  605. gamma_rules :=
  606. {gamma(~x)=>sqrt(pi)/2 when x=1/2,
  607. gamma(~n)=>factorial(n-1) when fixp n and n>0,
  608. gamma(~n)=>gamma_error(n) when fixp n,
  609. gamma(~x)=>(x-1)*gamma(x-1) when fixp(2*x) and x>1,
  610. gamma(~x)=>gamma(x+1)/x when fixp(2*x)};
  611. \end{verbatim}
  612. Here, rule by rule, cases of known or definitely uncomputable values
  613. are sorted out; e.g. the rule leading to the error expression
  614. will be applied for negative integers only, since the positive
  615. integers are caught by the preceding rule, and the
  616. last rule will apply for negative odd multiples of $1/2$ only.
  617. Alternatively the first rule could have been written as
  618. \begin{verbatim}
  619. gamma(1/2) => sqrt(pi)/2,
  620. \end{verbatim}
  621. but then the case $x=1/2$ should be excluded in the {\tt WHEN} part of the
  622. last rule explicitly because a rule without free variables cannot take
  623. precedence over the other rules.
  624. \section{Asymptotic Commands} \index{Asymptotic command}
  625. \label{sec-asymp}
  626. In expansions of polynomials involving variables that are known to be
  627. small, it is often desirable to throw away all powers of these variables
  628. beyond a certain point to avoid unnecessary computation. The command {\tt
  629. LET} may be used to do this. For example, if only powers of {\tt X} up to
  630. {\tt x\verb|^|7} are needed, the command
  631. \begin{verbatim}
  632. let x^8 = 0;
  633. \end{verbatim}
  634. will cause the system to delete all powers of {\tt X} higher than 7.
  635. {\it CAUTION:} This particular simplification works differently from most
  636. substitution mechanisms in {\REDUCE} in that it is applied during
  637. polynomial manipulation rather than to the whole evaluated expression.
  638. Thus, with the above rule in effect, {\tt x\verb|^|10/x\verb|^|5} would give the
  639. result zero, since the numerator would simplify to zero. Similarly
  640. {\tt x\verb|^|20/x\verb|^|10} would give a {\tt Zero divisor} error message,
  641. since both numerator and denominator would first simplify to zero.
  642. The method just described is not adequate when expressions involve several
  643. variables having different degrees of smallness. In this case, it is
  644. necessary to supply an asymptotic weight to each variable and count up the
  645. total weight of each product in an expanded expression before deciding
  646. whether to keep the term or not. There are two associated commands in the
  647. system to permit this type of asymptotic constraint. The command {\tt WEIGHT}
  648. \ttindex{WEIGHT}
  649. takes a list of equations of the form
  650. \begin{verbatim}
  651. <kernel form> = <number>
  652. \end{verbatim}
  653. where {\tt <number>} must be a positive integer (not just evaluate to a
  654. positive integer). This command assigns the weight {\tt <number>} to the
  655. relevant kernel form. A check is then made in all algebraic evaluations
  656. to see if the total weight of the term is greater than the weight level
  657. assigned to the calculation. If it is, the term is deleted. To compute
  658. the total weight of a product, the individual weights of each kernel form
  659. are multiplied by their corresponding powers and then added.
  660. The weight level of the system is initially set to 1. The user may change
  661. this setting by the command\ttindex{WTLEVEL}
  662. \begin{verbatim}
  663. wtlevel <number>;
  664. \end{verbatim}
  665. which sets {\tt <number>} as the new weight level of the system.
  666. {\tt <number>} must evaluate to a positive integer. WTLEVEL will also
  667. allow NIL as an argument, in which case the current weight level is returned.