3
$\begingroup$

Consider:

t = (1 + x)^3 (1 - y - x)^2
Expand[t]

Now:

CoefficientList[t, {x, y}]

The output is:

{{1, -2, 1}, {1, -4, 3}, {-2, 0, 3}, {-2, 4, 1}, {1, 2, 0}, {1, 0, 0}}

Now I don't understand this output. Can someone please explain how it relates to the expanded value of t above? Also, as a second equation, can someone show how to use this matrix to reproduce t?

$\endgroup$
2
  • 1
    $\begingroup$ Hint: each row corresponds to 1, x, x^2,..., x^5 and each column corresponds to 1, y, y^2; and the entries corresponds to their products. $\endgroup$
    – Michael E2
    Aug 25, 2015 at 21:54
  • 2
    $\begingroup$ What you ask in the second question is shown in the documentation for CoefficientList under "Properties & Relations." $\endgroup$
    – Michael E2
    Aug 25, 2015 at 21:56

4 Answers 4

6
$\begingroup$

This is in the docs, of course.

{1, x, x^2, x^3, x^4, x^5}.{{1, -2, 1}, {1, -4, 3}, {-2, 0, 3}, {-2, 4, 1}, {1, 2, 0}, {1, 0, 0}}.{1, y, y^2} is your polynomial.

More generally, the matrix output of CoefficientList[poly, {var1, var2}] is such that

{1, var1, …, var1^n}.CoefficientList[poly, {var1, var2}].{1, var2, …, var2^m} == poly

where the powers n, m respectively are the highest powers to which var1, var2 appear in poly.

$\endgroup$
3
  • 1
    $\begingroup$ It is in the docs, but I've always found the documentation for this particular function somewhat opaque. Your reconstruction of the polynomial makes it totally clear what's going on (+1), to the point where it'd be nice if that was somewhere in the documentation (maybe it is and I missed it?). $\endgroup$
    – march
    Aug 25, 2015 at 21:57
  • $\begingroup$ An example appears under "Basic Examples" of "Matrix of coefficients for a quadratic form: CoefficientList[1 + a x^2 + b x y + c y^2, {x, y}]" - I didn't know for certain what CoefficientList did, but that summary line alone made me about 90% sure that my guess would be correct. I didn't even need to look at the output of that line in the docs to formulate my model. I presumed other people would do the same :P $\endgroup$ Aug 25, 2015 at 22:00
  • $\begingroup$ This is one of the worst documented functions in mathematica $\endgroup$ Dec 16, 2022 at 20:59
4
$\begingroup$
t = (1 + x)^3 (1 - y - x)^2;

cl = CoefficientList[t, {x, y}];

(pwrs = Array[x^(#1 - 1) y^(#2 - 1) &, Dimensions[cl]])//MatrixForm

enter image description here

t == Total[cl*pwrs, 2] // Simplify

True

$\endgroup$
3
$\begingroup$

This won't really fit in a comment, but I wanted to briefly expand on Patrick Stevens' and Bob Hanlon's answers. If they would like to incorporate this answer into theirs, I will delete this answer.

In order to generalize the clever observation that

{1, var1, …, var1^n}.CoefficientList[poly, {var1, var2}].{1, var2, …, var2^m}

is the polynomial in question when there are two variables, there is a variant in which Dot works to reconstruct the polynomial in an arbitrary number of variables.

By way of example, take

poly = x + y^2 + z^3;
vars = {x, y, z};
cl = CoefficientList[poly, vars];
degrees = Exponent[poly, vars];

We can reconstruct the polynomial from the CoefficientList cl and the list of polynomial degrees degrees with

Dot[cl, Sequence @@ Reverse @ MapThread[PowerRange[1, #1^#2, #1] &, {vars, powers}]]

To pick this apart, note that

Reverse @ MapThread[PowerRange[1, #1^#2, #1] &, {vars, powers}]

merely automates creating the lists of monomials:

(* {{1, z, z^2, z^3}, {1, y, y^2}, {1, x}} *)

We have to reverse the list because the ordering of the tensors in Dot matters. Then, Dot does the rest.

$\endgroup$
1
$\begingroup$

From the documentation for CoefficientList:

Fold[
 FromDigits[Reverse[#1], #2] &,
 CoefficientList[t, {x, y}],
 {x, y}]
% - t // Simplify
(*
  1 + x + (-2 - 2 x) x^2 + 
   x^4 (1 + x) + (-2 - 4 x + 4 x^3 + 2 x^4) y + (1 + 3 x + x^2 (3 + x)) y^2

  0
*)

Another alternative:

CoefficientList[t, {x, y}].y^Range[0, Exponent[t, y]].x^Range[0, Exponent[t, x]]
$\endgroup$

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