# Write list of polynomials as a matrix of coefficients times a list of monomials

I have the following problem: given a list of polynomials P = {p1, ..., pn} on the variables x, ..., x[n], find a list of monic monomials M = q1, ..., qm on x, ..., x[n] and a matrix A that does not depend on x, ..., x[n] such that A.M = P.

For example, taking P = {x x + x^2 x, a x^2 + b x, c x^2 x + d x^2} a possible solution is:

M = {x x, x^2, x, x^2, x^2 x},

A = {{1, 0, 0, 0, 1}, {0, a, b, 0, 0}, {0, 0, 0, c, d}}

I managed to find a long and clumsy solution by using a lot of replacement rules, but I want to know if there is an easy way of doing something like this.

Here is some code that might get you started. Will require some adjustment to get it exactly to what you want.

In:= pP = {x x + x^2 x, a x^2 + b x,
c x^2 x + d x^2}

Out= {x x + x^2 x, a x^2 + b x,
c x^2 x + d x^2}

In:= vars = {x, x};

In:= mlist = GroebnerBasisDistributedTermsList[pP, vars][]

Out= {{{{2, 1}, 1}, {{1, 1}, 1}}, {{{2, 0}, a}, {{0, 1},
b}}, {{{2, 1}, c}, {{0, 2}, d}}}

In:= ml2 =
mlist /. {{e1_Integer, e2_Integer}, c_} :> {c,
Times @@ (vars^{e1, e2})}

Out= {{{1, x^2 x}, {1, x x}}, {{a, x^2}, {b,
x}}, {{c, x^2 x}, {d, x^2}}}

In:= {aa, mm} = {ml2[[All, All, 1]], ml2[[All, All, 2]]}

Out= {{{1, 1}, {a, b}, {c, d}}, {{x^2 x,
x x}, {x^2, x}, {x^2 x, x^2}}}

Out= {x x + x^2 x, a x^2 + b x,
c x^2 x + d x^2}


Approach based on CoefficientRules

P = {x x + x^2 x, a x^2 + b x, c x^2 x + d x^2};
n = 2;
X = x /@ Range[n]

{x, x}

coeff = Thread@(# -> Range@Length[#]) &@
Sort@DeleteDuplicates[Join @@ CoefficientRules[P, X][[All, All, 1]]]

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

M = Inner[Power, X, Transpose@coeff[[All, 1]], Times]

{x, x^2, x x, x^2, x^2 x}

A = SparseArray[Join @@ MapIndexed[{#2[], #[]} -> #[] &,
CoefficientRules[P, X] /. coeff, {2}]];

A.M == P
`

True