# Convert coefficients of polynomials into a matrix

I have several sets of 5 polynomials of the form:

a0 = 0.00301472 - 0.0961879 beta - 0.860642 beta^2 - 4.73786 beta^3 -
0.00897697 z + 0.0389941 beta*z + 0.0131329 z^2 - 0.00552858 z^3


for a0,a1,...,a5, they form a vector A. My independent variables are beta and z.

I want a Matrix Q of the coefficients, such that when I multiply it by a vector of the form:

B={1, beta, z, beta z, beta^2, z^2, beta^3, z^3}


like A=Q.B i recover my vector of polynomials.

How do I get the matrix Q in the easiest way? I have many sets, so it would take me hours. I have been trying some solutions with Coefficient[], but I am not fully satisfied.

Thanks!

• CoefficientArrays may be useful
– acl
Mar 23, 2013 at 16:56

r := RandomReal[{1, -1}, 8];
b = {be, z, be z, be^2, z^2, be^3, z^3, 1};
as = {a1, a2, a3, a4, a5} = Table[b.r, {5}];

u = Unique[ConstantArray[x, Length@b - 1]];

k = (MonomialList[as /. Thread[Most@b -> u], u] /. Thread[u -> 1]);

(*Testing*)
k.b == as
(* True *)


This seems to do the trick:

qmatrix = # /. {Plus -> List, Times[const_Real, x_] -> const,
n_Real -> n} & /@ vectorA


I've assumed that your coefficients are Real numbers, and it appears to pass the test:

vectorA == qmatrix.bvector


where you defined the b vector as:

B = {1, beta, z, beta z, beta^2, z^2, beta^3, z^3}


Well I did it like this:

a1 = -3.99345 be^3 - 1.40171 be^2 + 0.157222 z be - 0.200001 be -
0.00629434 z^3 + 0.0129681 z^2 - 0.0112943 z + 0.00689754

a2 = 9.81754 be^3 + 9.13266 be^2 - 1.26467 z be + 1.45187 be +
0.120649 z^3 - 0.300832 z^2 + 0.236402 z - 0.0706221

a3 = -12.1568 be^3 - 9.23052 be^2 + 1.26135 z be - 1.44396 be -
0.12561 z^3 + 0.311995 z^2 - 0.24268 z + 0.069102

a4 = 42.7015 be^3 - 8.47056 be^2 + 0.920634 z be + 0.632936 be -
0.471948 z^3 + 1.46434 z^2 - 1.74767 z + 1.19797

a5 = -43.9295 be^3 - 5.29011 be^2 + 3.93688 z be + 1.15722 be -
1.18691 z^3 + 3.42683 z^2 - 2.15405 z + 0.684838

As = {a1, a2, a3, a4, a5};
B = {1, be, z, be z, be^2, z^2, be^3, z^3};

inda = {1, 2, 1, 2, 3, 1, 4, 1};
indb = {1, 1, 2, 2, 1, 3, 1, 4};

CoefficientTableList = Table[CoefficientList[As[[i]], {be, z}], {i, 5}];

QM = Table[
CoefficientTableList[[i]][[inda[[j]], indb[[j]]]], {i, 5}, {j,
8}];


As an example that it worked indeed:

(QM.B)[[1]]

0.00689754 - 0.0112943 z + 0.0129681 z^2 - 0.00629434 z^3 -
0.200001 be + 0.157222 z be - 1.40171 be^2 -  3.99345 be^3


The part of defining these indices inda and indb by hand and using them in Table, seems a bit inelegant to me. Maybe someone can help there. Another option would be using CoefficientArrays that gives the coefficients according to their order, but I couldn't figure an easy way to handle with that.