Search code examples
pythonmathsympysymbolic-math

Simplify roots in sympy


I have the following code in sympy

from sympy import *
x,y,G=symbols('x y G')
G=x**(3./2.) - y
g_inv=solve(G, x)
if len(g_inv)>1: g_inv=g_inv[-1]
dginvdy=diff(g_inv, y)

The problem is that this gives me

     ____
  3 ╱  2 
2⋅╲╱  y  
─────────
   3⋅y

and not 2*y**(-1./3)/3 as I expected. I have tried simplify() and even cancel() but no luck. Also, if I define the variables with real=True I can't invert it with solvefor some reason. If I define only yas being real I get

2⋅sign(y)
─────────
  3 _____
3⋅╲╱ │y│ 

which is closer (?) but still not what I want. Defining y as positive also didn't do the trick.

This may seem like something silly but it tremendously complicates the calculations I do from then on.

Any ideas?


Solution

  • I think you need to use sympy.factor here rather than simplify:

    In [2]: dginvdy
    Out[2]: 2*(y**2)**(1/3)/(3*y)
    In [3]: factor(dginvdy)
    Out[3]: 2/(3*y**(1/3))
    

    The sympy docs go into some detail about this.