Go forward to Simplifying Formulas. Go backward to Selecting Subformulas. Go up to Algebra.

Algebraic Manipulation
======================

The commands in this section perform general-purpose algebraic
manipulations.  They work on the whole formula at the top of the stack
(unless, of course, you have made a selection in that formula).

Many algebra commands prompt for a variable name or formula.  If you
answer the prompt with a blank line, the variable or formula is taken
from top-of-stack, and the normal argument for the command is taken
from the second-to-top stack level.

The `a v' (`calc-alg-evaluate') command performs the normal default
simplifications on a formula; for example, `a - -b' is changed to `a +
b'.  These simplifications are normally done automatically on all Calc
results, so this command is useful only if you have turned default
simplifications off with an `m O' command.  *Note Simplification
Modes::.

It is often more convenient to type `=', which is like `a v' but which
also substitutes stored values for variables in the formula.  Use `a
v' if you want the variables to ignore their stored values.

If you give a numeric prefix argument of 2 to `a v', it simplifies as
if in algebraic simplification mode.  This is equivalent to typing `a
s'; See Simplifying Formulas.  If you give a numeric prefix of 3
or more, it uses extended simplification mode (`a e').

If you give a negative prefix argument -1, -2, or -3, it simplifies in
the corresponding mode but only works on the top-level function call
of the formula.  For example, `(2 + 3) * (2 + 3)' will simplify to `(2
+ 3)^2', without simplifying the sub-formulas `2 + 3'.  As another
example, typing `V R +' to sum the vector `[1, 2, 3, 4]' produces the
formula `reduce(add, [1, 2, 3, 4])' in no-simplify mode.  Using `a v'
will evaluate this all the way to 10; using `C-u - a v' will evaluate
it only to `1 + 2 + 3 + 4'.  (See Reducing and Mapping.)

The `=' command corresponds to the `evalv' function, and the related
`N' command, which is like `=' but temporarily disables symbolic (`m
s') mode during the evaluation, corresponds to the `evalvn' function.
(These commands interpret their prefix arguments differently than `a
v'; `=' treats the prefix as the number of stack elements to evaluate
at once, and `N' treats it as a temporary different working
precision.)

The `evalvn' function can take an alternate working precision as an
optional second argument.  This argument can be either an integer, to
set the precision absolutely, or a vector containing a single integer,
to adjust the precision relative to the current precision.  Note that
`evalvn' with a larger than current precision will do the calculation
at this higher precision, but the result will as usual be rounded back
down to the current precision afterward.  For example, `evalvn(pi -
3.1415)' at a precision of 12 will return `9.265359e-5'; `evalvn(pi -
3.1415, 30)' will return `9.26535897932e-5' (computing a 25-digit
result which is then rounded down to 12); and `evalvn(pi - 3.1415,
[-2])' will return `9.2654e-5'.

The `a "' (`calc-expand-formula') command expands functions into their
defining formulas wherever possible.  For example, `deg(x^2)' is
changed to `180 x^2 / pi'.  Most functions, like `sin' and `gcd', are
not defined by simple formulas and so are unaffected by this command.
One important class of functions which *can* be expanded is the
user-defined functions created by the `Z F' command.  *Note Algebraic
Definitions::.  Other functions which `a "' can expand include the
probability distribution functions, most of the financial functions,
and the hyperbolic and inverse hyperbolic functions.  A numeric prefix
argument affects `a "' in the same way as it does `a v': A positive
argument expands all functions in the formula and then simplifies in
various ways; a negative argument expands and simplifies only the
top-level function call.

The `a M' (`calc-map-equation') [`mapeq'] command applies
a given function or operator to one or more equations.  It is analogous
to `V M', which operates on vectors instead of equations.
See Reducing and Mapping.  For example, `a M S' changes
`x = y+1' to `sin(x) = sin(y+1)', and `a M +' with `x = y+1' and `6'
on the stack produces `x+6 = y+7'.  With two equations on the stack,
`a M +' would add the lefthand sides together and the righthand sides
together to get the two respective sides of a new equation.

Mapping also works on inequalities.  Mapping two similar inequalities
produces another inequality of the same type.  Mapping an inequality
with an equation produces an inequality of the same type.  Mapping a
`<=' with a `<' or `!=' (not-equal) produces a `<'.  If inequalities
with opposite direction (e.g., `<' and `>') are mapped, the direction
of the second inequality is reversed to match the first: Using `a M +'
on `a < b' and `a > 2' reverses the latter to get `2 < a', which then
allows the combination `a + 2 < b + a', which the `a s' command can
then simplify to get `2 < b'.

Using `a M *', `a M /', `a M n', or `a M &' to negate
or invert an inequality will reverse the direction of the inequality.
Other adjustments to inequalities are *not* done automatically;
`a M S' will change `x < y' to `sin(x) < sin(y)' even
though this is not true for all values of the variables.

With the Hyperbolic flag, `H a M' [`mapeqp'] does a plain mapping
operation without reversing the direction of any inequalities.  Thus,
`H a M &' would change `x > 2' to `1/x > 0.5'.  (This change is
mathematically incorrect, but perhaps you were fixing an inequality
which was already incorrect.)

With the Inverse flag, `I a M' [`mapeqr'] always reverses the
direction of the inequality.  You might use `I a M C' to change `x <
y' to `cos(x) > cos(y)' if you know you are working with small
positive angles.

The `a b' (`calc-substitute') [`subst'] command substitutes all
occurrences of some variable or sub-expression of an expression with a
new sub-expression.  For example, substituting `sin(x)' with `cos(y)'
in `2 sin(x)^2 + x sin(x) + sin(2 x)' produces `2 cos(y)^2 + x cos(y)
+ sin(2 x)'.  Note that this is a purely structural substitution; the
lone `x' and the `sin(2 x)' stayed the same because they did not look
like `sin(x)'.  See Rewrite Rules, for a more general method for
doing substitutions.

The `a b' command normally prompts for two formulas, the old one and
the new one.  If you enter a blank line for the first prompt, all
three arguments are taken from the stack (new, then old, then target
expression).  If you type an old formula but then enter a blank line
for the new one, the new formula is taken from top-of-stack and the
target from second-to-top.  If you answer both prompts, the target is
taken from top-of-stack as usual.

Note that `a b' has no understanding of commutativity or
associativity.  The pattern `x+y' will not match the formula `y+x'.
Also, `y+z' will not match inside the formula `x+y+z' because the `+'
operator is left-associative, so the "deep structure" of that formula
is `(x+y) + z'.  Use `d U' (`calc-unformatted-language') mode to see
the true structure of a formula.  The rewrite rule mechanism,
discussed later, does not have these limitations.

As an algebraic function, `subst' takes three arguments: Target
expression, old, new.  Note that `subst' is always evaluated
immediately, even if its arguments are variables, so if you wish to
put a call to `subst' onto the stack you must turn the default
simplifications off first (with `m O').