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.


1 Answer 1




we do

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

such that

Dot @@ {xi, xj}


xi0 xj0 + xi1 xj1 + xi2 xj2

  • $\begingroup$ I'm not sure how this answers the question? $\endgroup$
    – Sean
    Commented Mar 14, 2022 at 4:15
  • $\begingroup$ @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. $\endgroup$
    – user49048
    Commented Mar 14, 2022 at 4:21
  • 1
    $\begingroup$ Dot @@ {xi, xj} == Dot[xi, xj] == xi.xj $\endgroup$ Commented Mar 14, 2022 at 4:41
  • $\begingroup$ if you had an issue with the syntax, please have a look at the comment that @AsukaMinato left $\endgroup$
    – user49048
    Commented Mar 14, 2022 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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