How can I solve y = (x + 1) ** 3 -2 for x in sympy?

I would like to solve y = (x + 1) ** 3 -2 for x in sympy to find my inverse function.
I tried to use solve , but I did not understand what I expected

Here is what I wrote in the IPython console in cmd (sympy 1.0 on Python 3.5.2):

 In [1]: from sympy import * In [2]: x, y = symbols('x y') In [3]: n = Eq(y,(x+1)**3 - 2) In [4]: solve(n,x) Out [4]: [-(-1/2 - sqrt(3)*I/2)*(-27*y/2 + sqrt((-27*y - 54)**2)/2 - 27)**(1/3)/3 - 1, -(-1/2 + sqrt(3)*I/2)*(-27*y/2 + sqrt((-27*y - 54)**2)/2 - 27)**(1/3)/3 - 1, -(-27*y/2 + sqrt((-27*y - 54)**2)/2 - 27)**(1/3)/3 - 1] 

I looked at the last item in the list in Out [4] , but it is not equal to x = (y + 2) ** (1/3) - 1 (which I expected).
Why is sympy displaying the wrong result, and what can I do to make sympy output the solution I was looking for?

I tried using solveset but got the same results as using solve .

 In [13]: solveset(n,x) Out[13]: {-(-1/2 - sqrt(3)*I/2)*(-27*y/2 + sqrt((-27*y - 54)**2)/2 - 27)**(1/3)/ 3 - 1, -(-1/2 + sqrt(3)*I/2)*(-27*y/2 + sqrt((-27*y - 54)**2)/2 - 27)**(1/3)/3 - 1, -(-27*y/2 + sqrt((-27*y - 54)**2)/2 - 27)**(1/3)/3 - 1} 
+7
sympy
source share
2 answers

If you state that x and y positive, then there is only one solution:

 import sympy as sy x, y = sy.symbols("xy", positive=True) n = sy.Eq(y, (x+1)**3 - 2) s = sy.solve(n, x) print(s) 

gives

 [(y + 2)**(1/3) - 1] 
+3
source share

Sympy gave you the correct result: your last result is equivalent to (y + 2) ** (1/3) - 1.

What you are looking for is simplify :

 >>> from sympy import symbols, Eq, solve, simplify >>> x, y = symbols("xy") >>> n = Eq(y, (x+1)**3 - 2) >>> s = solve(n, x) >>> simplify(s[2]) (y + 2)**(1/3) - 1 

edit: Working with sympy 0.7.6.1, after upgrading to 1.0 it no longer works.

+4
source share

All Articles