calc: Basic Simplifications

 
 11.3.1 Basic Simplifications
 ----------------------------
 
 This section describes basic simplifications which Calc performs in many
 situations.  For example, both binary simplifications and algebraic
 simplifications begin by performing these basic simplifications.  You
 can type ‘m I’ to restrict the simplifications done on the stack to
 these simplifications.
 
    The most basic simplification is the evaluation of functions.  For
 example, ‘2 + 3’ is evaluated to ‘5’, and ‘sqrt(9)’ is evaluated to ‘3’.
 Evaluation does not occur if the arguments to a function are somehow of
 the wrong type ‘tan([2,3,4])’), range (‘tan(90)’), or number
 (‘tan(3,5)’), or if the function name is not recognized (‘f(5)’), or if
 Symbolic mode (SeeSymbolic Mode) prevents evaluation (‘sqrt(2)’).
 
    Calc simplifies (evaluates) the arguments to a function before it
 simplifies the function itself.  Thus ‘sqrt(5+4)’ is simplified to
 ‘sqrt(9)’ before the ‘sqrt’ function itself is applied.  There are very
 few exceptions to this rule: ‘quote’, ‘lambda’, and ‘condition’ (the
 ‘::’ operator) do not evaluate their arguments, ‘if’ (the ‘? :’
 operator) does not evaluate all of its arguments, and ‘evalto’ does not
 evaluate its lefthand argument.
 
    Most commands apply at least these basic simplifications to all
 arguments they take from the stack, perform a particular operation, then
 simplify the result before pushing it back on the stack.  In the common
 special case of regular arithmetic commands like ‘+’ and ‘Q’ [‘sqrt’],
 the arguments are simply popped from the stack and collected into a
 suitable function call, which is then simplified (the arguments being
 simplified first as part of the process, as described above).
 
    Even the basic set of simplifications are too numerous to describe
 completely here, but this section will describe the ones that apply to
 the major arithmetic operators.  This list will be rather technical in
 nature, and will probably be interesting to you only if you are a
 serious user of Calc’s algebra facilities.
 
    As well as the simplifications described here, if you have stored any
 rewrite rules in the variable ‘EvalRules’ then these rules will also be
 applied before any of the basic simplifications.  SeeAutomatic
 Rewrites, for details.
 
    And now, on with the basic simplifications:
 
    Arithmetic operators like ‘+’ and ‘*’ always take two arguments in
 Calc’s internal form.  Sums and products of three or more terms are
 arranged by the associative law of algebra into a left-associative form
 for sums, ‘((a + b) + c) + d’, and (by default) a right-associative form
 for products, ‘a * (b * (c * d))’.  Formulas like ‘(a + b) + (c + d)’
 are rearranged to left-associative form, though this rarely matters
 since Calc’s algebra commands are designed to hide the inner structure
 of sums and products as much as possible.  Sums and products in their
 proper associative form will be written without parentheses in the
 examples below.
 
    Sums and products are _not_ rearranged according to the commutative
 law (‘a + b’ to ‘b + a’) except in a few special cases described below.
 Some algebra programs always rearrange terms into a canonical order,
 which enables them to see that ‘a b + b a’ can be simplified to ‘2 a b’.
 If you are using Basic Simplification mode, Calc assumes you have put
 the terms into the order you want and generally leaves that order alone,
 with the consequence that formulas like the above will only be
 simplified if you explicitly give the ‘a s’ command.  SeeAlgebraic
 Simplifications.
 
    Differences ‘a - b’ are treated like sums ‘a + (-b)’ for purposes of
 simplification; one of the default simplifications is to rewrite ‘a +
 (-b)’ or ‘(-b) + a’, where ‘-b’ represents a “negative-looking” term,
 into ‘a - b’ form.  “Negative-looking” means negative numbers, negated
 formulas like ‘-x’, and products or quotients in which either term is
 negative-looking.
 
    Other simplifications involving negation are ‘-(-x)’ to ‘x’; ‘-(a b)’
 or ‘-(a/b)’ where either ‘a’ or ‘b’ is negative-looking, simplified by
 negating that term, or else where ‘a’ or ‘b’ is any number, by negating
 that number; ‘-(a + b)’ to ‘-a - b’, and ‘-(b - a)’ to ‘a - b’.  (This,
 and rewriting ‘(-b) + a’ to ‘a - b’, are the only cases where the order
 of terms in a sum is changed by the default simplifications.)
 
    The distributive law is used to simplify sums in some cases: ‘a x + b
 x’ to ‘(a + b) x’, where ‘a’ represents a number or an implicit 1 or -1
 (as in ‘x’ or ‘-x’) and similarly for ‘b’.  Use the ‘a c’, ‘a f’, or ‘j
 M’ commands to merge sums with non-numeric coefficients using the
 distributive law.
 
    The distributive law is only used for sums of two terms, or for
 adjacent terms in a larger sum.  Thus ‘a + b + b + c’ is simplified to
 ‘a + 2 b + c’, but ‘a + b + c + b’ is not simplified.  The reason is
 that comparing all terms of a sum with one another would require time
 proportional to the square of the number of terms; Calc omits
 potentially slow operations like this in basic simplification mode.
 
    Finally, ‘a + 0’ and ‘0 + a’ are simplified to ‘a’.  A consequence of
 the above rules is that ‘0 - a’ is simplified to ‘-a’.
 
    The products ‘1 a’ and ‘a 1’ are simplified to ‘a’; ‘(-1) a’ and ‘a
 (-1)’ are simplified to ‘-a’; ‘0 a’ and ‘a 0’ are simplified to ‘0’,
 except that in Matrix mode where ‘a’ is not provably scalar the result
 is the generic zero matrix ‘idn(0)’, and that if ‘a’ is infinite the
 result is ‘nan’.
 
    Also, ‘(-a) b’ and ‘a (-b)’ are simplified to ‘-(a b)’, where this
 occurs for negated formulas but not for regular negative numbers.
 
    Products are commuted only to move numbers to the front: ‘a b 2’ is
 commuted to ‘2 a b’.
 
    The product ‘a (b + c)’ is distributed over the sum only if ‘a’ and
 at least one of ‘b’ and ‘c’ are numbers: ‘2 (x + 3)’ goes to ‘2 x + 6’.
 The formula ‘(-a) (b - c)’, where ‘-a’ is a negative number, is
 rewritten to ‘a (c - b)’.
 
    The distributive law of products and powers is used for adjacent
 terms of the product: ‘x^a x^b’ goes to ‘x^(a+b)’ where ‘a’ is a number,
 or an implicit 1 (as in ‘x’), or the implicit one-half of ‘sqrt(x)’, and
 similarly for ‘b’.  The result is written using ‘sqrt’ or ‘1/sqrt’ if
 the sum of the powers is ‘1/2’ or ‘-1/2’, respectively.  If the sum of
 the powers is zero, the product is simplified to ‘1’ or to ‘idn(1)’ if
 Matrix mode is enabled.
 
    The product of a negative power times anything but another negative
 power is changed to use division: ‘x^(-2) y’ goes to ‘y / x^2’ unless
 Matrix mode is in effect and neither ‘x’ nor ‘y’ are scalar (in which
 case it is considered unsafe to rearrange the order of the terms).
 
    Finally, ‘a (b/c)’ is rewritten to ‘(a b)/c’, and also ‘(a/b) c’ is
 changed to ‘(a c)/b’ unless in Matrix mode.
 
    Simplifications for quotients are analogous to those for products.
 The quotient ‘0 / x’ is simplified to ‘0’, with the same exceptions that
 were noted for ‘0 x’.  Likewise, ‘x / 1’ and ‘x / (-1)’ are simplified
 to ‘x’ and ‘-x’, respectively.
 
    The quotient ‘x / 0’ is left unsimplified or changed to an infinite
 quantity, as directed by the current infinite mode.  SeeInfinite
 Mode.
 
    The expression ‘a / b^(-c)’ is changed to ‘a b^c’, where ‘-c’ is any
 negative-looking power.  Also, ‘1 / b^c’ is changed to ‘b^(-c)’ for any
 power ‘c’.
 
    Also, ‘(-a) / b’ and ‘a / (-b)’ go to ‘-(a/b)’; ‘(a/b) / c’ goes to
 ‘a / (b c)’; and ‘a / (b/c)’ goes to ‘(a c) / b’ unless Matrix mode
 prevents this rearrangement.  Similarly, ‘a / (b:c)’ is simplified to
 ‘(c:b) a’ for any fraction ‘b:c’.
 
    The distributive law is applied to ‘(a + b) / c’ only if ‘c’ and at
 least one of ‘a’ and ‘b’ are numbers.  Quotients of powers and square
 roots are distributed just as described for multiplication.
 
    Quotients of products cancel only in the leading terms of the
 numerator and denominator.  In other words, ‘a x b / a y b’ is canceled
 to ‘x b / y b’ but not to ‘x / y’.  Once again this is because full
 cancellation can be slow; use ‘a s’ to cancel all terms of the quotient.
 
    Quotients of negative-looking values are simplified according to
 ‘(-a) / (-b)’ to ‘a / b’, ‘(-a) / (b - c)’ to ‘a / (c - b)’, and ‘(a -
 b) / (-c)’ to ‘(b - a) / c’.
 
    The formula ‘x^0’ is simplified to ‘1’, or to ‘idn(1)’ in Matrix
 mode.  The formula ‘0^x’ is simplified to ‘0’ unless ‘x’ is a negative
 number, complex number or zero.  If ‘x’ is negative, complex or ‘0.0’,
 ‘0^x’ is an infinity or an unsimplified formula according to the current
 infinite mode.  The expression ‘0^0’ is simplified to ‘1’.
 
    Powers of products or quotients ‘(a b)^c’, ‘(a/b)^c’ are distributed
 to ‘a^c b^c’, ‘a^c / b^c’ only if ‘c’ is an integer, or if either ‘a’ or
 ‘b’ are nonnegative real numbers.  Powers of powers ‘(a^b)^c’ are
 simplified to ‘a^(b c)’ only when ‘c’ is an integer and ‘b c’ also
 evaluates to an integer.  Without these restrictions these
 simplifications would not be safe because of problems with principal
 values.  (In other words, ‘((-3)^1:2)^2’ is safe to simplify, but
 ‘((-3)^2)^1:2’ is not.)  SeeDeclarations, for ways to inform Calc
 that your variables satisfy these requirements.
 
    As a special case of this rule, ‘sqrt(x)^n’ is simplified to
 ‘x^(n/2)’ only for even integers ‘n’.
 
    If ‘a’ is known to be real, ‘b’ is an even integer, and ‘c’ is a
 half- or quarter-integer, then ‘(a^b)^c’ is simplified to ‘abs(a^(b
 c))’.
 
    Also, ‘(-a)^b’ is simplified to ‘a^b’ if ‘b’ is an even integer, or
 to ‘-(a^b)’ if ‘b’ is an odd integer, for any negative-looking
 expression ‘-a’.
 
    Square roots ‘sqrt(x)’ generally act like one-half powers ‘x^1:2’ for
 the purposes of the above-listed simplifications.
 
    Also, note that ‘1 / x^1:2’ is changed to ‘x^(-1:2)’, but ‘1 /
 sqrt(x)’ is left alone.
 
    Generic identity matrices (SeeMatrix Mode) are simplified by the
 following rules: ‘idn(a) + b’ to ‘a + b’ if ‘b’ is provably scalar, or
 expanded out if ‘b’ is a matrix; ‘idn(a) + idn(b)’ to ‘idn(a + b)’;
 ‘-idn(a)’ to ‘idn(-a)’; ‘a idn(b)’ to ‘idn(a b)’ if ‘a’ is provably
 scalar, or to ‘a b’ if ‘a’ is provably non-scalar; ‘idn(a) idn(b)’ to
 ‘idn(a b)’; analogous simplifications for quotients involving ‘idn’; and
 ‘idn(a)^n’ to ‘idn(a^n)’ where ‘n’ is an integer.
 
    The ‘floor’ function and other integer truncation functions vanish if
 the argument is provably integer-valued, so that ‘floor(round(x))’
 simplifies to ‘round(x)’.  Also, combinations of ‘float’, ‘floor’ and
 its friends, and ‘ffloor’ and its friends, are simplified in appropriate
 ways.  SeeInteger Truncation.
 
    The expression ‘abs(-x)’ changes to ‘abs(x)’.  The expression
 ‘abs(abs(x))’ changes to ‘abs(x)’; in fact, ‘abs(x)’ changes to ‘x’ or
 ‘-x’ if ‘x’ is provably nonnegative or nonpositive (See
 Declarations).
 
    While most functions do not recognize the variable ‘i’ as an
 imaginary number, the ‘arg’ function does handle the two cases ‘arg(i)’
 and ‘arg(-i)’ just for convenience.
 
    The expression ‘conj(conj(x))’ simplifies to ‘x’.  Various other
 expressions involving ‘conj’, ‘re’, and ‘im’ are simplified, especially
 if some of the arguments are provably real or involve the constant ‘i’.
 For example, ‘conj(a + b i)’ is changed to ‘conj(a) - conj(b) i’, or to
 ‘a - b i’ if ‘a’ and ‘b’ are known to be real.
 
    Functions like ‘sin’ and ‘arctan’ generally don’t have any default
 simplifications beyond simply evaluating the functions for suitable
 numeric arguments and infinity.  The algebraic simplifications described
 in the next section do provide some simplifications for these functions,
 though.
 
    One important simplification that does occur is that ‘ln(e)’ is
 simplified to 1, and ‘ln(e^x)’ is simplified to ‘x’ for any ‘x’.  This
 occurs even if you have stored a different value in the Calc variable
 ‘e’; but this would be a bad idea in any case if you were also using
 natural logarithms!
 
    Among the logical functions, !(A <= B) changes to A > B and so on.
 Equations and inequalities where both sides are either negative-looking
 or zero are simplified by negating both sides and reversing the
 inequality.  While it might seem reasonable to simplify ‘!!x’ to ‘x’,
 this would not be valid in general because ‘!!2’ is 1, not 2.
 
    Most other Calc functions have few if any basic simplifications
 defined, aside of course from evaluation when the arguments are suitable
 numbers.