Vector matrix factor

Let's 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 decomposed into a 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 formatting, maybe someone could suggest how best to do this?)

Is there a way to get the math to do this matrix factorization? In my particular case, the terms are not simple expressions such as "a", "b", "c", "d". But things are indexed by list, for example.

 W[{3,0,0,0}] W[{1,1,0,0}] W[{0,0,1,0}] 

Thanks!

+6
source share
1 answer

Maybe:

 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 case you want coefficients for all variables, use the compact form:

 CoefficientArrays[x][[2]] // MatrixForm 

In the event that you do not want the 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} 

So you can restore your expression.

+9
source

Source: https://habr.com/ru/post/902667/


All Articles