2
$\begingroup$

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$

$\endgroup$
6
  • 1
    $\begingroup$ 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] $\endgroup$
    – Michael E2
    Commented Jan 13, 2019 at 0:41
  • $\begingroup$ 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}]. $\endgroup$
    – Michael E2
    Commented Jan 13, 2019 at 0:42
  • $\begingroup$ @MichaelE2 I am ignoring only those entries for which the constant is zero as in coefficientlist matrix. $\endgroup$ Commented Jan 13, 2019 at 0:48
  • 1
    $\begingroup$ Do you mean Fold[FromDigits[Reverse[#1], #2] &, Unitize@mat * list, {x, y}]? $\endgroup$
    – Michael E2
    Commented Jan 13, 2019 at 1:43
  • $\begingroup$ @MichaelE2 Exactly. $\endgroup$ Commented Jan 13, 2019 at 1:53

5 Answers 5

2
$\begingroup$

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 *)
$\endgroup$
9
$\begingroup$
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

$\endgroup$
3
$\begingroup$

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  *)
$\endgroup$
3
$\begingroup$

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
$\endgroup$
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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