Say I have an expression that is a polynomial in a variable, for example in the variable $x$, in which terms appear for which the exponent of $x$ also includes variables. For example:

pol = a + b x^n + c x^(n + 1)

Is there a clean way to find the coefficient in front of any given term?

I could not get the built-in function Coefficient to work directly, namely

{Coefficient[pol, x, 0], Coefficient[pol, x, n], Coefficient[pol, x, n + 1]}

yields the output

{a, b + c x, c}

while I want it to yield

{a, b, c}

To fix this I used

Coefficient2[pol_, var_, exp_] := Coefficient[Coefficient[pol, var, exp], var, 0]

This was good enough to yield the correct results in my case, but it feels a bit nasty. Is there a better way?

  • $\begingroup$ If you want to treat n and n+1 differently, why not redefine them like n0=n,n1=n+1,.. Or put them in an array? $\endgroup$
    – Sumit
    Commented Oct 28, 2020 at 13:48
  • $\begingroup$ The problem is that Mathematica does not know that n is an integer and therefore the Coefficient functions fail. I actually think that your "nasty" hack is pretty slick! $\endgroup$
    – Natas
    Commented Oct 28, 2020 at 14:22
  • $\begingroup$ @Sumit Arrays do not seem to be the right data type for what I am after. Say I have pol1 = a + b x^n + c x^(2n) and pol2 = d + e x^n + f x^(2n) and I want to calculate the coefficient of $x^{2n}$ in the product pol1 * pol2. If pol1 and pol2 were stored as arrays I would have to write something that multiplies arrays like they are polynomials right? But I do not want to do that since Mathematica already knows how to multiply polynomials. Natas, thanks. Maybe it is not too bad. I just thought that there might be a way to do this that feels more mathematically sound. $\endgroup$
    – Pjotr5
    Commented Oct 28, 2020 at 14:32
  • $\begingroup$ Just differentiate with respect to x n times and evaluate at x = 0! :P (Just kidding, but I guess it is how you'd do it "mathematically".) $\endgroup$
    – thorimur
    Commented Oct 28, 2020 at 15:09
  • 1
    $\begingroup$ Another—though, honestly, probably slower—way to do it, which I've checked works, would be to ask for Coefficient[pol * x^(-n),x,0], taking advantage of this weirdly asymmetrical behavior and the fact that Mathematica doesn't care whether the variable exponents are positive integers or not. Really, though, what mathematica should do is provide a choice function that depends on n. After all, if n is 0, the constant term is a+b, not a! $\endgroup$
    – thorimur
    Commented Oct 29, 2020 at 13:16

1 Answer 1


Here is a possibility for any polynomial pol with variable x for power m

powcoeff[pol_, x_, m_] := Simplify[Total[
        Select[Level[Expand[pol], 1], Exponent[#, x] == m &]]/x^m]

pol1 = a1 + b1 x^n + c1 x^(n + 1);
pol2 = a2 + b2 x^n (f x + 1) + c2 x^(n + 1);
pol3 = pol1 pol2;

powcoeff[pol1, x, n]
powcoeff[pol2, x, n + 1]
powcoeff[pol3, x, n]
powcoeff[pol3, x, n + 1]
powcoeff[pol3, x, 6 n + 1]


c2 + b2 f

a2 b1 + a1 b2

a2 c1 + a1 (c2 + b2 f)



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.