# Reconstructing a polynomial from its coefficient array

A polynomial coefficient matrix:

mat =
CoefficientList[3 + 5 x^3 + 4 y^3 + 2 x + 6 x^2 y + 7 x y^2 + 8 x y, {x, y}];


$$$$\left( \begin{array}{cccc} 3 & 0 & 0 & 4 \\ 2 & 8 & 7 & 0 \\ 0 & 6 & 0 & 0 \\ 5 & 0 & 0 & 0 \\ \end{array} \right)$$$$

Another matrix:

list =
{{a1, b1, c1, d1}, {e1, f1, g1, h1}, {i1, j1, k1, l1}, {m1, n1,o1, p1}};


whose matrix form is: $$$$\left( \begin{array}{cccc} a1 & b1 & c1 & d1 \\ e1 & f1 & g1 & h1 \\ i1 & j1 & k1 & l1 \\ m1 & n1 & o1 & p1 \\ \end{array} \right)$$$$

How can I generate the following polynomial automatically?

$$\text{a1}+\text{d1} y^3+\text{e1} x+\text{f1} x y+\text{g1} x y^2+\text{j1} x^2 y+\text{m1} x^3$$

• Why are some entries of the matrix ignored? Maybe this, if that is a mistake: {{a1, b1, c1, d1}, {e1, f1, g1, h1}, {i1, j1, k1, l1}, {m1, n1, o1, p1}}.y^Range[0, 3].x^Range[0, 3] – Michael E2 Jan 13 '19 at 0:41
• There's an example in the docs for CoefficientList for how to recover the polynomial from the matrix: Look for Fold[FromDigits[Reverse[#1], #2] &, %, {x, y}]. – Michael E2 Jan 13 '19 at 0:42
• @MichaelE2 I am ignoring only those entries for which the constant is zero as in coefficientlist matrix. – Mark Robinson Jan 13 '19 at 0:48
• Do you mean Fold[FromDigits[Reverse[#1], #2] &, Unitize@mat * list, {x, y}]? – Michael E2 Jan 13 '19 at 1:43
• @MichaelE2 Exactly. – Mark Robinson Jan 13 '19 at 1:53

Using mat as the template:

Plus @@ Flatten[Table[If[mat[[i, j]] == 0, 0, list[[i, j]] x^(i - 1) y^(j - 1)],
{i, 1, 4}, {j, 1, 4}]]
(* a1 + e1 x + m1 x^3 + f1 x y + j1 x^2 y + g1 x y^2 + d1 y^3 *)

InternalFromCoefficientList[mat, {x, y}]


3 + 2 x + 5 x^3 + 8 x y + 6 x^2 y + 7 x y^2 + 4 y^3

InternalFromCoefficientList[list Unitize[mat], {x, y}]


a1 + e1 x + m1 x^3 + f1 x y + j1 x^2 y + g1 x y^2 + d1 y^3

Adapting an example from the documentation for CoefficientList:

Fold[FromDigits[Reverse[#1], #2] &, Unitize@mat*list, {x, y}]
(*  a1 + e1 x + m1 x^3 + f1 x y + j1 x^2 y + g1 x y^2 + d1 y^3  *)


Terse:

Total[Array[x^# y^#2 &, {4, 4}, 0] list Unitize@mat, 2]

a1 + e1 x + m1 x^3 + f1 x y + j1 x^2 y + g1 x y^2 + d1 y^3


You will have to tell Mathematica where the zero coefficients are, but if you do that it can be done like so:

list = {{a1, 0, 0, d1}, {e1, f1, g1, 0}, {0, 0, 0, l1}, {m1, 0, 0, 0}};
Fold[FromDigits[Reverse[#1], #2] &, list, {x, y}] // Expand


a1 + e1 x + m1 x^3 + f1 x y + g1 x y^2 + d1 y^3 + l1 x^2 y^3

This is discussed in the documentation of CoefficientList in the section Properties & Relations.