# Checking if two trigonometric expressions are equal

Say I have two trigonometric expressions which are a bit complicated. Is there a quick way to check if they reduce to the same thing (that they are equal) using Mathematica?

I was solving this: $y'' + y = \cos 4x,$ $y(0) = 6, y'(0) = 11$ using Laplace transformation technique, and I get this: $y = \frac{-1}{15}\cos(4x) + \left(\frac{1}{15} + 6\right) \cos(x) + 11 \sin(x)$.
I wanted to check if my answer is correct and Mathematica gave me this:

1/30 (182 Cos[x] - 5 Cos[x] Cos[3 x] + 3 Cos[x] Cos[5 x]
+ 330 Sin[x] + 5 Sin[x] Sin[3 x] + 3 Sin[x] Sin[5 x])


Try using FullSimplify:

FullSimplify[Sin[x] == Tan[x] Cos[x]]


This returns True if Sin[x] == Tan[x] Cos[x] (which it does).

Please note that == (Equal) should be used instead of a single equal sign (Set). More complicated trig identities can be difficult to reason about. Mathematica may not be able to properly determine whether they are true or not. You can still ask for Mathematica's opinion in these cases however.

If the identity is very difficult, you may consider using PossibleZeroQ :

PossibleZeroQ[Sin[x] - Tan[x] Cos[x]]


Instead of equating the left hand side with the right hand side, subtract them. Then give these expression to PossibleZeroQ. PossibleZeroQ tests whether it is likely that a given expression is equal to 0.

If you believe the identity is false but would like an example, you can get this using FindInstance. Here, we ask Mathematica to find an instance where Sin[x] does not equal (!=) Tan[x] Cos[x]:

FindInstance[Sin[x] != Tan[x] Cos[x], x]


Mathematica doesn't return any answer. This makes sense because both sides are equal.

• Great!! FullySimplify worked just well. And my answer is correct :D Thank you a lot! Jun 26 '12 at 13:31
• If FindInstance[expr1[x] != expr2[x], x] doesn't yields any answer it doesn't mean that expr1[x] == expr2[x] !!!!! Jun 26 '12 at 13:53
• Oh I most certainly didn't mean to imply that. It's simply just useful in these kinds of cases. Like PossibleZeroQ, its results don't really guarantee anything. Jun 26 '12 at 14:53
• Try Sin[x] == Tan[x] Cos[x] without FullSimplify, it returns True because of built-in rewrite rules, so your example is completely inadequate. Jun 26 '12 at 15:32
• Yes. I get it. The purpose of explaining it this way was to present a general method instead of presenting 6 different cases. Thanks. Jun 29 '12 at 17:58

There is no best general way to check if any two trigonometric expressions are equal. One can use TrigReduce, TrigExpand, TrigFactor, TrigToExp, Together and Apart (especially with the Trig->True option), Simplify, FullSimplify, etc. All these functions have their advantages and we discuss some of them. For more complicated examples when using Simplify and FullSimplify we can encounter problems with timings and/or memory allocation, we give an appropriate example later.

In case of your example I recommend to evaluate TrigReduce on the difference of the both expressions.

Sometimes you needn't use any simplifications, and quite inevident expressions are simplified automatically by built-in rewrite rules.

First we consider a few examples where no Mathematica functions are needed.

ex 1.

$$\sum_{\small{k = 1}}^{\small{k = m-1}} \;\frac{1}{\sin^{2}(\frac{k\; \pi}{m})} = \frac{m^{2} -1}{3}$$

Sum[ 1/Sin[ k Pi/m]^2, {k, 1, m - 1}]

1/3 (-1 + m^2)


if we set e.g. m = 21, the above doesn't simplify and we need e.g. FullSimplify

m = 21;
FullSimplify @ Sum[ 1/Sin[ k Pi/m]^2, {k, 1, m - 1}]

440/3


The larger one sets m, the more time it takes.

Here is another example where we need no simplifications and the result is obtained with help of built-in rewrite rules :

ex 2.

$$\sum_{\small{k = 0}}^{\small{k = m-1}} \; \cos(a + k b) = \frac{\sin(\frac{n b}{2})}{\sin(\frac{b}{2})} \cos(\frac{2 a+(n-1) b}{2})$$

(Sum[ Cos[ a + k b], {k, 0, m - 1}] -
Sin[ (m b)/2]/Sin[ b/2]  Cos[ (2 a + (m - 1) b)/2]) /. m -> 137

0


but if we make no substitution we'll need e.g. Simplify

Simplify[ (Sum[ Cos[ a + k b], {k, 0, m - 1}] -
Sin[ (m b)/2]/Sin[ b/2]  Cos[ (2 a + (m - 1) b)/2]) ]

