2

V 12.1 on windows.

I remember asking something similar many years ago. I was hoping Mathematica now could have done this automatically:

$$ \int_{-\pi}^{\pi} \cos (n x) \cos (m x) \, dx $$

For integers $n,m$ is zero for $n \ne m$ and $\pi$ for $n=m$. This can be verified as follows

Clear["Global`*"];
res = Flatten[
   Table[{n, m, Integrate[Cos[n   x] Cos[m  x], {x, -Pi, Pi}]}, 
          {n, 1,5}, {m, 1, 5}], 1];
Grid[PrependTo[res, {"n", "m", "result"}], Frame -> All]

Mathematica graphics

I was looking at this at Maple forum question, and saw that Maple 2020 can now give this result automatically as shown there, which is by using

restart;
int(cos(n*x)*cos(m*x), x = -Pi..Pi, allsolutions) assuming n::posint,m::posint

enter image description here

convert(%, piecewise, n);

enter image description here

Mathematica does not do the above directly

Assuming[Element[{n, m}, Integers], 
       FullSimplify[Integrate[Cos[n  x] Cos[m  x], {x, -Pi, Pi}]]]
(*0*)

Assuming[Element[{n, m}, Integers] && n > 0 && m > 0, 
       FullSimplify[Integrate[Cos[n  x] Cos[m  x], {x, -Pi, Pi}]]]
(*0*)

I had to do the following to get same result. Which is more work compared to Maple (one has to know to take the limit).

res = Integrate[Cos[n  x] Cos[m  x], {x, -Pi, Pi}]

enter image description here

Assuming[Element[m, Integers], Limit[res, n -> m]]
(* Pi *)

Assuming[Element[{n, m}, Integers] && n != m, Simplify@res]
(* 0 *)

Any one knows or a trick to make Integrate do the following automatically without having to do post-processing each time? Assumptions are needed ofcourse.

Is the reason Integrate does not do it automatically because needing to use Limit to get the right result?

Nasser
  • 143,286
  • 11
  • 154
  • 359
  • 1
    (1) There are prior threads on this, for example there is this MSE thread, Also on Wolfram Community there is this. – Daniel Lichtblau May 20 '20 at 14:37
  • 1
    (2) To respond to the unasked "But Maple handles it..." I will offer an opinion. I think it would be a foolish waste of resources for the respective companies to to engage in an arm's race over such integrals. It might make sense for Mathematica to handle these as part of a larger effort to accomodate families of orthogonal basis functions. I do not know if that is under consideration though, as there are many other worthwhile undertakings it would compete with. – Daniel Lichtblau May 20 '20 at 14:41
  • 1
    sol[m_, n_] = Integrate[ Piecewise[{{Cos[n x] Cos[m x], m != n}, {1, m == n == 0}}, Cos[n x]^2], {x, -Pi, Pi}] and for the integer case Assuming[Element[{m, n}, Integers], sol[m, n] // Simplify] – Bob Hanlon May 20 '20 at 17:39

0 Answers0