9

I'd like to solve y = (x+1)**3 - 2 for x in sympy to find its inverse function.
I tried using solve, but I didn't get what I expected.

Here's what I wrote in 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 was looking at the last element in the list in Out [4], but it doesn't equal x = (y+2)**(1/3) - 1 (which I was expecting).
Why did sympy output the wrong result, and what can I do to make sympy output the solution I was looking for?

I tried using solveset, but I 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}
Georgy
  • 12,464
  • 7
  • 65
  • 73
DragonautX
  • 860
  • 1
  • 12
  • 22

2 Answers2

5

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

What you're looking for is simplify:

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

edit: Worked with sympy 0.7.6.1, after updating to 1.0 it doesn't work anymore.

L3viathan
  • 26,748
  • 2
  • 58
  • 81
4

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

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

yields

[(y + 2)**(1/3) - 1]
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • This works (sounds odd though, the solution should work for all real numbers), but when I try to extend it to another polynomial equation like `n = Eq(y, (x+1)**5)`, I get an empty set. Do you know what I can do to get the x = y**(1/5) -1 I was expecting? (I can ask this in another question if I'm supposed to.) – DragonautX Sep 29 '16 at 01:22
  • @DragonautX: Sorry, I don't have a good answer to your general question. – unutbu Oct 01 '16 at 17:50
  • That's okay. In the end, I can always do it by hand and work from there, or i can try another tool. – DragonautX Oct 01 '16 at 19:20