[comp.sources.misc] v15i045: Patch for GNU Emacs Calc, version 1.04 -> 1.05, part 18/20

daveg@csvax.cs.caltech.edu (David Gillespie) (10/15/90)

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

#!/bin/sh
# this is part 18 of a multipart archive
# do not concatenate these parts, unpack them in order with /bin/sh
# file calc.patch continued
#
CurArch=18
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! There must be the same number of equations as variables.  Since
X! only plain numbers are allowed as guesses, the Hyperbolic flag has
X! no effect when solving a system of equations.
X! 
X! If Calc leaves the @code{root} or @code{wroot} function in symbolic
X! form on the stack, be sure to press @kbd{w} (@code{calc-why}) to see
X! Calc's explanation for why no root was found.
X! 
X! @kindex a N
X! @kindex a X
X! @pindex calc-find-minimum
X! @pindex calc-find-maximum
X! @tindex minimize
X! @tindex maximize
X! @cindex Minimization, numerical
X! The @kbd{a N} (@code{calc-find-minimum}) [@code{minimize}] command
X! finds a minimum value for a formula.  It is very similar in operation
X! to @code{calc-find-root}:  You give the formula and an initial guess
X! on the stack, and are prompted for the name of a variable.  The guess
X! may be either a number near the desired minimum, or an interval enclosing
X! the desired minimum.  The function returns a vector containing the
X! value of the the variable which minimizes the formula's value, along
X! with the minimum value itself.
X! 
X! Note that this command looks for a @emph{local} minimum.  Many functions
X! have more than one minimum; some, like @samp{x sin(x)}, have infinitely
X! many.  In fact, there is no easy way to define the ``global'' minimum
X! of @samp{x sin(x)} but Calc can still locate any particular local minimum
X! for you.  Calc basically goes downhill from the initial guess until it
X! finds a point at which the function's value is greater both to the left
X! and to the right.  Calc does not use derivatives when minimizing a function.
X! 
X! Most functions are smooth and flat near their minimum values.  Because
X! of this flatness, if the current precision is, say, 12 digits, the
X! variable can only be determined meaningfully to about six digits.  Thus
X! you should set the precision to twice as many digits as you need in your
X! answer.
X! 
X! The @kbd{H a N} [@code{wminimize}] command, analogously to @kbd{H a R},
X! expands the guess interval to enclose a minimum rather than requiring
X! that the minimum lie inside the interval you supply.
X! 
X! The @kbd{a X} (@code{calc-find-maximum}) [@code{maximize}] and
X! @kbd{H a X} [@code{wmaximize}] commands effectively minimize the
X! negative of the formula you supply.
X! 
X! The formula must evaluate to a real number at all points inside the
X! interval (or near the initial guess if the guess is a number).  If
X! the initial guess is a complex number the variable will be minimized
X! over the complex numbers; if it is real or an interval it will
X! be minimized over the reals.
X! 
X! It is also possible to minimize over many variables.  In this case,
X! the variable name should be replaced by a vector of variables, and
X! the initial guess should be an equal-sized vector of initial guesses.
X! Note that, unlike the case of multidimensional root finding, the
X! formula being minimized should still be a single formula, @emph{not} a
X! vector.  Beware that multidimensional minimization is currently
X! @emph{very} slow.
X! 
X! @node Logical Operations, Rewrite Rules, Numerical Solutions, Algebra
X  @section Logical Operations
X  
X  The following commands and algebraic functions return true/false values,
X  where 1 represents ``true'' and 0 represents ``false.''  In cases where
X  a truth value is required (such as for the condition part of a rewrite
X! rule, or as the condition for a @kbd{Z [  Z ]} control structure), any
X! nonzero number is accepted to mean ``true.''
X  
X  @kindex a =
X  @pindex calc-equal-to
X***************
X*** 7400,7406 ****
X  operation pops two values from the stack and pushes back either a 1 or
X  a 0, or a formula @samp{a = b} if the values' equality cannot be determined.
X  
X! Note that @kbd{C-u 3 a =} or @samp{a = b = c} does not return true if
X  @samp{a}, @samp{b}, and @samp{c} are equal.  (It is interpreted as
X  @samp{(a = b) = c}, and returns true if @samp{c} is 1 when @samp{a} and
X  @samp{b} are equal, or 0 when they are unequal.)  Only negative prefix
X--- 11849,11855 ----
X  operation pops two values from the stack and pushes back either a 1 or
X  a 0, or a formula @samp{a = b} if the values' equality cannot be determined.
X  
X! Note that @kbd{C-u 3 a =} or @samp{a = b = c} does @emph{not} return true if
X  @samp{a}, @samp{b}, and @samp{c} are equal.  (It is interpreted as
X  @samp{(a = b) = c}, and returns true if @samp{c} is 1 when @samp{a} and
X  @samp{b} are equal, or 0 when they are unequal.)  Only negative prefix
X***************
X*** 7444,7451 ****
X  @pindex calc-logical-or
X  @tindex lor
X  The @kbd{a |} (@code{calc-logical-or}) [@samp{lor(a,b)} or @samp{a || b}]
X! function is true if either of its arguments is true (nonzero).  The
X! result is whichever argument was nonzero, choosing arbitrarily if both
X  are nonzero.  If both @samp{a} and @samp{b} are zero, the result is
X  zero.
X  
X--- 11893,11900 ----
X  @pindex calc-logical-or
X  @tindex lor
X  The @kbd{a |} (@code{calc-logical-or}) [@samp{lor(a,b)} or @samp{a || b}]
X! function is true if either or both of its arguments are true (nonzero).
X! The result is whichever argument was nonzero, choosing arbitrarily if both
X  are nonzero.  If both @samp{a} and @samp{b} are zero, the result is
X  zero.
X  
X***************
X*** 7457,7469 ****
X  true (nonzero).  It is left in symbolic form if @samp{a} is not a
X  number.
X  
X  @tindex if
X! The @samp{if(a,b,c)} or @samp{a ? b : c} function is equal to either
X! @samp{b} or @samp{c} if @samp{a} is a nonzero number or zero, respectively.
X! If @samp{a} is not a number, the test is left in symbolic form and
X! neither @samp{b} nor @samp{c} is evaluated in any way.  This is the
X! only Calc function whose arguments are not automatically evaluated
X! when the function itself is evaluated.
X  
X  @kindex a @{
X  @pindex calc-in-set
X--- 11906,11930 ----
X  true (nonzero).  It is left in symbolic form if @samp{a} is not a
X  number.
X  
X+ @kindex a :
X+ @pindex calc-logical-if
X  @tindex if
X! @cindex Arguments, not evaluated
X! The @kbd{a :} (@code{calc-logical-if}) [@samp{if(a,b,c)} or @samp{a ? b : c}]
X! function is equal to either @samp{b} or @samp{c} if @samp{a} is a nonzero
X! number or zero, respectively.  If @samp{a} is not a number, the test is
X! left in symbolic form and neither @samp{b} nor @samp{c} is evaluated in
X! any way.  In algebraic formulas, this is one of the few Calc functions
X! whose arguments are not automatically evaluated when the function itself
X! is evaluated.  The others are @code{lambda} and @code{quote}.
X! 
X! As a special case, if @samp{a} evaluates to a vector, then both @samp{b}
X! and @samp{c} are evaluated; the result is a vector of the same length
X! as @samp{a} whose elements are chosen from corresponding elements of
X! @samp{b} and @samp{c} according to whether each element of @samp{a}
X! is zero or nonzero.  Each of @samp{b} and @samp{c} must be either a
X! vector of the same length as @samp{a}, or a non-vector which is matched
X! with all elements of @samp{a}.
X  
X  @kindex a @{
X  @pindex calc-in-set
X***************
X*** 7503,7514 ****
X  @tindex real
X  @tindex constant
X  The @samp{integer(a)} function returns true if @samp{a} is an integer.
X! This is shorthand for @samp{typeof(a) = 1}.  The @samp{real(a)} function
X  is true if @samp{a} is a real number, either integer, fraction, or
X  float.  The @samp{constant(a)} function returns true if @samp{a} is
X  any of the objects for which @code{typeof} would produce an integer
X  code result except for variables, and provided that the components of
X! an object like a vector or error form are themselves constant.
X  
X  @tindex refers
X  The @samp{refers(a,b)} function is true if the variable (or sub-expression)
X--- 11964,11975 ----
X  @tindex real
X  @tindex constant
X  The @samp{integer(a)} function returns true if @samp{a} is an integer.
X! The @samp{real(a)} function
X  is true if @samp{a} is a real number, either integer, fraction, or
X  float.  The @samp{constant(a)} function returns true if @samp{a} is
X  any of the objects for which @code{typeof} would produce an integer
X  code result except for variables, and provided that the components of
X! an object like a vector or error form are themselves constant.@refill
X  
X  @tindex refers
X  The @samp{refers(a,b)} function is true if the variable (or sub-expression)
X***************
X*** 7518,7653 ****
X  @code{refers} will be left unevaluated is if @samp{a} is a plain
X  variable (different from @samp{b}).
X  
X! @node Calculus, , Logical Operations, Algebra
X! @section Calculus
X  
X! The following calculus commands do not automatically simplify their
X! inputs or outputs using @code{calc-simplify}.  You may find it helps
X! to do this by hand by typing @kbd{a s} or @kbd{a e}.  It may also help
X! to use @kbd{a x} and/or @kbd{a c} to arrange a result in the most
X! readable way.
X  
X! @kindex a d
X! @kindex H a d
X! @pindex calc-derivative
X! @tindex deriv
X! @tindex tderiv
X! The @kbd{a d} (@code{calc-derivative}) [@code{deriv}] command computes
X! the derivative of the expression on the top of the stack with respect to
X! some variable, which it will prompt you to enter.  Normally, variables
X! in the formula other than the specified differentiation variable are
X! considered constant, i.e., @samp{deriv(y,x)} is reduced to zero.  With
X! the Hyperbolic flag, the @code{tderiv} (total derivative) operation is used
X! instead, in which derivatives of variables are not reduced to zero.
X  
X! If you use the @code{deriv} function directly in an algebraic formula,
X! you can also write @samp{deriv(f,x,x0)} which represents the derivative
X! of @code{f} with respect to @code{x}, evaluated at the point @samp{x=x0}.
X  
X! If the formula being differentiated contains functions which Calc does
X! not know, the derivatives of those functions are produced by adding
X! primes (apostrophe characters).  For example, @samp{deriv(f(2x), x)}
X! produces @samp{2 f'(2 x)}, where the function @code{f'} represents the
X! derivative of @code{f}.  If you enter a definition for @code{f'}, for
X! example using the @kbd{Z F} (@code{calc-user-define-formula}) command,
X! the Calculator will use it.  @xref{Algebraic Definitions}.
X  
X! For multi-argument functions @samp{f(x,y,z)}, the derivative with respect
X! to the first argument is written @samp{f'(x,y,z)}; derivatives with
X! respect to the other arguments are @samp{f'2(x,y,z)} and @samp{f'3(x,y,z)}.
X! Various higher-order derivatives can be formed in the obvious way, e.g.,
X! @samp{f'@var{}'(x)} (the second derivative of @code{f}) or
X! @samp{f'@var{}'2'3(x,y,z)} (@code{f} differentiated with respect to each
X! argument once).@refill
X  
X! @kindex a i
X! @pindex calc-integral
X! @tindex integ
X! The @kbd{a i} (@code{calc-integral}) [@code{integ}] command computes the
X! indefinite integral of the expression on the top of the stack with
X! respect to a variable.  The integrator is not guaranteed to work for
X! all integrable functions, but it will work for any polynomial and for
X! most simple functions.
X  
X! If you use the @code{integ} function directly in an algebraic formula,
X! you can also write @samp{integ(f,x,v)} which expresses the resulting
X! indefinite integral in terms of variable @code{v} instead of @code{x},
X! or @samp{integ(f,x,a,b)}, which represents a definite integral from
X! @code{a} to @code{b}.
X  
X! The Calculator remembers all the integrals it has done.  If conditions
X! change in a way that would invalidate the old integrals, they will be
X! thrown out.  If you suspect this is not happening when it should,
X! use the @code{calc-flush-caches} command; @pxref{Caches}.
X  
X! @kindex a I
X! @pindex calc-integral-limit
X! The @kbd{a I} (@code{calc-integral-limit}) command displays or, with a
X! numeric prefix argument, sets the current ``integration nesting limit.''
X! This is the depth to which Calc will pursue integration by substitution
X! or integration by parts before abandoning an approach as fruitless.
X! The default value is 3.  If the integrator is taking too long, you
X! might consider lowering this limit.  However, the lower this limit is,
X! the greater the chance that Calc will be unable to integrate a function
X! it could otherwise handle.  Raising this limit allows the Calculator to
X! solve more integrals, though the time it takes may grow exponentially.
X! You can monitor the integrator's actions by creating an Emacs buffer called
X! @code{*Trace*}.  If such a buffer exists, the @kbd{a i} command will write
X! a log of its actions there.
X  
X! @kindex a t
X! @pindex calc-taylor
X! @tindex taylor
X! The @kbd{a t} (@code{calc-taylor}) [@code{taylor}] command computes a
X! power series expansion or Taylor series of a function.  You specify the
X! variable and the desired number of terms.  You may give an expression of
X! the form @samp{@var{var} = @var{a}} or @samp{@var{var} - @var{a}} to
X! produce a Taylor expansion about the point @var{a}.  You may specify the
X! number of terms with a numeric prefix argument; otherwise the command
X! will prompt you for the number of terms.  Note that many series
X! expansions have coefficients of zero for some terms, so you may appear
X! to get fewer terms than you asked for.@refill
X  
X! If the @kbd{a i} command is unable to find a symbolic integral for a
X! function, you can get an approximation by integrating the function's
X! Taylor series.
X  
X! @kindex a S
X! @pindex calc-solve-for
X! @tindex solve
X! The @kbd{a S} (@code{calc-solve-for}) [@code{solve}] command rearranges
X! an equation to solve for a specific variable.  An equation is an
X! expression of the form @samp{L = R}.  If the input is not an equation,
X! it is treated like an equation of the form @samp{X = 0}.  For example,
X! the command @kbd{a S x} will rearrange @samp{y = 3x + 6} to the form,
X! @samp{x = y/3 - 2}.
X  
X! @kindex H a S
X! @tindex fsolve
X! Some equations have more than one solution.  The Hyperbolic flag
X! (@code{H a S}) [@code{fsolve}] tells the solver to report the fully
X! general family of solutions.  It will invent variables @code{n1},
X! @code{n2}, @dots{}, which represent independent arbitrary integers, and
X! @code{s1}, @code{s2}, @dots{}, which represent independent arbitrary
X! signs (either @samp{+1} or @samp{-1}).  If you don't use the Hyperbolic
X! flag, Calc will use zero in place of all arbitrary integers, and plus
X! one in place of all arbitrary signs.  Note that variables like @code{n1}
X! and @code{s1} are not given any special interpretation in Calc except by
X! @code{calc-solve-for} itself.  As usual, you can use the @code{calc-let}
X! command to obtain solutions for various actual values of these
X! variables.
X  
X! @kindex I a S
X! @kindex H I a S
X! @tindex finv
X! @tindex ffinv
X! With the Inverse flag, @kbd{I a S} [@code{finv}] treats the expression
X! on top of the stack as a function of the specified variable and solves
X! to find the inverse function, written in terms of the same variable.
X! For example, @kbd{I a S x} inverts @samp{2x + 6} to @samp{x/2 - 3}.
X! You can use both Inverse and Hyperbolic [@code{ffinv}] to obtain a
X! fully general inverse, as described above.
X  
X  @node Units, Store and Recall, Algebra, Top
X  @chapter Operating on Units
X  
X--- 11979,12746 ----
X  @code{refers} will be left unevaluated is if @samp{a} is a plain
X  variable (different from @samp{b}).
X  
X! @tindex negative
X! The @samp{negative(a)} function returns true if @samp{a} ``looks'' negative,
X! because it is a negative number, because it is of the form @samp{-x},
X! or because it is a product or quotient with a term that looks negative.
X! This is most useful in rewrite rules.  Beware that @samp{negative(a)}
X! evaluates to 1 or 0 for @emph{any} argument @samp{a}, so it can only
X! be stored in a formula if the default simplifications are turned off
X! first with @kbd{m O}.
X! 
X! @tindex variable
X! The @samp{variable(a)} function is true if @samp{a} is a variable,
X! or false if not.  If @samp{a} is a function call, this test is left
X! in symbolic form.
X! 
X! @tindex nonvar
X! The @samp{nonvar(a)} function is true if @samp{a} is a non-variable.
X! If its argument is a variable it is left unsimplified; it never
X! actually returns zero.  However, since Calc's condition-testing
X! commands consider anything but a nonzero number to be ``false,''
X! this is often good enough.
X! 
X! @tindex istrue
X! The @samp{istrue(a)} function returns 1 if @samp{a} is a nonzero
X! number, or 0 if @samp{a} is anything else.  Calls to @code{istrue}
X! can only be entered or manipulated if @kbd{m O} mode is used to
X! make sure it is not evaluated prematurely.
X  
X! @node Rewrite Rules, , Logical Operations, Algebra
X! @section Rewrite Rules
X  
X! @cindex Rewrite rules
X! @cindex Transformations
X! @cindex Pattern matching
X! @kindex a r
X! @pindex calc-rewrite
X! The @kbd{a r} (@code{calc-rewrite}) [@code{rewrite}] command makes
X! substitutions in a formula according to a specified pattern or patterns.
X! It is like a generalized version of @code{calc-substitute}.  You provide
X! Calc with one or more @dfn{rewrite rules}, which take the form of vectors
X! of two or three elements:  @samp{[old, new]} or @samp{[old, new, cond]}.
X  
X! To specify a set of rules, make a vector of rule vectors.  (Calc will
X! interpret this structure as a matrix as long as the rules either all
X! include or all omit the @samp{cond} part.  Whether or not this is true
X! makes no difference to @code{calc-rewrite} itself.)  If you have only
X! one rule you can write it as a plain vector if you wish.  (If the
X! rewrite rule itself contains vectors, it's safest to enclose your
X! rule in a surrounding vector to avoid ambiguity:  @samp{[[[a,b], [b,a]]]}
X! is a rule that exchanges the two parts of a 2-vector.  This is a pair
X! of vectors for @samp{old} and @samp{new}, enclosed in brackets to
X! make a rule, then enclosed again in brackets to make a rule set.)
X  
X! When you are prompted to enter the rewrite rules, you can either type
X! the vector directly (as an algebraic entry), or type the name of a
X! Calc variable into which you have stored a rules vector, or enter
X! a blank line in which case the rules vector or variable will be taken
X! from the top of the stack.  It is most convenient to store rules you
X! use often in a variable and invoke them by naming the variable.  You
X! may also wish to use @kbd{Z V} (@code{calc-permanent-variable}) to save the
X! rules permanently; @pxref{Storing Variables}.
X  
X! Rewrite rules are compiled into a special internal form for faster
X! matching.  If you enter a rule set directly it must be recompiled
X! for every @kbd{a r} command.  If you store the rules in a variable
X! and refer to them through that variable, they will be compiled
X! once and saved away along with the variable for later reference.
X! 
X! @menu
X! * Basic Rewrite Rules::
X! * Conditional Rewrite Rules::
X! * Algebraic Properties of Rewrite Rules::
X! * Other Features of Rewrite Rules::
X! * Nested Formulas with Rewrite Rules::
X! * Selections with Rewrite Rules::
X! * Matching Commands::
X! * Examples of Rewrite Rules::
X! @end menu
X  
X! @node Basic Rewrite Rules, Conditional Rewrite Rules, Rewrite Rules, Rewrite Rules
X! @subsection Basic Rewrite Rules
X  
X! To match a particular formula @samp{x} with a particular rule vector
X! @samp{[old, new]}, Calc compares the structure of @samp{x} with the
X! structure of @samp{old}.  Variables that appear in @samp{old} are
X! treated as ``meta-variables''; the corresponding positions in @samp{x}
X! may contain any sub-formulas.  For example, the pattern @samp{f(x,y)}
X! would match the expression @samp{f(12, a+1)} with the meta-variable
X! @samp{x} corresponding to 12 and with @samp{y} corresponding to
X! @samp{a+1}.  However, this pattern would not match @samp{f(12)} or
X! @samp{g(12, a+1)}, since there is no assignment of the meta-variables
X! that will allow the pattern to match these expressions.  Notice that
X! if the pattern is a single variable, it will match any expression.
X  
X! If a given meta-variable appears more than once in @samp{old}, the
X! corresponding sub-formulas of @samp{x} must be identical.  Thus
X! the pattern @samp{f(x,x)} would match @samp{f(12, 12)} and
X! @samp{f(a+1, a+1)} but not @samp{f(12, a+1)} or @samp{f(x+y, y+x)}.
X  
X! Things other than variables must match exactly between the pattern
X! and the target formula.  To match a particular variable exactly, use
X! the pseudo-function @samp{quote(v)} in the pattern.  For example, the
X! pattern @samp{x+quote(y)} matches @samp{x+y}, @samp{2+y}, or
X! @samp{sin(x)+y}.
X  
X! The special variable names @samp{e}, @samp{pi}, and @samp{i} always
X! match literally.  Thus the pattern @samp{sin(d + e + f)} acts
X! exactly like @samp{sin(d + quote(e) + f)}.
X  
X! If the @samp{old} pattern is found to match a given formula, that
X! formula is replaced by @samp{new}, where any occurrences in @samp{new}
X! of meta-variables from the pattern are replaced with the sub-formulas
X! that they matched.  Thus, applying the rule @samp{[f(x,y), g(y+x,x)]}
X! to @samp{f(12, a+1)} would produce @samp{g(a+13, 12)}.
X  
X! @node Conditional Rewrite Rules, Algebraic Properties of Rewrite Rules, Basic Rewrite Rules, Rewrite Rules
X! @subsection Conditional Rewrite Rules
X  
X! If a @samp{cond} part is also present in the rule, this is an
X! additional condition that must be satisfied before the rule is
X! accepted.  Once @samp{old} has been successfully matched to the
X! target expression, @samp{cond} is evaluated (with the meta-variables
X! substituted for the values they matched) and simplified with
X! @code{calc-simplify}.  If the result is a nonzero number, the rule
X! is accepted.  If the result is zero or if it is still a symbolic
X! formula, the rule is rejected.  @xref{Logical Operations}, for a
X! number of functions that return 1 or 0 according to the result of
X! some test.
X  
X! For example, the formula @samp{n > 0} simplifies to 1 or 0 if @samp{n}
X! is replaced by a positive or nonpositive number, respectively.  Thus,
X! the rule @samp{[f(x,y), g(y+x,x), x+y > 0]} would apply to
X! @samp{f(0, 4)} but not to @samp{f(-3, 2)} or @samp{f(12, a+1)}.  (In the
X! former case, the condition can be shown not to be satisfied; in the
X! latter case, the condition merely cannot be shown to be satisfied, but
X! that is enough to reject the rule.)
X! 
X! @node Algebraic Properties of Rewrite Rules, Other Features of Rewrite Rules, Conditional Rewrite Rules, Rewrite Rules
X! @subsection Algebraic Properties of Rewrite Rules
X! 
X! The rewrite mechanism understands the algebraic properties of functions
X! like @samp{+} and @samp{*}.  In particular, pattern matching takes
X! the associativity and commutativity of the following functions into
X! account:
X! 
X! @example
X! + - *  = !=  && ||  and or xor  gcd lcm  max min  beta
X! @end example
X! 
X! For example, the rewrite rule:
X! 
X! @example
X! [ a x + b x, (a + b) x ]
X! @end example
X! 
X! @noindent
X! will match formulas of the form,
X! 
X! @example
X! a x + b x,  x a + x b,  a x + x b,  x a + b x
X! @end example
X! 
X! Rewrites also understand the relationship between the @samp{+} and @samp{-}
X! operators.  The above rewrite rule will also match the formulas,
X! 
X! @example
X! a x - b x,  x a - x b,  a x - x b,  x a - b x
X! @end example
X! 
X! @noindent
X! by matching @samp{b} in the pattern to @samp{-b} from the formula.
X! 
X! Applied to a sum of many terms like @samp{r + a x + s + b x + t}, this
X! pattern will check all pairs of terms for possible matches.  The rewrite
X! will take whichever suitable pair it discovers first.
X! 
X! In general, a pattern using an associative operator like @samp{a + b}
X! will try @samp{2 n} different ways to match a sum of @samp{n} terms
X! like @samp{x + y + z - w}.  First, @samp{a} is matched against each
X! of @samp{x}, @samp{y}, @samp{z}, and @samp{-w} in turn, with @samp{b}
X! being matched to the remainders @samp{y + z - w}, @samp{x + z - w}, etc.
X! If none of these succeed, then @samp{b} is matched against each of the
X! four terms with @samp{a} matching the remainder.  Half-and-half matches,
X! like @samp{(x + y) + (z - w)}, are not tried.
X! 
X! The pattern @samp{-x} will actually match any expression.  For example,
X! the rule
X! 
X! @example
X! [ f(-x), -f(x) ]
X! @end example
X! 
X! @noindent
X! will rewrite @samp{f(a)} to @samp{-f(-a)}.  To avoid this, either use
X! a @code{plain} marker as described below, or add a @samp{negative(x)}
X! condition.  The @code{negative} function is true if its argument
X! ``looks'' negative, for example, because it is a negative number or
X! because it is a formula like @samp{-x}.  The new rule using this
X! condition is:
X! 
X! @example
X! [ f(x), -f(-x), negative(x) ]
X! @end example
X! 
X! In the same way, the pattern @samp{x - y} will match the sum @samp{a + b}
X! by matching @samp{y} to @samp{-b}.
X! 
X! It is also possible to specify optional arguments in patterns.  The rule
X! 
X! @example
X! [ opt(a) x + opt(b) (x^opt(c) + opt(d)), f(a, b, c, d) ]
X! @end example
X! 
X! @noindent
X! will match the formula
X! 
X! @example
X! 5 (x^2 - 4) + 3 x
X! @end example
X! 
X! @noindent
X! in a fairly straightforward manner, but it will also match reduced
X! formulas like
X! 
X! @example
X! x + x^2,    2(x + 1) - x,    x + x
X! @end example
X! 
X! @noindent
X! producing, respectively,
X! 
X! @example
X! f(1, 1, 2, 0),   f(-1, 2, 1, 1),   f(1, 1, 1, 0)
X! @end example
X! 
X! (The latter two formulas can be entered only if default simplifications
X! have been turned off with @kbd{m O}.)
X! 
X! The default value for a term of a sum is zero.  The default value for
X! a part of a product, for a power, or for the denominator of a
X! quotient, is one.  Also, @samp{-x} matches the pattern @samp{opt(a) b}
X! with @samp{a = -1}.
X! 
X! In particular, the distributive-law rule can be refined to
X! 
X! @example
X! [ opt(a) x + opt(b) x, (a + b) x ]
X! @end example
X! 
X! @noindent
X! so that it will convert, e.g., @samp{a x + x}, to @samp{(a + 1) x}.
X! 
X! As another example, the obvious rule to replace @samp{sin(x)^2 + cos(x)^2}
X! by 1,
X! 
X! @example
X! [ sin(x)^2 + cos(x)^2, 1 ]
X! @end example
X! 
X! @noindent
X! misses many cases because the sine and cosine may both be multiplied by
X! an equal factor.  Here's a more successful rule:
X! 
X! @example
X! [ opt(a) sin(x)^2 + opt(a) cos(x)^2, a ]
X! @end example
X! 
X! Note that this rule will @emph{not} match @samp{sin(x)^2 + 6 cos(x)^2}
X! because one @samp{a} ``matched'' 1 but the other matched 6.
X! 
X! Calc automatically converts a rule like
X! 
X! @example
X! [ f(x-1, x), g(x) ]
X! @end example
X! 
X! @noindent
X! into the form
X! 
X! @example
X! [ f(temp, x), g(x), temp = x-1 ]
X! @end example
X! 
X! @noindent
X! As a result, this rule will successfully match @samp{f(6, 7)}, binding
X! @samp{temp} and @samp{x} to 6 and 7, respectively, then verifying that
X! they differ by one even though @samp{6} does not superficially look
X! like @samp{x-1}.
X! 
X! However, Calc does not solve equations to interpret a rule.  The
X! following rule,
X! 
X! @example
X! [ f(x-1, x+1), g(x) ]
X! @end example
X! 
X! @noindent
X! will not work.  That is, it will match @samp{f(a - 1 + b, a + 1 + b)}
X! but not @samp{f(6, 8)}.  Calc always interprets at least one occurrence
X! of a variable by literal matching.  If the variable appears ``isolated''
X! then Calc is smart enough to use it for literal matching.  But in this
X! last example, Calc is forced to rewrite the rule to @samp{[f(x-1, temp),
X! g(x), temp = x+1]} where the @samp{x-1} term must correspond to an
X! actual ``something-minus-one'' in the target formula.
X! 
X! Calc does this rewriting or ``conditionalizing'' for any sub-pattern
X! which involves only the functions in the following list, operating
X! only on constants and meta-variables which have already been matched
X! elsewhere in the pattern.  When matching a function call, Calc is
X! careful to match arguments which are plain variables before arguments
X! which are calls to any of the functions below, so that a pattern like
X! @samp{f(x-1, x)} can be conditionalized even though the isolated
X! @samp{x} comes after the @samp{x-1}.
X! 
X! @example
X! + - * / \ % ^  abs sign round trunc floor ceil  max min  re im conj arg
X! @end example
X! 
X! You can suppress all of the special treatments described in this
X! section by surrounding a function call with a @code{plain} marker.
X! This marker causes the function call which is its argument to be
X! matched literally, without regard to commutativity, associativity,
X! negation, or conditionalization.  When you use @code{plain}, the ``deep
X! structure'' of the formula being matched can show through.  For
X! example,
X! 
X! @example
X! [ plain(a - a b), f(a, b) ]
X! @end example
X! 
X! @noindent
X! will match only literal subtractions.  However, the @code{plain}
X! code does not affect its arguments' arguments.  In this case,
X! commutativity and associativity is still considered while matching
X! the @samp{a b} sub-pattern, and the whole pattern will match
X! @samp{x - y x} as well as @samp{x - x y}.  We could go still
X! further and use
X! 
X! @example
X! [ plain(a - plain(a b)), f(a, b) ]
X! @end example
X! 
X! @noindent
X! which will do a completely strict match for the pattern.
X! 
X! By contrast, the @code{quote} marker means that not only the
X! function name but the arguments must be literally the same.
X! The above pattern will match @samp{x - x y} but
X! 
X! @example
X! [ quote(a - a b), f(a, b) ]
X! @end example
X! 
X! @noindent
X! will match only the single formula @samp{a - a b}.  Also,
X! 
X! @example
X! [ quote(a - quote(a b)), f(a, b) ]
X! @end example
X! 
X! @noindent
X! will match only @samp{a - quote(a b)}---probably not the desired
X! effect!
X! 
X! A certain amount of algebra is also done when substituting the
X! meta-variables on the righthand side of a rule.  For example,
X! in the rule
X! 
X! @example
X! [ a + f(b), f(a + b) ]
X! @end example
X! 
X! @noindent
X! matching @samp{f(x) - y} would produce @samp{f((-y) + x)} if
X! taken literally, but the rewrite mechanism will simplify the
X! righthand side to @samp{f(x - y)} automatically.  (Of course,
X! the default simplifications would do this anyway, so this
X! special simplification is only noticeable if you have turned the
X! default simplifications off.)  This rewriting is done only when
X! a meta-variable expands to a ``negative-looking'' expression.
X! If this simplification is not desirable, you can use a @code{plain}
X! marker on the righthand side:
X! 
X! @example
X! [ a + f(b), f(plain(a + b)) ]
X! @end example
X! 
X! @noindent
X! In this example, we are still allowing the pattern-matcher to
X! use all the algebra it can muster, but the righthand side will
X! always simplify to a literal addition like @samp{f((-y) + x)}.
X! 
X! @node Other Features of Rewrite Rules, Nested Formulas with Rewrite Rules, Algebraic Properties of Rewrite Rules, Rewrite Rules
X! @subsection Conditional Rewrite Rules
X! 
X! Certain ``function names'' serve as markers in rewrite rules.
X! Here is a complete list of these markers.  First are listed the
X! markers that work inside a pattern; then come the markers that
X! work in the righthand side of a rule.
X! 
X! One kind of marker, @samp{import(x)}, takes the place of a whole
X! rule.  Here @samp{x} is the name of a variable containing another
X! rule set; those rules are ``spliced into'' the rule set that
X! imports them.  For example, if @samp{[[f(a+b), f(a) + f(b)],
X! [f(a b), a f(b), real(a)]]} is stored in variable
X! @samp{linearF}, then the rule set @samp{[[f(0), 0], import(linearF)]}
X! will apply all three rules.  It is possible to modify the imported
X! rules slightly:  @samp{import(x, v1, x1, v2, x2, @dots{})} imports
X! the rule set @samp{x} with all occurrences of @samp{v1}, as either
X! a variable name or a function name, replaced with @samp{x1} and
X! so on.  (If @samp{v1} is used as a function name, then @samp{x1}
X! must be either a function name itself or a @code{lambda} expression;
X! @pxref{Reducing and Mapping}.)  For example, @samp{[[g(0), 0],
X! import(linearF, f, g)]} applies the linearity rules to the function
X! @samp{g} instead of @samp{f}.  Imports can be nested, but the
X! import-with-renaming feature may not properly rename sub-imports.
X! 
X! The special functions allowed in patterns are:
X! 
X! @table @samp
X! @item quote(x)
X! @findex quote
X! This pattern matches exactly @samp{x}; variable names in @samp{x} are
X! not interpreted as meta-variables.  The only flexibility is that
X! numbers are compared for numeric equality, so that the pattern
X! @samp{f(quote(12))} will match both @samp{f(12)} and @samp{f(12.0)}.
X! (Numbers are always treated this way by the rewrite mechanism:
X! The rule @samp{[f(x,x), g(x)]} will match @samp{f(12, 12.0)}.
X! The rewrite may produce either @samp{g(12)} or @samp{g(12.0)}
X! as a result in this case.)
X! 
X! @item plain(x)
X! @findex plain
X! Here @samp{x} must be a function call @samp{f(x1,x2,@dots{})}.  This
X! pattern matches a call to function @samp{f} with the specified
X! argument patterns.  No special knowledge of the properties of the
X! function @samp{f} is used in this case; @samp{+} is not commutative or
X! associative.  Unlike @code{quote}, the arguments @samp{x1,x2,@dots{}}
X! are treated as patterns.  If you wish them to be treated ``plainly''
X! as well, you must enclose them with more @code{plain} markers:
X! @samp{plain(plain(-a) + plain(b c))}.
X! 
X! @item opt(x,def)
X! @findex opt
X! Here @samp{x} must be a variable name.  This must appear as an
X! argument to a function or an element of a vector; it specifies that
X! the argument or element is optional.
X! As an argument to @samp{+}, @samp{-}, @samp{*}, @samp{&&}, or @samp{||},
X! or as the second argument to @samp{/} or @samp{^}, the value @code{def}
X! may be omitted.  The pattern @samp{x + opt(y)} matches a sum by
X! binding one summand to @samp{x} and the other to @samp{y}, and it
X! matches anything else by binding the whole expression to @samp{x} and
X! zero to @samp{y}.  The other operators above work similarly.
X! For general miscellanous functions, the default value @code{def}
X! must be specified.  Optional arguments are dropped starting with
X! the rightmost one during matching.  For example, the pattern
X! @samp{f(opt(a,0), b, opt(c,b))} will match @samp{f(b)}, @samp{f(a,b)},
X! or @samp{f(a,b,c)}.  Default values of zero and @samp{b} are
X! supplied in this example for the omitted arguments.  Note that
X! the literal variable @samp{b} will be the default in the latter
X! case, @emph{not} the value that matched the meta-variable @samp{b}.
X! In other words, the default @code{def} is effectively quoted.@refill
X! 
X! @item condition(x,c)
X! @findex condition
X! This matches the pattern @samp{x}, with the attached condition
X! @samp{c}.  The rule @samp{[condition(f(n x), integer(n)), n f(x)]}
X! is functionally equivalent to @samp{[f(n x), n f(x), integer(n)]}.
X! While it may seem more efficient to put the condition inside
X! the rule as in @samp{f(condition(n,integer(n)) x)}, Calc is
X! actually smart enough to test a condition as soon as all the
X! meta-variables it depends upon have been bound, regardless of
X! the condition's location in the rule.  Conditions of the form
X! @samp{a && b && c} are broken up into separate conditions
X! which are each tested as soon as possible.  Certain conditions
X! are handled as special cases by the rewrite rule system and
X! tested very efficiently:  Where @samp{x} is any meta-variable,
X! these conditions are @samp{integer(x)}, @samp{real(x)},
X! @samp{constant(x)}, @samp{negative(x)}, @samp{x >= y} where @samp{y}
X! is either a constant or another meta-variable and @samp{>=} may
X! be replaced by any of the six relational operators, and
X! @samp{x % a = b} where @samp{a} and @samp{b} are constants.
X! 
X! @item cons(h,t)
X! @findex cons
X! This matches any vector of one or more elements.  The first
X! element is matched to @samp{h}; a vector of the remaining
X! elements is matched to @samp{t}.  Note that vectors of fixed
X! length can also be matched as actual vectors:  The rule
X! @samp{[cons(a,cons(b,[])), cons(a+b,[]))]} is equivalent
X! to the rule @samp{[[a,b], [a+b]]}.  (Note that the latter
X! rule @emph{must} be enclosed in another pair of brackets
X! to make a rule set, or else Calc will interpret it as a
X! rule set consisting of two rules, the latter one being
X! invalid.
X! 
X! @item apply(f,args)
X! @findex apply (rewrites)
X! This matches any function call.  The name of the function, in
X! the form of a variable, is matched to @samp{f}.  The arguments
X! of the function, as a vector of zero or more objects, are
X! matched to @samp{args}.  Constants, variables, and vectors
X! do @emph{not} match an @code{apply} pattern.  For example,
X! @samp{apply(f,x)} matches any function call, @samp{apply(quote(f),x)}
X! matches any call to the function @samp{f}, @samp{apply(f,[a,b])}
X! matches any function call with exactly two arguments, and
X! @samp{apply(quote(f), cons(a,cons(b,x)))} matches any call
X! to the function @samp{f} with two or more arguments.  Another
X! way to implement the latter, if the rest of the rule does not
X! need to refer to the first two arguments of @samp{f} by name,
X! would be @samp{apply(quote(f), condition(x, vlen(x) >= 2))}.
X! Here's a more interesting sample use of @code{apply}:
X! 
X! @example
X! [ apply(f,[x+n]), n + apply(f,[x]),
X!   in(f, [floor,ceil]) && integer(n) ]
X! @end example
X! 
X! Note, however, that this will be slower to match than a rule
X! set with two separate rules.  The reason is that Calc sorts
X! the rules of a rule set according to top-level function name;
X! if the top-level function is @code{apply}, Calc must try the
X! rule for every single formula it tries.  If the top-level
X! function is, say, @code{floor}, then Calc invokes the rule only
X! for formulas which are calls to @code{floor}.
X! 
X! @item select(x)
X! @findex select
X! This is used for applying rules to formulas with selections;
X! @pxref{Selections with Rewrite Rules}.
X! @end table
X! 
X! Special functions for the righthand sides of rules are:
X! 
X! @table @samp
X! @item quote(x)
X! The notation @samp{quote(x)} is changed to @samp{x} when the
X! righthand side is used.  As far as the rewrite rule is concerned,
X! @code{quote} is invisible.  However, @code{quote} has the special
X! property in Calc that its argument is not evaluated.  Thus,
X! while it will not work to put the rule @samp{[t(a), typeof(a)]}
X! on the stack because @samp{typeof(a)} is evaluated immediately
X! to produce @samp{[t(a), 100]}, you can use @code{quote} to
X! protect the righthand side:  @samp{[t(a), quote(typeof(a))]}.
X! 
X! @item plain(x)
X! Special properties of and simplifications for the function call
X! @samp{x} are not used.  Meta-variables in the arguments of @samp{x}
X! are substituted as usual.
X! 
X! @item cons(h,t)
X! Where @samp{t} is a vector, this is converted into an expanded
X! vector during rewrite processing.  Note that @code{cons} is a regular
X! Calc function which normally does this anyway; the only way @code{cons}
X! is treated specially by rewrites is that @code{cons} on the righthand
X! side of a rule will be evaluated even if default simplifications
X! have been turned off.
X! 
X! @item apply(f,args)
X! Where @samp{f} is a variable and @samp{args} is a vector, this
X! is converted to a function call.  Once again, note that @code{apply}
X! is also a regular Calc function.
X! 
X! @item eval(x)
X! @findex eval
X! The formula @samp{x} is handled in the usual way, then the
X! default simplifications are applied to it even if they have
X! been turned off normally.  This allows you to treat any function
X! similarly to the way @code{cons} and @code{apply} are always
X! treated.  However, there is a slight difference:  @samp{cons(2+3, [])}
X! with default simplifications off will be converted to @samp{[2+3]},
X! whereas @samp{eval(cons(2+3, []))} will be converted to @samp{[5]}.
X! 
X! @item evalsimp(x)
X! @findex evalsimp
X! The formula @samp{x} has meta-variables substituted in the usual
X! way, then algebraically simplified as if by the @kbd{a s} command.
X! 
X! @item select(x)
X! @xref{Selections with Rewrite Rules}.
X! @end table
X! 
X! There is also one special function you can use in conditions, both
X! as the third part of a rule vector and as the second argument of
X! @code{condition}.
X! 
X! @table @samp
X! @item quote(x)
X! The notation @samp{quote(x)} in a condition works the same as it
X! does on the righthand side of the rule; @code{quote} is removed
X! when the condition is tested during rewriting, but it suppresses
X! evaluation of its contents when the rule is otherwise manipulated
X! by Calc.  For example, @samp{[a, f(a), variable(a)]} cannot easily
X! be manipulated on the Calc stack because @samp{variable(a)} will
X! immediately be evaluated to 1.  But @samp{[a, f(a), quote(variable(a))]}
X! will work properly.
X! @end table
X! 
X! @node Nested Formulas with Rewrite Rules, Selections with Rewrite Rules, Other Features of Rewrite Rules, Rewrite Rules
X! @subsection Nested Formulas with Rewrite Rules
X! 
X! When @kbd{a r} (@code{calc-rewrite}) is used, it takes an expression from
X! the top of the stack and attempts to match any of the specified rules
X! to any part of the expression, starting with the whole expression
X! and then, if that fails, trying deeper and deeper sub-expressions.
X! For each part of the expression, the rules are tried in the order
X! they appear in the rules vector.  The first rule to match the first
X! sub-expression wins; it replaces the matched sub-expression according
X! to the @samp{new} part of the rule.  By default, only one substitution
X! is made per @code{calc-rewrite} command.
X! 
X! Given a positive numeric prefix argument, @code{calc-rewrite} will
X! perform up to that many substitutions before stopping.  First, the
X! top-level formula is matched and substituted repeatedly until it
X! no longer matches the pattern; then, sub-formulas are tried, and
X! so on.  If the limit still has not been reached, the top-level
X! formula is checked again (in case a substitution of one of its
X! arguments causes it again to match).  This continues until no
X! further matches can be made, or until the limit is reached.
X! 
X! Given a negative numeric prefix argument, @code{calc-rewrite} will
X! match and substitute the top-level expression up to that many times,
X! but will not attempt to match the rules to any sub-expressions.
X! 
X! A prefix argument of zero is equivalent to a prefix of 25.  This is
X! meant to be a convenient limit that is usually enough to finish the
X! job, but not enough to be too slow in case the rule transformations
X! get into an infinite loop.
X  
X+ @node Selections with Rewrite Rules, Matching Commands, Nested Formulas with Rewrite Rules, Rewrite Rules
X+ @subsection Selections with Rewrite Rules
X+ 
X+ If a sub-formula of the current formula is selected (as by @kbd{j s};
X+ @pxref{Selecting Subformulas}), the @kbd{a r} (@code{calc-rewrite})
X+ command applies only to that sub-formula.  Together with a negative
X+ prefix argument, you can use this fact to apply a rewrite to one
X+ specific part of a formula without affecting any other parts.
X+ 
X+ @kindex j r
X+ @pindex calc-rewrite-selection
X+ The @kbd{j r} (@code{calc-rewrite-selection}) command allows more
X+ sophisticated operations on selections.  This command prompts for
X+ the rules in the same way as @kbd{a r}, but it then applies those
X+ rules to the whole formula in question even though a sub-formula
X+ of it has been selected.  However, the selected sub-formula will
X+ first have been surrounded by a @samp{select( )} function call.
X+ (Calc's evaluator does not understand the function name @code{select};
X+ this is only a tag used by the @kbd{j r} command.)
X+ 
X+ For example, suppose the formula on the stack is @samp{2 (a + b)^2}
X+ and the sub-formula @samp{a + b} is selected.  This formula will
X+ be rewritten to @samp{2 select(a + b)^2} and then the rewrite
X+ rules will be applied in the usual way.  The rewrite rules can
X+ include references to @code{select} to tell where in the pattern
X+ the selected sub-formula should appear.
X+ 
X+ If there is still exactly one @samp{select( )} function call in
X+ the formula after rewriting is done, it indicates which part of
X+ the formula should be selected afterwards.  Otherwise, the
X+ formula will be unselected.
X+ 
X+ You can make @kbd{j r} act much like @kbd{a r} by enclosing both parts
X+ of the rewrite rule with @samp{select()}.  However, @kbd{j r}
X+ allows you to use the current selection in more flexible ways.
X+ Suppose you wished to make a rule which removed the exponent from
X+ the selected term; the rule @samp{[select(a)^x, select(a)]} would
X+ work.  In the above example, it would rewrite @samp{2 select(a + b)^2}
X+ to @samp{2 select(a + b)}.  This would then be returned to the
X+ stack as @samp{2 (a + b)} with the @samp{a + b} selected.
X+ 
X+ As with other selection commands, @kbd{j r} operates on the stack
X+ entry that contains the cursor.  (If the cursor is on the top-of-stack
X+ @samp{.} marker, it works as if the cursor were on the formula
X+ at stack level 1.)
X+ 
X+ If you don't specify a set of rules, the rules are taken from the
X+ top of the stack, just as with @kbd{a r}.  In this case, the
X+ cursor must indicate stack entry 2 or above as the formula to be
X+ rewritten (otherwise the same formula would be used as both the
X+ target and the rewrite rules).
X+ 
X+ If the indicated formula has no selection, the cursor position within
X+ the formula temporarily selects a sub-formula for the purposes of this
X+ command.  If the cursor is not on any sub-formula (e.g., it is in
X+ the line-number area to the left of the formula), the @samp{select( )}
X+ markers are ignored by the rewrite mechanism and the rules are allowed
X+ to apply anywhere in the formula.
X+ 
X+ As a special feature, the normal @kbd{a r} command ignores @samp{select( )}
X+ calls in rewrite rules.  For example, if you used the above rule
X+ @samp{[select(a)^x, select(a)]} with @kbd{a r}, it would apply the
X+ rule as if it were @samp{[a^x, a]}.  Thus, you can write general
X+ purpose rules that include @samp{select( )} hints inside them so
X+ that they will ``do the right thing'' in both @kbd{a r} and @kbd{j r},
X+ both with and without selections.
X+ 
X+ @node Matching Commands, Examples of Rewrite Rules, Selections with Rewrite Rules, Rewrite Rules
X+ @subsection Matching Commands
X+ 
X+ The @kbd{a m} (@code{calc-match}) [@code{match}] function takes a
X+ vector of formulas and a rewrite-rule-style pattern, and produces
X+ a vector of all formulas which match the pattern.  The command
X+ prompts you to enter the pattern; as for @kbd{a r}, you can enter
X+ a single pattern (i.e., a formula with meta-variables), or a
X+ vector of patterns, or a variable which contains patterns, or
X+ you can give a blank response in which case the patterns are taken
X+ from the top of the stack.  The pattern set will be compiled once
X+ and saved if it is stored in a variable.
X+ 
X+ For example, @samp{match(a+b, [x, x+y, x-y, 7, x+y+z])}
X+ will return @samp{[x+y, x-y, x+y+z]}.
X+ 
X+ The @code{import} mechanism is not available for pattern sets.
X+ Conditional patterns must use the @code{condition} pseudo-function
X+ discussed above (@pxref{Other Features of Rewrite Rules}).
X+ 
X+ The @kbd{a m} command does not provide you with any information about
X+ which meta-variables in the pattern matched which parts of the vector
X+ elements.  To get this information you should map @code{rewrite} across
X+ the vector instead.  For example, @samp{map(lambda(x, rewrite(x, [a+b, b])),
X+ [x, x+y, x-y, 7, x+y+z])} might produce the result @samp{[x, y, -y, 7, y+z]}.
X+ Note that it would not have worked to write @samp{map(rewrite, [a+b, b],
X+ [ @dots{} ])} because @code{map} would try to map pairwise across both
X+ vectors; storing the rule in a variable would avoid this difficulty.
X+ @xref{Reducing and Mapping}.
X+ 
X+ The @kbd{a m} command can also be used to extract all vector elements
X+ which satisfy any condition:  If the pattern is @samp{condition(x, x>0)}
X+ then only positive vector elements will remain.
X+ 
X+ With the Inverse flag [@code{matchnot}], this command extracts all
X+ vector elements which do @emph{not} match the given pattern.
X+ 
X+ @node Examples of Rewrite Rules, , Matching Commands, Rewrite Rules
X+ @subsection Examples of Rewrite Rules
X+ 
X+ Returning to the example of substituting the pattern
X+ @samp{sin(x)^2 + cos(x)^2} with 1, we saw that the rule
X+ @samp{[opt(a) sin(x)^2 + opt(a) cos(x)^2, a]} does a good job of
X+ finding suitable cases.  Another solution would be to use the rule
X+ @samp{[cos(x)^2, 1 - sin(x)^2]}, followed by algebraic simplification
X+ if necessary.  This rule will be the most effective way to do the job,
X+ but at the expense of making some changes that you might not desire.@refill
X+ 
X+ Another algebraic rewrite rule is @samp{[exp(x+y), exp(x) exp(y)]}.
X+ To make this work with the @kbd{j r} command so that it can be
X+ easily targeted to a particular exponential in a large formula,
X+ you might wish to write the rule as @samp{[select(exp(x+y)),
X+ select(exp(x) exp(y))]}.  The @samp{select} markers will be
X+ ignored by the regular @kbd{a r} command
X+ (@pxref{Selections with Rewrite Rules}).@refill
X+ 
X+ As another example, we could define our own factorial function with the
X+ rules @samp{[[f(0), 1], [f(n), n*f(n-1), n>0]]}.  Enter this rules
X+ vector and store it in a variable: @kbd{s factrules}.  Now, given a
X+ factorial formula like @samp{f(5)} on the stack, type @samp{C-u 0 a r
X+ factrules} to apply these rules repeatedly.  After six applications,
X+ @code{calc-rewrite} will stop with 120 on the stack.  If we expect to
X+ use these rules often, we could define the above sequence of keystrokes
X+ as a keyboard macro.  We could then assign this macro to a single
X+ key sequence which effectively means, ``simplify all calls to the function
X+ @samp{f} on top-of-stack.''  @xref{Keyboard Macros}.@refill
X+ 
X  @node Units, Store and Recall, Algebra, Top
X  @chapter Operating on Units
X  
X***************
X*** 7694,7700 ****
X  
X  @kindex u s
X  @pindex calc-simplify-units
X! The @kbd{u s} (@code{calc-simplify-units}) command simplifies a units
X  expression.  It uses @kbd{a s} (@code{calc-simplify}) to simplify the
X  expression first as a regular algebraic formula; it then looks for
X  features that can be further simplified by converting one object's units
X--- 12787,12794 ----
X  
X  @kindex u s
X  @pindex calc-simplify-units
X! The @kbd{u s} (@code{calc-simplify-units}) [@code{usimplify}] command
X! simplifies a units
X  expression.  It uses @kbd{a s} (@code{calc-simplify}) to simplify the
X  expression first as a regular algebraic formula; it then looks for
X  features that can be further simplified by converting one object's units
X***************
X*** 7702,7708 ****
X  simplify to @samp{5.023 m}.  When different but compatible units are
X  added, the righthand term's units are converted to match those of the
X  lefthand term.  @xref{Simplification Modes}, for a way to have this done
X! automatically at all times.
X  
X  Units simplification also handles quotients of two units with the same
X  dimensionality, as in @samp{2 in s/L cm} to @samp{5.08 s/L}; fractional
X--- 12796,12802 ----
X  simplify to @samp{5.023 m}.  When different but compatible units are
X  added, the righthand term's units are converted to match those of the
X  lefthand term.  @xref{Simplification Modes}, for a way to have this done
X! automatically at all times.@refill
X  
X  Units simplification also handles quotients of two units with the same
X  dimensionality, as in @samp{2 in s/L cm} to @samp{5.08 s/L}; fractional
X***************
X*** 7807,7814 ****
X  @kindex u V
X  @pindex calc-view-units-table
X  The @kbd{u V} (@code{calc-enter-units-table}) command is like @kbd{u v} except
X! that the cursor is not moved into the Units Table buffer.  To return
X! from the Units Table buffer after a @kbd{u v}, type @kbd{M-x calc}
X  again or use the regular Emacs @kbd{C-x o} (@code{other-window})
X  command.  You can also kill the buffer with @kbd{C-x k} if you wish;
X  the actual units table is safely stored inside the Calculator.
X--- 12901,12909 ----
X  @kindex u V
X  @pindex calc-view-units-table
X  The @kbd{u V} (@code{calc-enter-units-table}) command is like @kbd{u v} except
X! that the cursor is not moved into the Units Table buffer.  You can
X! type @kbd{u V} again to remove the Units Table from the display.  To
X! return from the Units Table buffer after a @kbd{u v}, type @kbd{M-x calc}
X  again or use the regular Emacs @kbd{C-x o} (@code{other-window})
X  command.  You can also kill the buffer with @kbd{C-x k} if you wish;
X  the actual units table is safely stored inside the Calculator.
X***************
X*** 8105,8111 ****
X  The @kbd{C-y} command yanks the most recently killed text back into the
X  Calculator.  It pushes this value onto the top of the stack regardless of
X  the cursor position.  In general it re-parses the killed text as a number
X! or formula (or list of these seperated by commas or newlines).  However if
X  the thing being yanked is something that was just killed from the Calculator
X  itself, its full internal structure is yanked.  For example, if you have
X  set the floating-point display mode to show only four significant digits,
X--- 13200,13206 ----
X  The @kbd{C-y} command yanks the most recently killed text back into the
X  Calculator.  It pushes this value onto the top of the stack regardless of
X  the cursor position.  In general it re-parses the killed text as a number
X! or formula (or list of these separated by commas or newlines).  However if
X  the thing being yanked is something that was just killed from the Calculator
X  itself, its full internal structure is yanked.  For example, if you have
X  set the floating-point display mode to show only four significant digits,
X***************
X*** 8163,8168 ****
X--- 13258,13268 ----
X  then grabbing with a prefix argument of 8 would correctly split the
X  line into two error forms.
X  
X+ If you give a prefix argument of @kbd{C-u} only, the region between
X+ point and mark is taken as a continuous piece of text; line breaks
X+ are insignificant (they are treated the same as spaces).  In this
X+ mode, the region is never interpreted as a rectangle.
X+ 
X  If a different language has been specified (@pxref{Language Modes}),
X  the grabbed text will be interpreted according to the that language.
X  
X***************
X*** 8225,8231 ****
X  buffer (@samp{*Calc Edit*}) for editing the top-of-stack value using
X  regular Emacs commands.  With a numeric prefix argument, it edits the
X  specified number of stack entries at once.  (An argument of zero edits
X! the entire stack.)  When you press @key{RET}, @key{LFD}, or @kbd{C-c C-c}
X  (all bound to @code{calc-edit-finish}), the Calculator parses the
X  lines of text in this buffer as numbers or formulas, replaces the
X  original stack elements in the original buffer with these new values,
X--- 13325,13333 ----
X  buffer (@samp{*Calc Edit*}) for editing the top-of-stack value using
X  regular Emacs commands.  With a numeric prefix argument, it edits the
X  specified number of stack entries at once.  (An argument of zero edits
X! the entire stack; a negative argument edits one specific stack entry.)
X! 
X! When you press @key{RET}, @key{LFD}, or @kbd{C-c C-c}
X  (all bound to @code{calc-edit-finish}), the Calculator parses the
SHAR_EOF
echo "End of part 18, continue with part 19"
echo "19" > s2_seq_.tmp
exit 0