Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}?

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

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
share|improve this answer
add comment

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)$$

share|improve this answer
    
Thank you all of you very much –  SergeyFomin Sep 13 '12 at 9:04
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.