Identity to check

Examples
World 1 · float64

Numerically

World 2 · arithmetic modulo p

Randomly, but exactly

World 3 · canonical form

Symbolically

What is going on here?

Numerically, we evaluate $f$ and $g$ in float64 at $k$ random points. Agreement within the tolerance is evidence, not a proof: the points may “miss” the places where the functions differ (as with $\sqrt{x^2}$ versus $x$ on $[0,5]$), and rounding errors can fake a difference that does not exist (Wilkinson’s polynomial) or hide one that does ($e^{\pi\sqrt{163}}$ differs from an integer by about $7.5\cdot 10^{-13}$, while the gap between neighboring float64 numbers there is $32$).

Randomly, but exactly. For rational expressions we compute exactly modulo the prime $p = 2^{31}-1$. The guarantee comes from the Schwartz–Zippel lemma: if $P \neq 0$ is a polynomial of degree $d$ over a field $F$ and $S\subseteq F$ is finite, then $$\Pr_{r\in S^n}\bigl[P(r) = 0\bigr] \le \frac{d}{|S|}.$$ We apply it to the numerator $P = N$ of the difference $f - g = N/D$. The error is one-sided: a point found with $f(r)\not\equiv g(r)$ proves that $f\neq g$; agreement in $k$ independent trials gives certainty “only” up to an error probability $\le (d/p)^k$. (Strictly speaking, we reason about $N \bmod p$ — if all the coefficients of $N$ were divisible by $p$, the test would not detect it; that is why in practice $p$ is chosen at random as well.)

Symbolically, we bring $f - g$ to the canonical form $N/D$ (expanded polynomials with rational coefficients). $N = 0$ is a proof of the identity of rational functions — assuming that the CAS implementation is correct. For $\sin$ and $\cos$ of a single variable a trick works: the substitution $t = \tan\frac{x}{2}$, $\sin x = \frac{2t}{1+t^2}$, $\cos x = \frac{1-t^2}{1+t^2}$ turns the expression into a rational function of $t$. Outside such classes the method is silent (Richardson’s theorem).

Formal proof. The ring tactic in Lean does the same as “World 3” — it normalizes both sides to canonical form — but in addition it builds a certificate that is checked independently by the system’s small kernel. Then we do not have to trust the implementation of the tactic itself.

Explore on your own

  1. For $\sqrt{x^2}$ and $x$, change the sampling interval from $[0, 5]$ to $[-5, 5]$. What changes in each world, and why does “World 3” give no answer at all?
  2. Wilkinson’s polynomial: shrink the interval to $[0, 1]$ or increase the tolerance. When does the numerical test “pass”? Does that say anything about the identity?
  3. In $g$ for $(x+1)^{10}$, change the coefficient $252$ to $253$. How many trials modulo $p$ suffice to detect the difference? Answer without the applet, using the Schwartz–Zippel lemma.
  4. Enter $\sin(2x)$ and $2\sin x\cos x$. Why can our mini‑CAS not prove this, even though the identity is true? What rule would it need to know? (In SymPy: expand_trig.)