# Reduce expressions into (simplified) matrix and vector forms

I have some computation that results in a rather complicated looking expression that could be much simplified by converting it into matrix or vector expressions. For example, instead of x0 y0 + x1 y1 + x2 y2 appearing, it would be simpler to have $$x^T y$$.

A simple example, after declaring xi={xi0,xi1,xi2}, etc:

In[43]:= Dot[xi, xj]

Out[43]= xi0 xj0 + xi1 xj1 + xi2 xj2


How do I reduce that Out[43] back into a matrix expression? i.e. In[43] itself is actually simpler than Out[43]. The final expressions I'm working with are generated by taking derivatives of matrix/vector expressions and are multiple lines long and involve both matrix and vector operations.

This is more about computational considerations than pure simplicity, I'm OK with ugly auto-generated code; I want to convert these Mathematica results into C++ code, and the vector expressions can result in a lot more efficient code through SSE instructions, more efficient matrix multiplications, etc.

With

xi={xi0,xi1,xi2}

we do

xj = Last@Normal@CoefficientArrays[xi0 xj0 + xi1 xj1 + xi2 xj2, xi]

such that

Dot @@ {xi, xj}


yields

xi0 xj0 + xi1 xj1 + xi2 xj2

• I'm not sure how this answers the question?
– Sean
Mar 14 at 4:15
• @Sean We have an equation; the one from the OP. Given a vector xi -which is defined from your post- I am obtaining another vector xj such that the Dot product yields the original algebraic equation. You explicitly mentioned that you wanted the Dot product to give you back the equation. From the way the post is written this is what I understood you wanted. It is possible I misunderstood what you wanted to do and/or you mis-wrote what you wanted. Perhaps you could offer some clarifications.
– kcr
Mar 14 at 4:21
• Dot @@ {xi, xj} == Dot[xi, xj] == xi.xj Mar 14 at 4:41
• if you had an issue with the syntax, please have a look at the comment that @AsukaMinato left
– kcr
Mar 14 at 4:43