[英]How can I solve y = (x+1)**3 -2 for x in sympy?
I'd like to solve y = (x+1)**3 - 2
for x
in sympy to find its inverse function. 我想解决
y = (x+1)**3 - 2
为x
在sympy找到其反函数。
I tried using solve
, but I didn't get what I expected. 我尝试使用
solve
,但我没有得到我的预期。
Here's what I wrote in IPython console in cmd (sympy 1.0 on Python 3.5.2): 这是我在cmd中的IPython控制台中编写的内容(Python 3.5.2上的sympy 1.0):
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). 我在
Out [4]
查看列表中的最后一个元素,但它不等于x = (y+2)**(1/3) - 1
(我期待的)。
Why did sympy output the wrong result, and what can I do to make sympy output the solution I was looking for? 为什么sympy会输出错误的结果,我该怎样做才能让sympy输出我正在寻找的解决方案?
I tried using solveset
, but I got the same results as using solve
. 我尝试使用
solveset
,但是我得到了与使用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}
Sympy gave you the correct result: your last result is equivalent to (y+2)**(1/3) - 1. Sympy给出了正确的结果:你的最后结果相当于(y + 2)**(1/3) - 1。
What you're looking for is simplify
: 您正在寻找的是
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. 编辑:使用sympy 0.7.6.1,更新到1.0后,它不再起作用了。
If you declare that x
and y
are positive, then there is only one solution: 如果您声明
x
和y
是正数,那么只有一个解决方案:
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.