2

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}]; 

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

Another matrix:

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

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

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$

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Mark Robinson
  • 615
  • 3
  • 9

5 Answers5

9
Internal`FromCoefficientList[mat, {x, y}]

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

Internal`FromCoefficientList[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

kglr
  • 394,356
  • 18
  • 477
  • 896
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  *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
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
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
2

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.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
2

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 *)
John Doty
  • 13,712
  • 1
  • 22
  • 42