home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / info / calc.info-22 (.txt) < prev    next >
GNU Info File  |  1994-12-22  |  47KB  |  774 lines

  1. This is Info file calc.info, produced by Makeinfo-1.55 from the input
  2. file calc.texinfo.
  3.    This file documents Calc, the GNU Emacs calculator.
  4.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  5.    Permission is granted to make and distribute verbatim copies of this
  6. manual provided the copyright notice and this permission notice are
  7. preserved on all copies.
  8.    Permission is granted to copy and distribute modified versions of
  9. this manual under the conditions for verbatim copying, provided also
  10. that the section entitled "GNU General Public License" is included
  11. exactly as in the original, and provided that the entire resulting
  12. derived work is distributed under the terms of a permission notice
  13. identical to this one.
  14.    Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions, except that the section entitled "GNU General Public License"
  17. may be included in a translation approved by the author instead of in
  18. the original English.
  19. File: calc.info,  Node: Automatic Rewrites,  Next: Debugging Rewrites,  Prev: Matching Commands,  Up: Rewrite Rules
  20. Automatic Rewrites
  21. ------------------
  22. It is possible to get Calc to apply a set of rewrite rules on all
  23. results, effectively adding to the built-in set of default
  24. simplifications.  To do this, simply store your rule set in the
  25. variable `EvalRules'.  There is a convenient `s E' command for editing
  26. `EvalRules'; *note Operations on Variables::..
  27.    For example, suppose you want `sin(a + b)' to be expanded out to
  28. `sin(b) cos(a) + cos(b) sin(a)' wherever it appears, and similarly for
  29. `cos(a + b)'.  The corresponding rewrite rule set would be,
  30.      [ sin(a + b)  :=  cos(a) sin(b) + sin(a) cos(b),
  31.        cos(a + b)  :=  cos(a) cos(b) - sin(a) sin(b) ]
  32.    To apply these manually, you could put them in a variable called
  33. `trigexp' and then use `a r trigexp' every time you wanted to expand
  34. trig functions.  But if instead you store them in the variable
  35. `EvalRules', they will automatically be applied to all sines and
  36. cosines of sums.  Then, with `2 x' and `45' on the stack, typing `+ S'
  37. will (assuming degrees mode) result in `0.7071 sin(2 x) + 0.7071 cos(2
  38. x)' automatically.
  39.    As each level of a formula is evaluated, the rules from `EvalRules'
  40. are applied before the default simplifications.  Rewriting continues
  41. until no further `EvalRules' apply.  Note that this is different from
  42. the usual order of application of rewrite rules:  `EvalRules' works
  43. from the bottom up, simplifying the arguments to a function before the
  44. function itself, while `a r' applies rules from the top down.
  45.    Because the `EvalRules' are tried first, you can use them to
  46. override the normal behavior of any built-in Calc function.
  47.    It is important not to write a rule that will get into an infinite
  48. loop.  For example, the rule set `[f(0) := 1, f(n) := n f(n-1)]'
  49. appears to be a good definition of a factorial function, but it is
  50. unsafe.  Imagine what happens if `f(2.5)' is simplified.  Calc will
  51. continue to subtract 1 from this argument forever without reaching
  52. zero.  A safer second rule would be `f(n) := n f(n-1) :: n>0'.  Another
  53. dangerous rule is `g(x, y) := g(y, x)'.  Rewriting `g(2, 4)', this
  54. would bounce back and forth between that and `g(4, 2)' forever.  If an
  55. infinite loop in `EvalRules' occurs, Emacs will eventually stop with a
  56. "Computation got stuck or ran too long" message.
  57.    Another subtle difference between `EvalRules' and regular rewrites
  58. concerns rules that rewrite a formula into an identical formula.  For
  59. example, `f(n) := f(floor(n))' "fails to match" when `n' is already an
  60. integer.  But in `EvalRules' this case is detected only if the
  61. righthand side literally becomes the original formula before any
  62. further simplification.  This means that `f(n) := f(floor(n))' will get
  63. into an infinite loop if it occurs in `EvalRules'.  Calc will replace
  64. `f(6)' with `f(floor(6))', which is different from `f(6)', so it will
  65. consider the rule to have matched and will continue simplifying that
  66. formula; first the argument is simplified to get `f(6)', then the rule
  67. matches again to get `f(floor(6))' again, ad infinitum.  A much safer
  68. rule would check its argument first, say, with `f(n) := f(floor(n)) ::
  69. !dint(n)'.
  70.    (What really happens is that the rewrite mechanism substitutes the
  71. meta-variables in the righthand side of a rule, compares to see if the
  72. result is the same as the original formula and fails if so, then uses
  73. the default simplifications to simplify the result and compares again
  74. (and again fails if the formula has simplified back to its original
  75. form).  The only special wrinkle for the `EvalRules' is that the same
  76. rules will come back into play when the default simplifications are
  77. used.  What Calc wants to do is build `f(floor(6))', see that this is
  78. different from the original formula, simplify to `f(6)', see that this
  79. is the same as the original formula, and thus halt the rewriting.  But
  80. while simplifying, `f(6)' will again trigger the same `EvalRules' rule
  81. and Calc will get into a loop inside the rewrite mechanism itself.)
  82.    The `phase', `schedule', and `iterations' markers do not work in
  83. `EvalRules'.  If the rule set is divided into phases, only the phase 1
  84. rules are applied, and the schedule is ignored.  The rules are always
  85. repeated as many times as possible.
  86.    The `EvalRules' are applied to all function calls in a formula, but
  87. not to numbers (and other number-like objects like error forms), nor to
  88. vectors or individual variable names.  (Though they will apply to
  89. *components* of vectors and error forms when appropriate.)  You might
  90. try to make a variable `phihat' which automatically expands to its
  91. definition without the need to press `=' by writing the rule
  92. `quote(phihat) := (1-sqrt(5))/2', but unfortunately this rule will not
  93. work as part of `EvalRules'.
  94.    Finally, another limitation is that Calc sometimes calls its built-in
  95. functions directly rather than going through the default
  96. simplifications.  When it does this, `EvalRules' will not be able to
  97. override those functions.  For example, when you take the absolute
  98. value of the complex number `(2, 3)', Calc computes `sqrt(2*2 + 3*3)'
  99. by calling the multiplication, addition, and square root functions
  100. directly rather than applying the default simplifications to this
  101. formula.  So an `EvalRules' rule that (perversely) rewrites `sqrt(13)
  102. := 6' would not apply.  (However, if you put Calc into symbolic mode so
  103. that `sqrt(13)' will be left in symbolic form by the built-in square
  104. root function, your rule will be able to apply.  But if the complex
  105. number were `(3,4)', so that `sqrt(25)' must be calculated, then
  106. symbolic mode will not help because `sqrt(25)' can be evaluated exactly
  107. to 5.)
  108.    One subtle restriction that normally only manifests itself with
  109. `EvalRules' is that while a given rewrite rule is in the process of
  110. being checked, that same rule cannot be recursively applied.  Calc
  111. effectively removes the rule from its rule set while checking the rule,
  112. then puts it back once the match succeeds or fails.  (The technical
  113. reason for this is that compiled pattern programs are not reentrant.)
  114. For example, consider the rule `foo(x) := x :: foo(x/2) > 0' attempting
  115. to match `foo(8)'.  This rule will be inactive while the condition
  116. `foo(4) > 0' is checked, even though it might be an integral part of
  117. evaluating that condition.  Note that this is not a problem for the
  118. more usual recursive type of rule, such as `foo(x) := foo(x/2)',
  119. because there the rule has succeeded and been reactivated by the time
  120. the righthand side is evaluated.
  121.    If `EvalRules' has no stored value (its default state), or if
  122. anything but a vector is stored in it, then it is ignored.
  123.    Even though Calc's rewrite mechanism is designed to compare rewrite
  124. rules to formulas as quickly as possible, storing rules in `EvalRules'
  125. may make Calc run substantially slower.  This is particularly true of
  126. rules where the top-level call is a commonly used function, or is not
  127. fixed.  The rule `f(n) := n f(n-1) :: n>0' will only activate the
  128. rewrite mechanism for calls to the function `f', but `lg(n) + lg(m) :=
  129. lg(n m)' will check every `+' operator