# Strange result of FourierCosCoefficient on $\cos^2 t$

The following strange answers were just reported in Mathematics.SE. The OP was asking about the Fourier cosine series of the function $$\cos^2t=\frac12(1+\cos 2t).$$

That question is about WA, but Mathematica (mine is version 11) gives the outputs:

In[1]:=FourierCosCoefficient[Cos[t]^2, t, n]
Out[1]:=0
In[2]:=FourierCosCoefficient[(1+Cos[2t])/2, t, n]
Out[2]:=1/2 (DiscreteDelta[-2 + n] + 2 DiscreteDelta[n])


I assume we all know that the second output is correct, and the first is wrong.

Can somebody shed more light to this?

• clearly this is a bug. You can submit a bug report to wolfram@support.com – Nasser Mar 9 at 12:26
• Can someone try for other powers of cosine? How about FourierSinCoefficient[] and powers of sine? – J. M. will be back soon Mar 9 at 12:30
• A great idea, @J.M.iscomputer-less! FourierSinCoefficient[Sin[t]^3, t, n] also gives output $0$. The same with the fifth power of sine and the fourth power of cosine (for the respective sine/cosine series). – Jyrki Lahtonen Mar 9 at 12:32
• Interestingly, FourierCosCoefficient[1/2 + 1/2 Cos[2 t], t, n] (so, with (1+Cos[2t])/2 expanded) also gives 0. Looks like a bug to me. – Sjoerd C. de Vries Mar 9 at 14:48
• This gets the right answer: mathematica.stackexchange.com/a/149469/4999 – Michael E2 Mar 9 at 14:58

@user64494 is correct that the result arises because some of Mathematica's solvers return results that are only generically correct (that is, incorrect only for finitely many exceptions or on a manifold of dimension lower than the dimension of the domain of the problem). Clearly (I think it's clear), WRI does not consider all such behavior a bug. However, users may well feel that such behavior is undesirable and may insist on calling it a bug. I can explain where the "genericity problem" arises in this example.

FourierCosCoefficient works by first trying table lookup, which succeeds when the expression is in Fourier-series form (e.g. the OP's second example). When table lookup fails, it tries Integrate. Here the problem comes from integrals like this one, which is only generically true (for n != 0):

Integrate[Cos[n t], t]
(*  Sin[n t]/n  *)


This leads to limits of such functions at t -> Pi, which gives the result 0, which again is incorrect for n == 0. One can get around this specific problem by using the identity $$\sin x = x \mathop{\text{sinc}} x$$. For example:

Limit[Sin[n t]/n, t -> Pi, Direction -> "FromBelow", Assumptions -> n ∈ Integers]
(*  0  *)

Limit[Sin[n t]/n /. Sin -> (# Sinc[#] &), t -> Pi, Direction -> "FromBelow",
Assumptions -> n ∈ Integers]
(*  π Sinc[n π]  *)


We can use the Villegas-Gayley trick to catch Limit and apply the identity:

InternalInheritedBlock[{Limit},
Unprotect[Limit];
Limit[f_, args__] /; ! TrueQ[$$in] := Block[{$$in = True},
Limit[f /. Sin -> (# Sinc[#] &), args]];
Protect[Limit];
FourierCosCoefficient[Cos[t]^2, t, n]
]
(*  1/2 (Sinc[(-2 + n) π] + 2 Sinc[n π] + Sinc[(2 + n) π])  *)

FunctionExpand[%, Assumptions -> n ∈ Integers] // InputForm
(*  (KroneckerDelta[2 - n] + 2*KroneckerDelta[n] + KroneckerDelta[2 + n])/2  *)

Table[%, {n, 0, 5}]
(*  {1, 0, 1/2, 0, 0, 0}  *)


I'm pretty sure you don't always want to replace all the sines by sincs, but I think it shows what is going on in the OP's examples.

• This is neat. How did you figure out that Limit[] was the culprit? – J. M. will be back soon Mar 10 at 16:36
• @J.M.iscomputer-less Trace[code, _f, TraceInternal -> True] with f equal to Integrate and Limit got me started. I just thought I'd try to see if M did what I would do by hand. – Michael E2 Mar 10 at 16:44

This is not a bug: Mathematica produces a generic answer by default and most of the coefficients equal zero. The Fourier expansion of Cos[2t] is produced by

FourierCosSeries[Cos[t]^2, t, 5]


$$\frac{1}{2} \cos (2 t)+\frac{1}{2}$$

• Thanks for testing this. Yet, I think the real question is why Cos[t]^2 and (1+Cos[2t])/2 give different answers. I would think that the generic answers for both would be the same. – Jyrki Lahtonen Mar 9 at 13:33
• @Jyrki Lahtonen: The command FourierCosCoefficient[1/2 + Cos[2 t]/2, t, n] // Simplify also produces 0. – user64494 Mar 9 at 14:55
• @Jyrki Lahtonen: I'd like to add that the results of FourierCoefficient[(1 + Cos[2 t])/2, t, n] and FourierCoefficient[(Cos[ t]^2, t, n] coincide. – user64494 Mar 9 at 15:42
• @Jyrki Lahtonen: Up to the help, the FourierCosCoefficient is aimed at basic functions Cos[n*t]. The FourierCosCoefficient command does not do any trig transforms in most of cases.. I think this makes the difference asked in your question – user64494 Mar 9 at 16:37
• Why would trig transforms (assuming you mean applications of trig identities) make any difference whatsoever? According to Mathematica help FourierCosCoefficient[f,t,n]` calculates the integral $$\frac2{\pi}\int_{t=0}^\pi f(t)\cos nt\,dt.$$ It should not matter whether trig identities are applied or not. The integral is still the same. – Jyrki Lahtonen Mar 10 at 11:12