0


It works even if we add the option Simplify[ expr, Trig -> False].

ex 3.

$$\sum_{\small{k = 0}}^{\small{k = n-2}} \; \tan(\frac{\pi}{2^{n - k}}) = \cot(\frac{\pi}{2^{n}})$$

Here we define e.g.

f[n_] := Sum[2^k Tan[Pi/2^(n - k)], {k, 0, n - 2}] - Cot[Pi/2^n]


In this case the following functions work equally well if we set e.g. n == 15 :

Together[ f[n], Trig -> True] == Simplify[ f[n]] == TrigReduce[ f[n]] == 0

True


however for larger n we can easily see advantages of various approaches, e.g. for n == 21 we observe that Together[ expr, Trig->True] is the best, while Simplify cannot tackle such an expression :

TrigReduce[ f] // AbsoluteTiming
Together[f, Trig -> True] // AbsoluteTiming

{0.1404000, 0}
{0.0312000, 0}


while

Simplify[f] // AbsoluteTiming


returns If we evaluate e.g. f[n], where we haven't assigned to n any value, we get ComplexInfinity.

At last we consider two simple examples :

ex 4.

expr1 = -1 + 50 Cos[x]^2 - 400 Cos[x]^4 + 1120 Cos[x]^6 - 1280 Cos[x]^8;
expr2 = -512 Cos[x]^10 + Cos[10 x];


In this case one can use

TrigReduce[ expr1 - expr2]

0


as well as one of these

Together[ expr1 - expr2, Trig -> True]


or

Apart[ expr1 - expr2, Trig -> True]


ex 5.

exprA =        - 1/15 Cos[4 x] + (1/15 + 6) Cos[x] + 11 Sin[x];
exprB =   1/30 (  182 Cos[x] - 5 Cos[x] Cos[3 x] + 3 Cos[x] Cos[5 x] + 330 Sin[x]
+ 5 Sin[x] Sin[3 x] + 3 Sin[x] Sin[5 x] );


compare various ways (TrigReduce and TrigExpand are optimal here) :

TrigReduce[exprA - exprB] // AbsoluteTiming
TrigExpand[exprA - exprB] // AbsoluteTiming

{0., 0}
{0., 0}


but FullSimplify and TrigFactor also work, though are a bit slower :

TrigFactor[exprA - exprB] // AbsoluteTiming
FullSimplify[exprA - exprB] // AbsoluteTiming

{0.0156000, 0}
{0.0312000, 0}


Thus using TrigReduce seems to be more appropriate for this type of trigonometric expressions. We can observe that timing for is a multiple of 0.0156000 for TrigFactor and FullSimplify.

• Fantastic! This also worked. Thank you! Jun 26 '12 at 13:33
• @stupidity Note that is an appropriate approach to simplify purely trigonemetric expressions. Jun 26 '12 at 13:42
1. Use PossibleZeroQ

expr1 = -1/15 Cos[4 x] + (1/15 + 6) Cos[x] + 11 Sin[x];
expr2 = 1/30 (182 Cos[x] - 5 Cos[x] Cos[3 x] + 3 Cos[x] Cos[5 x] +
330 Sin[x] + 5 Sin[x] Sin[3 x] + 3 Sin[x] Sin[5 x]);
PossibleZeroQ[expr1 - expr2]

(* Out= True *)

2. Use Mathematica's InputForm for showing examples. Wastes much less time for others who later work with them. Took me three tries to get all the typos out of my transcription of expr1.

Just for fun, here is another approach (that I would use intuitively, if I didn't know the other functions existed):

expr1 = -1 + 50 Cos[x]^2 - 400 Cos[x]^4 + 1120 Cos[x]^6 - 1280 Cos[x]^8;
expr2 = -512 Cos[x]^10 + Cos[10 x];

Integrate[(expr1 - expr2)^2, {x, -∞, ∞}]

 0

• Sin[x]*Exp[-x^2] - x*Cos[x]*Exp[-x^2]
– acl
Jun 26 '12 at 13:55
• @acl point noted - see edit. Jun 26 '12 at 14:17
• I'd use Abs but +1 this is how I think too (not sure that's good, but...)
– acl
Jun 26 '12 at 14:34
• @acl Abs does not return an analytical solution (in the 10 min I left it to run) for your example. This is why I used ^2 Jun 27 '12 at 7:29
• I'd also do that numerically with NIntegrate to start with (and Quiet it to avoid complaints if it is zero). I don't trust mathematica to work reliably with analytical expressions (as in, I don't trust it to finish or choose the branches I want etc), so I almost never use it for that.
– acl
Jun 27 '12 at 9:56