Write a function that returns the coefficient of x^n

Just do what SeriesCoefficient does yourself

coeff[polynomial_, variable_, order_] := 
 D[polynomial, {variable, order}]/(order!) /. variable -> 0

coeff[7 x^2 - 3 x^3, x, 2]
(* 7 *)

coeff[347 x^19 - 7 x^2 - 3 x^3, x, 19]
(* 347 *)

Here's a pattern-matching version (not tested carefully since the question is already answered):

coeff[poly_, var_, orders_List] := Cases[Expand@poly, a_. var^# :> a] & /@ orders

Usage: orders is a list of the wanted orders, so

poly = (x + 5) (x - 1) (x^2 + y); Expand@poly
coeff[poly, x, {3, 1, 8, 4}]
(* -5 x^2 + 4 x^3 + x^4 - 5 y + 4 x y + x^2 y *)
(* {{4}, {4 y}, {}, {1}} *)

Tags:

Polynomials