v15i040: Patch for GNU Emacs Calc, version 1.04 -> 1.05, part 13/20

David Gillespie daveg at csvax.cs.caltech.edu
Mon Oct 15 11:19:14 AEST 1990


Posting-number: Volume 15, Issue 40
Submitted-by: daveg at csvax.cs.caltech.edu (David Gillespie)
Archive-name: calc-1.05/part13

#!/bin/sh
# this is part 13 of a multipart archive
# do not concatenate these parts, unpack them in order with /bin/sh
# file calc.patch continued
#
CurArch=13
if test ! -r s2_seq_.tmp
then echo "Please unpack part 1 first!"
     exit 1; fi
( read Scheck
  if test "$Scheck" != $CurArch
  then echo "Please unpack part $Scheck next!"
       exit 1;
  else exit 0; fi
) < s2_seq_.tmp || exit 1
sed 's/^X//' << 'SHAR_EOF' >> calc.patch
X  @kbd{T} (tangent), @kbd{E} (@samp{e^x}) and @kbd{L} (natural
X***************
X*** 1835,1841 ****
X  Let's compute the sine and cosine of an angle, and verify the
X  identity @samp{sin(@var{x})^2 + cos(@var{x})^2 = 1}.  We'll
X  arbitrarily pick -64 degrees as a good value for @var{x}.  With
X! the angular mode set to degrees (@kbd{m d}), do:
X  
X  @group
X  @example
X--- 1931,1937 ----
X  Let's compute the sine and cosine of an angle, and verify the
X  identity @samp{sin(@var{x})^2 + cos(@var{x})^2 = 1}.  We'll
X  arbitrarily pick -64 degrees as a good value for @var{x}.  With
X! the angular mode set to degrees (type @kbd{m d}), do:
X  
X  @group
X  @example
X***************
X*** 1843,1849 ****
X  1:  -64        1:  -0.89879   1:  -64        1:  0.43837        .
X      .              .              .              .
X  
X!  64 n RET RET      S             TAB             C            x hypot
X  @end example
X  @end group
X  
X--- 1939,1945 ----
X  1:  -64        1:  -0.89879   1:  -64        1:  0.43837        .
X      .              .              .              .
X  
X!  64 n RET RET      S             TAB             C             f h
X  @end example
X  @end group
X  
X***************
X*** 1850,1855 ****
X--- 1946,1954 ----
X  (For brevity, we're showing only five digits of the results here.
X  You can of course do these calculations to any precision you like.)
X  
X+ Remember, @kbd{f h} is the @code{calc-hypot}, or square-root of sum
X+ of squares, command.
X+ 
X  Another identity is @samp{tan(@var{x}) = sin(@var{x}) / cos(@var{x})}.
X  
X  @group
X***************
X*** 1880,1886 ****
X  How can the angle be the same?  The answer is that the @kbd{/} operation
X  loses information about the signs of its inputs.  Because the quotient
X  is negative, we know exactly one of the inputs was negative, but we
X! can't tell which one.  There is an @code{arctan2} function which
X  computes the inverse tangent of the quotient of a pair of numbers.
X  Since you feed it the two original numbers, it has enough information
X  to give you a full 360-degree answer.
X--- 1979,1985 ----
X  How can the angle be the same?  The answer is that the @kbd{/} operation
X  loses information about the signs of its inputs.  Because the quotient
X  is negative, we know exactly one of the inputs was negative, but we
X! can't tell which one.  There is an @kbd{f T} [@code{arctan2}] function which
X  computes the inverse tangent of the quotient of a pair of numbers.
X  Since you feed it the two original numbers, it has enough information
X  to give you a full 360-degree answer.
X***************
X*** 1892,1898 ****
X      .                         1:  0.43837        .
X                                    .
X  
X!    U U          x arctan2       X M-2 n       x arctan2         -
X  @end example
X  @end group
X  
X--- 1991,1997 ----
X      .                         1:  0.43837        .
X                                    .
X  
X!    U U            f T           X M-2 n         f T             -
X  @end example
X  @end group
X  
X***************
X*** 1944,1950 ****
X  
X  @group
X  @example
X! 1:  1000       1:  6.9077     1:  1000       1:  3.
X      .              .              .              .
X  
X      1000           L              U              H L
X--- 2043,2049 ----
X  
X  @group
X  @example
X! 1:  1000       1:  6.9077     1:  1000       1:  3
X      .              .              .              .
X  
X      1000           L              U              H L
X***************
X*** 1959,1965 ****
X  
X  @group
X  @example
X! 2:  1000       1:  3.         1:  1000.      2:  1000.      1:  6.9077
X  1:  10             .              .          1:  2.71828        .
X      .                                            .
X  
X--- 2058,2064 ----
X  
X  @group
X  @example
X! 2:  1000       1:  3          1:  1000.      2:  1000.      1:  6.9077
X  1:  10             .              .          1:  2.71828        .
X      .                                            .
X  
X***************
X*** 1973,1978 ****
X--- 2072,2092 ----
X  that @kbd{P} with the hyperbolic prefix pushes the constant @samp{e}
X  onto the stack.
X  
X+ You may have noticed that both times we took the base-10 logarithm
X+ of 1000, we got an exact integer result.  Calc always tries to give
X+ an exact rational result for calculations involving rational numbers
X+ where possible.  But when we used @kbd{H E}, the result was a
X+ floating-point number for no apparent reason.  In fact, if we had
X+ computed @kbd{10 @key{RET} 3 ^} we @emph{would} have gotten an
X+ exact integer 1000.  But the @kbd{H E} command is rigged to generate
X+ a floating-point result all of the time so that @samp{1000 H E} will
X+ not waste time computing a thousand-digit integer when all you
X+ probably wanted was @samp{1e1000}.
X+ 
X+ (@bullet) @strong{Exercise 2.}  Find a pair of integer inputs to
X+ the @kbd{B} command for which Calc could find an exact rational
X+ result but doesn't.  @xref{Arithmetic Answer 2, 2}. (@bullet)
X+ 
X  The Calculator also has a set of functions relating to combinatorics
X  and statistics.  You may be familiar with the @dfn{factorial} function,
X  which computes the product of all the integers up to a given number.
X***************
X*** 1993,2009 ****
X  exact integer, you get an exact integer result (158 digits long
X  in this case).
X  
X! If you take the factorial of a non-integer, Calc uses the generalized
X! factorial function defined in terms of Euler's Gamma function.
X  
X  @group
X  @example
X! 3:  4.         3:  24.               1:  5.5        1:  52.342777846
X! 2:  4.5        2:  52.3427777846         .              .
X  1:  5.         1:  120.
X      .              .
X  
X!                    M-3 !              M-0 DEL 5.5      x gamma
X  @end example
X  @end group
X  
X--- 2107,2124 ----
X  exact integer, you get an exact integer result (158 digits long
X  in this case).
X  
X! If you take the factorial of a non-integer, Calc uses a generalized
X! factorial function defined in terms of Euler's Gamma function (which
X! is itself available as the @kbd{f g} command).
X  
X  @group
X  @example
X! 3:  4.         3:  24.               1:  5.5        1:  52.342777847
X! 2:  4.5        2:  52.3427777847         .              .
X  1:  5.         1:  120.
X      .              .
X  
X!                    M-3 !              M-0 DEL 5.5       f g
X  @end example
X  @end group
X  
X***************
X*** 2012,2018 ****
X  The binomial coefficient @var{n}-choose- at var{m} can be defined as
X  @samp{@var{n}! / @var{m}! (@var{n}- at var{m})!} for all reals @var{n} and
X  @var{m}.  The intermediate results in this formula can become quite
X! large even if the result is small; the @kbd{k b} command computes
X  a binomial coefficient in a way that avoids large intermediate
X  values.
X  
X--- 2127,2133 ----
X  The binomial coefficient @var{n}-choose- at var{m} can be defined as
X  @samp{@var{n}! / @var{m}! (@var{n}- at var{m})!} for all reals @var{n} and
X  @var{m}.  The intermediate results in this formula can become quite
X! large even if the final result is small; the @kbd{k c} command computes
X  a binomial coefficient in a way that avoids large intermediate
X  values.
X  
X***************
X*** 2026,2032 ****
X  1:  20             .              .
X      .
X  
X!  30 RET 20        k b            k f
X  @end example
X  @end group
X  
X--- 2141,2147 ----
X  1:  20             .              .
X      .
X  
X!  30 RET 20         k c            k f
X  @end example
X  @end group
X  
X***************
X*** 2035,2040 ****
X--- 2150,2156 ----
X  multiply them back together.  The result is the original number,
X  30045015.
X  
X+ @cindex Hash tables
X  Suppose a program you are writing needs a hash table with at least
X  10000 entries.  It's best to use a prime number as the actual size
X  of a hash table.  Calc can compute the next prime number after 10000:
X***************
X*** 2100,2105 ****
X--- 2216,2222 ----
X  @end example
X  @end group
X  
X+ @cindex Dot product
X  The dot product of two vectors is equal to the product of their
X  lengths times the cosine of the angle between them.  (Here the vector
X  is interpreted as a line from the origin @samp{(0,0,0)} to the
X***************
X*** 2125,2130 ****
X--- 2242,2249 ----
X  The inverse cosine finds that the angle between the vectors
X  is about 56 degrees.
X  
X+ @cindex Cross product
X+ @cindex Perpendicular vectors
X  The @dfn{cross product} of two vectors is a vector whose length
X  is the product of the lengths of the inputs times the sine of the
X  angle between them, and whose direction is perpendicular to both
X***************
X*** 2143,2158 ****
X  @end example
X  @end group
X  
X- Vector-related commands generally begin with the @kbd{v} prefix key.
X- Some are uppercase letters and some are lowercase.  To make it easier
X- to type these commands, the shift- at kbd{V} key acts the same as the
X- @kbd{v} prefix.
X- 
X  First we recall the original vectors and compute their cross product,
X  which we also store for later reference.  Now we divide the vector
X  by the product of the lengths of the original vectors.  The length of
X  this vector should be the sine of the angle; sure enough, it is!
X  
X  If we take the dot product of two perpendicular vectors we expect
X  to get zero, since the cosine of 90 degrees is zero.  Let's check
X  that the cross product is indeed perpendicular to both inputs:
X--- 2262,2278 ----
X  @end example
X  @end group
X  
X  First we recall the original vectors and compute their cross product,
X  which we also store for later reference.  Now we divide the vector
X  by the product of the lengths of the original vectors.  The length of
X  this vector should be the sine of the angle; sure enough, it is!
X  
X+ Vector-related commands generally begin with the @kbd{v} prefix key.
X+ Some are uppercase letters and some are lowercase.  To make it easier
X+ to type these commands, the shift- at kbd{V} key acts the same as the
X+ @kbd{v} prefix.  (@pxref{Mode Settings}, for a way to make all
X+ prefix keys have this property.)
X+ 
X  If we take the dot product of two perpendicular vectors we expect
X  to get zero, since the cosine of 90 degrees is zero.  Let's check
X  that the cross product is indeed perpendicular to both inputs:
X***************
X*** 2167,2172 ****
X--- 2287,2306 ----
X  @end example
X  @end group
X  
X+ @cindex Normalizing a vector
X+ @cindex Unit vectors
X+ (@bullet) @strong{Exercise 1.}  Given a vector on the top of the
X+ stack, what keystrokes would you use to @dfn{normalize} the
X+ vector, i.e., to reduce its length to one without changing its
X+ direction?  @xref{Vector Answer 1, 1}. (@bullet)
X+ 
X+ (@bullet) @strong{Exercise 2.}  Suppose a certain particle can be
X+ at any of several positions along a ruler.  You have a list of
X+ those positions in the form of a vector, and another list of the
X+ probabilities for the particle to be at the corresponding positions.
X+ Find the average position of the particle.
X+ @xref{Vector Answer 2, 2}. (@bullet)
X+ 
X  @node Matrix Tutorial, List Tutorial, Vector Analysis Tutorial, Vector/Matrix Tutorial
X  @subsection Matrices
X  
X***************
X*** 2253,2258 ****
X--- 2387,2398 ----
X  rows in the matrix is different from the number of elements in the
X  vector.
X  
X+ (@bullet) @strong{Exercise 1.}  Use @samp{*} to sum along the rows
X+ of the above 2x3 matrix to get @samp{[6, 15]}.  Now use @samp{*} to
X+ sum along the columns to get @samp{[5, 7, 9]}.
X+ @xref{Matrix Answer 1, 1}. (@bullet)
X+ 
X+ @cindex Identity matrix
X  An @dfn{identity matrix} is a square matrix with ones along the
X  diagonal and zeros elsewhere.  It has the property that multiplication
X  by an identity matrix, on the left or on the right, always produces
X***************
X*** 2304,2309 ****
X--- 2444,2451 ----
X  @end example
X  @end group
X  
X+ @cindex Systems of linear equations
X+ @cindex Linear equations, systems of
X  Matrix inverses are related to systems of linear equations in algebra.
X  Suppose we had the following set of equations:
X  
X***************
X*** 2352,2360 ****
X--- 2494,2545 ----
X  The result is 6.00001, reasonably correct considering that we cheated
X  and didn't type all the digits of @samp{c}.
X  
X+ (@bullet) @strong{Exercise 2.}  Algebraic entry allows you to make
X+ vectors and matrices that include variables.  Solve the following
X+ system of equations to get expressions for @samp{x} and @samp{y}
X+ in terms of @samp{a} and @samp{b}.
X+ 
X+ @group
X+ @example
X+    x + a y = 6
X+    x + b y = 10
X+ @end example
X+ @end group
X+ 
X+ @xref{Matrix Answer 2, 2}. (@bullet)
X+ 
X+ @cindex Least-squares for over-determined systems
X+ @cindex Over-determined systems of equations
X+ (@bullet) @strong{Exercise 3.}  A system of equations is ``over-determined''
X+ if it has more equations than variables.  It is often the case that
X+ there are no values for the variables that will satisfy all the
X+ equations at once, but it is still useful to find a set of values
X+ which ``nearly'' satisfy all the equations.  In terms of matrix equations,
X+ you can't solve @samp{A * X = B} directly because the matrix @samp{A}
X+ is not square for an over-determined system.  Matrix inversion works
X+ only for square matrices.  One common trick is to multiply both sides
X+ on the left by the transpose of @samp{A}:  @samp{trn(A)*A*X = trn(A)*B}.
X+ Now @samp{trn(A)*A} is a square matrix so a solution is possible.  It
X+ turns out that the @samp{X} vector you compute in this way will be a
X+ ``least-squares'' solution, which can be regarded as the ``closest''
X+ solution to the set of equations.  Use Calc to solve the following
X+ over-determined system:
X+ 
X+ @group
X+ @example
X+     a + 2b + 3c = 6
X+    4a + 5b + 6c = 2
X+    7a + 6b      = 3
X+    2a + 4b + 6c = 11
X+ @end example
X+ @end group
X+ 
X+ @xref{Matrix Answer 3, 3}. (@bullet)
X+ 
X  @node List Tutorial, , Matrix Tutorial, Vector/Matrix Tutorial
X  @subsection Vectors as Lists
X  
X+ @cindex Lists
X  Although Calc has a number of features for manipulating vectors and
X  matrices as mathematical objects, you can also treat vectors as
X  simple lists of values.  For example, we saw that the @kbd{k f}
X***************
X*** 2406,2411 ****
X--- 2591,2599 ----
X  other a plain number.)  In the final step, we take the square root
X  of each element.
X  
X+ (@bullet) @strong{Exercise 1.}  Compute a vector of powers of two
X+ from @samp{2^-4} to @samp{2^4}.  @xref{List Answer 1, 1}. (@bullet)
X+ 
X  You can also @dfn{reduce} a binary operator across a vector.
X  For example, reducing @samp{*} computes the product of all the
X  elements in the vector:
X***************
X*** 2439,2444 ****
X--- 2627,2650 ----
X  sum of pairwise products of the elements to get the same answer
X  for the dot product as before.
X  
X+ A slight variant of vector reduction is the @dfn{accumulate} operation,
X+ @kbd{H V R}.  This produces a vector of the intermediate results from
X+ a corresponding reduction.  Here we compute a table of factorials:
X+ 
X+ @group
X+ @example
X+ 1:  [1, 2, 3, 4, 5, 6]    1:  [1, 2, 6, 24, 120, 720]
X+     .                         .
X+ 
X+     v x 6 RET                 H V R *
X+ @end example
X+ @end group
X+ 
X+ @cindex Least-squares for fitting a straight line
X+ @cindex Fitting data to a line
X+ @cindex Line, fitting data to
X+ @cindex Data, extracting from buffers
X+ @cindex Columns of data, extracting
X  As a larger example, let's try to fit a straight line to some data,
X  using the method of least squares.  Suppose we have the following
X  list of values in a file we have loaded into Emacs:
X***************
X*** 2472,2479 ****
X  manual and find this table there.  (Press @kbd{g}, then type
X  @kbd{List Tutorial}, to jump straight to this section.)
X  
X! Position the cursor at the upper-left corner of this table, right
X  to the left of the @samp{1.34}.  Press @kbd{C-@@} to set the mark.
X  Now position the cursor to the lower-right, just after the @samp{1.354}.
X  You have now defined this region as an Emacs ``rectangle.''  Still
X  in the Info buffer, type @kbd{M-x calc-grab-region}.  This command
X--- 2678,2686 ----
X  manual and find this table there.  (Press @kbd{g}, then type
X  @kbd{List Tutorial}, to jump straight to this section.)
X  
X! Position the cursor at the upper-left corner of this table, just
X  to the left of the @samp{1.34}.  Press @kbd{C-@@} to set the mark.
X+ (This may also be found on @kbd{C-2}, @kbd{C-SPC}, or @kbd{C-NUL}.)
X  Now position the cursor to the lower-right, just after the @samp{1.354}.
X  You have now defined this region as an Emacs ``rectangle.''  Still
X  in the Info buffer, type @kbd{M-x calc-grab-region}.  This command
X***************
X*** 2488,2493 ****
X--- 2695,2704 ----
X  @end example
X  @end group
X  
X+ (If the size of this matrix is awkward, you may wish to type @kbd{v .}
X+ [the letter `v' followed by a period] to enable ``abbreviated vectors''
X+ display mode.)
X+ 
X  We want to treat this as a pair of lists.  The first step is to
X  transpose this matrix into a pair of rows.  Remember, a matrix is
X  just a vector of vectors.  So we can unpack the matrix into a pair
X***************
X*** 2545,2552 ****
X  @end group
X  
X  These are @samp{sum(x)}, @samp{sum(x^2)}, @samp{sum(y)}, and @samp{sum(x y)},
X! respectively.  Finally, we also need @samp{N}, the number of data points.
X! This is just the length of either of our lists.
X  
X  @group
X  @example
X--- 2756,2764 ----
X  @end group
X  
X  These are @samp{sum(x)}, @samp{sum(x^2)}, @samp{sum(y)}, and @samp{sum(x y)},
X! respectively.  (We could have used @kbd{*} to compute @samp{sum(x^2)} and
X! @samp{sum(x y)}.)  Finally, we also need @samp{N}, the number of data
X! points.  This is just the length of either of our lists.
X  
X  @group
X  @example
X***************
X*** 2581,2588 ****
X  @end example
X  @end group
X  
X! That's the slope @var{m}.  The intercept @var{b} can now be found with the
X! simple formula @samp{(sum(y) - @var{m} sum(x)) / N}.
X  
X  @group
X  @example
X--- 2793,2800 ----
X  @end example
X  @end group
X  
X! That gives us the slope @var{m}.  The y-intercept @var{b} can now
X! be found with the simple formula, @samp{(sum(y) - @var{m} sum(x)) / N}.
X  
X  @group
X  @example
X***************
X*** 2602,2608 ****
X  1:  [0.699, 0.735, ... ]    1:  [0.273, 0.309, ... ]
X      .                           .
X  
X!     r 1 r 8 *                   r 9 +
X  @end example
X  @end group
X  
X--- 2814,2820 ----
X  1:  [0.699, 0.735, ... ]    1:  [0.273, 0.309, ... ]
X      .                           .
X  
X!     r 1 r 8 *                   r 9 +    s 0
X  @end example
X  @end group
X  
X***************
X*** 2625,2642 ****
X  
X  First we compute a vector of differences, then we take the absolute
X  values of these differences, then we reduce the @code{max} function
X! across the vector.  (Even though @code{max} is not normally assigned
X! a letter, so that you would have to type @kbd{x max} to get it,
X! reducing @code{max} and @code{min} is so common that @kbd{X} and
X! @kbd{N} are accepted for those functions in that context.  In
X! general, you answer the @kbd{V M} or @kbd{V R} prompt with the
X! actual key sequence that invokes the function you want.  You could
X! have typed @kbd{V R x max @key{RET}} if you had preferred.)
X  
X  As another example, a theorem about binomial coefficients tells
X  us that the alternating sum of binomial coefficients @var{n}-choose-0 minus
X! @var{n}-choose-1 plus @var{n}-choose-2, and so on, always comes out
X! to zero.  Let's verify this for @var{n}=6.
X  
X  @group
X  @example
X--- 2837,2918 ----
X  
X  First we compute a vector of differences, then we take the absolute
X  values of these differences, then we reduce the @code{max} function
X! across the vector.  (The @code{max} function is on the two-key sequence
X! @kbd{f x}; because it is so common to use @code{max} in a vector
X! operation, the letters @kbd{X} and @kbd{N} are also accepted for
X! @code{max} and @code{min} in this context.  In general, you answer
X! the @kbd{V M} or @kbd{V R} prompt with the actual key sequence that
X! invokes the function you want.  You could have typed @kbd{V R f x} or
X! even @kbd{V R x max @key{RET}} if you had preferred.)
X! 
X! If your system has the GNUPLOT program, you can see graphs of your
X! data and your straight line to see how well they match.  (The following
X! instructions assume you have GNUPLOT correctly installed and are using
X! the X window system.  They may not work in other circumstances.)
X! 
X! Let's start by plotting the original data.  Recall the ``x'' and ``y''
X! vectors onto the stack and press @kbd{g f}.  This ``fast'' graphing
X! command does everything you need to do for simple, straightforward
X! plotting of data.
X! 
X! @group
X! @example
X! 2:  [1.34, 1.41, 1.49, ... ]
X! 1:  [0.234, 0.298, 0.402, ... ]
X!     .
X! 
X!     r 1 r 2    g f
X! @end example
X! @end group
X! 
X! If all goes well, you will shortly get a new window containing a graph
X! of the data.  (If not, contact your GNUPLOT or Calc installer to find
X! out what went wrong.)
X! 
X! Next, let's add the line we got from our least-squares fit:
X! 
X! @group
X! @example
X! 2:  [1.34, 1.41, 1.49, ... ]
X! 1:  [0.273, 0.309, 0.351, ... ]
X!     .
X! 
X!     DEL r 0    g a  g p
X! @end example
X! @end group
X! 
X! It's not very useful to get symbols to mark the data points on this
X! second curve; you can type @kbd{g S g p} to remove them.  Type @kbd{g K}
X! to remove the graphics window when you are done.
X! 
X! (@bullet) @strong{Exercise 2.}  An earlier exercise showed how to do
X! least squares fitting to a general system of equations.  Our 19 data
X! points are really 19 equations of the form @samp{y = m x + b} for
X! different pairs of @samp{(x,y)}.  Use the matrix-transpose method
X! to solve for @samp{m} and @samp{b}, duplicating the above result.
X! @xref{List Answer 2, 2}. (@bullet)
X! 
X! @cindex Geometric mean
X! (@bullet) @strong{Exercise 3.}  If the input data do not form a
X! rectangle, you can use @kbd{C-u M-x calc-grab-region} to grab the
X! data the way Emacs normally works with regions---it reads left-to-right,
X! top-to-bottom, treating line breaks the same as spaces.  Use this
X! command to find the geometric mean of the following numbers.  (The
X! geometric mean is the @var{n}th root of the product of @var{n} numbers.)
X! 
X! @example
X! 2.3, 6, 22, 15.1, 7,
X!   15, 14, 7.5,
X!   2.5
X! @end example
X! 
X! @xref{List Answer 3, 3}. (@bullet)
X  
X  As another example, a theorem about binomial coefficients tells
X  us that the alternating sum of binomial coefficients @var{n}-choose-0 minus
X! @var{n}-choose-1 plus @var{n}-choose-2, and so on up to
X! @var{n}-choose- at var{n}, always comes out to zero.  Let's verify this
X! for @var{n}=6. at refill
X  
X  @group
X  @example
X***************
X*** 2665,2670 ****
X--- 2941,3033 ----
X  to each element of the vector, substituting each element's value for
X  @samp{n} in turn.
X  
X+ Another group of operators that are often useful with @kbd{V M} are
X+ the relational operators:  @kbd{a =}, for example, compares two numbers
X+ and gives the result 1 if they are equal, or 0 if not.  Similarly,
X+ @kbd{a <} checks for one number being less than another.
X+ 
X+ Other useful vector operations include @kbd{v v}, to reverse a
X+ vector end-for-end; @kbd{V S}, to sort the elements of a vector
X+ into increasing order; and @kbd{v r} and @kbd{v c}, to extract
X+ one row or column of a matrix, or (in both cases) to extract one
X+ element of a plain vector.  With a negative argument, @kbd{v r}
X+ and @kbd{v c} instead delete one row, column, or vector element.
X+ 
X+ @cindex Divisor functions
X+ (@bullet) @strong{Exercise 4.}  The @var{k}th @dfn{divisor function}
X+ is the sum of the @var{k}th powers of all the divisors of an
X+ integer @var{n}.  Figure out a method for computing the divisor
X+ function for reasonably small values of @var{n}.  As a test,
X+ the 0th and 1st divisor functions of 30 are 8 and 72, respectively.
X+ @xref{List Answer 4, 4}. (@bullet)
X+ 
X+ @cindex Square-free numbers
X+ @cindex Duplicate values in a list
X+ (@bullet) @strong{Exercise 5.}  The @kbd{k f} command produces a
X+ list of prime factors for a number.  Sometimes it is important to
X+ know that a number is @dfn{square-free}, i.e., that no prime occurs
X+ more than once in its list of prime factors.  Find a sequence of
X+ keystrokes to tell if a number is square-free; your method should
X+ leave 1 on the stack if it is, or 0 if it isn't.
X+ @xref{List Answer 5, 5}. (@bullet)
X+ 
X+ @cindex Triangular lists
X+ (@bullet) @strong{Exercise 6.}  Build a list of lists that looks
X+ like the following diagram.  (You may wish to use the @kbd{v /}
X+ command to enable multi-line display of vectors.)
X+ 
X+ @group
X+ @example
X+ 1:  [ [1],
X+       [1, 2],
X+       [1, 2, 3],
X+       [1, 2, 3, 4],
X+       [1, 2, 3, 4, 5],
X+       [1, 2, 3, 4, 5, 6] ]
X+ @end example
X+ @end group
X+ 
X+ @xref{List Answer 6, 6}. (@bullet)
X+ 
X+ (@bullet) @strong{Exercise 7.}  Build the following list of lists.
X+ 
X+ @group
X+ @example
X+ 1:  [ [0],
X+       [1, 2],
X+       [3, 4, 5],
X+       [6, 7, 8, 9],
X+       [10, 11, 12, 13, 14],
X+       [15, 16, 17, 18, 19, 20] ]
X+ @end example
X+ @end group
X+ 
X+ @xref{List Answer 7, 7}. (@bullet)
X+ 
X+ @cindex Maximizing a function over a list of values
X+ (@bullet) @strong{Exercise 8.}  Compute a list of values of Bessel's
X+ J1 function @samp{besJ(1,x)} for @samp{x} from 0 to 5 in steps of 0.25.
X+ Find the value of @samp{x} (from among the above set of values) for
X+ which @samp{besJ(1,x)} is a maximum.  Use an ``automatic'' method,
X+ i.e., just reading along the list to find the smallest value is
X+ not allowed!  (There is an @kbd{a X} command which does this kind
X+ of thing automatically; @pxref{Numerical Solutions}.)
X+ @xref{List Answer 8, 8}. (@bullet)
X+ 
X+ @cindex Digits, vectors of
X+ (@bullet) @strong{Exercise 9.}  You are given an integer in the range
X+ @samp{0 <= N < 10^m} for @samp{m=12} (i.e., an integer of less than
X+ twelve digits).  Convert this integer into a vector of @samp{m}
X+ digits, each in the range from 0 to 9.  In vector-of-digits notation,
X+ add one to this integer to produce a vector of @samp{m+1} digits
X+ (since there could be a carry out of the most significant digit).
X+ Convert this vector back into a regular integer.  A good integer
X+ to try is 25129925999.  @xref{List Answer 9, 9}. (@bullet)
X+ 
X+ (@bullet) @strong{Exercise 10.}  Your friend Joe tried to use
X+ @samp{V R a =} to test if all numbers in a list were equal.  What
X+ happened?  How would you do this test?  @xref{List Answer 10, 10}. (@bullet)
X+ 
X  @node Types Tutorial, Algebra Tutorial, Vector/Matrix Tutorial, Tutorial
X  @section Types Tutorial
X  
X***************
X*** 2718,2723 ****
X--- 3081,3091 ----
X  @end example
X  @end group
X  
X+ (@bullet) @strong{Exercise 1.}  A calculation has produced the
X+ result 1.26508260337.  You suspect it is the square root of the
X+ product of @samp{pi} and some rational number.  Is it?  (Be sure
X+ to allow for roundoff error!)  @xref{Types Answer 1, 1}. (@bullet)
X+ 
X  Complex numbers can be stored in both rectangular and polar form.
X  
X  @group
X***************
X*** 2763,2768 ****
X--- 3131,3145 ----
X  form, then we take the sine of that angle.  Note that the trigonometric
X  functions will accept HMS forms directly as input.
X  
X+ @cindex Beatles
X+ (@bullet) @strong{Exercise 2.}  The Beatles' @emph{Abbey Road} is
X+ 47 minutes and 26 seconds long, and contains 17 songs.  What is the
X+ average length of a song on @emph{Abbey Road}?  If the Extended Disco
X+ Version of @emph{Abbey Road} added 20 seconds to the length of each
X+ song, how long would the album be?  @xref{Types Answer 2, 2}. (@bullet)
X+ 
X+ @cindex Slope and angle of a line
X+ @cindex Angle and slope of a line
X  An @dfn{error form} represents a mean value with an attached standard
X  deviation, or error estimate.  Suppose our measurements indicate that
X  a certain telephone pole is about 30 meters away, with an estimated
X***************
X*** 2776,2782 ****
X      .            1:  30 +/- 1        .                     .
X                       .
X  
X!     8 p .2           30 p 1          /                     I T
X  @end example
X  @end group
X  
X--- 3153,3159 ----
X      .            1:  30 +/- 1        .                     .
X                       .
X  
X!     8 p .2 RET       30 p 1          /                     I T
X  @end example
X  @end group
X  
X***************
X*** 2784,2789 ****
X--- 3161,3174 ----
X  original error estimates were valid standard deviations, there is about
X  a 60% chance that the result is correct within 0.59 degrees.
X  
X+ @cindex Torus, volume of
X+ (@bullet) @strong{Exercise 3.}  The volume of a torus (a donut) is
X+ @samp{2 pi^2 R r^2} where @samp{R} is the radius of the circle that
X+ defines the center of the tube and @samp{r} is the radius of the tube
X+ itself.  Suppose @samp{R} is 20 cm and @samp{r} is 4 cm, each known to
X+ within 5 percent.  What is the volume and the relative uncertainty of
X+ the volume?  @xref{Types Answer 3, 3}. (@bullet)
X+ 
X  An @dfn{interval form} represents a range of values.  While an
X  error form is best for making statistical estimates, intervals give
X  you exact bounds on an answer.  Suppose we additionally know that
X***************
X*** 2824,2829 ****
X--- 3209,3225 ----
X  The Calculator automatically keeps track of which end values should
X  be open and which should be closed.
X  
X+ (@bullet) @strong{Exercise 4.}  It is an error to divide by zero;
X+ Calc leaves the result in symbolic form.  When would you expect
X+ Calc to do the same for a division of interval forms?
X+ @xref{Types Answer 4, 4}. (@bullet)
X+ 
X+ (@bullet) @strong{Exercise 5.}  Two easy ways of squaring a number
X+ are @key{RET *} and @kbd{2 ^}.  Normally these produce the same
X+ answer.  Would you expect this still to hold true for interval forms?
X+ If not, which of these will result in a larger interval?
X+ @xref{Types Answer 5, 5}. (@bullet)
X+ 
X  A @dfn{modulo form} is used for performing arithmetic modulo @var{M}.
X  For example, arithmetic involving time is generally done modulo 12
X  or 24 hours.
X***************
X*** 2855,2862 ****
X  much more efficient because it avoids the huge intermediate value
X  that arises in the second one.
X  
X! The Calc data types can't be combined in all possible ways, but it
X! is possible to use HMS forms as parts of error forms, intervals,
X  modulo forms, or as the phase part of a polar complex number.
X  For example, the @code{calc-time} command pushes the current time
X  of day on the stack as an HMS/modulo form.
X--- 3251,3267 ----
X  much more efficient because it avoids the huge intermediate value
X  that arises in the second one.
X  
X! @cindex Fermat, primality test of
X! (@bullet) @strong{Exercise 6.}  A theorem due to Pierre de Fermat
X! says that @samp{x^(n-1) mod n = 1} if @samp{n} is a prime number
X! and @samp{x} is an integer less than @samp{n}.  If @samp{n} is
X! @emph{not} a prime number, this will @emph{not} be true for most
X! values of @samp{x}.  Thus we can test informally if a number is
X! prime by trying this formula for several values of @samp{x}.
X! Use this test to tell whether the following numbers are prime:
X! 811749613, 15485863.  @xref{Types Answer 6, 6}. (@bullet)
X! 
X! It is possible to use HMS forms as parts of error forms, intervals,
X  modulo forms, or as the phase part of a polar complex number.
X  For example, the @code{calc-time} command pushes the current time
X  of day on the stack as an HMS/modulo form.
X***************
X*** 2872,2877 ****
X--- 3277,3293 ----
X  
X  This calculation tells me it is six hours and 22 minutes until midnight.
X  
X+ (@bullet) @strong{Exercise 7.}  A rule of thumb is that one year
X+ is about @samp{pi * 10^7} seconds.  What time will it be that
X+ many seconds from right now?  @xref{Types Answer 7, 7}. (@bullet)
X+ 
X+ (@bullet) @strong{Exercise 8.}  You are preparing to order packaging
X+ for the CD release of the Extended Disco Version of @emph{Abbey Road}.
X+ You are told that the songs will actually be anywhere from 20 to 60
X+ seconds longer than the originals.  One CD can hold about 75 minutes
X+ of music.  Should you order single or double packages?
X+ @xref{Types Answer 8, 8}. (@bullet)
X+ 
X  Another kind of data the Calculator can manipulate is numbers with
X  @dfn{units}.  This isn't strictly a new data type; it's simply an
X  application of algebraic expressions, where we use variables with
X***************
X*** 2924,2930 ****
X  
X  @group
X  @example
X! 1:  55 mph     1:  88.5139 kph   1:   88.5139 km / hr   1:  8.20147e-8 c
X      .              .                  .                     .
X  
X   ' 55 mph RET      u c kph RET        u c km/hr RET         u c c RET
X--- 3340,3346 ----
X  
X  @group
X  @example
X! 1:  55 mph     1:  88.5139 kph   1:   88.5139 km / hr   1:  8.201407e-8 c
X      .              .                  .                     .
X  
X   ' 55 mph RET      u c kph RET        u c km/hr RET         u c c RET
X***************
X*** 2962,2967 ****
X--- 3378,3399 ----
X  @kbd{M-#} again to re-enter the Calculator when you're done looking
X  at the units table.
X  
X+ (@bullet) @strong{Exercise 9.}  How many seconds are there really
X+ in a year?  @xref{Types Answer 9, 9}. (@bullet)
X+ 
X+ @cindex Speed of light
X+ (@bullet) @strong{Exercise 10.}  Supercomputer designs are limited by
X+ the speed of light (and of electricity, which is nearly as fast).
X+ Suppose a computer has a 4.1 ns (nanosecond) clock cycle, and its
X+ cabinet is one meter across.  Is speed of light going to be a
X+ significant factor in its design?  @xref{Types Answer 10, 10}. (@bullet)
X+ 
X+ (@bullet) @strong{Exercise 11.}  Sam the Slug normally travels about
X+ five yards in an hour.  He has obtained a supply of Power Pills; each
X+ Power Pill he eats doubles his speed.  How many Power Pills can he
X+ swallow and still travel legally on most US highways?
X+ @xref{Types Answer 11, 11}. (@bullet)
X+ 
X  @node Algebra Tutorial, Programming Tutorial, Types Tutorial, Tutorial
X  @section Algebra and Calculus Tutorial
X  
X***************
X*** 2978,2989 ****
X  @end example
X  @end group
X  
X  There are also commands for doing common algebraic operations on
X! formulas.
X  
X  @group
X  @example
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
X      .                                     .
X  
X      a x                                   a c x RET
X--- 3410,3425 ----
X  @end example
X  @end group
X  
X+ (@bullet) @strong{Exercise 1.}  Do @kbd{' x RET Q 2 ^} and
X+ @kbd{' x RET 2 ^ Q} both wind up with the same result (@samp{x})?
X+ Why or why not?  @xref{Algebra Answer 1, 1}. (@bullet)
X+ 
X  There are also commands for doing common algebraic operations on
X! formulas.  Continuing with the formula from the last example,
X  
X  @group
X  @example
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
X      .                                     .
X  
X      a x                                   a c x RET
X***************
X*** 2991,2997 ****
X  @end group
X  
X  First we ``expand'' using the distributive law, then we ``collect''
X! terms involving powers of @samp{x}.
X  
X  Let's find the value of this expression when @samp{x} is 2 and @samp{y}
X  is one half.
X--- 3427,3433 ----
X  @end group
X  
X  First we ``expand'' using the distributive law, then we ``collect''
X! terms involving like powers of @samp{x}.
X  
X  Let's find the value of this expression when @samp{x} is 2 and @samp{y}
X  is one half.
X***************
X*** 3011,3016 ****
X--- 3447,3453 ----
X  next expression on the stack.  After this command, the variable goes
X  back to its original value, if any.
X  
X+ @cindex Maximum of a function using Calculus
X  Let's find the maximum value of our original expression when @samp{y}
X  is one half and @samp{x} ranges over all possible values.  We can
X  do this by taking the derivative with respect to @samp{x} and examining
X***************
X*** 3091,3097 ****
X  
X  @group
X  @example
X! 1:  34 - 24 x^2 = 0    1:  x = -1.19023 s1     1:  1.19023
X      .                      .                       .
X  
X      r 3                    H a S x RET  s 5        1 n l s1 RET
X--- 3528,3534 ----
X  
X  @group
X  @example
X! 1:  34 - 24 x^2 = 0    1:  x = -1.19023 s1     1:  x = 1.19023
X      .                      .                       .
X  
X      r 3                    H a S x RET  s 5        1 n l s1 RET
X***************
X*** 3132,3138 ****
X  Here we have used a vector mapping operation to evaluate the function
X  at several values of @samp{s1} at once.  @kbd{V M $} is like @kbd{V M '}
X  except that it takes the formula from the top of the stack.  The
X! formula is taken as a function to apply across the vector at the
X  next-to-top stack level.  Once again, we press @key{RET} to use the
X  default argument list.
X  
X--- 3569,3575 ----
X  Here we have used a vector mapping operation to evaluate the function
X  at several values of @samp{s1} at once.  @kbd{V M $} is like @kbd{V M '}
X  except that it takes the formula from the top of the stack.  The
X! formula is interpreted as a function to apply across the vector at the
X  next-to-top stack level.  Once again, we press @key{RET} to use the
X  default argument list.
X  
X***************
X*** 3139,3144 ****
X--- 3576,3582 ----
X  If there had been several different values, we could have used
X  @kbd{V R X} to find the global maximum.
X  
X+ @cindex Area under a curve
X  What is the area under the portion of this curve from @samp{x} = 1 to 2?
X  This is simply the integral of the function:
X  
X***************
X*** 3163,3168 ****
X--- 3601,3615 ----
X  @end example
X  @end group
X  
X+ (@bullet) @strong{Exercise 2.}  Find the values for which the
X+ original formula @samp{17 x^2 - 6 x^4 + 3} is zero.  (There are
X+ four, two of which are complex numbers.)  @xref{Algebra Answer 2, 2}. (@bullet)
X+ 
X+ (@bullet) @strong{Exercise 3.}  Find the integral from 1 to @samp{y}
X+ of @samp{x sin(pi x)} (where the sine is calculated in radians).
X+ Find the values of the integral for integer @samp{y} from 1 to 5.
X+ @xref{Algebra Answer 3, 3}. (@bullet)
X+ 
X  Calc's integrator can do many simple integrals symbolically, but many
X  others are beyond its capabilities.  Suppose we wish to find the area
X  under the curve @samp{sin(x) ln(x)} over the same range of @samp{x}.  If
X***************
X*** 3171,3176 ****
X--- 3618,3625 ----
X  solution.  In fact, there is no closed-form solution to this integral.
X  Now what do we do?
X  
X+ @cindex Integration, numerical
X+ @cindex Numerical integration
X  One approach would be to do the integral numerically.  While there
X  is no built-in command for this in Calc, you can do it yourself
X  using vector mapping and reduction.  This is rather slow, though,
X***************
X*** 3179,3190 ****
X  
X  @group
X  @example
X! 1:  [1, 2, ... ]    1:  [10, 11, ... ]    1:  [1, 1.1, ... ]
X!     .                   .                     .
X  
X!     v x 10 RET          9 +                   0.1 *
X! @end example
X! @end group
X  
X  @group
X  @example
X--- 3628,3644 ----
X  
X  @group
X  @example
X! 3:  10                  1:  [1, 1.1, 1.2,  ...  , 1.8, 1.9 ]
X! 2:  1                       .
X! 1:  0.1
X!     .
X  
X!  10 RET 1 RET .1 RET        C-u v x
X! @end example
X! @end group
X! 
X! (Note that we have used the extended version of @kbd{v x}; we could
X! also have used plain @kbd{v x} as follows:  @kbd{v x 10 RET 9 + .1 *}.)
X  
X  @group
X  @example
X***************
X*** 3243,3249 ****
X  Better!  By increasing the precision and/or asking for more terms
X  in the Taylor series, we can get a result as accurate as we like.
X  
X! @node Programming Tutorial, , Algebra Tutorial, Tutorial
X  @section Programming Tutorial
X  
X  The Calculator is written entirely in Emacs Lisp, a highly extensible
X--- 3697,3747 ----
X  Better!  By increasing the precision and/or asking for more terms
X  in the Taylor series, we can get a result as accurate as we like.
X  
X! @cindex Simpson's rule
X! @cindex Integration by Simpson's rule
X! (@bullet) @strong{Exercise 4.}  Our first method approximated the
X! curve by stairsteps of width 0.1; the total area was then the sum
X! of the areas of the rectangles under these stairsteps.  Our second
X! method approximated the function by a polynomial, which turned out
X! to be a better approximation than stairsteps.  A third method is
X! @dfn{Simpson's rule}, which is like the stairstep method except
X! that the steps are not required to be flat.  Simpson's rule boils
X! down to the formula,
X! 
X! @example
X! (h/3) * (f(a) + 4 f(a+h) + 2 f(a+2h) + 4 f(a+3h) + ...
X!               + 2 f(a+(n-2)*h) + 4 f(a+(n-1)*h) + f(a+n*h))
X! @end example
X! 
X! @noindent
X! where @samp{n} (which must be even) is the number of slices and @samp{h}
X! is the width of each slice.  These are 10 and 0.1 in our example.
X! For reference, here is the corresponding equation for the stairstep
X! method:
X! 
X! @example
X! h * (f(a) + f(a+h) + f(a+2h) + f(a+3h) + ...
X!           + f(a+(n-2)*h) + f(a+(n-1)*h))
X! @end example
X! 
X! Compute the integral from 1 to 2 of @samp{sin(x) ln(x)} using
X! Simpson's rule with 10 slices.  @xref{Algebra Answer 4, 4}. (@bullet)
X! 
X! Aside from the commands we've seen so far, Calc also provides a
X! large set of commands for operating on parts of formulas.  You
X! indicate the desired sub-formula by placing the cursor on any part
X! of the formula before giving a @dfn{selection} command.  Selections won't
X! be covered in the tutorial; @pxref{Selecting Subformulas}, for
X! details and examples.
X! 
X! Another powerful algebraic facility is the @kbd{a r} and @kbd{j r}
X! commands, which manipulate a formula using @dfn{rewrite rules}.
X! @xref{Rewrite Rules}, for an explanation of this feature.
X! 
X! @c hard exercise: simplify (2^(n r) - 2^(r*(n - 1))) / (2^r - 1) 2^(n - 1)
X! @c                to 2^((n-1)*(r-1)).
X! 
X! @node Programming Tutorial, Answers to Exercises, Algebra Tutorial, Tutorial
X  @section Programming Tutorial
X  
X  The Calculator is written entirely in Emacs Lisp, a highly extensible
X***************
X*** 3303,3308 ****
X--- 3801,3818 ----
X  @end example
X  @end group
X  
X+ (@bullet) @strong{Exercise 1.}  Define a keyboard macro to negate
X+ the item in level 3 of the stack, without disturbing the rest of
X+ the stack.  @xref{Programming Answer 1, 1}. (@bullet)
X+ 
X+ (@bullet) @strong{Exercise 2.}  Define a keyboard macro to compute
X+ @samp{sin(x) / x}, where @samp{x} is the number on the top of the
X+ stack.  @xref{Programming Answer 2, 2}. (@bullet)
X+ 
X+ (@bullet) @strong{Exercise 3.}  Define a keyboard macro to compute
X+ the average (mean) value of a list of numbers.
X+ @xref{Programming Answer 3, 3}. (@bullet)
X+ 
X  In many programs, some of the steps must execute several times.
X  Calc has @dfn{looping} commands that allow this.  Loops are useful
X  inside keyboard macros, but actually work at any time.
X***************
X*** 3325,3330 ****
X--- 3835,3841 ----
X  If you make a mistake while entering the body of the loop,
X  type @kbd{Z C-g} to cancel the loop command.
X  
X+ @cindex Fibonacci numbers
X  Here's another example:
X  
X  @group
X***************
X*** 3343,3349 ****
X  of the loop body by hand; @kbd{C-j}, also on the Line-Feed or @key{LFD}
X  key if you have one, pushes a copy of the number in level 2.)
X  
X! A fascinating property of Fibonacci numbers is that the @var{n}th
X  Fibonacci number can be found directly by computing @samp{phi^n / sqrt(5)}
X  and then rounding to the nearest integer, where @samp{phi}, the
X  ``golden ratio,'' is @samp{(1 + sqrt(5)) / 2}.
X--- 3854,3862 ----
X  of the loop body by hand; @kbd{C-j}, also on the Line-Feed or @key{LFD}
X  key if you have one, pushes a copy of the number in level 2.)
X  
X! @cindex Golden ratio
X! @cindex Phi, golden ratio
X! A fascinating property of the Fibonacci numbers is that the @var{n}th
X  Fibonacci number can be found directly by computing @samp{phi^n / sqrt(5)}
X  and then rounding to the nearest integer, where @samp{phi}, the
X  ``golden ratio,'' is @samp{(1 + sqrt(5)) / 2}.
X***************
X*** 3358,3363 ****
X--- 3871,3893 ----
X  @end example
X  @end group
X  
X+ @cindex Continued fractions
X+ (@bullet) @strong{Exercise 4.}  The @dfn{continued fraction}
X+ representation of @samp{phi} is @samp{1 + 1/(1 + 1/(1 + 1/( ... )))}.
X+ We can compute an approximate value by carrying this however far
X+ and then replacing @samp{1/( ... )} by 1.  Approximate @samp{phi}
X+ using a twenty-term continued fraction.
X+ @xref{Programming Answer 4, 4}. (@bullet)
X+ 
X+ (@bullet) @strong{Exercise 5.}  Linear recurrences like the one for
X+ Fibonacci numbers can be expressed in terms of matrices.  Given a
X+ vector @samp{[a, b]} determine a matrix which, when multiplied by this
X+ vector, produces the vector @samp{[b, c]}, where @samp{a}, @samp{b} and
X+ @samp{c} are three successive Fibonacci numbers.  Now write a program
X+ that, given an integer @var{n}, computes the @var{n}th Fibonacci number
X+ using matrix arithmetic.  @xref{Programming Answer 5, 5}. (@bullet)
X+ 
X+ @cindex Harmonic numbers
X  A more sophisticated kind of loop is the @dfn{for} loop.  Suppose
X  we wish to compute the 20th ``harmonic'' number, which is equal to
X  the sum of the reciprocals of the integers from 1 to 20.
X***************
X*** 3400,3405 ****
X--- 3930,3939 ----
X  sequence @kbd{s + 7} adds the top-of-stack into the value in
X  variable 7.
X  
X+ (@bullet) @strong{Exercise 6.}  Use a ``for'' loop to find the first
X+ harmonic number which is greater than 4.0.
X+ @xref{Programming Answer 6, 6}. (@bullet)
X+ 
X  Of course, if we're going to be using variables in our programs,
X  we have to worry about the programs clobbering values that the
X  caller was keeping in those same variables.  This is easy to
X***************
X*** 3426,3436 ****
X  the contents of the stack, and the values of named variables,
X  survive past the @kbd{Z '} command.
X  
X  The @dfn{Bernoulli numbers} are a sequence with the interesting
X  property that all of the odd Bernoulli numbers are zero, and the
X  even ones, while difficult to compute, can be roughly approximated
X  by the formula @samp{2 n! / (2 pi)^n}.  Let's write a keyboard
X! macro to compute (approximate) Bernoulli numbers.
X  
X  @group
X  @example
X--- 3960,3974 ----
X  the contents of the stack, and the values of named variables,
X  survive past the @kbd{Z '} command.
X  
X+ @cindex Bernoulli numbers, approximate
X  The @dfn{Bernoulli numbers} are a sequence with the interesting
X  property that all of the odd Bernoulli numbers are zero, and the
X  even ones, while difficult to compute, can be roughly approximated
X  by the formula @samp{2 n! / (2 pi)^n}.  Let's write a keyboard
X! macro to compute (approximate) Bernoulli numbers.  (Calc has a
X! command, @kbd{k b}, to compute exact Bernoulli numbers, but
X! this command is very slow for large @var{n} since the higher
X! Bernoulli numbers are very large fractions.)
X  
X  @group
X  @example
X***************
X*** 3453,3463 ****
X  
X  @group
X  @example
X! 2:  0.0756823    1:  0          1:  0.25305    1:  0          1:  1.16659
X! 1:  0.0757575        .              .              .              .
X      .
X  
X!    5 RET 66 /   DEL DEL 11 K    DEL 12 K       DEL 13 K       DEL 14 K
X  @end example
X  @end group
X  
X--- 3991,4002 ----
X  
X  @group
X  @example
X! 3:  0.0756823    1:  0          1:  0.25305    1:  0          1:  1.16659
X! 2:  5:66             .              .              .              .
X! 1:  0.0757575
X      .
X  
X! 10 k b RET c f   M-0 DEL 11 K   DEL 12 K       DEL 13 K       DEL 14 K
X  @end example
X  @end group
X  
X***************
X*** 3479,3485 ****
X  we execute it, the list we are building will be in stack level 2
X  (initially this is an empty list), and the next Bernoulli number
X  will be in level 1.  The effect is to append the Bernoulli number
X! onto the end of the list.
X  
X  If you make a mistake when entering a keyboard macro, you can edit
X  it using @kbd{Z E}.  First, you must attach it to a key with @kbd{Z K}.
X--- 4018,4032 ----
X  we execute it, the list we are building will be in stack level 2
X  (initially this is an empty list), and the next Bernoulli number
X  will be in level 1.  The effect is to append the Bernoulli number
X! onto the end of the list.  (To create a table of exact fractional
X! Bernoulli numbers, just replace @kbd{K} with @kbd{k b} in the above
X! sequence of keystrokes.)
X! 
X! With loops and conditionals, you can program essentially anything
X! in Calc.  One other command that makes looping easier is @kbd{Z /},
X! which takes a condition from the stack and breaks out of the enclosing
X! loop if the condition is true (non-zero).  You can use this to make
X! ``while'' and ``until'' style loops.
X  
X  If you make a mistake when entering a keyboard macro, you can edit
X  it using @kbd{Z E}.  First, you must attach it to a key with @kbd{Z K}.
X***************
X*** 3488,3495 ****
X  
X  @group
X  @example
X! 1:  3                   1:  17          Keyboard Macro Editor.
X!     .                       .           Original keys: 1 7 RET
X  
X                                          type "1\r"
X                                          type "2"
X--- 4035,4042 ----
X  
X  @group
X  @example
X! 1:  3                   1:  3           Keyboard Macro Editor.
X!     .                       .           Original keys: 1 RET 2 +
X  
X                                          type "1\r"
X                                          type "2"
X***************
X*** 3535,3543 ****
X  calc-kbd-pop          # Restore values (Z ')
X  @end example
X  
X- If you don't know how to write a particular command in @file{macedit}
X- format, you can always write it as keystrokes in a @code{type} command.
X- 
X  Press @kbd{C-c C-c} to finish editing.
X  
X  @group
X--- 4082,4087 ----
X***************
X*** 3549,3554 ****
X--- 4093,6123 ----
X  @end example
X  @end group
X  
X+ If you don't know how to write a particular command in @file{macedit}
X+ format, you can always write it as keystrokes in a @code{type} command.
X+ There is also a @code{keys} command which interprets the rest of the
X+ line as standard Emacs keystroke names.  In fact, @file{macedit} defines
X+ a handy @code{read-kbd-macro} command which reads the current region
X+ of the current buffer as a sequence of keystroke names, and defines that
X+ sequence on the @kbd{K} (and @kbd{C-x e}) key.  Try reading in the macro
X+ in the following form:  Press @kbd{C-@@} (or @kbd{C-SPC}) at one end of
X+ the text below, then type @kbd{M-x read-kbd-macro} at the other.
X+ 
X+ @group
X+ @example
X+ Z ` 0 s 1 DEL
X+     1 TAB
X+     Z (  & s + 1 DEL  1 Z )
X+     r 1
X+ Z '
X+ @end example
X+ @end group
X+ 
X+ (@bullet) @strong{Exercise 7.}  A general algorithm for solving
X+ equations numerically is @dfn{Newton's Method}.  Given the equation
X+ @samp{f(x) = 0} for any function @samp{f}, and an initial guess
X+ @samp{x0} which is reasonably close to the desired solution, apply
X+ this formula over and over:
X+ 
X+ @example
X+ new_x = x - f(x)/f'(x)
X+ @end example
X+ 
X+ where @samp{f'(x)} is the derivative of @samp{f}.  The @samp{x}
X+ values will quickly converge to a solution, i.e., eventually
X+ @samp{new_x} and @samp{x} will be equal to within the limits
X+ of the current precision.  Write a program which takes a formula
X+ involving the variable @samp{x}, and an initial guess @samp{x0},
X+ on the stack, and produces a value of @samp{x} for which the formula
X+ is zero.  Use it to find a solution of @samp{sin(cos(x)) = 0.5}
X+ near @samp{x = 4.5}.  (Use angles measured in radians.)  Note that
X+ the built-in @kbd{a R} (@code{calc-find-root}) command uses Newton's
X+ method when it is able.  @xref{Programming Answer 7, 7}. (@bullet)
X+ 
X+ @cindex Digamma function
X+ @cindex Gamma constant, Euler's
X+ (@bullet) @strong{Exercise 8.}  The @dfn{digamma} function @samp{psi(z)}
X+ is defined as the derivative of @samp{ln(gamma(z))}.  For large
X+ values of @samp{z}, it can be approximated by the infinite sum
X+ 
X+ @example
X+ psi(z) ~= ln(z) - 1/2z - sum(bern(2 n) / 2 n z^(2 n))
X+ @end example
X+ 
X+ @noindent
X+ where @code{sum} represents the sum over @var{n} from 1 to infinity
X+ (or to some limit high enough to give the desired accuracy), and
X+ the @code{bern} function produces (exact) Bernoulli numbers.
X+ While this sum is not guaranteed to converge, in practice it is safe.
X+ An interesting mathematical constant is Euler's gamma, which is equal
X+ to about 0.5772.  One way to compute it is by the formula,
X+ @samp{gamma = -psi(1)}.  Unfortunately, 1 isn't a large enough argument
X+ for the above formula to work (5 is a much safer value for @code{z}).
X+ Fortunately, we can compute @samp{psi(1)} from @samp{psi(5)} using
X+ the recurrence @samp{psi(z+1) = psi(z) + 1/z}.  Your task:  Develop
X+ a program to compute @samp{psi(z)}; it should ``pump up'' @code{z}
X+ if necessary to be greater than 5, then use the above summation
X+ formula.  Use your function to compute @samp{gamma} to twelve decimal
X+ places.  @xref{Programming Answer 8, 8}. (@bullet)
X+ 
X+ @cindex Polynomial, list of coefficients
X+ (@bullet) @strong{Exercise 9.}  Given a polynomial in @samp{x} and
X+ a number @var{m} on the stack, where the polynomial is of degree
X+ @var{m} or less (i.e., does not have any terms higher than @samp{x^@var{m}}),
X+ write a program to convert the polynomial into a list-of-coefficients
X+ notation.  For example, @samp{5 x^4 + (x + 1)^2} with @var{m} = 6
X+ should produce the list @samp{[1, 2, 1, 0, 5, 0, 0]}.  Also develop
X+ a way to convert from this form back to the standard algebraic form.
X+ @xref{Programming Answer 9, 9}. (@bullet)
X+ 
X+ @cindex Recursion
X+ (@bullet) @strong{Exercise 10.}  The @dfn{Stirling numbers of the
X+ first kind} are defined by the recurrences,
X+ 
X+ @example
X+ s(n,n) = 1   for n >= 0,
X+ s(n,0) = 0   for n > 0,
X+ s(n+1,m) = s(n,m-1) - n s(n,m)   for n >= m >= 1.
X+ @end example
X+ 
X+ @noindent
X+ This can be implemented using a @dfn{recursive} program in Calc; the
X+ program must invoke itself in order to calculate the two righthand
X+ terms in the general formula.  Since it always invokes itself with
X+ ``simpler'' arguments, it's easy to see that it will always
X+ eventually finish the computation.  Recursion is a little
X+ difficult with Emacs keyboard macros since the macro is executed
X+ before its definition is complete.  So here's the recommended
X+ strategy:  Create a ``dummy macro'' and assign it to a key with,
X+ e.g., @kbd{Z K s}.  Now enter the true definition, using the
X+ @kbd{z s} command to call itself recursively, then assign it to
X+ the same key with @kbd{Z K s}.  Now the @kbd{z s} command will
X+ run the complete recursive program.  The task:  Write a program
X+ that computes Stirling numbers of the first kind.  Test it with
X+ @emph{small} inputs like @samp{s(4,2)}.  (There is a built-in
X+ command for Stirling numbers, @kbd{k s}, which you can use to
X+ check your answers.)  @xref{Programming Answer 10, 10}. (@bullet)
X+ 
X+ 
X+ This ends the tutorial section of the Calc manual.  Now you know enough
X+ about Calc to use it effectively for many kinds of calculations.  But
X+ Calc has many features that were not even touched upon here.  The rest
X+ of this manual tells the whole story.
SHAR_EOF
echo "End of part 13, continue with part 14"
echo "14" > s2_seq_.tmp
exit 0



More information about the Comp.sources.misc mailing list