# Why do I get a wrong result from SeriesCoefficient?

Bug introduced in 7.0.1 or earlier and persisting through 12.1
Reported to Wolfram, Inc. as CASE:3790521

Consider the following code:

func[x_] = Sin[x^3]/(x - 1/3);
c[n_] = SeriesCoefficient[func[x], {x, 0, n}]


Piecewise[{{((I/2)*3^(1 + n)*(-1 + E^((2*I)/27)))/E^(I/27), n >= 0}}, 0]

That looks at least suspicious. Substituting n = 0 yields a nonzero result:

c[0] // FullSimplify


-3 Sin[1/27]

But the function is zero at x == 0; moreover, Series agrees with me:

Normal @ Series[func[x], {x, 0, 5}]


-3 x^3 - 9 x^4 - 27 x^5

What's happening here?

• If you use SetDelayed (:=) to define your functions so that their value is calculated only after the parameters have been assigned their value, you will get the correct answer, i.e. c[0] == 0. Dec 3 '16 at 19:07
• @MarcoB it's not a solution, merely a workaround. I intentionally define my function to symbolic result of SeriesCoefficient for arbitrary n, not as a delayed call to SeriesCoefficient with particular numeric n. I could just as easily avoid defining c[n_] and instead just copy-paste the output of SeriesCoefficient, with the same results. Dec 3 '16 at 19:13
• @JHM erm... how would I pass parameter to the result if I did as you suggest? It'd be as good as c=... with usage of c/.n->.... Dec 3 '16 at 19:28
• @JHM and MarcoB: There's nothing wrong with Set vs SetDelayed. The OP did not make any mistake. The point is that SeriesCoefficient can compute a general nth term, without putting in a number for n. When it does that, it seems that the result is plainly wrong in this case. If you're bothered by the Set/SetDelayed, just don't define any functions and look at the result of SeriesCoefficient[Sin[x^3]/(x - 1/3), {x, 0, n}]. Dec 3 '16 at 19:50
• @JHM Nope, there's absolutely no problem with the definitions, and I don't think there's any point arguing about this. Let me put the question in a different way to you, without using any definitions: SeriesCoefficient[Sin[x^3]/(x - 1/3), {x, 0, n}] /. n -> 0 and SeriesCoefficient[Sin[x^3]/(x - 1/3), {x, 0, 0}] give different results. Why? The result from SeriesCoefficient[Sin[x^3]/(x - 1/3), {x, 0, n}] looks incorrect. Dec 3 '16 at 19:57

A search for SeriesCoefficient on this website returns numerous instances of incorrect results, the current question being one. An often successful workaround is to factor the function into two or more simpler pieces, each of which SeriesCoefficient can treat correctly.

sc1 = SeriesCoefficient[1/(x - 1/3), {x, 0, n1}];
sc2 = SeriesCoefficient[Sin[x^3], {x, 0, n2}];
c[n_] = Sum[sc1 sc2 /. n1 -> n - n2, {n2, 0, n}]
(* Sum[Piecewise[{{-3^(1 + n - n2), n - n2 >= 0}}, 0]*
Piecewise[{{((I/2)*(-1)^(n2/6)*(-1 + (-1)^((5*n2)/3)))/(n2/3)!,
Mod[n2, 3] == 0 && n2 >= 0}}, 0],
{n2, 0, n}] *)


c[n] is the correct expression, although somewhat ungainly.

Simplify[Sum[c[n] x^n, {n, 0, 12}] == Series[Sin[x^3]/(x - 1/3), {x, 0, 12}]// Normal]
(* True *)


A simpler expression can be obtained from

Simplify[PiecewiseExpand[sc1 sc2 /. n1 -> n - n2 /. n2 -> 3 n2,
Assumptions -> n2 >= 0 && n2 ∈ Integers], n - 3 n2 >= 0]
(* -((I I^n2 3^(1 + n - 3 n2) (-1 + (-1)^n2))/(2 n2!)) *)

cs[n_] = Sum[%, {n2, 0, Floor[n/3]}]
(* (I 3^(1 + n) E^(-(I/27)) (-Gamma[1 + Floor[n/3], -(I/27)] +
E^((2 I)/27) Gamma[1 + Floor[n/3], I/27]))/(2 Gamma[1 + Floor[n/3]]) *)

Simplify[Sum[cs[n] x^n, {n, 0, 12}] == Series[Sin[x^3]/(x - 1/3), {x, 0, 12}]// Normal]
(* True *)


A simple workaround is: Let SeriesCoefficient expand around an arbitrary point a and then set a to zero. (You get DifferenceRoot objects. Working with MMA Version 8.0 ).

func[x_] = Sin[x^3]/(x - 1/3);
c[n_] = SeriesCoefficient[func[x], {x, a, n},
Assumptions -> n >= 0] /. a -> 0 // Simplify

Table[c[n] , {n, 0, 12}]

(*   {0, 0, 0, -3, -9, -27, -81, -243, -729,
-(4373/2), -(13119/2), -(39357/2), -(118071/2)}   *)

Simplify[Sum[c[n] x^n, {n, 0, 12}] ==
Series[Sin[x^3]/(x - 1/3), {x, 0, 12}] // Normal]

(*   True   *)