sympy
¶pymbolic
can help take care of many structural transformations on your expression trees with great ease. Its main purpose is to help with program transformation after all, not to be a full computer algebra system. That said, if you need a full computer algebra system for things like calculus and simplification, it's easy to get your expressions converted between pymbolic
and sympy
:
import sympy as sp
from pymbolic import var
x = var("x")
y = var("y")
expr = (x**2 + 2*x + 1)/(x**2 + x)
print(expr)
Let's import pymbolic's sympy interoperability code.
# pymbolic.interop.sympy in newer versions of pymbolic
from pymbolic.sympy_interface import (
PymbolicToSympyMapper, SympyToPymbolicMapper)
p2s = PymbolicToSympyMapper()
s2p = SympyToPymbolicMapper()
sympy_expr = p2s(expr)
print(sympy_expr)
sympy_result = sp.cancel(sympy_expr)
print(sympy_result)
result = s2p(sympy_result)
print(result)
One thing to note is that PymbolicToSympyMapper
is a regular pymbolic
mapper, and its behavior can be overridden in case something about the translation to sympy is not quite what you want.
SympyToPymbolicMapper
also behaves very similarly (and can be overridden similarly) although it is not entirely the same kind of mapper.