Showing steps for TrigExpand


If I enter a double equal sign and the following in Mathematica 10:

== integrate 1/(1+x^4)

Then wolfram alpha allows me to select a Step-by-step solution, which gives me ideas and help on how to do the problem.

My question is, how can I get a step-by-step solution for:

== expand cos(4x)


Posted 2015-01-11T19:42:43.193

Reputation: 14 143

3Internally TrigExpand is converting to complex exponentials, expanding, and converting back. So a "show steps" capability would need to do it differently, e.g. via the trig sum/product laws. Which is not a bad thing at all, I just wanted to point out that it would differ from the internal workings of TrigExpand. – Daniel Lichtblau – 2015-01-11T20:18:43.537


See this answer about step by step of integral

– xyz – 2015-01-13T02:59:06.693



At a crude level, the evaluator that does the TrigExpand can be emulated by a FixedPoint or ReplaceRepeated. If we want to see the steps behind this particular kind of evaluation, it seems possible that a simple substitute for the built-in TrigExpand can be implemented based on FixedPointList instead of FixedPoint, to keep track of the steps. Here is a simple attempt to do that:

SetAttributes[expansionStep, HoldAll]
expansionStep[expr_] := 
   Hold[expr] /. {Tan[x_] :> Sin[x]/Cos[x], Cot[x_] :> Cos[x]/Sin[x], 
      Sec[x_] :> 1/Cos[x], 
      Csc[x_] :> 1/Sin[x]} /. {Cos[
       HoldPattern[Times[n_?EvenQ, x__]]] :> (Cos[n/2 x]^2 - 
        Sin[n/2 x]^2), 
     Sin[HoldPattern[Times[n_?EvenQ, x__]]] :> 
      2 Cos[n/2 x] Sin[n/2 x]}]

trigExpandSteps[expr_] := 
 DeleteDuplicates[FixedPointList[expansionStep, expr]]

I really implemented only two trig relations here, based on expanding $\sin(2x)$ and $\cos(2x)$. These are applied repeatedly. But to get away with only these two rules, I have to make sure all trig expressions are first converted to sines and cosines. This requires working with held expressions because Mathematica has the annoying habit of simplifying perfectly good sines and cosines into ugly $\csc$, $\sec$, and also the less ugly but equally unwanted $\tan$ and $\cot$ functions. So at each step, I first "standardize" the current expression into sines and cosines only, within a Hold. Then I apply the trig identities and allow the result to be Expanded. This is done iteratively in FixedPointList until the expression no longer changes.

And here are some tests:

trigExpandSteps[Cos[4 x]]

$\left\{\cos (4 x),\\\cos ^2(2 x)-\sin ^2(2 x),\\\sin ^4(x)+\cos ^4(x)-6 \sin ^2(x) \cos ^2(x)\right\}$

trigExpandSteps[Sin[4 x]]

$\left\{\sin (4 x),\\2 \sin (2 x) \cos (2 x),\\4 \sin (x) \cos ^3(x)-4 \sin ^3(x) \cos (x)\right\}$

trigExpandSteps[Tan[4 x]]

$\left\{\tan (4 x),\\\frac{2 \sin (2 x) \cos (2 x)}{\cos ^2(2 x)-\sin ^2(2 x)},\\\frac{4 \sin (x) \cos ^3(x)}{\left(\cos ^2(x)-\sin ^2(x)\right)^2-4 \sin ^2(x) \cos ^2(x)}-\frac{4 \sin ^3(x) \cos (x)}{\left(\cos ^2(x)-\sin ^2(x)\right)^2-4 \sin ^2(x) \cos ^2(x)}\right\}$


Posted 2015-01-11T19:42:43.193

Reputation: 93 191

If memory serves, the implementation here is similar to what Roman Maeder did in Programming in Mathematica. On the flip side, for Cos[n x], I'd cheat and use the relation with Chebyshev polynomials… – J. M.'s ennui – 2015-06-11T06:10:41.577

@J.M. I never read that book - but I read Robby Villegas' "Working with Unevaluated Expressions" which mentions Maeder. Anyway, if you use Co[n x]` formulas directly, there are of course fewer steps to be seen. That may be good or bad, depending on the audience. – Jens – 2015-06-11T16:26:25.653


{WolframAlpha["Expand Cos[4 x]", {{"Result", 1}, "Output"}], 
 WolframAlpha["Expand Cos[4 x]", {{"Result", 2}, "Output"}]}


{HoldComplete[Cos[x]^4 - 6 Cos[x]^2 Sin[x]^2 + Sin[x]^4],
HoldComplete[2 Sin[[Pi]/4 - 2 x] Sin[[Pi]/4 + 2 x]]}

Is this what you're seeking?

David G. Stork

Posted 2015-01-11T19:42:43.193

Reputation: 31 784

Unfortunately, it is two possible answers, not step-by-step solution – ybeltukov – 2015-01-11T20:13:05.647