# 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

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.

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 '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. Sep 18 '13 at 22:56

## 2 Answers

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[1] b[1] + a[2] b[2]) /. {a[1] b[1] ->
Dot[a, b] - a[2] b[2]}]


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[
expr /. MapThread[Rule, {v, u^Range[Length[v]]}],
u,
Length[v] + 1];
res[[1]] == 0
)
]


This can handle simple cases like:

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

Out[191]= a x + b y + c z

Out[192]= {a, b, c}


Or harder cases like:

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

Out[298]= 3 x + y

Out[299]= {3, 1, 0}


Or, to use your example

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

Out[217]= {a[1], a[2]}


It goes without saying that v should not contain anything that does not explicitly appear in the expression, e.g. {1,x,x^2} won't work as v unless "1" appears in the expression.