calc: Algebraic Simplifications

 
 11.3.2 Algebraic Simplifications
 --------------------------------
 
 This section describes all simplifications that are performed by the
 algebraic simplification mode, which is the default simplification mode.
 If you have switched to a different simplification mode, you can switch
 back with the ‘m A’ command.  Even in other simplification modes, the ‘a
 s’ command will use these algebraic simplifications to simplify the
 formula.
 
    There is a variable, ‘AlgSimpRules’, in which you can put rewrites to
 be applied.  Its use is analogous to ‘EvalRules’, but without the
 special restrictions.  Basically, the simplifier does ‘a r AlgSimpRules’
 with an infinite repeat count on the whole expression being simplified,
 then it traverses the expression applying the built-in rules described
 below.  If the result is different from the original expression, the
 process repeats with the basic simplifications (including ‘EvalRules’),
 then ‘AlgSimpRules’, then the built-in simplifications, and so on.
 
    Sums are simplified in two ways.  Constant terms are commuted to the
 end of the sum, so that ‘a + 2 + b’ changes to ‘a + b + 2’.  The only
 exception is that a constant will not be commuted away from the first
 position of a difference, i.e., ‘2 - x’ is not commuted to ‘-x + 2’.
 
    Also, terms of sums are combined by the distributive law, as in ‘x +
 y + 2 x’ to ‘y + 3 x’.  This always occurs for adjacent terms, but
 Calc’s algebraic simplifications compare all pairs of terms including
 non-adjacent ones.
 
    Products are sorted into a canonical order using the commutative law.
 For example, ‘b c a’ is commuted to ‘a b c’.  This allows easier
 comparison of products; for example, the basic simplifications will not
 change ‘x y + y x’ to ‘2 x y’, but the algebraic simplifications; it
 first rewrites the sum to ‘x y + x y’ which can then be recognized as a
 sum of identical terms.
 
    The canonical ordering used to sort terms of products has the
 property that real-valued numbers, interval forms and infinities come
 first, and are sorted into increasing order.  The ‘V S’ command uses the
 same ordering when sorting a vector.
 
    Sorting of terms of products is inhibited when Matrix mode is turned
 on; in this case, Calc will never exchange the order of two terms unless
 it knows at least one of the terms is a scalar.
 
    Products of powers are distributed by comparing all pairs of terms,
 using the same method that the default simplifications use for adjacent
 terms of products.
 
    Even though sums are not sorted, the commutative law is still taken
 into account when terms of a product are being compared.  Thus ‘(x + y)
 (y + x)’ will be simplified to ‘(x + y)^2’.  A subtle point is that ‘(x
 - y) (y - x)’ will _not_ be simplified to ‘-(x - y)^2’; Calc does not
 notice that one term can be written as a constant times the other, even
 if that constant is -1.
 
    A fraction times any expression, ‘(a:b) x’, is changed to a quotient
 involving integers: ‘a x / b’.  This is not done for floating-point
 numbers like ‘0.5’, however.  This is one reason why you may find it
 convenient to turn Fraction mode on while doing algebra; SeeFraction
 Mode.
 
    Quotients are simplified by comparing all terms in the numerator with
 all terms in the denominator for possible cancellation using the
 distributive law.  For example, ‘a x^2 b / c x^3 d’ will cancel ‘x^2’
 from the top and bottom to get ‘a b / c x d’.  (The terms in the
 denominator will then be rearranged to ‘c d x’ as described above.)  If
 there is any common integer or fractional factor in the numerator and
 denominator, it is canceled out; for example, ‘(4 x + 6) / 8 x’
 simplifies to ‘(2 x + 3) / 4 x’.
 
    Non-constant common factors are not found even by algebraic
 simplifications.  To cancel the factor ‘a’ in ‘(a x + a) / a^2’ you
 could first use ‘j M’ on the product ‘a x’ to Merge the numerator to ‘a
 (1+x)’, which can then be simplified successfully.
 
    Integer powers of the variable ‘i’ are simplified according to the
 identity ‘i^2 = -1’.  If you store a new value other than the complex
 number ‘(0,1)’ in ‘i’, this simplification will no longer occur.  This
 is not done by the basic simplifications; in case someone (unwisely)
 wants to use the name ‘i’ for a variable unrelated to complex numbers,
 they can use basic simplification mode.
 
    Square roots of integer or rational arguments are simplified in
 several ways.  (Note that these will be left unevaluated only in
 Symbolic mode.)  First, square integer or rational factors are pulled
 out so that ‘sqrt(8)’ is rewritten as ‘2 sqrt(2)’.  Conceptually
 speaking this implies factoring the argument into primes and moving
 pairs of primes out of the square root, but for reasons of efficiency
 Calc only looks for primes up to 29.
 
    Square roots in the denominator of a quotient are moved to the
 numerator: ‘1 / sqrt(3)’ changes to ‘sqrt(3) / 3’.  The same effect
 occurs for the square root of a fraction: ‘sqrt(2:3)’ changes to
 ‘sqrt(6) / 3’.
 
    The ‘%’ (modulo) operator is simplified in several ways when the
 modulus ‘M’ is a positive real number.  First, if the argument is of the
 form ‘x + n’ for some real number ‘n’, then ‘n’ is itself reduced modulo
 ‘M’.  For example, ‘(x - 23) % 10’ is simplified to ‘(x + 7) % 10’.
 
    If the argument is multiplied by a constant, and this constant has a
 common integer divisor with the modulus, then this factor is canceled
 out.  For example, ‘12 x % 15’ is changed to ‘3 (4 x % 5)’ by factoring
 out 3.  Also, ‘(12 x + 1) % 15’ is changed to ‘3 ((4 x + 1:3) % 5)’.
 While these forms may not seem “simpler,” they allow Calc to discover
 useful information about modulo forms in the presence of declarations.
 
    If the modulus is 1, then Calc can use ‘int’ declarations to evaluate
 the expression.  For example, the idiom ‘x % 2’ is often used to check
 whether a number is odd or even.  As described above, ‘2 n % 2’ and ‘(2
 n + 1) % 2’ are simplified to ‘2 (n % 1)’ and ‘2 ((n + 1:2) % 1)’,
 respectively; Calc can simplify these to 0 and 1 (respectively) if ‘n’
 has been declared to be an integer.
 
    Trigonometric functions are simplified in several ways.  Whenever a
 products of two trigonometric functions can be replaced by a single
 function, the replacement is made; for example, ‘tan(x) cos(x)’ is
 simplified to ‘sin(x)’.  Reciprocals of trigonometric functions are
 replaced by their reciprocal function; for example, ‘1/sec(x)’ is
 simplified to ‘cos(x)’.  The corresponding simplifications for the
 hyperbolic functions are also handled.
 
    Trigonometric functions of their inverse functions are simplified.
 The expression ‘sin(arcsin(x))’ is simplified to ‘x’, and similarly for
 ‘cos’ and ‘tan’.  Trigonometric functions of inverses of different
 trigonometric functions can also be simplified, as in ‘sin(arccos(x))’
 to ‘sqrt(1 - x^2)’.
 
    If the argument to ‘sin’ is negative-looking, it is simplified to
 ‘-sin(x)’, and similarly for ‘cos’ and ‘tan’.  Finally, certain special
 values of the argument are recognized; SeeTrigonometric and
 Hyperbolic Functions.
 
    Hyperbolic functions of their inverses and of negative-looking
 arguments are also handled, as are exponentials of inverse hyperbolic
 functions.
 
    No simplifications for inverse trigonometric and hyperbolic functions
 are known, except for negative arguments of ‘arcsin’, ‘arctan’,
 ‘arcsinh’, and ‘arctanh’.  Note that ‘arcsin(sin(x))’ can _not_ safely
 change to ‘x’, since this only correct within an integer multiple of ‘2
 pi’ radians or 360 degrees.  However, ‘arcsinh(sinh(x))’ is simplified
 to ‘x’ if ‘x’ is known to be real.
 
    Several simplifications that apply to logarithms and exponentials are
 that ‘exp(ln(x))’, ‘e^ln(x)’, and ‘10^log10(x)’ all reduce to ‘x’.
 Also, ‘ln(exp(x))’, etc., can reduce to ‘x’ if ‘x’ is provably real.
 The form ‘exp(x)^y’ is simplified to ‘exp(x y)’.  If ‘x’ is a suitable
 multiple of ‘pi i’ (as described above for the trigonometric functions),
 then ‘exp(x)’ or ‘e^x’ will be expanded.  Finally, ‘ln(x)’ is simplified
 to a form involving ‘pi’ and ‘i’ where ‘x’ is provably negative,
 positive imaginary, or negative imaginary.
 
    The error functions ‘erf’ and ‘erfc’ are simplified when their
 arguments are negative-looking or are calls to the ‘conj’ function.
 
    Equations and inequalities are simplified by canceling factors of
 products, quotients, or sums on both sides.  Inequalities change sign if
 a negative multiplicative factor is canceled.  Non-constant
 multiplicative factors as in ‘a b = a c’ are canceled from equations
 only if they are provably nonzero (generally because they were declared
 so; SeeDeclarations).  Factors are canceled from inequalities only
 if they are nonzero and their sign is known.
 
    Simplification also replaces an equation or inequality with 1 or 0
 (“true” or “false”) if it can through the use of declarations.  If ‘x’
 is declared to be an integer greater than 5, then ‘x < 3’, ‘x = 3’, and
 ‘x = 7.5’ are all simplified to 0, but ‘x > 3’ is simplified to 1.  By a
 similar analysis, ‘abs(x) >= 0’ is simplified to 1, as is ‘x^2 >= 0’ if
 ‘x’ is known to be real.