"""Pins for PREREG_flat_regime_2026-09-21.md: the flat gate (prestep='flat').""" from __future__ import annotations import math import pytest from simroot.pure import pure_root XTOL, MAXFEV = 1e-12, 200 PROBS = ( (lambda t: t ** 3 - 2.0 * t - 5.0, 1.0, 4.0), (lambda t: math.exp(t) - 2.0, 0.0, 3.0), (lambda t: 6.0 * (t - 2.0) ** 5, 0.0, 10.0), (lambda t: t ** 19, -10.0, 100.0), ) @pytest.mark.parametrize("f,a,b", PROBS) @pytest.mark.parametrize("guard", ("project", "credit", "safe")) def test_the_default_is_untouched(f, a, b, guard): ref = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard) off = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep="none") assert ref == off @pytest.mark.parametrize("f,a,b", PROBS) @pytest.mark.parametrize("guard", ("project", "credit")) @pytest.mark.parametrize("ext", ("none", "pattern")) def test_the_guarantee_holds_with_the_gate(f, a, b, guard, ext): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep="flat", extrapolate=ext) assert ok and min(a, b) <= x <= max(a, b) half = math.ceil(math.log2(abs(b - a) / XTOL)) assert n <= (2 * half + 3 if guard == "project" else half + 4), n @pytest.mark.parametrize("f,a,b", PROBS) @pytest.mark.parametrize("guard", ("project", "credit")) @pytest.mark.parametrize("ext", ("none", "pattern")) def test_the_guarantee_holds_with_the_twice_gate(f, a, b, guard, ext): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep="flat2", extrapolate=ext) assert ok and min(a, b) <= x <= max(a, b) half = math.ceil(math.log2(abs(b - a) / XTOL)) assert n <= (2 * half + 3 if guard == "project" else half + 4), n def test_the_twice_gate_is_silent_where_the_model_is_exact_and_fires_on_a_power(): st = {} pure_root(lambda t: 3.0 * t - 1.0, -2.0, 5.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flat2", stats=st) assert st["pre_refused"] == 0 st = {} pure_root(lambda t: t ** 9, -1.0, 2.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flat2", stats=st) assert st["pre_refused"] > 0 @pytest.mark.parametrize("f,a,b", PROBS) @pytest.mark.parametrize("guard", ("project", "credit")) @pytest.mark.parametrize("ext", ("none", "pattern")) def test_the_guarantee_holds_with_the_creep_gate(f, a, b, guard, ext): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep="flatcreep", extrapolate=ext) assert ok and min(a, b) <= x <= max(a, b) half = math.ceil(math.log2(abs(b - a) / XTOL)) assert n <= (2 * half + 3 if guard == "project" else half + 4), n def test_the_creep_gate_is_silent_on_a_line_fires_on_a_power_and_spares_a_simple_root(): """Measured before pinning: silent where the model is exact, fires on x^9, and does NOT fire on x^3 - 2x - 5 over [1, 4], where the single flag fired once.""" runs = {} for name, f, a, b in (("line", lambda t: 3.0 * t - 1.0, -2.0, 5.0), ("x9", lambda t: t ** 9, -1.0, 2.0), ("cubic", lambda t: t ** 3 - 2.0 * t - 5.0, 1.0, 4.0)): st = {} pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatcreep", stats=st) runs[name] = st["pre_refused"] assert runs["line"] == 0 and runs["x9"] > 0 and runs["cubic"] == 0, runs def test_the_gate_never_fires_where_the_model_is_exact(): """On a straight line the model's step lands on the root: r = 0, never >= 1/4.""" st = {} pure_root(lambda t: 3.0 * t - 1.0, -2.0, 5.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flat", stats=st) assert st["pre_refused"] == 0 def test_the_gate_fires_on_a_pure_power(): """On x^9 the tangent step leaves (8/9)^9 = 0.35 of |f| on the same side.""" st = {} pure_root(lambda t: t ** 9, -1.0, 2.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flat", stats=st) assert st["pre_refused"] > 0 # ---- PREREG_flat_stick_2026-09-21: the sticky flat regime and its midpoint ablation ---- from simroot.pure import _order_from_promise, _order_split # noqa: E402 @pytest.mark.parametrize("m", (2.0, 3.0, 5.0, 9.0, 19.0, 100.0)) def test_the_order_is_read_back_from_its_own_promise(m): r = (1.0 - 1.0 / m) ** m assert _order_from_promise(r) == pytest.approx(m, rel=1e-9) def test_no_finite_order_at_or_above_one_over_e_gives_the_midpoint(): assert math.isinf(_order_from_promise(math.exp(-1.0))) assert math.isinf(_order_from_promise(0.9)) assert _order_split(-1.0, -3.0, 2.0, 5.0, math.inf) == 0.5 @pytest.mark.parametrize("m", (1, 3, 5, 9)) def test_the_split_lands_on_the_root_of_an_exact_power(m): f = lambda t: (t - 0.3) ** m # noqa: E731 assert _order_split(-1.0, f(-1.0), 2.0, f(2.0), float(m)) == pytest.approx(0.3, abs=1e-12) def test_the_split_does_not_overflow_on_extreme_ends(): x = _order_split(0.0, -1e-300, 1.0, 1e300, 2.0) assert 0.0 <= x < 1e-100 x = _order_split(0.0, -1e300, 1.0, 1e-300, 2.0) assert 1.0 - x < 1e-100 and x <= 1.0 @pytest.mark.parametrize("f,a,b", PROBS) @pytest.mark.parametrize("guard", ("project", "credit")) @pytest.mark.parametrize("ext", ("none", "pattern")) @pytest.mark.parametrize("gate", ("flatstick", "flatstickmid")) def test_the_guarantee_holds_with_the_sticky_regime(f, a, b, guard, ext, gate): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep=gate, extrapolate=ext) assert ok and min(a, b) <= x <= max(a, b) half = math.ceil(math.log2(abs(b - a) / XTOL)) assert n <= (2 * half + 3 if guard == "project" else half + 4), n @pytest.mark.parametrize("gate", ("flatstick", "flatstickmid")) def test_the_sticky_regime_is_silent_on_a_line_and_holds_on_a_power(gate): st = {} pure_root(lambda t: 3.0 * t - 1.0, -2.0, 5.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep=gate, stats=st) assert st["pre_refused"] == 0 st = {} _x, n, _ok = pure_root(lambda t: t ** 9, -1.0, 2.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep=gate, stats=st) # once entered, every later non-forced step is a split: many, not one assert st["pre_refused"] >= 3, st def test_an_infinite_end_gives_the_midpoint_not_nan(): """Audit bug 2026-09-21: an infinite |f| made lq nan, and the nan step reached the safeguard before the loop checked finiteness.""" assert _order_split(0.0, -math.inf, 2.0, 1.0, 3.0) == 1.0 assert _order_split(0.0, -math.inf, 2.0, math.inf, 3.0) == 1.0