home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / info / calc.info-6 (.txt) < prev    next >
GNU Info File  |  1994-12-22  |  50KB  |  922 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: Programming Tutorial,  Next: Answers to Exercises,  Prev: Algebra Tutorial,  Up: Tutorial
  20. Programming Tutorial
  21. ====================
  22. The Calculator is written entirely in Emacs Lisp, a highly extensible
  23. language.  If you know Lisp, you can program the Calculator to do
  24. anything you like.  Rewrite rules also work as a powerful programming
  25. system.  But Lisp and rewrite rules take a while to master, and often
  26. all you want to do is define a new function or repeat a command a few
  27. times.  Calc has features that allow you to do these things easily.
  28.    (Note that the programming commands relating to user-defined keys
  29. are not yet supported under Lucid Emacs 19.)
  30.    One very limited form of programming is defining your own functions.
  31. Calc's `Z F' command allows you to define a function name and key
  32. sequence to correspond to any formula.  Programming commands use the
  33. shift-`Z' prefix; the user commands they create use the lower case `z'
  34. prefix.
  35.      1:  1 + x + x^2 / 2 + x^3 / 6         1:  1 + x + x^2 / 2 + x^3 / 6
  36.          .                                     .
  37.      
  38.          ' 1 + x + x^2/2! + x^3/3! RET         Z F e myexp RET RET RET y
  39.    This polynomial is a Taylor series approximation to `exp(x)'.  The
  40. `Z F' command asks a number of questions.  The above answers say that
  41. the key sequence for our function should be `z e'; the `M-x' equivalent
  42. should be `calc-myexp'; the name of the function in algebraic formulas
  43. should also be `myexp'; the default argument list `(x)' is acceptable;
  44. and finally `y' answers the question "leave it in symbolic form for
  45. non-constant arguments?"
  46.      1:  1.3495     2:  1.3495     3:  1.3495
  47.          .          1:  1.34986    2:  1.34986
  48.                         .          1:  myexp(a + 1)
  49.                                        .
  50.      
  51.          .3 z e         .3 E           ' a+1 RET z e
  52. First we call our new `exp' approximation with 0.3 as an argument, and
  53. compare it with the true `exp' function.  Then we note that, as
  54. requested, if we try to give `z e' an argument that isn't a plain
  55. number, it leaves the `myexp' function call in symbolic form.  If we
  56. had answered `n' to the final question, `myexp(a + 1)' would have
  57. evaluated by plugging in `a + 1' for `x' in the defining formula.
  58.    (*) *Exercise 1.*  The "sine integral" function `Si(x)' is defined
  59. as the integral of `sin(t)/t' for `t = 0' to `x' in radians.  (It was
  60. invented because this integral has no solution in terms of basic
  61. functions; if you give it to Calc's `a i' command, it will ponder it
  62. for a long time and then give up.)  We can use the numerical
  63. integration command, however, which in algebraic notation is written
  64. like `ninteg(f(t), t, 0, x)' with any integrand `f(t)'.  Define a `z s'
  65. command and `Si' function that implement this.  You will need to edit
  66. the default argument list a bit.  As a test, `Si(1)' should return
  67. 0.946083.  (Hint:  `ninteg' will run a lot faster if you reduce the
  68. precision to, say, six digits beforehand.) *Note 1: Programming Answer
  69. 1. (*)
  70.    The simplest way to do real "programming" of Emacs is to define a
  71. "keyboard macro".  A keyboard macro is simply a sequence of keystrokes
  72. which Emacs has stored away and can play back on demand.  For example,
  73. if you find yourself typing `H a S x RET' often, you may wish to
  74. program a keyboard macro to type this for you.
  75.      1:  y = sqrt(x)          1:  x = y^2
  76.          .                        .
  77.      
  78.          ' y=sqrt(x) RET       C-x ( H a S x RET C-x )
  79.      
  80.      1:  y = cos(x)           1:  x = s1 arccos(y) + 2 pi n1
  81.          .                        .
  82.      
  83.          ' y=cos(x) RET           X
  84. When you type `C-x (', Emacs begins recording.  But it is also still
  85. ready to execute your keystrokes, so you're really "training" Emacs by
  86. walking it through the procedure once.  When you type `C-x )', the
  87. macro is recorded.  You can now type `X' to re-execute the same
  88. keystrokes.
  89.    You can give a name to your macro by typing `Z K'.
  90.      1:  .              1:  y = x^4         1:  x = s2 sqrt(s1 sqrt(y))
  91.                             .                   .
  92.      
  93.        Z K x RET            ' y=x^4 RET         z x
  94. Notice that we use shift-`Z' to define the command, and lower-case `z'
  95. to call it up.
  96.    Keyboard macros can call other macros.
  97.      1:  abs(x)        1:  x = s1 y                1:  2 / x    1:  x = 2 / y
  98.          .                 .                           .            .
  99.      
  100.       ' abs(x) RET   C-x ( ' y RET a = z x C-x )    ' 2/x RET       X
  101.    (*) *Exercise 2.*  Define a keyboard macro to negate the item in
  102. level 3 of the stack, without disturbing the rest of the stack.  *Note
  103. 2: Programming Answer 2. (*)
  104.    (*) *Exercise 3.*  Define keyboard macros to compute the following
  105. functions:
  106.   1. Compute `sin(x) / x', where `x' is the number on the top of the
  107.      stack.
  108.   2. Compute the base-`b' logarithm, just like the `B' key except the
  109.      arguments are taken in the opposite order.
  110.   3. Produce a vector of integers from 1 to the integer on the top of
  111.      the stack.
  112. *Note 3: Programming Answer 3. (*)
  113.    (*) *Exercise 4.*  Define a keyboard macro to compute the average
  114. (mean) value of a list of numbers.  *Note 4: Programming Answer 4. (*)
  115.    In many programs, some of the steps must execute several times.
  116. Calc has "looping" commands that allow this.  Loops are useful inside
  117. keyboard macros, but actually work at any time.
  118.      1:  x^6          2:  x^6        1: 360 x^2
  119.          .            1:  4             .
  120.                           .
  121.      
  122.        ' x^6 RET          4         Z < a d x RET Z >
  123. Here we have computed the fourth derivative of `x^6' by enclosing a
  124. derivative command in a "repeat loop" structure.  This structure pops a
  125. repeat count from the stack, then executes the body of the loop that
  126. many times.
  127.    If you make a mistake while entering the body of the loop, type
  128. `Z C-g' to cancel the loop command.
  129.    Here's another example:
  130.      3:  1               2:  10946
  131.      2:  1               1:  17711
  132.      1:  20                  .
  133.          .
  134.      
  135.      1 RET RET 20       Z < TAB C-j + Z >
  136. The numbers in levels 2 and 1 should be the 21st and 22nd Fibonacci
  137. numbers, respectively.  (To see what's going on, try a few repetitions
  138. of the loop body by hand; `C-j', also on the Line-Feed or LFD key if
  139. you have one, makes a copy of the number in level 2.)
  140.    A fascinating property of the Fibonacci numbers is that the `n'th
  141. Fibonacci number can be found directly by computing `phi^n / sqrt(5)'
  142. and then rounding to the nearest integer, where `phi', the "golden
  143. ratio," is `(1 + sqrt(5)) / 2'.  (For convenience, this constant is
  144. available from the `phi' variable, or the `I H P' command.)
  145.      1:  1.61803         1:  24476.0000409    1:  10945.9999817    1:  10946
  146.          .                   .                    .                    .
  147.      
  148.          I H P               21 ^                 5 Q /                R
  149.    (*) *Exercise 5.*  The "continued fraction" representation of `phi'
  150. is `1 + 1/(1 + 1/(1 + 1/( ... )))'.  We can compute an approximate
  151. value by carrying this however far and then replacing the innermost
  152. `1/( ... )' by 1.  Approximate `phi' using a