Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a way to simplify dot products etc.

Here is a minimal example: Lets say I have two vectors a and b:


Now I evaluate their dot product:

c = a.b

which should give me something like this

a[1]b[1] + a[2]b[2]

What I need is a way to transform this expression back into a dot product. So I am looking for a function that realises that I can write this as a.b (similiar to "Factor" for polynomial expressions), e.g.


should give


In a more general context, I would like to display scalars as vector/matrix/tensor expressions, meaning mathematica should also realize that

m[1,1] + m[2,2]

is simply


and display it as such.

Is that possible?

Thank you!

share|improve this question
Do you realize that there is no unique inverse for a dot product? There are infinitely many dot products that give the same result. See, for instance, this one: {a1, a2}.{b1 + c/a1, b2 - c/a2} // Simplify. The second vector is a different one for every value of c. Yet the dot product is the same every time. – Sjoerd C. de Vries Sep 18 '13 at 21:58
I realise that. But isn't it possible to define a function that would output a[1]b[1] + a[2]b[2] as a.b if a and b were already defined to be a={a[1],a[2]}, b={b[1],b[2]} because in most cases, that is exactly the "inverse" I am looking for. It would remove a lot of clutter in my equations, since I have vectors with >100 entries... – user9599 Sep 18 '13 at 22:25
Dear physicslol, welcome to Mathematica.SE! Please don't post comments as answers, as it works against the Q&A and model of the site and can be unhelpful for future visitors. I've turned your answer into a proper comment. – Verbeia Sep 18 '13 at 22:56

It's very simple in principle, provided that you're sure you have an expression that is in fact going to simplify into a dot product:

   (a[1] b[1] + a[2] b[2]) /. {a[1] b[1] -> 
    Dot[a, b] - a[2] b[2]}]


Likewise for the trace:

Simplify[(a[1, 1] + a[2, 2]) /. {a[1, 1] -> Tr[a] - a[2, 2]}]


So you just pick one of the terms in the sum defining your compact expression, and replace that term with the compact expression minus the missing terms. Then Simplify will afterwards take care of the rest by canceling the individual terms. It' is however important that the compact expression doesn't contain symbols that expand to something longer again under Simplify. So here a and b themselves shouldn't have values assigned to them.

share|improve this answer

Your Answer


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.