即使正确判定双方不等式,也无法判定身份。
from sympy import *
assert not 2 * sqrt(2 - sqrt(3)) == -sqrt(2) + sqrt(6)
assert 2 * sqrt(2 - sqrt(3)) <= -sqrt(2) + sqrt(6)
assert 2 * sqrt(2 - sqrt(3)) >= -sqrt(2) + sqrt(6)
assert not 2 * sqrt(2 - sqrt(3)) == sqrt(8 - 4 * sqrt(3))
assert 2 * sqrt(2 - sqrt(3)) <= sqrt(8 - 4 * sqrt(3))
assert 2 * sqrt(2 - sqrt(3)) >= sqrt(8 - 4 * sqrt(3))
原因是,正如 @OscarBenjamin 所评论的,“a==b 测试的是结构平等而不是数学平等。”
相等运算符 (
) 测试表达式是否具有相同的形式,而不是它们在数学上是否等价。==
https://github.com/sympy/sympy/wiki/Faq#why-does-sympy-say-that-two-equal-expressions-are-unequal
双等号 (
) 用于测试相等性。然而,这测试的是精确的表达式,而不是象征性的。==
https://docs.sympy.org/latest/explanation/gotchas.html#double-equals-signs
除了使用
simplify(lhs - rhs)
并检查表达式是否减少到0之外,还可以使用Eq(lhs, rhs)
。
from sympy import *
assert Eq(2 * sqrt(2 - sqrt(3)), -sqrt(2) + sqrt(6))
此类与 == 运算符不同。 == 运算符测试两个表达式之间的结构是否完全相等;此类以数学方式比较表达式。
https://docs.sympy.org/latest/modules/core.html#sympy.core.relational.Equality