# 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

1
– J. M.♦ 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

1
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
show 2 more comments

## 2 Answers

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 doesn`t work for more comlex case, for example expr = (a1 + a2) u1 + a2 u2 + a3 u3; – SergeyFomin Sep 13 '12 at 18:53

2
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
show 1 more comment

````expr = a1 u1 + a2 u2 + a3 u3;
HoldForm[#1.#2] & @@ Transpose[Apply[List, expr, {0, 1}]]

(* {a1,a2,a3}.{u1,u2,u3} *)
````
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 18, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9383078813552856, "perplexity_flag": "middle"}