# How to simplify sin3x cosx

## Transformation and simplification of mathematical expressions

Transcript

1 Chapter 4 Transforming and Simplifying Mathematical Expressions Although the title of this chapter sounds abstract, the chapter covers one of the most important topics in this book: transforming and simplifying mathematical expressions. In mathematics, there are often many different ways to express the same thing. so you can write x, but also (x +) (x), which is less a question of taste than of the particular application you are interested in. So we have already addressed one of the fundamental problems of simplification: Depending on the purpose, you will use the Simplifying different, contradicting goals, of course, makes it difficult to automate such a process. Mathematica provides you with two central commands for simplifying expressions, Simplify and FullSimplify, which both try to bring an expression into the simplest possible form without considering the objective. Simplify is the everyday command that works with a limited repertoire of tricks, but is sufficient in most situations to bring expressions into the simplest possible form. FullSimplify uses all of Mathematica's capabilities, which in some cases (especially when algebraic numbers are involved) can lead to further significant simplifications, but at the expense of computing time. In addition, there are a number of commands to initiate different transformations, some of which have contradicting objectives. Finally, with rules you have compiled yourself, you can persuade Mathematica to carry out some transformations that it does not want to undertake for whatever reason. However, it is often a lack of mathematical accuracy, as in the case of the transformation p xy 7! p x p y (just substitute x = y =!), which prevents Mathematica from doing such a transformation automatically. Computer algebra systems such as Mathematica separate for calculating and simplifying and usually return the results of algorithmic procedures in a form directly produced by the algorithm, without processing them before output. Particularly when integrating, very long expressions can arise that can be further simplified, what you do As a user, however, you have to do it yourself. In the simplest case, you can attach == Simplify to every integration command. Please note, however, that processing it takes some time. You can access the result

2 34 4 Reshaping and simplifying mathematical expressions but also using more specific transformations that may lead to a simplification faster than this all-round command. Such more special commands with partially opposing effects are: Expand and Factor Expand multiplies all products, Factor tries to factor expressions as much as possible. The special variants PowerExpand and LogicalExpand only affect powers or logical expressions. Together and Apart can be used to process rational functions. Together brings several fractions to a common denominator, Apart performs a partial fraction decomposition. TrigReduce, TrigExpand, TrigFactor are special commands that are particularly helpful when processing trigonometric expressions. TrigToExp, ExpToTrig converts trigonometric expressions into exponential notation (which is sometimes more favorable for more complex transformations) or vice versa. Simplifying expressions is also a mathematically demanding topic, the algorithmic implementation of which is often close to the limit of algorithmic unsolvability in principle, which makes it all the more important for the user of a system like Mathematica to know and be able to identify sub-classes with good simplification behavior. After a somewhat more detailed presentation of the problem of simplification, this chapter first focuses on pseudorational expressions as the most important such subclass. Then, using trigonometric applications, we will demonstrate which questions arise for expressions that can be meaningfully transformed in several different directions, and how you can find clever answers. With the integration of trigonometric functions, we also take up a topic from the chapter again. Do not regard this part only as an introduction to working with trigonometric functions, but also as an example for dealing with other classes of functions, which we cannot all deal with individually in this book. The commands there may be different, but the basic considerations are similar. An important topic in simplifying expressions, saved for the next chapter, is simplifying expressions that contain algebraic numbers (especially root symbols).

