How to force Mathematica to output an expression such as


as a Dot product like this one:


or A.U

  • $\begingroup$ Didn't you just accept an answer a couple of hours ago ? $\endgroup$ – b.gates.you.know.what Sep 13 '12 at 15:43
  • 1
    $\begingroup$ Tell me, why shouldn't I close this question as a duplicate of this? $\endgroup$ – J. M. will be back soon Sep 13 '12 at 15:46
  • $\begingroup$ @J.M May be he wants to visualize them in traditional form. SergeyFomin are you asking about the display formatting? Then rephrase your question. $\endgroup$ – PlatoManiac Sep 13 '12 at 15:48
  • $\begingroup$ 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. $\endgroup$ – SergeyFomin Sep 13 '12 at 16:00
  • 1
    $\begingroup$ 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. $\endgroup$ – 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} *)
  • $\begingroup$ 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!) $\endgroup$ – rcollyer Sep 13 '12 at 18:23
  • $\begingroup$ @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. $\endgroup$ – rm -rf Sep 13 '12 at 18:24
  • $\begingroup$ 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 ... $\endgroup$ – rcollyer Sep 13 '12 at 18:28
  • $\begingroup$ You took my expression literally,your way doesn`t work for more comlex case, for example expr = (a1 + a2) u1 + a2 u2 + a3 u3; $\endgroup$ – SergeyFomin Sep 13 '12 at 18:53
  • 2
    $\begingroup$ 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 $\endgroup$ – rm -rf Sep 13 '12 at 18:56

How about

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

(* {a1,a2,a3}.{u1,u2,u3} *)
  • 1
    $\begingroup$ easier,no?It`s so complex at first glance. And this way is very artificial,because you know result $\endgroup$ – SergeyFomin Sep 13 '12 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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