Reputation: 3004
Say I have a vector that looks like this:
1/2 a + 1/3 b
b + c
2a + c
1/3c + 4d
Mathematically this can be factorised into matrix and a vector:
Matrix:
1/2 1/3 0 0
0 1 1 0
2 0 1 0
0 0 1/3 4
Vector:
a
b
c
d
(My apologies for the formatting, perhaps someone could suggest how better to do it?)
Is there any way to get mathematica to do this matrix factorisation? In my specific case the terms are not simple expressions such as "a", "b", "c", "d". But are things indexed by a list, e.g.
W[{3,0,0,0}]
W[{1,1,0,0}]
W[{0,0,1,0}]
Thanks!
Upvotes: 6
Views: 323
Reputation: 24336
Possibly:
x = {1/2 a + 1/3 b,
b + c,
2 a + c,
1/3 c + 4 d};
CoefficientArrays[x, {a, b, c, d}][[2]] // MatrixForm
In the case that you want coefficients for all variables, use the compact form:
CoefficientArrays[x][[2]] // MatrixForm
In the case that you do not want coefficients of all variables, part [[1]]
comes into play:
x2 = {1/2 a + 1/3 b + q - y,
b + c + 1/2 r,
2 a + c + 2 y,
1/3 c + 4 d};
CoefficientArrays[x2, {a, b, c, d}][[1]] // Normal
{q - y, r/2, 2 y, 0}
Such that you can reconstruct your expression.
Upvotes: 9