Coefficient, CoefficientList

In the next cell Coefficient returns the coefficient of a particular term of a polynomial.  In this case (-210c^2x^2y z^2) is a term of (c x - 2y + z)^7after it's expanded.

Clear[a, b, c, d, x, y, z] ; poly = (c x - 2 y + z)^7 ; Coefficient[poly, x^2 y z^4]

-210 c^2

We can get the same information from CoefficientList which returns a list of coefficients.  In the cell below Part[coeff, 3,2,5] returns the coefficient of x^(3-1)y^(2-1)z^(5-1).  In general if we say   lst=CoefficientList[poly,{x1,x2,x3,...}]  then  Part[lst,  n1, n2, ,n3, ...] will be the coefficient of x1^(n1-1) x2^(n2-1) x3^(n3-1).

coeff = CoefficientList[poly, {x, y, z}] ; Part[coeff, 3, 2, 5]

-210 c^2

The next line gives the coefficient of x^5.  As expected there is more than one term of poly with x^5 as a factor.

Coefficient[poly, x^5]

84 c^5 y^2 - 84 c^5 y z + 21 c^5 z^2

We can get the same result as the previous example if we use the next line.

Coefficient[poly, x, 5]

84 c^5 y^2 - 84 c^5 y z + 21 c^5 z^2

One can't get the result above directly from CoefficientList.  Instead pieces of the above result are included in the result of CoefficientList[poly,{x,y,z}].  The line below can be used to get pieces of the result above.  Specifically  coeff[[6]] contains all coefficients of x^5 (including those that are zero).

coeff[[6]]

{{0, 0, 21 c^5, 0, 0, 0, 0, 0}, {0, -84 c^5, 0, 0, 0, 0, 0, 0}, {84 c^5, 0, 0, 0, 0, 0, 0, 0}, ... , 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}}

Part[coeff, 6, 1, 3] is (-84 c^5) the coefficient of (x^(6 - 1) * y^(2 - 1) * z^(2 - 1)) = (x^6 * y * z) in poly. Part[coeff,6,3,1] is (84c^5) the coeficient of (x^(6 - 1) * y^(3 - 1) * z^(1 - 1)) = (x^5 * y^2) in poly.  All other coefficients under Coeff[[6]] are zero which agrees with the result of Coefficient[poly, x^5].

The function defined in the next cell is a slight modification of a solution Roberto Colistete sent to the MathGroup. This function takes the coefficients from CoefficientList and returns the polynomial with those coefficients.  See the discussions of MapIndexed, and Function for further explanation of some features used here.

CoefficientsToPolynomial[coeffs_, vars_List] /;(Length[Dimensions[coeffs]] === Length[vars]) := Plus @@ Flatten[ MapIndexed[#1 Times @@ (vars^(#2 - 1)) &, coeffs, {Length[vars]}] ]

The next cell shows another implementation of the same function by Michael Trott of Wolfram Research.

CoefficientsToPolynomial[coeffs_List, vars_List]/;(Length[Dimensions[coeffs]] === Length[vars] ...  Plus @@ Flatten[MapIndexed[(#1 Inner[Power, vars, #2 - 1, Times]) &, coeffs, {Length[vars]}]]

A most elegant implementation of the same function is given in the next cell. This is a slight modification of a solution Alan Hayes sent to the MathGroup.

CoefficientsToPolynomial[coeffs_, vars_List]/;(Length[Dimensions[coeffs]] === Length[vars]) := Dot @@ Reverse[Append[vars^(Range[Dimensions[coeffs]] - 1), coeffs]]

Either of the above definitions can be used on the examples below.

lst = CoefficientList[(3x - 2y + z + Exp[a])^3, {x, y, z}] ; poly = Factor @ CoefficientsToPolynomial[lst, {a, b, c}]

(3 a - 2 b + c + ^a)^3

lst2 = CoefficientList[a x^3 + c x y + b x^4 y + d y^2 + Exp[t], {x, y}] ; CoefficientsToPolynomial[lst2, {a, b}]

a^4 + a^4 b^2 + a b c + b^2 d + ^t

Also this hyperlink will take you to a Wolfram Research web site with more information.


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page