# How to force correct answers for Integrals of Cos[mx]*Cos[nx]? [duplicate]

This is a big problem if you do anything with a Fourier Series. This statement:

Assuming[Element[{m, n}, Integers], Integrate[Cos[m*x]*Cos[n*x], {x, 0, 2 Pi}]]


returns 0. But the right answer is non-zero whenever $|m|=|n|$.

This other arrangement:

Integrate[Cos[m*x]*Cos[n*x], {x, 0, 2 Pi},  Assumptions -> Element[{m, n}, Integers]]


gives an answer that goes $0/0$ when $|m|=|n|$.

I know there are generic and non-generic answers, but this is pretty dang simple. And Fourier series work is pretty common!

What I need is a solution that will work when I didn't know there was a Cos[n x] in it:

Integrate[Integrate[Cos[m*x]*f[x], {x, 0, 2 Pi},  Assumptions -> Element[{m}, Integers]]


If my f[x] happens to have a Cos[] in it, Mathematica will give me wrong answers, unless I anticipate the situation and code around it. How could I do that?

• Mathematica returns generic results. You are trying to get a non-generic result. You will likely need to change your assumptions to force Mathematica to consider the non-generic case.
– KAI
Commented Nov 17, 2014 at 22:24
• Commented Nov 17, 2014 at 22:47
• No, it's not simple. Not at all. I suspect it would require a considerable amount of special case pattern matching to implement any subset of integrals of this type. Seen next comment for why current method cannot do these. Commented Nov 17, 2014 at 23:45
• Currently much assumption handling is accomplished under the hood with Refine and Simplify. In a case like this, assumptions of integrality will have the predictable result. Here is a smaller variant: Refine[Sin[m*Pi]/m, Assumptions->Element[m,Integers]] Out[5]= 0. If this does not return 0, then Refine is effectively useless. And if it does, then Refine cannot do better than support a generic result, when used in Integrate. Commented Nov 17, 2014 at 23:46
• @DanielLichtblau Okay, not that I understand why the bug exists, how do I work around it? How do I make my integrals of products of sums of trigonometric functions not come out wrong? Commented Nov 30, 2014 at 20:38

At least with Mathematica version 10.0.1, using option Assumptions does provide a correct answer:

Integrate[Cos[m*x]*Cos[n*x], {x, 0, 2 Pi},
Assumptions -> Element[{m, n}, Integers]]

(* (Sin[2*(m - n)*Pi]/(m - n) + Sin[2*(m + n)*Pi]/(m + n))/2


I don't know why this form of the input leads to a different result than the form using Assuming.

• that form also goes 0/0 when |m|==|n|. Commented Nov 17, 2014 at 22:48
• Yes, it's an indeterminate form. Limit is a good function for handling those. Commented Nov 17, 2014 at 22:51
• Sure it 'goes 0/0', but so what? It is still correct. Sin[x]/x =1 at x=0. Commented Nov 18, 2014 at 7:36
• @murray, this is why Assuming gives something different: mathematica.stackexchange.com/questions/42114/… Commented Nov 19, 2014 at 0:05