home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume15 / calc-1.05 / part13 < prev    next >
Text File  |  1990-10-14  |  57KB  |  1,457 lines

  1. Newsgroups: comp.sources.misc
  2. X-UNIX-From: daveg@csvax.cs.caltech.edu
  3. subject: v15i040: Patch for GNU Emacs Calc, version 1.04 -> 1.05, part 13/20
  4. from: daveg@csvax.cs.caltech.edu (David Gillespie)
  5. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  6.  
  7. Posting-number: Volume 15, Issue 40
  8. Submitted-by: daveg@csvax.cs.caltech.edu (David Gillespie)
  9. Archive-name: calc-1.05/part13
  10.  
  11. #!/bin/sh
  12. # this is part 13 of a multipart archive
  13. # do not concatenate these parts, unpack them in order with /bin/sh
  14. # file calc.patch continued
  15. #
  16. CurArch=13
  17. if test ! -r s2_seq_.tmp
  18. then echo "Please unpack part 1 first!"
  19.      exit 1; fi
  20. ( read Scheck
  21.   if test "$Scheck" != $CurArch
  22.   then echo "Please unpack part $Scheck next!"
  23.        exit 1;
  24.   else exit 0; fi
  25. ) < s2_seq_.tmp || exit 1
  26. sed 's/^X//' << 'SHAR_EOF' >> calc.patch
  27. X  @kbd{T} (tangent), @kbd{E} (@samp{e^x}) and @kbd{L} (natural
  28. X***************
  29. X*** 1835,1841 ****
  30. X  Let's compute the sine and cosine of an angle, and verify the
  31. X  identity @samp{sin(@var{x})^2 + cos(@var{x})^2 = 1}.  We'll
  32. X  arbitrarily pick -64 degrees as a good value for @var{x}.  With
  33. X! the angular mode set to degrees (@kbd{m d}), do:
  34. X  
  35. X  @group
  36. X  @example
  37. X--- 1931,1937 ----
  38. X  Let's compute the sine and cosine of an angle, and verify the
  39. X  identity @samp{sin(@var{x})^2 + cos(@var{x})^2 = 1}.  We'll
  40. X  arbitrarily pick -64 degrees as a good value for @var{x}.  With
  41. X! the angular mode set to degrees (type @kbd{m d}), do:
  42. X  
  43. X  @group
  44. X  @example
  45. X***************
  46. X*** 1843,1849 ****
  47. X  1:  -64        1:  -0.89879   1:  -64        1:  0.43837        .
  48. X      .              .              .              .
  49. X  
  50. X!  64 n RET RET      S             TAB             C            x hypot
  51. X  @end example
  52. X  @end group
  53. X  
  54. X--- 1939,1945 ----
  55. X  1:  -64        1:  -0.89879   1:  -64        1:  0.43837        .
  56. X      .              .              .              .
  57. X  
  58. X!  64 n RET RET      S             TAB             C             f h
  59. X  @end example
  60. X  @end group
  61. X  
  62. X***************
  63. X*** 1850,1855 ****
  64. X--- 1946,1954 ----
  65. X  (For brevity, we're showing only five digits of the results here.
  66. X  You can of course do these calculations to any precision you like.)
  67. X  
  68. X+ Remember, @kbd{f h} is the @code{calc-hypot}, or square-root of sum
  69. X+ of squares, command.
  70. X+ 
  71. X  Another identity is @samp{tan(@var{x}) = sin(@var{x}) / cos(@var{x})}.
  72. X  
  73. X  @group
  74. X***************
  75. X*** 1880,1886 ****
  76. X  How can the angle be the same?  The answer is that the @kbd{/} operation
  77. X  loses information about the signs of its inputs.  Because the quotient
  78. X  is negative, we know exactly one of the inputs was negative, but we
  79. X! can't tell which one.  There is an @code{arctan2} function which
  80. X  computes the inverse tangent of the quotient of a pair of numbers.
  81. X  Since you feed it the two original numbers, it has enough information
  82. X  to give you a full 360-degree answer.
  83. X--- 1979,1985 ----
  84. X  How can the angle be the same?  The answer is that the @kbd{/} operation
  85. X  loses information about the signs of its inputs.  Because the quotient
  86. X  is negative, we know exactly one of the inputs was negative, but we
  87. X! can't tell which one.  There is an @kbd{f T} [@code{arctan2}] function which
  88. X  computes the inverse tangent of the quotient of a pair of numbers.
  89. X  Since you feed it the two original numbers, it has enough information
  90. X  to give you a full 360-degree answer.
  91. X***************
  92. X*** 1892,1898 ****
  93. X      .                         1:  0.43837        .
  94. X                                    .
  95. X  
  96. X!    U U          x arctan2       X M-2 n       x arctan2         -
  97. X  @end example
  98. X  @end group
  99. X  
  100. X--- 1991,1997 ----
  101. X      .                         1:  0.43837        .
  102. X                                    .
  103. X  
  104. X!    U U            f T           X M-2 n         f T             -
  105. X  @end example
  106. X  @end group
  107. X  
  108. X***************
  109. X*** 1944,1950 ****
  110. X  
  111. X  @group
  112. X  @example
  113. X! 1:  1000       1:  6.9077     1:  1000       1:  3.
  114. X      .              .              .              .
  115. X  
  116. X      1000           L              U              H L
  117. X--- 2043,2049 ----
  118. X  
  119. X  @group
  120. X  @example
  121. X! 1:  1000       1:  6.9077     1:  1000       1:  3
  122. X      .              .              .              .
  123. X  
  124. X      1000           L              U              H L
  125. X***************
  126. X*** 1959,1965 ****
  127. X  
  128. X  @group
  129. X  @example
  130. X! 2:  1000       1:  3.         1:  1000.      2:  1000.      1:  6.9077
  131. X  1:  10             .              .          1:  2.71828        .
  132. X      .                                            .
  133. X  
  134. X--- 2058,2064 ----
  135. X  
  136. X  @group
  137. X  @example
  138. X! 2:  1000       1:  3          1:  1000.      2:  1000.      1:  6.9077
  139. X  1:  10             .              .          1:  2.71828        .
  140. X      .                                            .
  141. X  
  142. X***************
  143. X*** 1973,1978 ****
  144. X--- 2072,2092 ----
  145. X  that @kbd{P} with the hyperbolic prefix pushes the constant @samp{e}
  146. X  onto the stack.
  147. X  
  148. X+ You may have noticed that both times we took the base-10 logarithm
  149. X+ of 1000, we got an exact integer result.  Calc always tries to give
  150. X+ an exact rational result for calculations involving rational numbers
  151. X+ where possible.  But when we used @kbd{H E}, the result was a
  152. X+ floating-point number for no apparent reason.  In fact, if we had
  153. X+ computed @kbd{10 @key{RET} 3 ^} we @emph{would} have gotten an
  154. X+ exact integer 1000.  But the @kbd{H E} command is rigged to generate
  155. X+ a floating-point result all of the time so that @samp{1000 H E} will
  156. X+ not waste time computing a thousand-digit integer when all you
  157. X+ probably wanted was @samp{1e1000}.
  158. X+ 
  159. X+ (@bullet) @strong{Exercise 2.}  Find a pair of integer inputs to
  160. X+ the @kbd{B} command for which Calc could find an exact rational
  161. X+ result but doesn't.  @xref{Arithmetic Answer 2, 2}. (@bullet)
  162. X+ 
  163. X  The Calculator also has a set of functions relating to combinatorics
  164. X  and statistics.  You may be familiar with the @dfn{factorial} function,
  165. X  which computes the product of all the integers up to a given number.
  166. X***************
  167. X*** 1993,2009 ****
  168. X  exact integer, you get an exact integer result (158 digits long
  169. X  in this case).
  170. X  
  171. X! If you take the factorial of a non-integer, Calc uses the generalized
  172. X! factorial function defined in terms of Euler's Gamma function.
  173. X  
  174. X  @group
  175. X  @example
  176. X! 3:  4.         3:  24.               1:  5.5        1:  52.342777846
  177. X! 2:  4.5        2:  52.3427777846         .              .
  178. X  1:  5.         1:  120.
  179. X      .              .
  180. X  
  181. X!                    M-3 !              M-0 DEL 5.5      x gamma
  182. X  @end example
  183. X  @end group
  184. X  
  185. X--- 2107,2124 ----
  186. X  exact integer, you get an exact integer result (158 digits long
  187. X  in this case).
  188. X  
  189. X! If you take the factorial of a non-integer, Calc uses a generalized
  190. X! factorial function defined in terms of Euler's Gamma function (which
  191. X! is itself available as the @kbd{f g} command).
  192. X  
  193. X  @group
  194. X  @example
  195. X! 3:  4.         3:  24.               1:  5.5        1:  52.342777847
  196. X! 2:  4.5        2:  52.3427777847         .              .
  197. X  1:  5.         1:  120.
  198. X      .              .
  199. X  
  200. X!                    M-3 !              M-0 DEL 5.5       f g
  201. X  @end example
  202. X  @end group
  203. X  
  204. X***************
  205. X*** 2012,2018 ****
  206. X  The binomial coefficient @var{n}-choose-@var{m} can be defined as
  207. X  @samp{@var{n}! / @var{m}! (@var{n}-@var{m})!} for all reals @var{n} and
  208. X  @var{m}.  The intermediate results in this formula can become quite
  209. X! large even if the result is small; the @kbd{k b} command computes
  210. X  a binomial coefficient in a way that avoids large intermediate
  211. X  values.
  212. X  
  213. X--- 2127,2133 ----
  214. X  The binomial coefficient @var{n}-choose-@var{m} can be defined as
  215. X  @samp{@var{n}! / @var{m}! (@var{n}-@var{m})!} for all reals @var{n} and
  216. X  @var{m}.  The intermediate results in this formula can become quite
  217. X! large even if the final result is small; the @kbd{k c} command computes
  218. X  a binomial coefficient in a way that avoids large intermediate
  219. X  values.
  220. X  
  221. X***************
  222. X*** 2026,2032 ****
  223. X  1:  20             .              .
  224. X      .
  225. X  
  226. X!  30 RET 20        k b            k f
  227. X  @end example
  228. X  @end group
  229. X  
  230. X--- 2141,2147 ----
  231. X  1:  20             .              .
  232. X      .
  233. X  
  234. X!  30 RET 20         k c            k f
  235. X  @end example
  236. X  @end group
  237. X  
  238. X***************
  239. X*** 2035,2040 ****
  240. X--- 2150,2156 ----
  241. X  multiply them back together.  The result is the original number,
  242. X  30045015.
  243. X  
  244. X+ @cindex Hash tables
  245. X  Suppose a program you are writing needs a hash table with at least
  246. X  10000 entries.  It's best to use a prime number as the actual size
  247. X  of a hash table.  Calc can compute the next prime number after 10000:
  248. X***************
  249. X*** 2100,2105 ****
  250. X--- 2216,2222 ----
  251. X  @end example
  252. X  @end group
  253. X  
  254. X+ @cindex Dot product
  255. X  The dot product of two vectors is equal to the product of their
  256. X  lengths times the cosine of the angle between them.  (Here the vector
  257. X  is interpreted as a line from the origin @samp{(0,0,0)} to the
  258. X***************
  259. X*** 2125,2130 ****
  260. X--- 2242,2249 ----
  261. X  The inverse cosine finds that the angle between the vectors
  262. X  is about 56 degrees.
  263. X  
  264. X+ @cindex Cross product
  265. X+ @cindex Perpendicular vectors
  266. X  The @dfn{cross product} of two vectors is a vector whose length
  267. X  is the product of the lengths of the inputs times the sine of the
  268. X  angle between them, and whose direction is perpendicular to both
  269. X***************
  270. X*** 2143,2158 ****
  271. X  @end example
  272. X  @end group
  273. X  
  274. X- Vector-related commands generally begin with the @kbd{v} prefix key.
  275. X- Some are uppercase letters and some are lowercase.  To make it easier
  276. X- to type these commands, the shift-@kbd{V} key acts the same as the
  277. X- @kbd{v} prefix.
  278. X- 
  279. X  First we recall the original vectors and compute their cross product,
  280. X  which we also store for later reference.  Now we divide the vector
  281. X  by the product of the lengths of the original vectors.  The length of
  282. X  this vector should be the sine of the angle; sure enough, it is!
  283. X  
  284. X  If we take the dot product of two perpendicular vectors we expect
  285. X  to get zero, since the cosine of 90 degrees is zero.  Let's check
  286. X  that the cross product is indeed perpendicular to both inputs:
  287. X--- 2262,2278 ----
  288. X  @end example
  289. X  @end group
  290. X  
  291. X  First we recall the original vectors and compute their cross product,
  292. X  which we also store for later reference.  Now we divide the vector
  293. X  by the product of the lengths of the original vectors.  The length of
  294. X  this vector should be the sine of the angle; sure enough, it is!
  295. X  
  296. X+ Vector-related commands generally begin with the @kbd{v} prefix key.
  297. X+ Some are uppercase letters and some are lowercase.  To make it easier
  298. X+ to type these commands, the shift-@kbd{V} key acts the same as the
  299. X+ @kbd{v} prefix.  (@pxref{Mode Settings}, for a way to make all
  300. X+ prefix keys have this property.)
  301. X+ 
  302. X  If we take the dot product of two perpendicular vectors we expect
  303. X  to get zero, since the cosine of 90 degrees is zero.  Let's check
  304. X  that the cross product is indeed perpendicular to both inputs:
  305. X***************
  306. X*** 2167,2172 ****
  307. X--- 2287,2306 ----
  308. X  @end example
  309. X  @end group
  310. X  
  311. X+ @cindex Normalizing a vector
  312. X+ @cindex Unit vectors
  313. X+ (@bullet) @strong{Exercise 1.}  Given a vector on the top of the
  314. X+ stack, what keystrokes would you use to @dfn{normalize} the
  315. X+ vector, i.e., to reduce its length to one without changing its
  316. X+ direction?  @xref{Vector Answer 1, 1}. (@bullet)
  317. X+ 
  318. X+ (@bullet) @strong{Exercise 2.}  Suppose a certain particle can be
  319. X+ at any of several positions along a ruler.  You have a list of
  320. X+ those positions in the form of a vector, and another list of the
  321. X+ probabilities for the particle to be at the corresponding positions.
  322. X+ Find the average position of the particle.
  323. X+ @xref{Vector Answer 2, 2}. (@bullet)
  324. X+ 
  325. X  @node Matrix Tutorial, List Tutorial, Vector Analysis Tutorial, Vector/Matrix Tutorial
  326. X  @subsection Matrices
  327. X  
  328. X***************
  329. X*** 2253,2258 ****
  330. X--- 2387,2398 ----
  331. X  rows in the matrix is different from the number of elements in the
  332. X  vector.
  333. X  
  334. X+ (@bullet) @strong{Exercise 1.}  Use @samp{*} to sum along the rows
  335. X+ of the above 2x3 matrix to get @samp{[6, 15]}.  Now use @samp{*} to
  336. X+ sum along the columns to get @samp{[5, 7, 9]}.
  337. X+ @xref{Matrix Answer 1, 1}. (@bullet)
  338. X+ 
  339. X+ @cindex Identity matrix
  340. X  An @dfn{identity matrix} is a square matrix with ones along the
  341. X  diagonal and zeros elsewhere.  It has the property that multiplication
  342. X  by an identity matrix, on the left or on the right, always produces
  343. X***************
  344. X*** 2304,2309 ****
  345. X--- 2444,2451 ----
  346. X  @end example
  347. X  @end group
  348. X  
  349. X+ @cindex Systems of linear equations
  350. X+ @cindex Linear equations, systems of
  351. X  Matrix inverses are related to systems of linear equations in algebra.
  352. X  Suppose we had the following set of equations:
  353. X  
  354. X***************
  355. X*** 2352,2360 ****
  356. X--- 2494,2545 ----
  357. X  The result is 6.00001, reasonably correct considering that we cheated
  358. X  and didn't type all the digits of @samp{c}.
  359. X  
  360. X+ (@bullet) @strong{Exercise 2.}  Algebraic entry allows you to make
  361. X+ vectors and matrices that include variables.  Solve the following
  362. X+ system of equations to get expressions for @samp{x} and @samp{y}
  363. X+ in terms of @samp{a} and @samp{b}.
  364. X+ 
  365. X+ @group
  366. X+ @example
  367. X+    x + a y = 6
  368. X+    x + b y = 10
  369. X+ @end example
  370. X+ @end group
  371. X+ 
  372. X+ @xref{Matrix Answer 2, 2}. (@bullet)
  373. X+ 
  374. X+ @cindex Least-squares for over-determined systems
  375. X+ @cindex Over-determined systems of equations
  376. X+ (@bullet) @strong{Exercise 3.}  A system of equations is ``over-determined''
  377. X+ if it has more equations than variables.  It is often the case that
  378. X+ there are no values for the variables that will satisfy all the
  379. X+ equations at once, but it is still useful to find a set of values
  380. X+ which ``nearly'' satisfy all the equations.  In terms of matrix equations,
  381. X+ you can't solve @samp{A * X = B} directly because the matrix @samp{A}
  382. X+ is not square for an over-determined system.  Matrix inversion works
  383. X+ only for square matrices.  One common trick is to multiply both sides
  384. X+ on the left by the transpose of @samp{A}:  @samp{trn(A)*A*X = trn(A)*B}.
  385. X+ Now @samp{trn(A)*A} is a square matrix so a solution is possible.  It
  386. X+ turns out that the @samp{X} vector you compute in this way will be a
  387. X+ ``least-squares'' solution, which can be regarded as the ``closest''
  388. X+ solution to the set of equations.  Use Calc to solve the following
  389. X+ over-determined system:
  390. X+ 
  391. X+ @group
  392. X+ @example
  393. X+     a + 2b + 3c = 6
  394. X+    4a + 5b + 6c = 2
  395. X+    7a + 6b      = 3
  396. X+    2a + 4b + 6c = 11
  397. X+ @end example
  398. X+ @end group
  399. X+ 
  400. X+ @xref{Matrix Answer 3, 3}. (@bullet)
  401. X+ 
  402. X  @node List Tutorial, , Matrix Tutorial, Vector/Matrix Tutorial
  403. X  @subsection Vectors as Lists
  404. X  
  405. X+ @cindex Lists
  406. X  Although Calc has a number of features for manipulating vectors and
  407. X  matrices as mathematical objects, you can also treat vectors as
  408. X  simple lists of values.  For example, we saw that the @kbd{k f}
  409. X***************
  410. X*** 2406,2411 ****
  411. X--- 2591,2599 ----
  412. X  other a plain number.)  In the final step, we take the square root
  413. X  of each element.
  414. X  
  415. X+ (@bullet) @strong{Exercise 1.}  Compute a vector of powers of two
  416. X+ from @samp{2^-4} to @samp{2^4}.  @xref{List Answer 1, 1}. (@bullet)
  417. X+ 
  418. X  You can also @dfn{reduce} a binary operator across a vector.
  419. X  For example, reducing @samp{*} computes the product of all the
  420. X  elements in the vector:
  421. X***************
  422. X*** 2439,2444 ****
  423. X--- 2627,2650 ----
  424. X  sum of pairwise products of the elements to get the same answer
  425. X  for the dot product as before.
  426. X  
  427. X+ A slight variant of vector reduction is the @dfn{accumulate} operation,
  428. X+ @kbd{H V R}.  This produces a vector of the intermediate results from
  429. X+ a corresponding reduction.  Here we compute a table of factorials:
  430. X+ 
  431. X+ @group
  432. X+ @example
  433. X+ 1:  [1, 2, 3, 4, 5, 6]    1:  [1, 2, 6, 24, 120, 720]
  434. X+     .                         .
  435. X+ 
  436. X+     v x 6 RET                 H V R *
  437. X+ @end example
  438. X+ @end group
  439. X+ 
  440. X+ @cindex Least-squares for fitting a straight line
  441. X+ @cindex Fitting data to a line
  442. X+ @cindex Line, fitting data to
  443. X+ @cindex Data, extracting from buffers
  444. X+ @cindex Columns of data, extracting
  445. X  As a larger example, let's try to fit a straight line to some data,
  446. X  using the method of least squares.  Suppose we have the following
  447. X  list of values in a file we have loaded into Emacs:
  448. X***************
  449. X*** 2472,2479 ****
  450. X  manual and find this table there.  (Press @kbd{g}, then type
  451. X  @kbd{List Tutorial}, to jump straight to this section.)
  452. X  
  453. X! Position the cursor at the upper-left corner of this table, right
  454. X  to the left of the @samp{1.34}.  Press @kbd{C-@@} to set the mark.
  455. X  Now position the cursor to the lower-right, just after the @samp{1.354}.
  456. X  You have now defined this region as an Emacs ``rectangle.''  Still
  457. X  in the Info buffer, type @kbd{M-x calc-grab-region}.  This command
  458. X--- 2678,2686 ----
  459. X  manual and find this table there.  (Press @kbd{g}, then type
  460. X  @kbd{List Tutorial}, to jump straight to this section.)
  461. X  
  462. X! Position the cursor at the upper-left corner of this table, just
  463. X  to the left of the @samp{1.34}.  Press @kbd{C-@@} to set the mark.
  464. X+ (This may also be found on @kbd{C-2}, @kbd{C-SPC}, or @kbd{C-NUL}.)
  465. X  Now position the cursor to the lower-right, just after the @samp{1.354}.
  466. X  You have now defined this region as an Emacs ``rectangle.''  Still
  467. X  in the Info buffer, type @kbd{M-x calc-grab-region}.  This command
  468. X***************
  469. X*** 2488,2493 ****
  470. X--- 2695,2704 ----
  471. X  @end example
  472. X  @end group
  473. X  
  474. X+ (If the size of this matrix is awkward, you may wish to type @kbd{v .}
  475. X+ [the letter `v' followed by a period] to enable ``abbreviated vectors''
  476. X+ display mode.)
  477. X+ 
  478. X  We want to treat this as a pair of lists.  The first step is to
  479. X  transpose this matrix into a pair of rows.  Remember, a matrix is
  480. X  just a vector of vectors.  So we can unpack the matrix into a pair
  481. X***************
  482. X*** 2545,2552 ****
  483. X  @end group
  484. X  
  485. X  These are @samp{sum(x)}, @samp{sum(x^2)}, @samp{sum(y)}, and @samp{sum(x y)},
  486. X! respectively.  Finally, we also need @samp{N}, the number of data points.
  487. X! This is just the length of either of our lists.
  488. X  
  489. X  @group
  490. X  @example
  491. X--- 2756,2764 ----
  492. X  @end group
  493. X  
  494. X  These are @samp{sum(x)}, @samp{sum(x^2)}, @samp{sum(y)}, and @samp{sum(x y)},
  495. X! respectively.  (We could have used @kbd{*} to compute @samp{sum(x^2)} and
  496. X! @samp{sum(x y)}.)  Finally, we also need @samp{N}, the number of data
  497. X! points.  This is just the length of either of our lists.
  498. X  
  499. X  @group
  500. X  @example
  501. X***************
  502. X*** 2581,2588 ****
  503. X  @end example
  504. X  @end group
  505. X  
  506. X! That's the slope @var{m}.  The intercept @var{b} can now be found with the
  507. X! simple formula @samp{(sum(y) - @var{m} sum(x)) / N}.
  508. X  
  509. X  @group
  510. X  @example
  511. X--- 2793,2800 ----
  512. X  @end example
  513. X  @end group
  514. X  
  515. X! That gives us the slope @var{m}.  The y-intercept @var{b} can now
  516. X! be found with the simple formula, @samp{(sum(y) - @var{m} sum(x)) / N}.
  517. X  
  518. X  @group
  519. X  @example
  520. X***************
  521. X*** 2602,2608 ****
  522. X  1:  [0.699, 0.735, ... ]    1:  [0.273, 0.309, ... ]
  523. X      .                           .
  524. X  
  525. X!     r 1 r 8 *                   r 9 +
  526. X  @end example
  527. X  @end group
  528. X  
  529. X--- 2814,2820 ----
  530. X  1:  [0.699, 0.735, ... ]    1:  [0.273, 0.309, ... ]
  531. X      .                           .
  532. X  
  533. X!     r 1 r 8 *                   r 9 +    s 0
  534. X  @end example
  535. X  @end group
  536. X  
  537. X***************
  538. X*** 2625,2642 ****
  539. X  
  540. X  First we compute a vector of differences, then we take the absolute
  541. X  values of these differences, then we reduce the @code{max} function
  542. X! across the vector.  (Even though @code{max} is not normally assigned
  543. X! a letter, so that you would have to type @kbd{x max} to get it,
  544. X! reducing @code{max} and @code{min} is so common that @kbd{X} and
  545. X! @kbd{N} are accepted for those functions in that context.  In
  546. X! general, you answer the @kbd{V M} or @kbd{V R} prompt with the
  547. X! actual key sequence that invokes the function you want.  You could
  548. X! have typed @kbd{V R x max @key{RET}} if you had preferred.)
  549. X  
  550. X  As another example, a theorem about binomial coefficients tells
  551. X  us that the alternating sum of binomial coefficients @var{n}-choose-0 minus
  552. X! @var{n}-choose-1 plus @var{n}-choose-2, and so on, always comes out
  553. X! to zero.  Let's verify this for @var{n}=6.
  554. X  
  555. X  @group
  556. X  @example
  557. X--- 2837,2918 ----
  558. X  
  559. X  First we compute a vector of differences, then we take the absolute
  560. X  values of these differences, then we reduce the @code{max} function
  561. X! across the vector.  (The @code{max} function is on the two-key sequence
  562. X! @kbd{f x}; because it is so common to use @code{max} in a vector
  563. X! operation, the letters @kbd{X} and @kbd{N} are also accepted for
  564. X! @code{max} and @code{min} in this context.  In general, you answer
  565. X! the @kbd{V M} or @kbd{V R} prompt with the actual key sequence that
  566. X! invokes the function you want.  You could have typed @kbd{V R f x} or
  567. X! even @kbd{V R x max @key{RET}} if you had preferred.)
  568. X! 
  569. X! If your system has the GNUPLOT program, you can see graphs of your
  570. X! data and your straight line to see how well they match.  (The following
  571. X! instructions assume you have GNUPLOT correctly installed and are using
  572. X! the X window system.  They may not work in other circumstances.)
  573. X! 
  574. X! Let's start by plotting the original data.  Recall the ``x'' and ``y''
  575. X! vectors onto the stack and press @kbd{g f}.  This ``fast'' graphing
  576. X! command does everything you need to do for simple, straightforward
  577. X! plotting of data.
  578. X! 
  579. X! @group
  580. X! @example
  581. X! 2:  [1.34, 1.41, 1.49, ... ]
  582. X! 1:  [0.234, 0.298, 0.402, ... ]
  583. X!     .
  584. X! 
  585. X!     r 1 r 2    g f
  586. X! @end example
  587. X! @end group
  588. X! 
  589. X! If all goes well, you will shortly get a new window containing a graph
  590. X! of the data.  (If not, contact your GNUPLOT or Calc installer to find
  591. X! out what went wrong.)
  592. X! 
  593. X! Next, let's add the line we got from our least-squares fit:
  594. X! 
  595. X! @group
  596. X! @example
  597. X! 2:  [1.34, 1.41, 1.49, ... ]
  598. X! 1:  [0.273, 0.309, 0.351, ... ]
  599. X!     .
  600. X! 
  601. X!     DEL r 0    g a  g p
  602. X! @end example
  603. X! @end group
  604. X! 
  605. X! It's not very useful to get symbols to mark the data points on this
  606. X! second curve; you can type @kbd{g S g p} to remove them.  Type @kbd{g K}
  607. X! to remove the graphics window when you are done.
  608. X! 
  609. X! (@bullet) @strong{Exercise 2.}  An earlier exercise showed how to do
  610. X! least squares fitting to a general system of equations.  Our 19 data
  611. X! points are really 19 equations of the form @samp{y = m x + b} for
  612. X! different pairs of @samp{(x,y)}.  Use the matrix-transpose method
  613. X! to solve for @samp{m} and @samp{b}, duplicating the above result.
  614. X! @xref{List Answer 2, 2}. (@bullet)
  615. X! 
  616. X! @cindex Geometric mean
  617. X! (@bullet) @strong{Exercise 3.}  If the input data do not form a
  618. X! rectangle, you can use @kbd{C-u M-x calc-grab-region} to grab the
  619. X! data the way Emacs normally works with regions---it reads left-to-right,
  620. X! top-to-bottom, treating line breaks the same as spaces.  Use this
  621. X! command to find the geometric mean of the following numbers.  (The
  622. X! geometric mean is the @var{n}th root of the product of @var{n} numbers.)
  623. X! 
  624. X! @example
  625. X! 2.3, 6, 22, 15.1, 7,
  626. X!   15, 14, 7.5,
  627. X!   2.5
  628. X! @end example
  629. X! 
  630. X! @xref{List Answer 3, 3}. (@bullet)
  631. X  
  632. X  As another example, a theorem about binomial coefficients tells
  633. X  us that the alternating sum of binomial coefficients @var{n}-choose-0 minus
  634. X! @var{n}-choose-1 plus @var{n}-choose-2, and so on up to
  635. X! @var{n}-choose-@var{n}, always comes out to zero.  Let's verify this
  636. X! for @var{n}=6.@refill
  637. X  
  638. X  @group
  639. X  @example
  640. X***************
  641. X*** 2665,2670 ****
  642. X--- 2941,3033 ----
  643. X  to each element of the vector, substituting each element's value for
  644. X  @samp{n} in turn.
  645. X  
  646. X+ Another group of operators that are often useful with @kbd{V M} are
  647. X+ the relational operators:  @kbd{a =}, for example, compares two numbers
  648. X+ and gives the result 1 if they are equal, or 0 if not.  Similarly,
  649. X+ @kbd{a <} checks for one number being less than another.
  650. X+ 
  651. X+ Other useful vector operations include @kbd{v v}, to reverse a
  652. X+ vector end-for-end; @kbd{V S}, to sort the elements of a vector
  653. X+ into increasing order; and @kbd{v r} and @kbd{v c}, to extract
  654. X+ one row or column of a matrix, or (in both cases) to extract one
  655. X+ element of a plain vector.  With a negative argument, @kbd{v r}
  656. X+ and @kbd{v c} instead delete one row, column, or vector element.
  657. X+ 
  658. X+ @cindex Divisor functions
  659. X+ (@bullet) @strong{Exercise 4.}  The @var{k}th @dfn{divisor function}
  660. X+ is the sum of the @var{k}th powers of all the divisors of an
  661. X+ integer @var{n}.  Figure out a method for computing the divisor
  662. X+ function for reasonably small values of @var{n}.  As a test,
  663. X+ the 0th and 1st divisor functions of 30 are 8 and 72, respectively.
  664. X+ @xref{List Answer 4, 4}. (@bullet)
  665. X+ 
  666. X+ @cindex Square-free numbers
  667. X+ @cindex Duplicate values in a list
  668. X+ (@bullet) @strong{Exercise 5.}  The @kbd{k f} command produces a
  669. X+ list of prime factors for a number.  Sometimes it is important to
  670. X+ know that a number is @dfn{square-free}, i.e., that no prime occurs
  671. X+ more than once in its list of prime factors.  Find a sequence of
  672. X+ keystrokes to tell if a number is square-free; your method should
  673. X+ leave 1 on the stack if it is, or 0 if it isn't.
  674. X+ @xref{List Answer 5, 5}. (@bullet)
  675. X+ 
  676. X+ @cindex Triangular lists
  677. X+ (@bullet) @strong{Exercise 6.}  Build a list of lists that looks
  678. X+ like the following diagram.  (You may wish to use the @kbd{v /}
  679. X+ command to enable multi-line display of vectors.)
  680. X+ 
  681. X+ @group
  682. X+ @example
  683. X+ 1:  [ [1],
  684. X+       [1, 2],
  685. X+       [1, 2, 3],
  686. X+       [1, 2, 3, 4],
  687. X+       [1, 2, 3, 4, 5],
  688. X+       [1, 2, 3, 4, 5, 6] ]
  689. X+ @end example
  690. X+ @end group
  691. X+ 
  692. X+ @xref{List Answer 6, 6}. (@bullet)
  693. X+ 
  694. X+ (@bullet) @strong{Exercise 7.}  Build the following list of lists.
  695. X+ 
  696. X+ @group
  697. X+ @example
  698. X+ 1:  [ [0],
  699. X+       [1, 2],
  700. X+       [3, 4, 5],
  701. X+       [6, 7, 8, 9],
  702. X+       [10, 11, 12, 13, 14],
  703. X+       [15, 16, 17, 18, 19, 20] ]
  704. X+ @end example
  705. X+ @end group
  706. X+ 
  707. X+ @xref{List Answer 7, 7}. (@bullet)
  708. X+ 
  709. X+ @cindex Maximizing a function over a list of values
  710. X+ (@bullet) @strong{Exercise 8.}  Compute a list of values of Bessel's
  711. X+ J1 function @samp{besJ(1,x)} for @samp{x} from 0 to 5 in steps of 0.25.
  712. X+ Find the value of @samp{x} (from among the above set of values) for
  713. X+ which @samp{besJ(1,x)} is a maximum.  Use an ``automatic'' method,
  714. X+ i.e., just reading along the list to find the smallest value is
  715. X+ not allowed!  (There is an @kbd{a X} command which does this kind
  716. X+ of thing automatically; @pxref{Numerical Solutions}.)
  717. X+ @xref{List Answer 8, 8}. (@bullet)
  718. X+ 
  719. X+ @cindex Digits, vectors of
  720. X+ (@bullet) @strong{Exercise 9.}  You are given an integer in the range
  721. X+ @samp{0 <= N < 10^m} for @samp{m=12} (i.e., an integer of less than
  722. X+ twelve digits).  Convert this integer into a vector of @samp{m}
  723. X+ digits, each in the range from 0 to 9.  In vector-of-digits notation,
  724. X+ add one to this integer to produce a vector of @samp{m+1} digits
  725. X+ (since there could be a carry out of the most significant digit).
  726. X+ Convert this vector back into a regular integer.  A good integer
  727. X+ to try is 25129925999.  @xref{List Answer 9, 9}. (@bullet)
  728. X+ 
  729. X+ (@bullet) @strong{Exercise 10.}  Your friend Joe tried to use
  730. X+ @samp{V R a =} to test if all numbers in a list were equal.  What
  731. X+ happened?  How would you do this test?  @xref{List Answer 10, 10}. (@bullet)
  732. X+ 
  733. X  @node Types Tutorial, Algebra Tutorial, Vector/Matrix Tutorial, Tutorial
  734. X  @section Types Tutorial
  735. X  
  736. X***************
  737. X*** 2718,2723 ****
  738. X--- 3081,3091 ----
  739. X  @end example
  740. X  @end group
  741. X  
  742. X+ (@bullet) @strong{Exercise 1.}  A calculation has produced the
  743. X+ result 1.26508260337.  You suspect it is the square root of the
  744. X+ product of @samp{pi} and some rational number.  Is it?  (Be sure
  745. X+ to allow for roundoff error!)  @xref{Types Answer 1, 1}. (@bullet)
  746. X+ 
  747. X  Complex numbers can be stored in both rectangular and polar form.
  748. X  
  749. X  @group
  750. X***************
  751. X*** 2763,2768 ****
  752. X--- 3131,3145 ----
  753. X  form, then we take the sine of that angle.  Note that the trigonometric
  754. X  functions will accept HMS forms directly as input.
  755. X  
  756. X+ @cindex Beatles
  757. X+ (@bullet) @strong{Exercise 2.}  The Beatles' @emph{Abbey Road} is
  758. X+ 47 minutes and 26 seconds long, and contains 17 songs.  What is the
  759. X+ average length of a song on @emph{Abbey Road}?  If the Extended Disco
  760. X+ Version of @emph{Abbey Road} added 20 seconds to the length of each
  761. X+ song, how long would the album be?  @xref{Types Answer 2, 2}. (@bullet)
  762. X+ 
  763. X+ @cindex Slope and angle of a line
  764. X+ @cindex Angle and slope of a line
  765. X  An @dfn{error form} represents a mean value with an attached standard
  766. X  deviation, or error estimate.  Suppose our measurements indicate that
  767. X  a certain telephone pole is about 30 meters away, with an estimated
  768. X***************
  769. X*** 2776,2782 ****
  770. X      .            1:  30 +/- 1        .                     .
  771. X                       .
  772. X  
  773. X!     8 p .2           30 p 1          /                     I T
  774. X  @end example
  775. X  @end group
  776. X  
  777. X--- 3153,3159 ----
  778. X      .            1:  30 +/- 1        .                     .
  779. X                       .
  780. X  
  781. X!     8 p .2 RET       30 p 1          /                     I T
  782. X  @end example
  783. X  @end group
  784. X  
  785. X***************
  786. X*** 2784,2789 ****
  787. X--- 3161,3174 ----
  788. X  original error estimates were valid standard deviations, there is about
  789. X  a 60% chance that the result is correct within 0.59 degrees.
  790. X  
  791. X+ @cindex Torus, volume of
  792. X+ (@bullet) @strong{Exercise 3.}  The volume of a torus (a donut) is
  793. X+ @samp{2 pi^2 R r^2} where @samp{R} is the radius of the circle that
  794. X+ defines the center of the tube and @samp{r} is the radius of the tube
  795. X+ itself.  Suppose @samp{R} is 20 cm and @samp{r} is 4 cm, each known to
  796. X+ within 5 percent.  What is the volume and the relative uncertainty of
  797. X+ the volume?  @xref{Types Answer 3, 3}. (@bullet)
  798. X+ 
  799. X  An @dfn{interval form} represents a range of values.  While an
  800. X  error form is best for making statistical estimates, intervals give
  801. X  you exact bounds on an answer.  Suppose we additionally know that
  802. X***************
  803. X*** 2824,2829 ****
  804. X--- 3209,3225 ----
  805. X  The Calculator automatically keeps track of which end values should
  806. X  be open and which should be closed.
  807. X  
  808. X+ (@bullet) @strong{Exercise 4.}  It is an error to divide by zero;
  809. X+ Calc leaves the result in symbolic form.  When would you expect
  810. X+ Calc to do the same for a division of interval forms?
  811. X+ @xref{Types Answer 4, 4}. (@bullet)
  812. X+ 
  813. X+ (@bullet) @strong{Exercise 5.}  Two easy ways of squaring a number
  814. X+ are @key{RET *} and @kbd{2 ^}.  Normally these produce the same
  815. X+ answer.  Would you expect this still to hold true for interval forms?
  816. X+ If not, which of these will result in a larger interval?
  817. X+ @xref{Types Answer 5, 5}. (@bullet)
  818. X+ 
  819. X  A @dfn{modulo form} is used for performing arithmetic modulo @var{M}.
  820. X  For example, arithmetic involving time is generally done modulo 12
  821. X  or 24 hours.
  822. X***************
  823. X*** 2855,2862 ****
  824. X  much more efficient because it avoids the huge intermediate value
  825. X  that arises in the second one.
  826. X  
  827. X! The Calc data types can't be combined in all possible ways, but it
  828. X! is possible to use HMS forms as parts of error forms, intervals,
  829. X  modulo forms, or as the phase part of a polar complex number.
  830. X  For example, the @code{calc-time} command pushes the current time
  831. X  of day on the stack as an HMS/modulo form.
  832. X--- 3251,3267 ----
  833. X  much more efficient because it avoids the huge intermediate value
  834. X  that arises in the second one.
  835. X  
  836. X! @cindex Fermat, primality test of
  837. X! (@bullet) @strong{Exercise 6.}  A theorem due to Pierre de Fermat
  838. X! says that @samp{x^(n-1) mod n = 1} if @samp{n} is a prime number
  839. X! and @samp{x} is an integer less than @samp{n}.  If @samp{n} is
  840. X! @emph{not} a prime number, this will @emph{not} be true for most
  841. X! values of @samp{x}.  Thus we can test informally if a number is
  842. X! prime by trying this formula for several values of @samp{x}.
  843. X! Use this test to tell whether the following numbers are prime:
  844. X! 811749613, 15485863.  @xref{Types Answer 6, 6}. (@bullet)
  845. X! 
  846. X! It is possible to use HMS forms as parts of error forms, intervals,
  847. X  modulo forms, or as the phase part of a polar complex number.
  848. X  For example, the @code{calc-time} command pushes the current time
  849. X  of day on the stack as an HMS/modulo form.
  850. X***************
  851. X*** 2872,2877 ****
  852. X--- 3277,3293 ----
  853. X  
  854. X  This calculation tells me it is six hours and 22 minutes until midnight.
  855. X  
  856. X+ (@bullet) @strong{Exercise 7.}  A rule of thumb is that one year
  857. X+ is about @samp{pi * 10^7} seconds.  What time will it be that
  858. X+ many seconds from right now?  @xref{Types Answer 7, 7}. (@bullet)
  859. X+ 
  860. X+ (@bullet) @strong{Exercise 8.}  You are preparing to order packaging
  861. X+ for the CD release of the Extended Disco Version of @emph{Abbey Road}.
  862. X+ You are told that the songs will actually be anywhere from 20 to 60
  863. X+ seconds longer than the originals.  One CD can hold about 75 minutes
  864. X+ of music.  Should you order single or double packages?
  865. X+ @xref{Types Answer 8, 8}. (@bullet)
  866. X+ 
  867. X  Another kind of data the Calculator can manipulate is numbers with
  868. X  @dfn{units}.  This isn't strictly a new data type; it's simply an
  869. X  application of algebraic expressions, where we use variables with
  870. X***************
  871. X*** 2924,2930 ****
  872. X  
  873. X  @group
  874. X  @example
  875. X! 1:  55 mph     1:  88.5139 kph   1:   88.5139 km / hr   1:  8.20147e-8 c
  876. X      .              .                  .                     .
  877. X  
  878. X   ' 55 mph RET      u c kph RET        u c km/hr RET         u c c RET
  879. X--- 3340,3346 ----
  880. X  
  881. X  @group
  882. X  @example
  883. X! 1:  55 mph     1:  88.5139 kph   1:   88.5139 km / hr   1:  8.201407e-8 c
  884. X      .              .                  .                     .
  885. X  
  886. X   ' 55 mph RET      u c kph RET        u c km/hr RET         u c c RET
  887. X***************
  888. X*** 2962,2967 ****
  889. X--- 3378,3399 ----
  890. X  @kbd{M-#} again to re-enter the Calculator when you're done looking
  891. X  at the units table.
  892. X  
  893. X+ (@bullet) @strong{Exercise 9.}  How many seconds are there really
  894. X+ in a year?  @xref{Types Answer 9, 9}. (@bullet)
  895. X+ 
  896. X+ @cindex Speed of light
  897. X+ (@bullet) @strong{Exercise 10.}  Supercomputer designs are limited by
  898. X+ the speed of light (and of electricity, which is nearly as fast).
  899. X+ Suppose a computer has a 4.1 ns (nanosecond) clock cycle, and its
  900. X+ cabinet is one meter across.  Is speed of light going to be a
  901. X+ significant factor in its design?  @xref{Types Answer 10, 10}. (@bullet)
  902. X+ 
  903. X+ (@bullet) @strong{Exercise 11.}  Sam the Slug normally travels about
  904. X+ five yards in an hour.  He has obtained a supply of Power Pills; each
  905. X+ Power Pill he eats doubles his speed.  How many Power Pills can he
  906. X+ swallow and still travel legally on most US highways?
  907. X+ @xref{Types Answer 11, 11}. (@bullet)
  908. X+ 
  909. X  @node Algebra Tutorial, Programming Tutorial, Types Tutorial, Tutorial
  910. X  @section Algebra and Calculus Tutorial
  911. X  
  912. X***************
  913. X*** 2978,2989 ****
  914. X  @end example
  915. X  @end group
  916. X  
  917. X  There are also commands for doing common algebraic operations on
  918. X! formulas.
  919. X  
  920. X  @group
  921. X  @example
  922. X! 1:  18 x^2 + 6 y - 6 x^4 - 2 x^2 y    1:  (18 - 2 y) x^2 - 6 x^4 + 6 y
  923. X      .                                     .
  924. X  
  925. X      a x                                   a c x RET
  926. X--- 3410,3425 ----
  927. X  @end example
  928. X  @end group
  929. X  
  930. X+ (@bullet) @strong{Exercise 1.}  Do @kbd{' x RET Q 2 ^} and
  931. X+ @kbd{' x RET 2 ^ Q} both wind up with the same result (@samp{x})?
  932. X+ Why or why not?  @xref{Algebra Answer 1, 1}. (@bullet)
  933. X+ 
  934. X  There are also commands for doing common algebraic operations on
  935. X! formulas.  Continuing with the formula from the last example,
  936. X  
  937. X  @group
  938. X  @example
  939. X! 1:  18 x^2 + 6 y - 6 x^4 - 2 x^2 y    1:  -6 x^4 + (18 - 2 y) x^2  + 6 y
  940. X      .                                     .
  941. X  
  942. X      a x                                   a c x RET
  943. X***************
  944. X*** 2991,2997 ****
  945. X  @end group
  946. X  
  947. X  First we ``expand'' using the distributive law, then we ``collect''
  948. X! terms involving powers of @samp{x}.
  949. X  
  950. X  Let's find the value of this expression when @samp{x} is 2 and @samp{y}
  951. X  is one half.
  952. X--- 3427,3433 ----
  953. X  @end group
  954. X  
  955. X  First we ``expand'' using the distributive law, then we ``collect''
  956. X! terms involving like powers of @samp{x}.
  957. X  
  958. X  Let's find the value of this expression when @samp{x} is 2 and @samp{y}
  959. X  is one half.
  960. X***************
  961. X*** 3011,3016 ****
  962. X--- 3447,3453 ----
  963. X  next expression on the stack.  After this command, the variable goes
  964. X  back to its original value, if any.
  965. X  
  966. X+ @cindex Maximum of a function using Calculus
  967. X  Let's find the maximum value of our original expression when @samp{y}
  968. X  is one half and @samp{x} ranges over all possible values.  We can
  969. X  do this by taking the derivative with respect to @samp{x} and examining
  970. X***************
  971. X*** 3091,3097 ****
  972. X  
  973. X  @group
  974. X  @example
  975. X! 1:  34 - 24 x^2 = 0    1:  x = -1.19023 s1     1:  1.19023
  976. X      .                      .                       .
  977. X  
  978. X      r 3                    H a S x RET  s 5        1 n l s1 RET
  979. X--- 3528,3534 ----
  980. X  
  981. X  @group
  982. X  @example
  983. X! 1:  34 - 24 x^2 = 0    1:  x = -1.19023 s1     1:  x = 1.19023
  984. X      .                      .                       .
  985. X  
  986. X      r 3                    H a S x RET  s 5        1 n l s1 RET
  987. X***************
  988. X*** 3132,3138 ****
  989. X  Here we have used a vector mapping operation to evaluate the function
  990. X  at several values of @samp{s1} at once.  @kbd{V M $} is like @kbd{V M '}
  991. X  except that it takes the formula from the top of the stack.  The
  992. X! formula is taken as a function to apply across the vector at the
  993. X  next-to-top stack level.  Once again, we press @key{RET} to use the
  994. X  default argument list.
  995. X  
  996. X--- 3569,3575 ----
  997. X  Here we have used a vector mapping operation to evaluate the function
  998. X  at several values of @samp{s1} at once.  @kbd{V M $} is like @kbd{V M '}
  999. X  except that it takes the formula from the top of the stack.  The
  1000. X! formula is interpreted as a function to apply across the vector at the
  1001. X  next-to-top stack level.  Once again, we press @key{RET} to use the
  1002. X  default argument list.
  1003. X  
  1004. X***************
  1005. X*** 3139,3144 ****
  1006. X--- 3576,3582 ----
  1007. X  If there had been several different values, we could have used
  1008. X  @kbd{V R X} to find the global maximum.
  1009. X  
  1010. X+ @cindex Area under a curve
  1011. X  What is the area under the portion of this curve from @samp{x} = 1 to 2?
  1012. X  This is simply the integral of the function:
  1013. X  
  1014. X***************
  1015. X*** 3163,3168 ****
  1016. X--- 3601,3615 ----
  1017. X  @end example
  1018. X  @end group
  1019. X  
  1020. X+ (@bullet) @strong{Exercise 2.}  Find the values for which the
  1021. X+ original formula @samp{17 x^2 - 6 x^4 + 3} is zero.  (There are
  1022. X+ four, two of which are complex numbers.)  @xref{Algebra Answer 2, 2}. (@bullet)
  1023. X+ 
  1024. X+ (@bullet) @strong{Exercise 3.}  Find the integral from 1 to @samp{y}
  1025. X+ of @samp{x sin(pi x)} (where the sine is calculated in radians).
  1026. X+ Find the values of the integral for integer @samp{y} from 1 to 5.
  1027. X+ @xref{Algebra Answer 3, 3}. (@bullet)
  1028. X+ 
  1029. X  Calc's integrator can do many simple integrals symbolically, but many
  1030. X  others are beyond its capabilities.  Suppose we wish to find the area
  1031. X  under the curve @samp{sin(x) ln(x)} over the same range of @samp{x}.  If
  1032. X***************
  1033. X*** 3171,3176 ****
  1034. X--- 3618,3625 ----
  1035. X  solution.  In fact, there is no closed-form solution to this integral.
  1036. X  Now what do we do?
  1037. X  
  1038. X+ @cindex Integration, numerical
  1039. X+ @cindex Numerical integration
  1040. X  One approach would be to do the integral numerically.  While there
  1041. X  is no built-in command for this in Calc, you can do it yourself
  1042. X  using vector mapping and reduction.  This is rather slow, though,
  1043. X***************
  1044. X*** 3179,3190 ****
  1045. X  
  1046. X  @group
  1047. X  @example
  1048. X! 1:  [1, 2, ... ]    1:  [10, 11, ... ]    1:  [1, 1.1, ... ]
  1049. X!     .                   .                     .
  1050. X  
  1051. X!     v x 10 RET          9 +                   0.1 *
  1052. X! @end example
  1053. X! @end group
  1054. X  
  1055. X  @group
  1056. X  @example
  1057. X--- 3628,3644 ----
  1058. X  
  1059. X  @group
  1060. X  @example
  1061. X! 3:  10                  1:  [1, 1.1, 1.2,  ...  , 1.8, 1.9 ]
  1062. X! 2:  1                       .
  1063. X! 1:  0.1
  1064. X!     .
  1065. X  
  1066. X!  10 RET 1 RET .1 RET        C-u v x
  1067. X! @end example
  1068. X! @end group
  1069. X! 
  1070. X! (Note that we have used the extended version of @kbd{v x}; we could
  1071. X! also have used plain @kbd{v x} as follows:  @kbd{v x 10 RET 9 + .1 *}.)
  1072. X  
  1073. X  @group
  1074. X  @example
  1075. X***************
  1076. X*** 3243,3249 ****
  1077. X  Better!  By increasing the precision and/or asking for more terms
  1078. X  in the Taylor series, we can get a result as accurate as we like.
  1079. X  
  1080. X! @node Programming Tutorial, , Algebra Tutorial, Tutorial
  1081. X  @section Programming Tutorial
  1082. X  
  1083. X  The Calculator is written entirely in Emacs Lisp, a highly extensible
  1084. X--- 3697,3747 ----
  1085. X  Better!  By increasing the precision and/or asking for more terms
  1086. X  in the Taylor series, we can get a result as accurate as we like.
  1087. X  
  1088. X! @cindex Simpson's rule
  1089. X! @cindex Integration by Simpson's rule
  1090. X! (@bullet) @strong{Exercise 4.}  Our first method approximated the
  1091. X! curve by stairsteps of width 0.1; the total area was then the sum
  1092. X! of the areas of the rectangles under these stairsteps.  Our second
  1093. X! method approximated the function by a polynomial, which turned out
  1094. X! to be a better approximation than stairsteps.  A third method is
  1095. X! @dfn{Simpson's rule}, which is like the stairstep method except
  1096. X! that the steps are not required to be flat.  Simpson's rule boils
  1097. X! down to the formula,
  1098. X! 
  1099. X! @example
  1100. X! (h/3) * (f(a) + 4 f(a+h) + 2 f(a+2h) + 4 f(a+3h) + ...
  1101. X!               + 2 f(a+(n-2)*h) + 4 f(a+(n-1)*h) + f(a+n*h))
  1102. X! @end example
  1103. X! 
  1104. X! @noindent
  1105. X! where @samp{n} (which must be even) is the number of slices and @samp{h}
  1106. X! is the width of each slice.  These are 10 and 0.1 in our example.
  1107. X! For reference, here is the corresponding equation for the stairstep
  1108. X! method:
  1109. X! 
  1110. X! @example
  1111. X! h * (f(a) + f(a+h) + f(a+2h) + f(a+3h) + ...
  1112. X!           + f(a+(n-2)*h) + f(a+(n-1)*h))
  1113. X! @end example
  1114. X! 
  1115. X! Compute the integral from 1 to 2 of @samp{sin(x) ln(x)} using
  1116. X! Simpson's rule with 10 slices.  @xref{Algebra Answer 4, 4}. (@bullet)
  1117. X! 
  1118. X! Aside from the commands we've seen so far, Calc also provides a
  1119. X! large set of commands for operating on parts of formulas.  You
  1120. X! indicate the desired sub-formula by placing the cursor on any part
  1121. X! of the formula before giving a @dfn{selection} command.  Selections won't
  1122. X! be covered in the tutorial; @pxref{Selecting Subformulas}, for
  1123. X! details and examples.
  1124. X! 
  1125. X! Another powerful algebraic facility is the @kbd{a r} and @kbd{j r}
  1126. X! commands, which manipulate a formula using @dfn{rewrite rules}.
  1127. X! @xref{Rewrite Rules}, for an explanation of this feature.
  1128. X! 
  1129. X! @c hard exercise: simplify (2^(n r) - 2^(r*(n - 1))) / (2^r - 1) 2^(n - 1)
  1130. X! @c                to 2^((n-1)*(r-1)).
  1131. X! 
  1132. X! @node Programming Tutorial, Answers to Exercises, Algebra Tutorial, Tutorial
  1133. X  @section Programming Tutorial
  1134. X  
  1135. X  The Calculator is written entirely in Emacs Lisp, a highly extensible
  1136. X***************
  1137. X*** 3303,3308 ****
  1138. X--- 3801,3818 ----
  1139. X  @end example
  1140. X  @end group
  1141. X  
  1142. X+ (@bullet) @strong{Exercise 1.}  Define a keyboard macro to negate
  1143. X+ the item in level 3 of the stack, without disturbing the rest of
  1144. X+ the stack.  @xref{Programming Answer 1, 1}. (@bullet)
  1145. X+ 
  1146. X+ (@bullet) @strong{Exercise 2.}  Define a keyboard macro to compute
  1147. X+ @samp{sin(x) / x}, where @samp{x} is the number on the top of the
  1148. X+ stack.  @xref{Programming Answer 2, 2}. (@bullet)
  1149. X+ 
  1150. X+ (@bullet) @strong{Exercise 3.}  Define a keyboard macro to compute
  1151. X+ the average (mean) value of a list of numbers.
  1152. X+ @xref{Programming Answer 3, 3}. (@bullet)
  1153. X+ 
  1154. X  In many programs, some of the steps must execute several times.
  1155. X  Calc has @dfn{looping} commands that allow this.  Loops are useful
  1156. X  inside keyboard macros, but actually work at any time.
  1157. X***************
  1158. X*** 3325,3330 ****
  1159. X--- 3835,3841 ----
  1160. X  If you make a mistake while entering the body of the loop,
  1161. X  type @kbd{Z C-g} to cancel the loop command.
  1162. X  
  1163. X+ @cindex Fibonacci numbers
  1164. X  Here's another example:
  1165. X  
  1166. X  @group
  1167. X***************
  1168. X*** 3343,3349 ****
  1169. X  of the loop body by hand; @kbd{C-j}, also on the Line-Feed or @key{LFD}
  1170. X  key if you have one, pushes a copy of the number in level 2.)
  1171. X  
  1172. X! A fascinating property of Fibonacci numbers is that the @var{n}th
  1173. X  Fibonacci number can be found directly by computing @samp{phi^n / sqrt(5)}
  1174. X  and then rounding to the nearest integer, where @samp{phi}, the
  1175. X  ``golden ratio,'' is @samp{(1 + sqrt(5)) / 2}.
  1176. X--- 3854,3862 ----
  1177. X  of the loop body by hand; @kbd{C-j}, also on the Line-Feed or @key{LFD}
  1178. X  key if you have one, pushes a copy of the number in level 2.)
  1179. X  
  1180. X! @cindex Golden ratio
  1181. X! @cindex Phi, golden ratio
  1182. X! A fascinating property of the Fibonacci numbers is that the @var{n}th
  1183. X  Fibonacci number can be found directly by computing @samp{phi^n / sqrt(5)}
  1184. X  and then rounding to the nearest integer, where @samp{phi}, the
  1185. X  ``golden ratio,'' is @samp{(1 + sqrt(5)) / 2}.
  1186. X***************
  1187. X*** 3358,3363 ****
  1188. X--- 3871,3893 ----
  1189. X  @end example
  1190. X  @end group
  1191. X  
  1192. X+ @cindex Continued fractions
  1193. X+ (@bullet) @strong{Exercise 4.}  The @dfn{continued fraction}
  1194. X+ representation of @samp{phi} is @samp{1 + 1/(1 + 1/(1 + 1/( ... )))}.
  1195. X+ We can compute an approximate value by carrying this however far
  1196. X+ and then replacing @samp{1/( ... )} by 1.  Approximate @samp{phi}
  1197. X+ using a twenty-term continued fraction.
  1198. X+ @xref{Programming Answer 4, 4}. (@bullet)
  1199. X+ 
  1200. X+ (@bullet) @strong{Exercise 5.}  Linear recurrences like the one for
  1201. X+ Fibonacci numbers can be expressed in terms of matrices.  Given a
  1202. X+ vector @samp{[a, b]} determine a matrix which, when multiplied by this
  1203. X+ vector, produces the vector @samp{[b, c]}, where @samp{a}, @samp{b} and
  1204. X+ @samp{c} are three successive Fibonacci numbers.  Now write a program
  1205. X+ that, given an integer @var{n}, computes the @var{n}th Fibonacci number
  1206. X+ using matrix arithmetic.  @xref{Programming Answer 5, 5}. (@bullet)
  1207. X+ 
  1208. X+ @cindex Harmonic numbers
  1209. X  A more sophisticated kind of loop is the @dfn{for} loop.  Suppose
  1210. X  we wish to compute the 20th ``harmonic'' number, which is equal to
  1211. X  the sum of the reciprocals of the integers from 1 to 20.
  1212. X***************
  1213. X*** 3400,3405 ****
  1214. X--- 3930,3939 ----
  1215. X  sequence @kbd{s + 7} adds the top-of-stack into the value in
  1216. X  variable 7.
  1217. X  
  1218. X+ (@bullet) @strong{Exercise 6.}  Use a ``for'' loop to find the first
  1219. X+ harmonic number which is greater than 4.0.
  1220. X+ @xref{Programming Answer 6, 6}. (@bullet)
  1221. X+ 
  1222. X  Of course, if we're going to be using variables in our programs,
  1223. X  we have to worry about the programs clobbering values that the
  1224. X  caller was keeping in those same variables.  This is easy to
  1225. X***************
  1226. X*** 3426,3436 ****
  1227. X  the contents of the stack, and the values of named variables,
  1228. X  survive past the @kbd{Z '} command.
  1229. X  
  1230. X  The @dfn{Bernoulli numbers} are a sequence with the interesting
  1231. X  property that all of the odd Bernoulli numbers are zero, and the
  1232. X  even ones, while difficult to compute, can be roughly approximated
  1233. X  by the formula @samp{2 n! / (2 pi)^n}.  Let's write a keyboard
  1234. X! macro to compute (approximate) Bernoulli numbers.
  1235. X  
  1236. X  @group
  1237. X  @example
  1238. X--- 3960,3974 ----
  1239. X  the contents of the stack, and the values of named variables,
  1240. X  survive past the @kbd{Z '} command.
  1241. X  
  1242. X+ @cindex Bernoulli numbers, approximate
  1243. X  The @dfn{Bernoulli numbers} are a sequence with the interesting
  1244. X  property that all of the odd Bernoulli numbers are zero, and the
  1245. X  even ones, while difficult to compute, can be roughly approximated
  1246. X  by the formula @samp{2 n! / (2 pi)^n}.  Let's write a keyboard
  1247. X! macro to compute (approximate) Bernoulli numbers.  (Calc has a
  1248. X! command, @kbd{k b}, to compute exact Bernoulli numbers, but
  1249. X! this command is very slow for large @var{n} since the higher
  1250. X! Bernoulli numbers are very large fractions.)
  1251. X  
  1252. X  @group
  1253. X  @example
  1254. X***************
  1255. X*** 3453,3463 ****
  1256. X  
  1257. X  @group
  1258. X  @example
  1259. X! 2:  0.0756823    1:  0          1:  0.25305    1:  0          1:  1.16659
  1260. X! 1:  0.0757575        .              .              .              .
  1261. X      .
  1262. X  
  1263. X!    5 RET 66 /   DEL DEL 11 K    DEL 12 K       DEL 13 K       DEL 14 K
  1264. X  @end example
  1265. X  @end group
  1266. X  
  1267. X--- 3991,4002 ----
  1268. X  
  1269. X  @group
  1270. X  @example
  1271. X! 3:  0.0756823    1:  0          1:  0.25305    1:  0          1:  1.16659
  1272. X! 2:  5:66             .              .              .              .
  1273. X! 1:  0.0757575
  1274. X      .
  1275. X  
  1276. X! 10 k b RET c f   M-0 DEL 11 K   DEL 12 K       DEL 13 K       DEL 14 K
  1277. X  @end example
  1278. X  @end group
  1279. X  
  1280. X***************
  1281. X*** 3479,3485 ****
  1282. X  we execute it, the list we are building will be in stack level 2
  1283. X  (initially this is an empty list), and the next Bernoulli number
  1284. X  will be in level 1.  The effect is to append the Bernoulli number
  1285. X! onto the end of the list.
  1286. X  
  1287. X  If you make a mistake when entering a keyboard macro, you can edit
  1288. X  it using @kbd{Z E}.  First, you must attach it to a key with @kbd{Z K}.
  1289. X--- 4018,4032 ----
  1290. X  we execute it, the list we are building will be in stack level 2
  1291. X  (initially this is an empty list), and the next Bernoulli number
  1292. X  will be in level 1.  The effect is to append the Bernoulli number
  1293. X! onto the end of the list.  (To create a table of exact fractional
  1294. X! Bernoulli numbers, just replace @kbd{K} with @kbd{k b} in the above
  1295. X! sequence of keystrokes.)
  1296. X! 
  1297. X! With loops and conditionals, you can program essentially anything
  1298. X! in Calc.  One other command that makes looping easier is @kbd{Z /},
  1299. X! which takes a condition from the stack and breaks out of the enclosing
  1300. X! loop if the condition is true (non-zero).  You can use this to make
  1301. X! ``while'' and ``until'' style loops.
  1302. X  
  1303. X  If you make a mistake when entering a keyboard macro, you can edit
  1304. X  it using @kbd{Z E}.  First, you must attach it to a key with @kbd{Z K}.
  1305. X***************
  1306. X*** 3488,3495 ****
  1307. X  
  1308. X  @group
  1309. X  @example
  1310. X! 1:  3                   1:  17          Keyboard Macro Editor.
  1311. X!     .                       .           Original keys: 1 7 RET
  1312. X  
  1313. X                                          type "1\r"
  1314. X                                          type "2"
  1315. X--- 4035,4042 ----
  1316. X  
  1317. X  @group
  1318. X  @example
  1319. X! 1:  3                   1:  3           Keyboard Macro Editor.
  1320. X!     .                       .           Original keys: 1 RET 2 +
  1321. X  
  1322. X                                          type "1\r"
  1323. X                                          type "2"
  1324. X***************
  1325. X*** 3535,3543 ****
  1326. X  calc-kbd-pop          # Restore values (Z ')
  1327. X  @end example
  1328. X  
  1329. X- If you don't know how to write a particular command in @file{macedit}
  1330. X- format, you can always write it as keystrokes in a @code{type} command.
  1331. X- 
  1332. X  Press @kbd{C-c C-c} to finish editing.
  1333. X  
  1334. X  @group
  1335. X--- 4082,4087 ----
  1336. X***************
  1337. X*** 3549,3554 ****
  1338. X--- 4093,6123 ----
  1339. X  @end example
  1340. X  @end group
  1341. X  
  1342. X+ If you don't know how to write a particular command in @file{macedit}
  1343. X+ format, you can always write it as keystrokes in a @code{type} command.
  1344. X+ There is also a @code{keys} command which interprets the rest of the
  1345. X+ line as standard Emacs keystroke names.  In fact, @file{macedit} defines
  1346. X+ a handy @code{read-kbd-macro} command which reads the current region
  1347. X+ of the current buffer as a sequence of keystroke names, and defines that
  1348. X+ sequence on the @kbd{K} (and @kbd{C-x e}) key.  Try reading in the macro
  1349. X+ in the following form:  Press @kbd{C-@@} (or @kbd{C-SPC}) at one end of
  1350. X+ the text below, then type @kbd{M-x read-kbd-macro} at the other.
  1351. X+ 
  1352. X+ @group
  1353. X+ @example
  1354. X+ Z ` 0 s 1 DEL
  1355. X+     1 TAB
  1356. X+     Z (  & s + 1 DEL  1 Z )
  1357. X+     r 1
  1358. X+ Z '
  1359. X+ @end example
  1360. X+ @end group
  1361. X+ 
  1362. X+ (@bullet) @strong{Exercise 7.}  A general algorithm for solving
  1363. X+ equations numerically is @dfn{Newton's Method}.  Given the equation
  1364. X+ @samp{f(x) = 0} for any function @samp{f}, and an initial guess
  1365. X+ @samp{x0} which is reasonably close to the desired solution, apply
  1366. X+ this formula over and over:
  1367. X+ 
  1368. X+ @example
  1369. X+ new_x = x - f(x)/f'(x)
  1370. X+ @end example
  1371. X+ 
  1372. X+ where @samp{f'(x)} is the derivative of @samp{f}.  The @samp{x}
  1373. X+ values will quickly converge to a solution, i.e., eventually
  1374. X+ @samp{new_x} and @samp{x} will be equal to within the limits
  1375. X+ of the current precision.  Write a program which takes a formula
  1376. X+ involving the variable @samp{x}, and an initial guess @samp{x0},
  1377. X+ on the stack, and produces a value of @samp{x} for which the formula
  1378. X+ is zero.  Use it to find a solution of @samp{sin(cos(x)) = 0.5}
  1379. X+ near @samp{x = 4.5}.  (Use angles measured in radians.)  Note that
  1380. X+ the built-in @kbd{a R} (@code{calc-find-root}) command uses Newton's
  1381. X+ method when it is able.  @xref{Programming Answer 7, 7}. (@bullet)
  1382. X+ 
  1383. X+ @cindex Digamma function
  1384. X+ @cindex Gamma constant, Euler's
  1385. X+ (@bullet) @strong{Exercise 8.}  The @dfn{digamma} function @samp{psi(z)}
  1386. X+ is defined as the derivative of @samp{ln(gamma(z))}.  For large
  1387. X+ values of @samp{z}, it can be approximated by the infinite sum
  1388. X+ 
  1389. X+ @example
  1390. X+ psi(z) ~= ln(z) - 1/2z - sum(bern(2 n) / 2 n z^(2 n))
  1391. X+ @end example
  1392. X+ 
  1393. X+ @noindent
  1394. X+ where @code{sum} represents the sum over @var{n} from 1 to infinity
  1395. X+ (or to some limit high enough to give the desired accuracy), and
  1396. X+ the @code{bern} function produces (exact) Bernoulli numbers.
  1397. X+ While this sum is not guaranteed to converge, in practice it is safe.
  1398. X+ An interesting mathematical constant is Euler's gamma, which is equal
  1399. X+ to about 0.5772.  One way to compute it is by the formula,
  1400. X+ @samp{gamma = -psi(1)}.  Unfortunately, 1 isn't a large enough argument
  1401. X+ for the above formula to work (5 is a much safer value for @code{z}).
  1402. X+ Fortunately, we can compute @samp{psi(1)} from @samp{psi(5)} using
  1403. X+ the recurrence @samp{psi(z+1) = psi(z) + 1/z}.  Your task:  Develop
  1404. X+ a program to compute @samp{psi(z)}; it should ``pump up'' @code{z}
  1405. X+ if necessary to be greater than 5, then use the above summation
  1406. X+ formula.  Use your function to compute @samp{gamma} to twelve decimal
  1407. X+ places.  @xref{Programming Answer 8, 8}. (@bullet)
  1408. X+ 
  1409. X+ @cindex Polynomial, list of coefficients
  1410. X+ (@bullet) @strong{Exercise 9.}  Given a polynomial in @samp{x} and
  1411. X+ a number @var{m} on the stack, where the polynomial is of degree
  1412. X+ @var{m} or less (i.e., does not have any terms higher than @samp{x^@var{m}}),
  1413. X+ write a program to convert the polynomial into a list-of-coefficients
  1414. X+ notation.  For example, @samp{5 x^4 + (x + 1)^2} with @var{m} = 6
  1415. X+ should produce the list @samp{[1, 2, 1, 0, 5, 0, 0]}.  Also develop
  1416. X+ a way to convert from this form back to the standard algebraic form.
  1417. X+ @xref{Programming Answer 9, 9}. (@bullet)
  1418. X+ 
  1419. X+ @cindex Recursion
  1420. X+ (@bullet) @strong{Exercise 10.}  The @dfn{Stirling numbers of the
  1421. X+ first kind} are defined by the recurrences,
  1422. X+ 
  1423. X+ @example
  1424. X+ s(n,n) = 1   for n >= 0,
  1425. X+ s(n,0) = 0   for n > 0,
  1426. X+ s(n+1,m) = s(n,m-1) - n s(n,m)   for n >= m >= 1.
  1427. X+ @end example
  1428. X+ 
  1429. X+ @noindent
  1430. X+ This can be implemented using a @dfn{recursive} program in Calc; the
  1431. X+ program must invoke itself in order to calculate the two righthand
  1432. X+ terms in the general formula.  Since it always invokes itself with
  1433. X+ ``simpler'' arguments, it's easy to see that it will always
  1434. X+ eventually finish the computation.  Recursion is a little
  1435. X+ difficult with Emacs keyboard macros since the macro is executed
  1436. X+ before its definition is complete.  So here's the recommended
  1437. X+ strategy:  Create a ``dummy macro'' and assign it to a key with,
  1438. X+ e.g., @kbd{Z K s}.  Now enter the true definition, using the
  1439. X+ @kbd{z s} command to call itself recursively, then assign it to
  1440. X+ the same key with @kbd{Z K s}.  Now the @kbd{z s} command will
  1441. X+ run the complete recursive program.  The task:  Write a program
  1442. X+ that computes Stirling numbers of the first kind.  Test it with
  1443. X+ @emph{small} inputs like @samp{s(4,2)}.  (There is a built-in
  1444. X+ command for Stirling numbers, @kbd{k s}, which you can use to
  1445. X+ check your answers.)  @xref{Programming Answer 10, 10}. (@bullet)
  1446. X+ 
  1447. X+ 
  1448. X+ This ends the tutorial section of the Calc manual.  Now you know enough
  1449. X+ about Calc to use it effectively for many kinds of calculations.  But
  1450. X+ Calc has many features that were not even touched upon here.  The rest
  1451. X+ of this manual tells the whole story.
  1452. SHAR_EOF
  1453. echo "End of part 13, continue with part 14"
  1454. echo "14" > s2_seq_.tmp
  1455. exit 0
  1456.  
  1457.