# Simplify vector expression - is there something like "Factor" for vectors, dot products etc.?

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:

Array[a,2]
Array[b,2]


Now I evaluate their dot product:

c = a.b


which should give me something like this

ab + ab


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.

Function[c]


should give

a.b


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

Tr[m]


and display it as such.

Is that possible?

Thank you!

• 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. Sep 18, 2013 at 21:58
• I realise that. But isn't it possible to define a function that would output ab + ab as a.b if a and b were already defined to be a={a,a}, b={b,b} 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, 2013 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. Sep 18, 2013 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:

Clear[a,b];
Simplify[
(a b + a b) /. {a b ->
Dot[a, b] - a b}]


a.b

Likewise for the trace:

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


Tr[a]

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.

If you are trying to factor the product of a simple symbolic vector and some other vector, you can try this:

 DotFactor[expr_, v_] :=  Module[{u, res}, Drop[res, 1] /;
(
res = CoefficientList[
u,
Length[v] + 1];
res[] == 0
)
]


This can handle simple cases like:

 {a, b, c}.{x, y, z}
DotFactor[%, {x, y, z}]

Out= a x + b y + c z

Out= {a, b, c}


Or harder cases like:

In:= 3 x + y
DotFactor[%, {x, y, z}]

Out= 3 x + y

Out= {3, 1, 0}


In:= DotFactor[a b + a b, {b, b}]