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]
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?

  • 2
    $\begingroup$ clearly this is a bug. You can submit a bug report to [email protected] $\endgroup$
    – Nasser
    Mar 9, 2019 at 12:26
  • 1
    $\begingroup$ Can someone try for other powers of cosine? How about FourierSinCoefficient[] and powers of sine? $\endgroup$ Mar 9, 2019 at 12:30
  • $\begingroup$ 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). $\endgroup$ Mar 9, 2019 at 12:32
  • 1
    $\begingroup$ 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. $\endgroup$ Mar 9, 2019 at 14:48
  • 1
    $\begingroup$ This gets the right answer: mathematica.stackexchange.com/a/149469/4999 $\endgroup$
    – Michael E2
    Mar 9, 2019 at 14:58

2 Answers 2


@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:

 Limit[f_, args__] /; ! TrueQ[$in] := Block[{$in = True},
   Limit[f /. Sin -> (# Sinc[#] &), args]];
 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.

  • 2
    $\begingroup$ This is neat. How did you figure out that Limit[] was the culprit? $\endgroup$ Mar 10, 2019 at 16:36
  • 2
    $\begingroup$ @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. $\endgroup$
    – Michael E2
    Mar 10, 2019 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} $

  • $\begingroup$ 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. $\endgroup$ Mar 9, 2019 at 13:33
  • $\begingroup$ @Jyrki Lahtonen: The command FourierCosCoefficient[1/2 + Cos[2 t]/2, t, n] // Simplify also produces 0. $\endgroup$
    – user64494
    Mar 9, 2019 at 14:55
  • $\begingroup$ @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. $\endgroup$
    – user64494
    Mar 9, 2019 at 15:42
  • $\begingroup$ @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 $\endgroup$
    – user64494
    Mar 9, 2019 at 16:37
  • 4
    $\begingroup$ 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. $\endgroup$ Mar 10, 2019 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.