"""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 # ---- PREREG_flat_exit_2026-09-21: leaving the sticky regime by similarity at the midpoint ---- @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_exit(f, a, b, guard, ext): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep="flatstickexit", 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 _exit_stats(f, a, b): st = {} pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatstickexit", stats=st) return st.get("stick_entries", 0), st.get("stick_exits", 0) def test_the_exit_leaves_a_false_entry_and_keeps_a_true_one(): """Measured before pinning: a simple root that trips the flag (the cubic, exp) leaves the regime at a midpoint; pure powers, scale-invariant, never do.""" assert _exit_stats(lambda t: 3.0 * t - 1.0, -2.0, 5.0) == (0, 0) assert _exit_stats(lambda t: t ** 3 - 2.0 * t - 5.0, 1.0, 4.0) == (1, 1) assert _exit_stats(lambda t: math.exp(t) - 2.0, 0.0, 3.0) == (1, 1) for f, a, b in ((lambda t: t ** 9, -1.0, 2.0), (lambda t: t ** 19, -10.0, 100.0), (lambda t: 6.0 * (t - 2.0) ** 5, 0.0, 10.0)): ent, ex = _exit_stats(f, a, b) assert ent >= 1 and ex == 0, (ent, ex) def test_the_exit_does_not_touch_the_other_sticky_modes(): for gate in ("flatstick", "flatstickmid"): st = {} pure_root(lambda t: math.exp(t) - 2.0, 0.0, 3.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep=gate, stats=st) assert st.get("stick_exits", 0) == 0 # ---- PREREG_flat_judge_2026-09-21: one similarity judgment at entry and exit ---- @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_judge(f, a, b, guard, ext): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep="flatjudge", 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_judge_is_silent_on_a_line_and_holds_a_pure_power(): st = {} pure_root(lambda t: 3.0 * t - 1.0, -2.0, 5.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatjudge", stats=st) assert st.get("stick_entries", 0) == 0 and st["pre_refused"] == 0 st = {} pure_root(lambda t: t ** 19, -10.0, 100.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatjudge", stats=st) assert st.get("stick_entries", 0) >= 1 and st.get("stick_exits", 0) == 0, st # ---- PREREG_flat_analogy_2026-09-21: entry by analogy with the run's own past ---- from simroot.pure import _analogous_and_steep, _config3 # noqa: E402 @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_analogy_entry(f, a, b, guard, ext): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep="flatanalog", 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_analogy_entry_spares_simple_roots_and_catches_powers(): """Measured before pinning: the cubic and exp, which trip the single flag, never enter, and cost exactly what the rule costs without the regime.""" for f, a, b in ((lambda t: 3.0 * t - 1.0, -2.0, 5.0), (lambda t: t ** 3 - 2.0 * t - 5.0, 1.0, 4.0), (lambda t: math.exp(t) - 2.0, 0.0, 3.0)): st = {} _x, n1, _ = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatanalog", stats=st) _x, n0, _ = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project") assert st.get("stick_entries", 0) == 0 and n1 == n0 for f, a, b in ((lambda t: t ** 9, -1.0, 2.0), (lambda t: t ** 19, -10.0, 100.0), (lambda t: 6.0 * (t - 2.0) ** 5, 0.0, 10.0)): st = {} pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatanalog", stats=st) assert st.get("stick_entries", 0) >= 1 def test_a_line_is_never_analogous_and_an_exact_power_repeats_with_its_order(): line = [_config3(x, 3 * x, xp, 3 * xp, xp2, 3 * xp2) for x, xp, xp2 in ((0.1, 0.5, 1.0), (0.05, 0.1, 0.5))] assert line[0][0] == pytest.approx(line[0][1]) # phi = xi on a line assert not _analogous_and_steep(line[1], [line[0]]) p = lambda t: t ** 5 # noqa: E731 old = _config3(0.4, p(0.4), 0.8, p(0.8), 1.6, p(1.6)) new = _config3(0.1, p(0.1), 0.2, p(0.2), 0.4, p(0.4)) # the same shape, zoomed by 4 assert new[0] == pytest.approx(old[0]) and new[1] == pytest.approx(old[1]) assert _analogous_and_steep(new, [old]) # ---- PREREG_flat_analogy_mem_2026-09-21: the analogy remembered as a property of the run ---- @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_remembered_analogy(f, a, b, guard, ext): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep="flatanalogmem", 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_remembered_analogy_spares_simple_roots_and_enters_no_later(): """Measured before pinning: simple roots cost exactly the rule without the regime; on powers the entry is never later than the same-state analogy (x^19: 67 -> 57).""" for f, a, b in ((lambda t: 3.0 * t - 1.0, -2.0, 5.0), (lambda t: t ** 3 - 2.0 * t - 5.0, 1.0, 4.0), (lambda t: math.exp(t) - 2.0, 0.0, 3.0)): st = {} _x, n1, _ = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatanalogmem", stats=st) _x, n0, _ = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project") assert st.get("stick_entries", 0) == 0 and n1 == n0 for f, a, b in ((lambda t: t ** 9, -1.0, 2.0), (lambda t: t ** 19, -10.0, 100.0), (lambda t: 6.0 * (t - 2.0) ** 5, 0.0, 10.0)): _x, nm, _ = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatanalogmem") _x, na, _ = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatanalog") assert nm <= na, (nm, na) # ---- PREREG_defect_contra_flat_2026-09-21: composite prestep and the sign contradiction ---- @pytest.mark.parametrize("bad", ("defect+defect", "defect+band", "flatanalog+flatanalogmem", "none+defect", "defect+flat", "contra+slope")) def test_a_malformed_composite_is_refused(bad): with pytest.raises(ValueError): pure_root(lambda t: t - 1.0, 0.0, 3.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep=bad) @pytest.mark.parametrize("f,a,b", PROBS) @pytest.mark.parametrize("guard", ("project", "credit")) @pytest.mark.parametrize("pre", ("contra", "defect+contra", "defect+flatanalogmem", "defect+contra+flatanalog", "defect+contra+flatanalogmem")) def test_the_guarantee_holds_with_composites(f, a, b, guard, pre): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep=pre) 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 _fun9(t): return math.exp(t) - 2.0 - 0.01 / t ** 2 + 0.000002 / t ** 3 def test_contra_is_silent_on_a_line_and_breaks_the_fun9_well(): """Measured before pinning: fun9.4 ([2e-4, 27]) costs 39 without, 21 with contra.""" st = {} pure_root(lambda t: 3.0 * t - 1.0, -2.0, 5.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="contra", stats=st) assert st["pre_refused"] == 0 _x, n0, _ = pure_root(_fun9, 0.0002, 27.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project") _x, n1, ok = pure_root(_fun9, 0.0002, 27.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="contra") assert ok and n1 < n0, (n0, n1) # ---- PREREG_flat_order_point_2026-09-21: split at the point from the pair's order ---- from simroot.pure import _analogy_order # noqa: E402 @pytest.mark.parametrize("f,a,b", PROBS) @pytest.mark.parametrize("guard", ("project", "credit")) @pytest.mark.parametrize("pre", ("flatanalogpt", "flatanalogmempt", "defect+flatanalogmempt", "contra+flatanalogmempt")) def test_the_guarantee_holds_with_the_order_point(f, a, b, guard, pre): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep=pre) 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_pair_reads_the_order_of_an_exact_power(): p = lambda t: t ** 5 # noqa: E731 old = _config3(0.4, p(0.4), 0.8, p(0.8), 1.6, p(1.6)) new = _config3(0.1, p(0.1), 0.2, p(0.2), 0.4, p(0.4)) assert _analogy_order(new, [old]) == pytest.approx(5.0, rel=1e-9) def test_the_order_point_spares_simple_roots_and_beats_the_midpoint_on_powers(): """Measured before pinning (remembered analogy): x^9 50 -> 33, x^19 57 -> 32, 6(x-2)^5 53 -> 32; simple roots cost exactly the rule without the regime.""" for f, a, b in ((lambda t: 3.0 * t - 1.0, -2.0, 5.0), (lambda t: t ** 3 - 2.0 * t - 5.0, 1.0, 4.0), (lambda t: math.exp(t) - 2.0, 0.0, 3.0)): _x, n1, _ = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatanalogmempt") _x, n0, _ = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project") assert n1 == n0 for f, a, b in ((lambda t: t ** 9, -1.0, 2.0), (lambda t: t ** 19, -10.0, 100.0), (lambda t: 6.0 * (t - 2.0) ** 5, 0.0, 10.0)): _x, npt, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatanalogmempt") _x, nmid, _ = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="flatanalogmem") assert ok and npt < nmid, (npt, nmid) # ---- PREREG_flat_exp_limit_2026-09-21: translation similarity, m = inf ---- @pytest.mark.parametrize("bad", ("exp", "defect+exp", "contra+exp")) def test_exp_needs_the_flat_regime(bad): with pytest.raises(ValueError): pure_root(lambda t: t - 1.0, 0.0, 3.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep=bad) def test_a_translated_exponential_pair_reads_an_infinite_order(): e = math.exp old = _config3(3.0, e(3.0), 4.0, e(4.0), 5.0, e(5.0)) new = _config3(1.0, e(1.0), 2.0, e(2.0), 3.0, e(3.0)) # the same shape, moved by 2 assert new[0] == pytest.approx(old[0]) and new[1] == pytest.approx(old[1]) assert _analogy_order(new, [old]) is None # scaling reading: undefined assert _analogy_order(new, [old], translation=True) == math.inf p = lambda t: t ** 5 # noqa: E731 a = _config3(0.4, p(0.4), 0.8, p(0.8), 1.6, p(1.6)) b = _config3(0.1, p(0.1), 0.2, p(0.2), 0.4, p(0.4)) assert _analogy_order(b, [a], translation=True) == pytest.approx(5.0, rel=1e-9) @pytest.mark.parametrize("f,a,b", PROBS) @pytest.mark.parametrize("guard", ("project", "credit")) @pytest.mark.parametrize("pre", ("flatanalogpt+exp", "defect+flatanalogmempt+exp", "contra+flatanalogmempt+exp")) def test_the_guarantee_holds_with_the_exp_limit(f, a, b, guard, pre): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep=pre) 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_exp_limit_leaves_simple_roots_alone(): for f, a, b in ((lambda t: 3.0 * t - 1.0, -2.0, 5.0), (lambda t: t ** 3 - 2.0 * t - 5.0, 1.0, 4.0), (lambda t: math.exp(t) - 2.0, 0.0, 3.0)): n = [pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep=p)[1] for p in ("defect+flatanalogmempt", "defect+flatanalogmempt+exp")] assert n[0] == n[1], n # ---- PREREG_half_contra_2026-09-21: the half promised by the step vs the bracket ---- @pytest.mark.parametrize("f,a,b", PROBS) @pytest.mark.parametrize("guard", ("project", "credit")) @pytest.mark.parametrize("pre", ("half", "defect+flatanalogmempt+exp+half", "contra+flatanalogmempt+exp+half")) def test_the_guarantee_holds_with_half(f, a, b, guard, pre): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep=pre) 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_half_is_silent_on_a_line_and_breaks_the_fun9_flank(): """Measured before pinning: on [2e-4, 81] the flank costs 24 -> 16 in the best cell.""" st = {} pure_root(lambda t: 3.0 * t - 1.0, -2.0, 5.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep="half", stats=st) assert st.get("half_refused", 0) == 0 n = [pure_root(_fun9, 0.0002, 81.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep=p)[1] for p in ("defect+flatanalogmempt+exp", "defect+flatanalogmempt+exp+half")] assert n[1] < n[0], n # ---- PREREG_close_sub_2026-09-21: bracket closing (borrowed) and the overshoot branch ---- APRIME = "contra+flatanalogmempt+exp+half+close+sub" @pytest.mark.parametrize("bad", ("sub", "defect+sub", "contra+sub")) def test_sub_needs_the_flat_regime(bad): with pytest.raises(ValueError): pure_root(lambda t: t - 1.0, 0.0, 3.0, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep=bad) @pytest.mark.parametrize("f,a,b", PROBS) @pytest.mark.parametrize("guard", ("project", "credit")) @pytest.mark.parametrize("pre", ("close", APRIME)) def test_the_guarantee_holds_with_close_and_sub(f, a, b, guard, pre): x, n, ok = pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard=guard, prestep=pre, extrapolate="pattern") 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 _n(f, a, b, pre): return pure_root(f, a, b, XTOL, MAXFEV, similarity="ratio", scale="derived3", guard="project", prestep=pre, extrapolate="pattern")[1] def test_close_and_sub_measured_in_development(): """Measured before pinning: sin[3,4] 24 -> 7, sqrt|x| sgn 84 -> 19, x^19 unchanged.""" a_ = "contra+flatanalogmempt+exp+half" assert _n(math.sin, 3.0, 4.0, APRIME) < _n(math.sin, 3.0, 4.0, a_) sq = lambda x: math.sqrt(abs(x)) * (0.0 if x == 0 else math.copysign(1.0, x)) # noqa: E731 assert _n(sq, -1 / 3, 1.0, APRIME) < _n(sq, -1 / 3, 1.0, a_) p19 = lambda x: x ** 19 # noqa: E731 assert _n(p19, -10.0, 100.0, APRIME) == _n(p19, -10.0, 100.0, a_) assert _n(lambda x: 3.0 * x - 1.0, -2.0, 5.0, APRIME) == 4 def test_local_order_declines_when_the_slope_square_underflows(): """Bug 2026-09-21: fp != 0 but fp*fp == 0 raised ZeroDivisionError; the sweep harness charged it as a failure (Table 6 order cells). Now the estimator declines (m = 1).""" from simroot.pure import _local_order # three points on a line of slope 1e-170: fp ~ 1e-170, fp*fp underflows to 0 s = 1e-170 assert _local_order(3.0, 3.0 * s, 2.0, 2.0 * s, 1.0, 1.0 * s) == 1.0