3 The problem of simplification 35 The problem of simplification It cannot always be clearly stated which is the simplest form of a mathematical expression. A definition that is close to human thought might be that an expression is simple when it is composed of as few individual terms as possible. But that does not mean that this form is also mathematically favorable (for example, if the expression has to be integrated afterwards). The path to a simpler form of a mathematical expression is even less clear. (x) (x +) (x 4) is shorter in the expanded form x 4 6, with (x) 0 +, on the other hand, expanding it would be exactly the wrong way, it led to a sum of 0 terms. It is also difficult to answer the question of what we can expect in this direction at all. The simple representation of this expression can still be found by factoring. If you subtract, Mathematica leaves the result in this short form. Simplify [xx +9 xx 4 + x 5] (3 + x) (4 + x) 3% == Simplify + (3 + x) (4 + x) 3 Simplify is no longer able to get out of the expanded Polynomial to recover the short representation. The more powerful FullSimplify attempts another simplification, a decomposition of the polynomial according to the Horner scheme, which is more efficient than the expanded representation, at least for numerical purposes. % == Expand == Simplify xx + 9 x 3 + 8x 4 + x 5% == FullSimplify x (86 + x (460 + x (9 + x (8 + x)))) To display (3 + x To find (4 + x) 3, Mathematica would also have to dig into the neighborhood of the polynomial by varying each of the 5 coefficients before x 0; :::; x 4 by, say, ±; that would result in 5 polynomials to examine a five-fold increase in the effort without the chance of at least a significant percentage of cases of achieving a better result. Simplifying with Simplify Computer algebra systems such as Mathematica therefore separate the phase of calculating results and the phase of simplifying these results. From the result of the following fourth derivation you can still guess the multiple application of the quotient rules. Simplify (but also the more targeted Together command, which only breaks down

4 36 4 reshaping and simplifying mathematical expressions) bring the result into the second, simpler and preferable form for further processing. D "+ xpx; fx; 4g # + x 05 x 4 +8 x% == Simplify x + x (+ 90 xx) 9 = (+ 9 x) 7 = (x) 5 = 5 x 3 (+ 9x x ) 7 = (x) 5 = (x) 9 = + 3x (+ x) 5 = (x) 3 = When simplifying, Mathematica assumes that the user is actively involved in the simplification process. In the simplest case, this can be done with the Commands Simplify or FullSimplify happen, but more complicated actions are also possible. If you suspect that the expression is close to a simpler one, then you can compile and examine a list of these modified expressions yourself. The commands Simplify and FullSimplify simplify expressions by adding They try a number of common algorithmic tricks such as factoring, expanding, applying different identities, and the like, and returning the simplest of the results (the one with the fewest terms); the emphasis is on trying, because sometimes the result is more complicated than the original expression. You should be from de n commands don't expect miracles! Mathematica of course knows the simple rule Sin [x] + Cos [x] =. It is also used when the arguments of the trigonometric functions are formulated a little differently. There are also some rules for logarithmic functions built into Simplify. Simplify [Sin [x] + Cos [x]] Simplify Sin [a b] + Cos [(a b) (a + b)] Λ Simplify [E alog [b]] b a

5 The problem of simplification 37 The criterion for the simplicity of mathematical expressions is usually their brevity. Simplify therefore mostly recognizes whether it is better to multiply products or not. Simplify [(x) 0 +] + (+ x) 0 Simplify [(ab) (a + b)] 4 ab The Simplify arsenal is limited and optimized with regard to computing time, while FullSimplify tries to exploit all the capabilities of Mathematica, which is in In some cases (especially when algebraic numbers are involved) this can lead to further significant simplifications at the expense of computing time (the command can be aborted with ± Ξ Alt Π + ± Ξ. Π. Alternatively, you can set a time limit with the TimeConstraint option (in seconds) or exclude individual functions from simplification attempts using the ExcludeForms option.) If the result of a calculation is available and you do not know which of the many transformation commands will help, then Simplify is at least worth a try according to the motto: Doesn't help, so it doesn't do any harm. caution is only required with very complex expressions: It can happen that the computing time increases to limitless numbers. Simplify stops automatically after five minutes, FullSimplify, on the other hand, has no time limit in the default setting. Targeted transformation of expressions Often, however, the aim is not to simply simplify an expression, but rather to bring it into a special form that is favorable for further processing. In the relationship Log [x] = Log [x +] + Log [x] you will e.g. prefer the right expression when you want to integrate afterwards, the left expression when solving an equation. The simplification of expressions is part of a more comprehensive problem that Mathematica has to deal with: the targeted transformation of expressions into a form that is mathematically (semantically) equivalent, but textually (syntactically) different, the appearance of which is roughly specified. A number of commands are available for this, such as Expand and Factor, Together and Apart, TrigReduce, TrigExpand, TrigFactor, etc., which we will discuss in more detail below. Such special transformation commands are often more suitable than Simplify for simplifying expressions, because You can use them to specifically influence the appearance of the result and are not bound for better or for worse to what is happening in the black box. These commands are also usually faster because they only do part of the work of Simplify. But we come back to the relationship Log [x] = Log [x +] + Log [x]. Obviously, Mathematica does not use this when integrating, because the antiderivatives of the two sides calculated by the computer differ slightly.

6 38 4 Transformation and simplification of mathematical expressions Z Log x Λ dlx x Λ Log [+ x] + Log [+ x] + x Log + xs =% == Simplify Λ Log [+ x] + log [+ x] + x + Log + x Z (Log [x] + Log [x +]) dlx x Log [+ x] + xlog [+ x] + log [+ x] + xlog [+ x] s =% == Simplify x + (+ x) Log [+ x] + (+ x) Log [+ x] So when integrating, Mathematica takes a different approach than that of factoring the argument of the logarithm function. However, even FullSimplify cannot determine the equivalence of the results. s == s == FullSimplify x Log [+ x] + Log [+ x] Log [+ x] == 0 The reason for this lies in questions of mathematical accuracy, which we will return to below. Solve Log x Λ Λ == 0; x Even when solving an equation that contains the term Log [x], the two expressions are not equivalent. ΦΦ x! p Ψ Φ; x! p ΨΨ Solve [Log [x] + Log [x +] == 0; x] ΦΦ x! p ΨΨ You may suspect that the reason is that this is related to the more restricted (real) domain of the second expression. This argument is not valid, as we have already emphasized, because by default all functions are considered to be complex-valued. We will come back to this question below. Recognizing semantically equivalent expressions Another problem that arises occasionally is the comparison of results that have been achieved for the same task in different ways and have completely different looks. We were already confronted with this question in the above integration task, where the comparison with s == s == FullSimplify was attempted. Mathematica has the task of immediately examining s s == 0

7 The problem of simplification 39 transforms, as you can see from the output, since it is much easier to carry out a null test than to compare the expressions directly. The task of deciding whether a given expression is mathematically equivalent to the zero expression is known as the problem of simplification. Their algorithmic solution was the subject of 0 Hilbert's problem. Since the work of Matijasevich and Roberts at the end of the 1960s, it has been known that this task is one of the algorithmically unsolvable, i.e. there is no algorithm that is able to produce a sufficiently rich Class of expressions to identify all zero equivalents. When simplifying expressions, Mathematica, and thus also the advanced user, are often close to the limit of algorithmic undecidability in principle, which demands your ability to recognize from the appearance of concrete expressions which special types of simplifications are to be applied that are safe and lead to results in a time-friendly manner. Transformations and mathematical accuracy. Additional assumptions Mathematica knows a lot of transformations and performs some particularly elementary ones automatically. You will be all the more astonished that these three expressions are so resistant to simplification and are not each simply converted to x. Φ Sqrt [x]; Exp [3 Log [x]]; Tan [ArcSin [x]] Ψ ρ x; x 3; ff x p x Φ ArcTan [Tan [x]]; Log [Exp [x]]; Sqrt x ΛΨ == FullSimplify no ArcTan [Tan [x]]; Log [e x]; px Let's start with a plot of the first term. f = ArcTan [Tan [x]]; Plot [f; fx; 5; 5g; PlotRange! F; G]; So Mathematica was right not to simplify as carelessly as suggested, because ArcTan is only the inverse function of Tan in the monotonic interval ß

8 330 4 Transformation and simplification of mathematical expressions Mathematica knows the correct value for special arguments. Please also note our procedure: f is a variable for the symbolic expression and not a function. x is assigned a value through local substitution. The simplification is only correct for real x between ß and ß. You can pass such intervals to Simplify and FullSimplify as the second parameter. Now the simplification is carried out. f =: x! ß 5 ß 5 Simplify [f;

9 The problem of simplification 33 Complexes Reals Algebraics Rationals Integers Primes Booleans area C of complex numbers area R of real numbers area of ​​algebraic numbers area Q of rational numbers area Z of whole numbers area of ​​prime numbers Boolean area ftrue; Falseg Such a range specification can also be used in the Simplify command. It can be combined with other Boolean expressions. Here it is checked whether 49 is a prime number. Mathematica automatically simplifies the expression. As expected, this command simplifies our log printout.That leaves the third term: If x reals are assumed here, a simplification also takes place, but for the mathematically correct answer jxj instead of x. 49 Primes True Simplify [f; x Reals] x f = Sqrt x Λ; Simplify [f; x Reals] Abs [x] Two more simplification-resistant expressions, which you may have noticed on page 88, should be mentioned: The first concerns the merging of products of roots into a common radicand. Mathematica refuses to do this quite consistently. The reason becomes obviously, if you substitute x = y =: pppp = II =, but () () = = +. The expression simplifies to 0 only if one of the two radicands is a positive real number. w = p x p y p xy == Simplify p x p y p xy w =: ffx! ; y! G; fx! ; y! gg f0; g Simplify [w; x> 0] 0

10 33 4 Transformation and simplification of mathematical expressions The reciprocal values ​​of roots cause similar concerns. Here, too, simplification is refused because it leads to incorrect results for negative real numbers. For x = the left side is equal to = I, the right side is equal to p I = + I. r w = p == Simplify x x r x + p x w =: ffx! G; fx! gg f0; ig The reason for this behavior lies in the nature of the root function for complex numbers, which (like Log and ArcTan, by the way) is actually multi-valued. The definition of the complex root function in Mathematica doesn't seem too consistent, because adding a tiny imaginary part to it changes the answer fundamentally. w =: x! + 0:00 I == Chop 0 Even the corresponding limit value when x approaches the real axis is equal to 0. Limit [w =: fx! + aig; a! 0] However, this is not a deficit, it is mathematically necessary, because such multi-valued functions are not continuous along their branch cut. Further explanations can be found in section 3..7 of the Mathematica manual. The resistance to simplification of the log printout on page 37 is now clearer. For x . 0 l = Log [+ x] + Log [+ x] Log + x Λ; l =: x! i ß Simplify [l; x>] 0 Agree on your own transformation rules If Mathematica consistently refuses to simplify things, you can always persuade it to do so with well-defined rules. the ingredients for this, patterns and the rule operator !, you are already familiar with the following system two rules summarize root expressions: sqrtrules = ρ px py! p xy; p y! r ff; y

It is applied to an expression with the operator =: (ReplaceAll, one-time application of the rules) or ==: (ReplaceRepeated, application as often as possible). In our case, it not only simplifies products and Reciprocal values, but also quotients of roots, since quotients A Mathematica internally as Times [A; Power [B; ]], that is, they are composed of a product and a reciprocal value. (For this reason =: is not sufficient for B simplification.) Knowledge of such internals is particularly necessary for the establishment of such rule systems. come in connection with trigonometric expressions we return to it from page 347. (r p r) p p p x x x y xy; px x; p ==: sqrtrules y y f0; 0; 0g In this case, however, you can also achieve the same effect with the PowerExpand command. This also simplifies the root expressions to zero. To be more precise, Powerexpand has the opposite of the specified rules - it expands (among other things) roots of products and quotients into products and quotients of roots . Please note, therefore, that using PowerExpand can lead to incorrect results, because you have just seen that such transformations are not always mathematically exact! Polynomial, rational and pseudorational expressions The simplification problem can be decided particularly easily for polynomials and rational functions: You only have to multiply everything or bring it to a common main denominator and then combine similar summands (of the numerator). If all terms cancel each other out, the expression being examined is equal to 0, otherwise not. If you order the summands appropriately, then the polynomial result in the second case is even the rational normal form, i.e. two results that look different are also mathematically different. Expressions with a polynomial or rational structure therefore play an important role for Mathematica, whereby the definition of the variables is not seen as narrowly and function expressions such as Sin [a + b] or Cos [xy] (so-called generalized kernels) are also permissible. such expressions are called pseudorational. Of course, relationships between the generalized kernels of such pseudorational expressions are not included in their rational simplification. In this case, you cannot conclude that an expression in which not all terms have been abbreviated is also mathematically different from zero.

12 334 4 Transformation and Simplification of Mathematical Expressions The corresponding commands with which the polynomial or rational simplifications mentioned can be carried out are Expand and Together, and should have a central place in your repertoire immediately after Simplify. Expansion of products The function of Expand is easy to recognize in the example opposite: The command simply releases all brackets. x (xa) (xb) 3 x (a + x) (b + x) 3 Expand [%] from 3 x + 3a bx + from 3 x 3a bx 3 6ab x 3 b 3 x 3 + ax 4 + 6abx 4 + 3b x 4 ax 5 3bx 5 + x 6 A pattern can be specified in the optional second parameter. Factors in which this pattern (here simply the variable x) does not occur remain unaffected by Expand. The effect of this command is therefore an expansion only after the variable x. With Collect you can summarize the coefficients before the individual powers of x. Expand acts on this pseudorational expression as if it were the polynomial (A + B) 3 with A = Sin [x]; B = Cos [x] is the relationship Sin [x] + Cos [x] = is not taken into account. With fractions, Expand only affects the numerator. (This is also sufficient for rational normal forms.) With Together you can bring the result to a common main denominator. Expand [(x + a) (x + b) (a + b); x] from (a + b) + a (a + b) x + b (a + b) x + (a + b) x Collect [%; x] from (a + b) + + (a + b) x Expand (Sin [x] + Cos [x]) 3Λ Cos [x] 3 + 3Cos [x] Sin [x] + 3 Cos [x] Sin [x] + Sin [x] 3 Expand (x +) 3 (x) 4 Λ (+ x) + 3x 4 (+ x) + 4 3x 3 (+ x) + x 4 (+ x) 4 a (a + b) + b (a + b) x

13 Polynomial, rational and pseudorational expressions 335 ExpandAll, on the other hand, is applied to all partial expressions. (x +) 3 Λ ExpandAll == Together (x) 4 + 3x + 3x + x 3 4x + 6x 4x 3 + x 4 If you only want to expand the denominator ExpandDenominator makes it possible! The adjacent examples again demonstrate the difference between Expand and ExpandAll. While in the first example the kernel A = Sin [(x +)] remains unchanged, in the second example the inside of the kernel is also expanded. (x +) 3 Λ ExpandDenominator (x) 4 (x +) 3 4x + 6x 4x 3 + x 4 Expand [(Sin [(x +)] +)] + Sin [(+ x)] + Sin [(+ x)] ExpandAll [(Sin [(x +)] +)] + Sin [+ x + x] + Sin [+ x + x] Other expand commands The following commands have nothing to do with rational functions, but expand expressions in other meanings of this word; they are briefly listed here for the sake of completeness. Simplifying powers (PowerExpand) PowerExpand simplifies roots and powers. These transformations are not always mathematically exact (e.g. p n = n only applies to n 0) and should therefore be used with caution. This topic is discussed in more detail on page 33. PowerExpand Sin pnt ΛΛ Sin [nt] PowerExpand [(ab) c] acbc PowerExpand expands products and quotients that appear in the first argument of Power into products and quotients of powers or roots. Mathematica has no command for the inverse transformation. To do this, you must use the control system described on page 33.

14 336 4 Transformation and simplification of mathematical expressions Simplifying complex expressions (ComplexExpand) ComplexExpand simplifies expressions in which the real and imaginary parts are expressed by separate variables. ComplexExpand [Sin [a + bi]] Cosh [b] Sin [a] + I Cos [a] Sinh [b] The simplification is made under the assumption that all occurring variables are real numbers. Further examples can be found in the chapter on Page 66. Simplify logical expressions (LogicalExpand) The LogicalExpand command simplifies logical expressions according to the rules of Boolean algebra. In the second expression, Xor and implies are written with their operators. The brackets are required to group the terms correctly. LogicalExpand [Xor [a; b] jj implies [a; b]] True LogicalExpand [(a_b) ^ (a) b)] b &&! a Simplify the arguments of trigonometric expressions (TrigExpand) TrigExpand applies addition theorems to convert trigonometric or hyperbolic functions with compound arguments (angle sums, multiple angles) into those with simple arguments transform. TrigExpand [Cos [ff + fi]] Cos [ff] Cos [fi] Sin [ff] Sin [fi] Further commands for processing trigonometric expressions are presented on page 343. Collecting expressions according to variables (Collect) Mathematica arranges the individual terms of an expression (whether sum or product) according to an internally defined order, so that you have no influence on this output (this generally applies to function symbols with the attribute Orderless.) Haif = Expand + xy + by + xy + cy + x 3 + x 4 ya + x + x 3 + aby 4xy + ax y + 4y + by + cxy + bx y + x 4 y 4cy 3 + cy 4

15 Polynomial, Rational, and Pseudorational Expressions 337 With Collect you can combine the terms to the same power of a variable, such as x, in one expression. Collect [f; x] a + x 3 + aby + 4y + by + x 4 y 4cy 3 + cy 4 + x + ay + by + 4y + cy x However, these terms are not necessarily ordered according to x-powers, since the internal order is in the In most cases, the complexity (depth) of the terms goes first. Depth List% f; ; ; 3; 3; 3; 3; 3; 5; So 5g terms with brackets have a good chance of being further back than single terms (as in the first example x 3 or x 4 y, which are submerged under the x-free terms). However, this is not always the case, as the following example shows. Collect [f; y] a + x + x 3 + Depth List% f; ; ; 5; 5; 3; 3g from 4x + ax y b + cx + bx + x y 4cy 3 + c y 4 You can find further details on this internal order in section A.3.9 of the Mathematica manual. But we come back to the Collect command. You can get more detailed information, especially access to the individual coefficients of such a decomposition, with the CoefficientList and Coefficient functions. The first command returns the list of all coefficients in order of increasing x-power, the second can can be used to extract individual coefficients. CoefficientList [f; x] Φa + aby + 4y + b y 4cy 3 + c y 4; 4 y + cy; + ay + by; ; y Ψ coefficient f; x Λ or Coefficient [f; x; ] + ay + by coefficient [f; y n] and coefficient [f; y; n] are equivalent to each other for n> 0. The first command does not work for n = 0 (reason: y 0 is too simplified and there is no such variable.) Here the coefficients of the powers y 0; y and y of f (coefficient is of course also listable, so the command is distributed via the list in the third argument.)

16 338 4 Transformation and simplification of mathematical expressions Coefficient [f; y; f0; ; g] Φa + x + x 3; from 4x + ax; 4 + b + cx + bx + x 4Ψ The second parameter of Collect can be a list; in this case, it is grouped by all specified variables; note the difference in output between the first and second example; in the first example, f according to a and the coefficients according to y, in the second example it is the other way round. Collect [f; fa; yg] a + x + x 3 4xy + a b + x y b + cx + bx + x y 4cy 3 + c y 4 Collect [f; fy; ag] a + x + x 3 + 4x + a b + xyb + cx + bx + xy 4cy 3 + cy 4 CoefficientList and Coefficient can also be used to extract coefficients of multivariate terms. The first command extracts a list of lists of the Coefficient, the second a coefficient for a special term. CoefficientList [f; fx; yg] ΦΦ a; from; 4 + b; 4 c; c Ψ; f0; 4; c; 0; 0g; f; a; b; 0; 0g; f; 0; 0; 0; 0g; f0; 0; ; 0; 0g Ψ coefficient f; x y Λ A pure function can be transferred to Collect, which is applied to all groups found. Functions that further simplify this group are particularly suitable. Collect [f; x; Simplify] a + x 3 + aby + x 4 y + xy (+ cy) + x + ay + by + yb + (+ cy) a Collect not only works with individual variables, but also with cores like Cos [x] . h Sin [x] Expand Cos [x] + (Cos [x]) i Cos [x] + Cos [x] + Cos [x] 4 Cos [x] Sin [x] + Sin [x] Collect [%; Cos [x]] Cos [x] + Cos [x] 4 + Cos [x] (Sin [x]) + Sin [x]

17 Polynomial, Rational and Pseudorational Expressions 339 Factoring Sums Factor is the counterpart to Expand as far as the expansion of products is concerned. Factor tries to convert a sum of individual terms back into the product form. Factor [4x + 6x 4x 3 + x 4] (+ x) 4 Factor [x 4 y 4] (xy) (x + y) (x + y) Factor also works for pseudorational expressions with generalized kernels as variables a distinction is made between four such cores (Cos [3 x]; Sin [3 x]; Cos [x] and Sin [x]) a factorization is not possible. s = Integrate [Sin [x] 3 + Cos [x] 3; x] 3 Cos [x] 4 s == Factor + Cos [3 x] + 3 Sin [x] 4 + Sin [3 x] (9 Cos [x] + Cos [3 x] + 9 Sin [x] + Sin [3 x]) TrigExpand reduces the number of nuclei to two (Cos [x] and Sin [x]). Now a factor can be split off. s == TrigExpand 3 Cos [x] 4% == Factor + Cos [x] 3 + 3 Sin [x] Cos [x] Sin [x] 4 Cos [x] Sin [x] Sin [x] 3 (Cos [x] Sin [x]) 9 + Cos [x] + 4 Cos [x] Sin [x] + Sin [x] Simplify can also simplify the second factor. % == Simplify (Cos [x] Sin [x]) (4 + Sin [x]) 6 The TrigFactor command also effects these transformations, which tries to break down a trigonometric expression as far as possible into factors. s == TrigFactor (Cos [x] Sin [x]) (4 + Sin [x]) 6

18 340 4 Transformation and simplification of mathematical expressions In contrast to Expand, Factor acts equally in the numerator and denominator in the case of fractions. Factor only forms terms with integer coefficients and not (+ x) (+ x), which would also be correct here. Factor normally only forms terms with rational coefficients; therefore, + x cannot be simplified any further. With the GaussianIntegers! True or Extension! I you can also use complex coefficients (more precisely: numbers of the form a + bimit a; b Z). You can also use the extension option for product breakdowns on top of other basic range extensions. Factor x + x 6 Λ x (+ x) (3 + x) (+ x) (+ x) Factor x 4 (+ x) (+ x) 4 Λ Factor + x Λ + x Factor + x; GaussianIntegers! True Λ or Factor + x; Extension! I Λ (i + x) (i + x) factor x + 3x +; Extension! p 5 Λ 4 p p 3+ 5 x 3+ 5 + x extension! Automatic includes all algebraic numbers in an expression in the basic range. Factor x + p x +; Extension! Automatic Λ p + x FactorTerms is a restricted variant of Factor. The command simply places a common numerical factor in front of the expression. f = Expand (x) (x 3) 7 3 f == FactorTerms Λ; (8 9x 4 x + x 3 + 8x 4 4x 5) Working with Rational Functions This section focuses on functions that can be used to further process rational expressions.

19 Polynomial, rational and pseudorational expressions 34 Bringing fractions to a common denominator Together brings a sum of fractions to a common denominator. Together x + y Λ z y + xz z Together xx + Λ x 5 + 5x + x (+ x) (+ x) (3 + x) Together also works for pseudorational expressions like here when summarizing the result of this Integrate command. » 3 Sin [x] Integrate + Cos [x]; x Λ Cos Λ x Cos [x] + Cos x Cos [x] + Cos [x] (+ Cos [x])% == Together Λ Λ x 4 Cos Cos [x] + Cos x Cos [x] (+ Cos [x]) Simplify can, however, simplify the result even further. % == Simplify (4 Cos [x] + Cos [x]) 4 Determine numerator and denominator The numerator and denominator functions determine the numerator and denominator of a fraction. f = (x +) 3 (x 5); fnumerator [f]; Denominator [f] g f (+ x) 3; (5 + x) g The two commands are pure selectors and do not automatically form a common denominator. For this purpose, Together may have to be used beforehand. ax numerator x + b Λ x b x + ax x ax numerator Together x + b ΛΛ x b ax + bx

20 34 4 Transformation and simplification of mathematical expressions Shorten fractions Cancel shortens fractions, i.e. divides the greatest common factor from the numerator and the denominator of a fraction. The command also works for pseudorational functions, but only with regard to the rational structure found. Fractions are usually also shortened by Simplify, but the advantage of Cancel is that it really shortens and only shortens. Here the difference becomes clear: Since the shortened fraction is (somewhat) more complicated than the unabridged one, Simplify leaves the shortening up.Cancel 8x x + x 3 8 3x + 4x + x 3 Λ + x 3 + x f = x3; f == Simplify 6 (x) x 3 (x) 6 f == Cancel x x (+ x) 5 Partial fraction decomposition Apart calculates the partial fraction decomposition of a rational or pseudorational expression, i.e. splits it into the sum of fractions with the simplest possible denominators. Apart x Λ x 4 4 (+ x) + 4 (+ x) x (+ x) Such decompositions play an important role, for example, when integrating rational functions. Note that calculations are exact and Mathematica therefore only converts the denominator into factors Q decomposed, but not into complex linear factors or real linear and quadratic factors, as is usually taught in mathematics education. So much for the difference between theoretical and algorithmic mathematics! If an expression contains several variables, you can specify the variable according to which the decomposition should be carried out. All other variables are then regarded as (constant) parameters. However, only one variable can be specified, because Apart is Listable and is therefore distributed in this example via the list of parameters in the second argument. Apart xy (x y); xλ y x y + y (x y) xy apart; fx; ygλ (x y) ρ ff y x y + y (x y); x (x + y) + x x + y

21 Trigonometric Expressions 343 Trigonometric Expressions There is a particularly abundance of relationships between the trigonometric functions, so that you can easily lose your bearings if your transformations are not accompanied by clear objectives. A first approach could be to calculate the number of different functions in an expression reduce by tan and cot by sin cos Unfortunately, this is not possible with Mathematica. Although otherwise transformations are rarely carried out without being asked, which is a good thing, Mathematica knows no stopping the trigonometric functions and always immediately reverses the replacements mentioned. or Cos Sin are replaced. ρ ff Sin [x] Cos [x]; Cos [x]; Cos [x] Sin [x]; Sin [x] ftan [x]; Sec [x]; Cot [x]; Csc [x] g We therefore want to first theoretically discuss two objectives for trigonometric transformations that are plausible and simple, even if, for the reasons just mentioned, they cannot be executed in this pure form in any of the Mathematica commands presented below In such a case, however, you can get around by designing a control system that effects precisely these transformations. After an overview of the corresponding Mathematica commands, we will develop such a control system together from page 347 onwards. If you are only interested in the commands, then you can skip these two sections. A third strategy sometimes also helps in tricky situations: The use of the formulas Sin [x] = eix e ix i and Cos [x] = eix + e ix; with which trigonometric formulas can be transformed into the (complex) exponential notation. The success of this approach is due to the fact that Mathematica can calculate much better with such power expressions than with the trigonometric functions themselves. The corresponding transformations can be carried out with the commands TrigToExp (conversion into the exponential notation) and ExpToTrig (reverse conversion). Sin [x] + Cot [x] == TrigToExp i e i x i ei x i e i x i x e i x e i e i x e i x e i x% == Simplify i i i e i x + i e i x + i e i x i e 3 i x + e i x

22 344 4 Transformation and simplification of mathematical expressions% == ExpToTrig (i + i Cos [x] + i Cos [x] i Cos [3 x] 3 Sin [x] Sin [x] + Sin [3 x]) (+ Cos [x] + i Sin [x]) However, it often takes some skill to interpret the resulting results correctly. You can see from this example that Simplify does not always find the simplest representation. % == Simplify (Cos [x] + Cos [x]) Csc [x]% Sin [x] == Simplify Cot [x] How do you simplify trigonometric expressions? This section is optional, but recommended for a better understanding of the problem. To simplify trigonometric expressions, the six well-known trigonometric functions (Sin, Cos, Tan, Cot, Sec, Csc) should first be reduced to two of them (Sin, Cos). Unfortunately, as explained above, this is fundamentally not with Mathematica possible, but we want to assume here that these transformations were carried out and (or were not necessary at all because) there is a pseudorational expression in the kernel of which only sin and cos functions occur. The following two addition theorems are central to the further explanations: (Sin [x + y] = Sin [x] Cos [y] + Cos [x] Sin [y] (SP rules) Cos [x + y] = Cos [x ] Cos [y] Sin [x] Sin [y] They can be changed to the form (PS rules) 8> <>: Sin [x] Sin [y] = (Cos [xy] Cos [x + y]) Sin [x] Cos [y] = (Sin [x + y] + Sin [xy]) Cos [x] Cos [y] = (Cos [xy] + Cos [x + y]) The SP rules (SP stands for sum-product) are suitable for converting trigonometric functions with multiple arguments, i.e. terms like Sin [3 x + y] or Cos [5 x], into polynomial expressions in which only Sin [x]; Sin [y]; Cos [ x]; Cos [y] occur as nuclei. (sin [3 x] for example can be written as Sin [x + x], from which, after applying the SP rules twice, the well-known formula Sin [3 x] = 3Sin [x ] 4 Sin [x] 3 results.) In this way, the number of different kernels in a pseudorational trigonometric expression can be reduced significantly. If only trigonometric functions of the form Sin [nx] and Cos [nx] occur for different n N, see above lets s I rewrite the expression by applying the SP rules several times so that only Sin [x] and Cos [x] appear as kernels. Finally, apply the relationship Sin [x] = Cos [x] (or vice versa),

23 Trigonometric Expressions 345 then you have already brought the expression into a canonical form. If not all terms have been canceled out, then the expression being examined is really different from zero, i.e. the simplification problem for such trigonometric expressions is solved, in short, an efficient one and runtime-friendly simplification process. The PS-rules (PS stands for product-sum) have similarly good, if not quite as obvious properties. They are something like the counterpart to the SP-rules, because they transform products and powers of trigonometric functions into linear combinations with simple arguments of trigonometric functions with multiple angle arguments. In fact: If you replace two factors in a product of trigonometric functions according to the PS rule, then two products are created with one factor less each. Multiple application of the SP rules therefore resolves all products after a finite number of steps. The simplicity of this representation is based on its linearity, which makes it particularly suitable for integration and differentiation. It is also a canonical form (which can be calculated efficiently and run-time-friendly), at least for pseudopolynomial trigonometric expressions. These considerations result in two mutually inverse simplification strategies for trigonometric expressions: Using the SP rules to resolve multiple angle arguments in favor of products, or using the PS rules to resolve products in favor of multiple angle arguments. The first strategy follows roughly TrigExpand, the second TrigReduce. Mathematica commands for transforming trigonometric functions Mathematica has some commands specially designed for transforming expressions with trigonometric functions. Although none of them carries out the transformations described in the last section with pinpoint accuracy, they still allow a much more targeted one, especially compared to Simplify and FullSimplify Method. TrigExpand tries (according to the SP rules) to resolve sums and multiples in the arguments of the trigonometric functions, thereby increasing the polynomial degree of the transformed expressions. TrigExpand [Cos [ff + fi]] Cos [ff] Cos [fi] Sin [ff] Sin [fi] hh ii TrigExpand Cos x + ß 4 Cos [x] pp Cos [x] Sin [x] Sin [x] p For polynomial expressions in Sin and Cos, the result of TrigExpand is usually still predictable. TrigExpand [Cos [4x]] Cos [x] 4 6 Cos [x] Sin [x] + Sin [x] 4

24 346 4 Transformation and simplification of mathematical expressions If other trigonometric functions are involved, then the strategy followed by Mathematica becomes opaque. Here, for example, there is actually nothing to expand. TrigReduce works in exactly the opposite direction: It tries to resolve products and powers of trigonometric functions into linear combinations, and in return accepts more complex function arguments. The previously introduced Factor and Cancel commands take additional transformation rules into account when you use the Trig option; simple additional factorizations or factors are usually discovered in the process. TrigExpand [Sin [x] + Cot [x]] 3 4 Cos [x] + 8 Cos [x] Cot [x] + 7 Csc [x] 8 + Sin [x] 8 TrigReduce [Cos [ff] Cos [fi ]] Cos [ff fi] + Cos [ff + fi] TrigReduce Sin [x] 5Λ (0 Sin [x] 5 Sin [3 x] + Sin [5 x]) 6 Factor [Cos [3x]; Trig! True] Cos [x] (+ Cos [x]) Cancel Cos [ff]; Trig! True Λ Sin [ff] Sin [ff] Some obvious factorizations are overlooked. Factor [Cos [x]; Trig! True] Cos [x] A much more extensive functionality than Factor [:::; Trig! True] offers TrigFactor. The command tries various trigonometric transformations in order to find a representation that can be broken down into factors as far as possible. Cos [x] is now decomposed. However, Cos [3x] can be broken down even further, as the opposite calculation shows. The results of TrigFactor are therefore not as meaningful as those of Factor for polynomials. TrigFactor [Cos [x]] (Cos [x] Sin [x]) (Cos [x] + Sin [x]) TrigFactor [Cos [3x]] Cos [x] (+ Cos [x]) hihi 4Cos x + ß Cos x ß == TrigReduce Cos [x] You can see what the TrigFactor command can do in the result of the following calculation. It's a slightly weird way to use TrigFactor for Cos [nx]; n =; :::; 6 (all three functions Times, Cos and TrigFactor are listable.)

25 Trigonometric Expressions 347 Λ TrigFactor Cos [f; 3; 4; 5; 6gx] Φ (Cos [x] Sin [x]) (Cos [x] + Sin [x]); Cos [x] (+ Cos [x]); (Cos [x] Sin [x]) (Cos [x] + Sin [x]); Cos [x] (Cos [x] + Cos [4 x]); Ψ (Cos [x] Sin [x]) (Cos [x] + Sin [x]) (+ Sin [x]) (+ Sin [x]) A rule system for trigonometric functions If you have not read the penultimate section, you can skip these explanations. Accurate transformations can often only be achieved with such control systems, because Mathematica cannot provide a separate command for every special transformation. Here the two rules are initially written as s and c: s = Sin [x + y]! Sin [x] Cos [y] + Cos [x] Sin [y] c = Cos [x + y]! Cos [x] Cos [y] Sin [x] Sin [y] You are familiar with the syntax used with pattern variables x from the function definitions discussed in Chapter 9, as is the rule operator! We have used it many times. Such rules can be assigned to variables and put together as fs; c g in a list to form a rule system. Rules with the operator =: (ReplaceAll, one-time application of the rules) or ==: (ReplaceRepeated, application as often as possible or until no more changes occur) are used, as already described on page 33. Sin [x + y + z] according to the two given rules. You can see from the form of the result that the argument x + y + z (i.e. plus [x; y; z]) initially as x + (y + z ). The second step (therefore ==: instead of = :) applies the rules again to the new argument y + z. Sin [x + y + z] ==: fs; cg Cos [x] (Cos [z] Sin [y] + Cos [y] Sin [z]) + Sin [x] (Cos [y] Cos [z] Sin [y] Sin [z]) The result with Its many brackets are still quite confusing. We can resolve the brackets at the end with Expand, but this is not good because such a step should start as early as possible in the simplification process and should already be applied to partial results. Otherwise, a subsequent expand could (in more complicated cases ) face thousands of brackets. The additional rule d = a:> Expand [a]

26 348 4 Transformation and simplification of mathematical expressions (:> instead of !, because Expand should not be carried out now, when defining the rule, with a, but later, when applying the rule, with the value of a) does not do what is required: Sin [ xy + z] ==: fs; c; dg Cos [yz] Sin [x] Cos [x] Sin [yz] The reason lies in the way in which Mathematica searches for replacements. First, it examines whether one of the patterns fits the entire expression and, if necessary, applies the appropriate rule . If none of the rules match the entire expression, the partial expressions of the first level are examined and processed recursively, and so on if nothing has changed after a rule has been applied, ReplaceAll (with this partial expression) ends. Unfortunately, rule d always matches the overall expression and ReplaceAll has no reason to try substitutions for subexpressions like Cos [yz] as well. We use a different idea and formulate the distributive laws that Expand applies as rule d: d = (a + b) c! ac + bc It does not have the disadvantage just described, because it can only be used if something can really be multiplied. You will ask what happens to expressions of the form a (b + c): These are also covered by the agreed rule, because Times is orderless, in which case Mathematica tries to test products in all possible constellations of factors for the applicability of the rule. Sin [x y + z] ==: fs; c; dg Cos [y] Cos [z] Sin [x] Cos [x] Cos [z] Sin [y] + Cos [x] Cos [y] Sin [z] + Sin [x] Sin [y] Sin [z ] Unfortunately, multiple angles are not yet broken down, because Mathematica does not come up with the transformation Sin [x] = Sin [x + x] by itself. You can, however, effect this with the following two rules: s = Sin [n integer x] =; n>! Sin [x] Cos [(n) x] + Cos [x] Sin [(n) x] c = Cos [n integer x] =; n>! Cos [x] Cos [(n) x] Sin [x] Sin [(n) x] Of course, a rule like Sin [n Integer x] =; n>! Sin [x + (n) x] are not sufficient, because the decomposed argument would be simplified again immediately before the rule s can apply. s and c are conditional rules, because it is not only checked whether the expressions match certain patterns, but the assignments of the pattern variables must also meet additional properties. The syntax used is the same as for partially defined functions, see page 87. The restriction of the pattern variable n to whole numbers greater than, the termination of the rule applications guarantees a difficult problem in the general case.

27 Trigonometric Expressions 349 Sin [x] ==: fs; c; dg Cos [x] Sin [x] 0 Cos [x] 9 Sin [x] Cos [x] 7 Sin [x] 5 79 Cos [x] 5 Sin [x] Cos [x] 3 Sin [x] 9 Cos [x] Sin [x] Our little rule system already acts like TrigExpand on this expression. Now we want to use the relation Sin [x] + Cos [x] = to solve Sin powers with exponents greater than. The rule Sin [ x]! Cos [x] is not sufficient for this, because it would only replace sin powers, the exponent of which is exactly the same (mathematica, in contrast to CAS Reduce, for example, always regards its rules as exact rules, not as algebraic). The solution offers following rule, which again corresponds to a split n = + (n): sc = Sin [x] n Integer =; n>! Cos [x] Sin [x] n With this you can now e.g. Split Sin [5x] into an expression that only contains Sin [x] in the first power. With Factor, the expression can be broken down further into a product representation of Sin [5x] that trigfactor does not find: Sin [5x] ==: fs; c; d; scg Sin [x] Cos [x] Sin [x] + 6 Cos [x] 4 Sin [x] Collect [%; Sin [x]] Cos [x] + 6 Cos [x] 4 Sin [x]% == Factor Cos [x] + 4 Cos [x] + Cos [x] + 4 Cos [x] Sin [x] Sin [5x] == TrigFactor (+ Cos [x] + Cos [4 x]) Sin [x] Trigonometric transformations in applications The variety of possible mutually equivalent forms in which a trigonometric expression can occur becomes particularly clear when integrating You can just try a more complicated trigonometric expression, calculate the associated antiderivative and derive it again. From a mathematical point of view, the result agrees with the expression you started from. What do you think, how often you succeed in to close the circle syntactically with suitable transformations and to return to the starting point?