# How to output an expression as a Dot[] product

How to force Mathematica to output an expression such as

$a1u1+a2u2+a3u3$

as a Dot product like this one:

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


or A.U

-
Didn't you just accept an answer a couple of hours ago ? –  b.gatessucks Sep 13 '12 at 15:43
Tell me, why shouldn't I close this question as a duplicate of this? –  Ｊ. Ｍ. Sep 13 '12 at 15:46
@J.M May be he wants to visualize them in traditional form. SergeyFomin are you asking about the display formatting? Then rephrase your question. –  PlatoManiac Sep 13 '12 at 15:48
Many thanks for coefficient extraction you have provided in my first question, but now I ask you how to force mma to present my expression in vector form. –  SergeyFomin Sep 13 '12 at 16:00
This question seems to need additional assumptions and constraints, because it has no unique answer: $a_1u_1 + a_2u_2 + a_3u_3$ = $(a_1,a_2,a_3)\cdot(u_1,u_2,u_3)$ = $(u_1,a_2,a_3)\cdot(a_1,u_2,u_3)$ etc. = $(a_2,a_3,a_1)\cdot(u_2,u_3,u_1)$ etc. = $(1,1,1)\cdot(a_1u_1 a_2u_2,a_3u_3)$ etc. –  whuber Sep 13 '12 at 17:01

I'm going to answer this in the spirit of the question, making a few reasonable assumptions:

• that the two underlying vectors in $x = \sum_i a_i u_i$ are $\mathbf{a}=\{a_1,...,a_n\}$ and $\mathbf{u}=\{u_1,...,u_n\}$, thus giving $x = \mathbf{a}\cdot\mathbf{u}$. If not, there are several possibilities as in whuber's comment.
• The corresponding elements of the vectors $\mathbf{a}$ and $\mathbf{u}$ are ordered identically for all elements. In other words, for some ordering function $f$, $f(a_i,u_i)$ is the same for all $a_i$ and $u_i$. This is so that we aren't affected by the Orderless attribute of Times (in other words, don't try this for something like $\mathbf{a}=\{b, p, z\}$ and $\mathbf{u}=\{e,g,l\}$).
• There are no numerals involved (i.e. this is purely symbolic) and the primary intent is to be able to display the vectors in the desired form.

With the above, the following is a very simple way to achieve the output with a few replacements:

expr = a1 u1 + a2 u2 + a3 u3;
expr /. Times -> List /. List -> CenterDot /. Plus -> List
(* {a1, a2, a3}·{u1, u2, u3} *)

-
As an added note, it is very instructive to break the replacements into a sequence of replacements via % /. ... to see what it is they're doing. (+1, btw ... and enough with the edits!) –  rcollyer Sep 13 '12 at 18:23
@rcollyer I agree... I'll leave that to the reader — it's informative to do the replacements one at a time to see how it works, and it must be done in that order. –  rm -rf Sep 13 '12 at 18:24
I forgot to add "left as an exercise for the reader," as that's why I left the note. Of course, the first one is the tricky one ... –  rcollyer Sep 13 '12 at 18:28
You took my expression literally,your way doesnt work for more comlex case, for example expr = (a1 + a2) u1 + a2 u2 + a3 u3; –  SergeyFomin Sep 13 '12 at 18:53
Well, then you, as the question asker, should think of all complicated cases and present them in the question instead of making us guess what you might or might not have in mind... For this example, you can easily change a1 + a2 to, say, a4 and then use the above. In the end, change a4 back to a1 + a2 –  rm -rf Sep 13 '12 at 18:56

expr = a1 u1 + a2 u2 + a3 u3;