Hello,
In the following test code I am attempting to simplify the resulting expression
by replacing a subexpression with a function
import symengine as se
import sympy as sp
# Potential V
def V(x, y):
v = (x*x + y*y)/2 + y*(x*x - (y*y)/3)
return v
def main():
# Symbols
x, y, Ho = sp.symbols('x, y, Ho')
expr = sp.Pow(Ho - V(x, y), -1)
dexpr_dx = se.Derivative(expr, x, 1).xreplace({y*(x**2 + (-1/3)*y**2) + (-1/2)*x**2 + (-1/2)*y**2: V(x, y)})
print("dexpr_dx =", dexpr_dx)
if __name__ == "__main__":
main()
Taking in derivative w.r.t. x resulting in V(x, y) being replace the expression
dexpr_dx = -(-x - 2*x*y)/(Ho - y*(x**2 + (-1/3)*y**2) + (-1/2)*x**2 + (-1/2)*y**2)**2
which I would like to rewrite as -(-x - 2*x*y)/(Ho - V(x, y))**2
as I will be repeating the differentiation in the actual code.
In other words, I would like to reduce the output code bloat.
I've tried subs(), replace(), and xreplace() with no success.
Any insight in how to do this would be appreciated.