Skip to content

Equality Types Explained

James Sharkey edited this page Feb 21, 2018 · 2 revisions

Equality Types

The equality-checker returns two pieces of information about the equality of the provided test and target expressions; equal and equality_type. The key equal is either "true" or "false", but equality_type can take one of three values depending on the 'strength' of the equality. The possible values are exact, symbolic and numeric. There is little meaningful difference between the symbolic equality and numeric equality, but a significant difference between those and exact equality.

Exact Equality

Exact equality expresses a statement about the syntactic equality of the two expressions; they take the same form as one another up to commutativity of addition and multiplication. In this way, the expression x + y is exactly equal to y + x but x**2 + 2xy + y**2 is not exactly equal to (x + y)**2. It also allows distinguishing between expressions like cos(x) and cos(-x).

This exact equality is often unimportant, but allows a distinction between unfactorised and factorised forms to be made, or notational conventions to be enforced.

Symbolic Equality

Symbolic equality expresses the fact that the SymPy library can, making only algebraic operations, convert one expression into the other. In this sense it is a strong statement; the two expressions are mathematically equilvalent to one another. Currently this simplification goes beyond the SymPy default and treats all variables as real and positive, allowing sqrt(x**2) to be symbolically equal to x, and ensuring sin(x)**2 + cos(x)**2 can be found symbolically equal to 1.

Numeric Equality

Numeric equality is the fall-back case. It is a known impossibility in mathematics to find the zeroes of an arbitrary function (especially one involving exp() or log() or irrational numbers); and so SymPy not being able to prove things equal does not imply a lack of equality. Numeric equality is checked by sampling the expressions at random points in the half closed interval [0, 1) and computing the difference between them. If the difference is less than a small tolerance to account for floating point arithmetic, the two expressions are treated as equal. If the difference is greater, the two expressions cannot be equal. This sampling is done on the real line, but in the complex plane if functions are not defined wholly real in the interval.

Numeric checking is theoretically far more definite than symbolic equality (it will always be able to give the 'correct' answer, without the limitations of symbolic checking, for a large enough number of samples), but suffers from the risk of floating point arithmetic errors in this implementation causing incorrect results.

Thus all expressions found not to be equal will have numeric equality_type, and numeric equality or otherwise may need to be taken with a pinch of salt.

Clone this wiki locally