There is no direct support for this. SymPy automatically combines common terms with exponentiation. The only way to do this is not to use the evaluate=False mechanism. for instance
>>> Mul(x, x, evaluate=False) x*x
This exact issue has recently been discussed on the SymPy mailing list (https://groups.google.com/d/topic/sympy/qaJGesRbX_0/discussion). I posted some code there that will do this. I will repeat it here:
def pow_to_mul(expr): """ Convert integer powers in an expression to Muls, like a**2 => a*a. """ pows = list(expr.atoms(Pow)) if any(not e.is_Integer for b, e in (i.as_base_exp() for i in pows)): raise ValueError("A power contains a non-integer exponent") repl = zip(pows, (Mul(*[b]*e,evaluate=False) for b,e in (i.as_base_exp() for i in pows))) return expr.subs(repl)
Here how it works
>>> a = Symbol('a') >>> exp = a**2 >>> print(exp) a**2 >>> print(pow_to_mul(exp)) a*a
I’ll put the same disclaimer here as on the mailing list: “Evaluate = False is a bit of a hack, so keep in mind that it is fragile. Some functions will overestimate the expression, converting it back to Pow. Will break, because the expected the invariant will be broken down into expression = False (for example, I doubt that factor () will work correctly).
source share