# How to convert equation to vector (matrix) form?

How can I convert expression a1*u1+a2*u2+a3*u3 to the vector form of dot product A.U, where A={a1,a2,a3} and U={u1,u2,u3}?

-

If you define :

expr=a1*u1 + a2*u2 + a3*u3;
aVec={a1, a2, a3};


then you can get

uVec=Coefficient[expr, aVec];

Dot[aVec, uVec]==expr

-

I think you may try

vec={u1,u2,u3};
covec=(Normal@CoefficientArrays[a1*u1+a2*u2+a3*u3,vec])//Last


{a1, a2, a3}

where we get

covec.vec


a1 u1 + a2 u2 + a3 u3

This also works for system of equations.

{const, coeff} = Normal@
CoefficientArrays[{
a + x - y - z == 0,
b + x + 2 y + z == 0
}, {x, y, z}];
coeff. {x, y, z} + const


{a + x - y - z, b + x + 2 y + z}

See the important matrix

MatrixForm[coeff]


$$\left( \begin{array}{ccc} 1 & -1 & -1 \\ 1 & 2 & 1 \\ \end{array} \right)$$

-
Thank you all of you very much – SergeyFomin Sep 13 '12 at 9:04

I'm not sure if this is the correct way to interpret your question, but here goes.

You can separate the components of (a1*u1 + a2*u2 + a3*u3) into two vectors by the following:

vecs = Transpose[List @@@ (List @@ (a1*u1 + a2*u2 + a3*u3))]


{{a1, a2, a3}, {u1, u2, u3}}

The dot product can be formed:

Dot@@vecs


a1 u1 + a2 u2 + a3 u3

-

Another approach (similar to image_doctor's) uses replacement rules rather than Apply. Observe that the internal representation is

 FullForm[a1*u1 + a2*u2 + a3*u3]


Plus[Times[a1,u1],Times[a2,u2],Times[a3,u3]]

If we were to change the "Times" to "List" and change "Plus" to "List" then we'd be almost there:

prod=a1*u1 + a2*u2 + a3*u3 //. {Times -> List, Plus -> List}


{{a1,u1},{a2,u2},{a3,u3}}

All we need do is pick out the a's and u's separately. So we could define:

a = prod[[All, 1]];
u = prod[[All, 2]];


Then Dot[a, u] gives the original product.

-