I don't think there's a built-in function to do this, but it shouldn't be too hard to write your own, using .atoms to find the Pow's and .xreplace (or .subs if you are not using the development version) to replace them. Note that SymPy automatically converts a*a to a**2, so to keep it as a*a, you have to use Mul(a, a, evaluate=False).
evaluate=False is somewhat of a hack, so be aware that it is fragile. Some functions will reevaluate the expression, converting it back to Pow. Other functions will break because some expected invariant will be broken by the evaluate=False expression (e.g., I doubt factor() would work correctly). So I would not do this until the very end, before you send it to SQL.
Something like this should work:
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 for b, e in i.as_base_exp()), evaluate=False) for i in pows)
return expr.subs(repl)
# Or, in the development version, a better way is
# return expr.xreplace(dict(repl))
Disclaimer: I typed the above function on my iPad without even checking if the syntax was correct. It should work, though, assuming I remembered all my interfaces correctly and didn't forget something.
Aaron Meurer
I'm trying to use sympy to expand matrix math into valid SQL code. SQL doesn't support ** of course, so I would like to expand terms like a**2 to a*a. In the general context of a CAS, this is a silly thing to do, so I haven't been able to locate a function that would do this (expand, replace, rewrite).
Is this possible?
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sympy/-/mTnzsoyEj1UJ.
To post to this group, send email to
sy...@googlegroups.com.
To unsubscribe from this group, send email to
sympy+un...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.