home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / info / calc.info-7 (.txt) < prev    next >
GNU Info File  |  1994-12-22  |  50KB  |  1,003 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: List Answer 7,  Next: List Answer 8,  Prev: List Answer 6,  Up: Answers to Exercises
  20. List Tutorial Exercise 7
  21. ------------------------
  22. Here's one solution.  First, compute the triangular list from the
  23. previous exercise and type `1 -' to subtract one from all the elements.
  24.      1:  [ [0],
  25.            [0, 1],
  26.            [0, 1, 2],
  27.            ...
  28.      
  29.          1 -
  30.    The numbers down the lefthand edge of the list we desire are called
  31. the "triangular numbers" (now you know why!).  The `n'th triangular
  32. number is the sum of the integers from 1 to `n', and can be computed
  33. directly by the formula `n * (n+1) / 2'.
  34.      2:  [ [0], [0, 1], ... ]    2:  [ [0], [0, 1], ... ]
  35.      1:  [0, 1, 2, 3, 4, 5]      1:  [0, 1, 3, 6, 10, 15]
  36.          .                           .
  37.      
  38.          v x 6 RET 1 -               V M ' $ ($+1)/2 RET
  39. Adding this list to the above list of lists produces the desired result:
  40.      1:  [ [0],
  41.            [1, 2],
  42.            [3, 4, 5],
  43.            [6, 7, 8, 9],
  44.            [10, 11, 12, 13, 14],
  45.            [15, 16, 17, 18, 19, 20] ]
  46.            .
  47.      
  48.            V M +
  49.    If we did not know the formula for triangular numbers, we could have
  50. computed them using a `V U +' command.  We could also have gotten them
  51. the hard way by mapping a reduction across the original triangular list.
  52.      2:  [ [0], [0, 1], ... ]    2:  [ [0], [0, 1], ... ]
  53.      1:  [ [0], [0, 1], ... ]    1:  [0, 1, 3, 6, 10, 15]
  54.          .                           .
  55.      
  56.          RET                         V M V R +
  57. (This means "map a `V R +' command across the vector," and since each
  58. element of the main vector is itself a small vector, `V R +' computes
  59. the sum of its elements.)
  60. File: calc.info,  Node: List Answer 8,  Next: List Answer 9,  Prev: List Answer 7,  Up: Answers to Exercises
  61. List Tutorial Exercise 8
  62. ------------------------
  63. The first step is to build a list of values of `x'.
  64.      1:  [1, 2, 3, ..., 21]  1:  [0, 1, 2, ..., 20]  1:  [0, 0.25, 0.5, ..., 5]
  65.          .                       .                       .
  66.      
  67.          v x 21 RET              1 -                     4 /  s 1
  68.    Next, we compute the Bessel function values.
  69.      1:  [0., 0.124, 0.242, ..., -0.328]
  70.          .
  71.      
  72.          V M ' besJ(1,$) RET
  73. (Another way to do this would be `1 TAB V M f j'.)
  74.    A way to isolate the maximum value is to compute the maximum using
  75. `V R X', then compare all the Bessel values with that maximum.
  76.      2:  [0., 0.124, 0.242, ... ]   1:  [0, 0, 0, ... ]    2:  [0, 0, 0, ... ]
  77.      1:  0.5801562                      .                  1:  1
  78.          .                                                     .
  79.      
  80.          RET V R X                      V M a =                RET V R +    DEL
  81. It's a good idea to verify, as in the last step above, that only one
  82. value is equal to the maximum.  (After all, a plot of `sin(x)' might
  83. have many points all equal to the maximum value, 1.)
  84.    The vector we have now has a single 1 in the position that indicates
  85. the maximum value of `x'.  Now it is a simple matter to convert this
  86. back into the corresponding value itself.
  87.      2:  [0, 0, 0, ... ]         1:  [0, 0., 0., ... ]    1:  1.75
  88.      1:  [0, 0.25, 0.5, ... ]        .                        .
  89.          .
  90.      
  91.          r 1                         V M *                    V R +
  92.    If `a =' had produced more than one `1' value, this method would
  93. have given the sum of all maximum `x' values; not very useful!  In this
  94. case we could have used `v m' (`calc-mask-vector') instead.  This
  95. command deletes all elements of a "data" vector that correspond to
  96. zeros in a "mask" vector, leaving us with, in this example, a vector of
  97. maximum `x' values.
  98.    The built-in `a X' command maximizes a function using more efficient
  99. methods.  Just for illustration, let's use `a X' to maximize
  100. `besJ(1,x)' over this same interval.
  101.      2:  besJ(1, x)                 1:  [1.84115, 0.581865]
  102.      1:  [0 .. 5]                       .
  103.          .
  104.      
  105.      ' besJ(1,x), [0..5] RET            a X x RET
  106. The output from `a X' is a vector containing the value of `x' that
  107. maximizes the function, and the function's value at that maximum.  As
  108. you can see, our simple search got quite close to the right answer.
  109. File: calc.info,  Node: List Answer 9,  Next: List Answer 10,  Prev: List Answer 8,  Up: Answers to Exercises
  110. List Tutorial Exercise 9
  111. ------------------------
  112. Step one is to convert our integer into vector notation.
  113.      1:  25129925999           3:  25129925999
  114.          .                     2:  10
  115.                                1:  [11, 10, 9, ..., 1, 0]
  116.                                    .
  117.      
  118.          25129925999 RET           10 RET 12 RET v x 12 RET -
  119.      1:  25129925999              1:  [0, 2, 25, 251, 2512, ... ]
  120.      2:  [100000000000, ... ]         .
  121.          .
  122.      
  123.          V M ^   s 1                  V M \
  124. (Recall, the `\' command computes an integer quotient.)
  125.      1:  [0, 2, 5, 1, 2, 9, 9, 2, 5, 9, 9, 9]
  126.          .
  127.      
  128.          10 V M %   s 2
  129.    Next we must increment this number.  This involves adding one to the
  130. last digit, plus handling carries.  There is a carry to the left out of
  131. a digit if that digit is a nine and all the digits to the right of it
  132. are nines.
  133.      1:  [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1]   1:  [1, 1, 1, 0, 0, 1, ... ]
  134.          .                                          .
  135.      
  136.          9 V M a =                                  v v
  137.      1:  [1, 1, 1, 0, 0, 0, ... ]   1:  [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
  138.          .                              .
  139.      
  140.          V U *                          v v 1 |
  141. Accumulating `*' across a vector of ones and zeros will preserve only
  142. the initial run of ones.  These are the carries into all digits except
  143. the rightmost digit.  Concatenating a one on the right takes care of
  144. aligning the carries properly, and also adding one to the rightmost
  145. digit.
  146.      2:  [0, 0, 0, 0, ... ]     1:  [0, 0, 2, 5, 1, 2, 9, 9, 2, 6, 0, 0, 0]
  147.      1:  [0, 0, 2, 5, ... ]         .
  148.          .
  149.      
  150.          0 r 2 |                    V M +  10 V M %
  151. Here we have concatenated 0 to the *left* of the original number; this
  152. takes care of shifting the carries by one with respect to the digits
  153. that generated them.
  154.    Finally, we must convert this list back into an integer.
  155.      3:  [0, 0, 2, 5, ... ]        2:  [0, 0, 2, 5, ... ]
  156.      2:  1000000000000             1:  [1000000000000, 100000000000, ... ]
  157.      1:  [100000000000, ... ]          .
  158.          .
  159.      
  160.          10 RET 12 ^  r 1              |
  161.      1:  [0, 0, 20000000000, 5000000000, ... ]    1:  25129926000
  162.          .                                            .
  163.      
  164.          V M *                                        V R +
  165. Another way to do this final step would be to reduce the formula
  166. `10 $$ + $' across the vector of digits.
  167.      1:  [0, 0, 2, 5, ... ]        1:  25129926000
  168.          .                             .
  169.      
  170.                                        V R ' 10 $$ + $ RET
  171. File: calc.info,  Node: List Answer 10,  Next: List Answer 11,  Prev: List Answer 9,  Up: Answers to Exercises
  172. List Tutorial Exercise 10
  173. -------------------------
  174. For the list