# SymPy无法识别相等的值

##### 问题描述投票：0回答：1

SymPy中的简单计算很快就会产生难以理解的结果，如下面的三个`should_be`值。与正确值的比较给出`False`（尽管`math.isclose`得到`True`）。

``````from sympy import sqrt
phi = (1 + sqrt(5)) / 2
should_be_phi = -(1/2 + sqrt(5)/2)**2 + (1/2 + sqrt(5)/2)**3
should_be_half = -sqrt(5)/8 + 1/8 + (1/2 + sqrt(5)/2)**2/4
should_be_one = -sqrt(5)/4 + 1/4 + (1/2 + sqrt(5)/2)**2/2
print(should_be_phi == phi, should_be_half == 1/2, should_be_one == 1)
``````

[目前，我总是将这些怪兽复制到Wolfram Alpha，以获取合适的公式并删除重复项。

python sympy symbolic-math
##### 1个回答
0

``````In [6]: from sympy import *

In [7]: phi = (1 + sqrt(5)) / 2

In [8]: should_be_phi = -(S(1)/2 + sqrt(5)/2)**2 + (S(1)/2 + sqrt(5)/2)**3

In [9]: should_be_phi
Out[9]:
2           3
⎛1   √5⎞    ⎛1   √5⎞
- ⎜─ + ──⎟  + ⎜─ + ──⎟
⎝2   2 ⎠    ⎝2   2 ⎠

In [10]: simplify(should_be_phi)
Out[10]:
1   √5
─ + ──
2   2
``````

``````In [11]: should_be_phi == phi    # Expressions are not in the same form
Out[11]: False

In [12]: Eq(should_be_phi, phi)
Out[12]: True

In [13]: simplify(should_be_phi - phi)
Out[13]: 0
``````