I also tried directly doing

I think the thing here is that we are taking advantage of knowing the exponent is an integer, for which `exp(log(X)*Y)` is slower for small exponents. As pointed by this StackExchange page, there are even faster ways to do integer exponentiation, as exponentiation-by-squaring, but numerical precision issues can arise.

The CSE makes sense for the creation of intermediate variables/symbols representing (sub-)expressions. For now I seen two cases, the Symbol and the MatrixSymbol to represent Exprs and MatrixExprs, respectively.

The inclusion of MatrixSymbols/MatrixExprs in CSE is not so difficult, it just needs to add some more is_XXXX or isinstance conditions. I had already implemented basic support for that in older commits, but I removed it in order to focus the speedup objective.

I hope to have some time in the near future to get back to work on SymPy CSE.

]]>More generally why support just Exprs and MatrixExprs, what stops a solution on Basics. Is it the lack of a generic Basic Symbol?

]]>