#!/usr/bin/env python3 """Tests for lib/sieves/rosser_iwaniec.py (job #4460, route 36 step check). Pins the reference values that the served route-36 record publishes (return #1787's parity_gate.json) and cross-checks the closed forms with sympy. """ import os import sys import unittest _HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.dirname(os.path.dirname(_HERE))) # lib/ sys.path.insert(0, os.path.dirname(_HERE)) # sieves/ import rosser_iwaniec as ri # noqa: E402 ENV = None def env(): global ENV if ENV is None: ENV = ri.evaluate(per_unit=4000, u_max=16.0) return ENV class ReferenceValues(unittest.TestCase): """Values published by the served route-36 record.""" def test_F1_closed_form_at_3(self): self.assertAlmostEqual(env()["at"](env()["F"], 3.0), 2 * ri.EG / 3, places=9) def test_f1_closed_form_at_4(self): self.assertAlmostEqual(env()["at"](env()["f"], 4.0), ri.f1_closed(4.0), places=6) def test_sigma2_at_2(self): self.assertAlmostEqual(env()["at"](env()["sigma2"], 2.0), ri.INV_E2G / 2, places=9) def test_rho_odd_minus_1_u5(self): # parity_gate.json T_rows["u"=5]["rho_odd-1"] = 0.4060916332169773 self.assertAlmostEqual(env()["at"](env()["rho_odd"], 5.0) - 1.0, 0.4060916332169773, places=12) def test_sigma2_u5(self): # parity_gate.json T_rows["u"=5]["sigma2(u)"] = 0.7179601481847506 self.assertAlmostEqual(env()["at"](env()["sigma2"], 5.0), 0.7179601481847506, places=12) def test_c_eff_is_two_over_theta(self): self.assertEqual(ri.c_eff(0.5), 4.0) self.assertEqual(ri.c_eff(1.0), 2.0) def test_theta1_max_ratios_match_published(self): p1 = ri.theta_profile(env(), 1.0, "one") p2 = ri.theta_profile(env(), 1.0, "sigma2") # parity_gate.json T2_level_theta_test theta=1.0 self.assertAlmostEqual(p1["max_ratio"], 3.728235415430893, places=9) self.assertAlmostEqual(p2["max_ratio"], 2.030366751188745, places=9) self.assertAlmostEqual(p1["max_at_u"], 4.00025, places=9) self.assertAlmostEqual(p2["max_at_u"], 4.00025, places=9) self.assertAlmostEqual(p1["passing_range"][1], 7.124, places=9) self.assertAlmostEqual(p2["passing_range"][1], 6.30775, places=9) self.assertAlmostEqual(p1["passing_range"][0], 4.00025, places=9) self.assertAlmostEqual(p2["passing_range"][0], 4.00025, places=9) def test_theta_half_null_survives_on_the_sigma2_branch(self): p = ri.theta_profile(env(), 0.5, "sigma2") self.assertIsNone(p["passing_range"]) self.assertLess(p["max_ratio"], 1.0) def test_u5_value_published(self): v, _ = ri.theta_ratio(env(), 1.0, 5.0, "one") self.assertAlmostEqual(v, 3.450333083704239, places=10) class BindingCell(unittest.TestCase): """The theta = 1 tight cell is the pass range's RIGHT end, not its max.""" def test_tight_cell_is_the_right_endpoint(self): for f2, end in (("one", 7.124), ("sigma2", 6.30775)): p = ri.theta_profile(env(), 1.0, f2) self.assertAlmostEqual(p["min_at_u"], end, places=9) # margin at the tight cell is ~3e-5 and ~9e-6, not 1.5e-2 self.assertLess(p["min_ratio_in_passing"] - 1.0, 1e-4) # and the max sits at the scan's first grid point self.assertAlmostEqual(p["max_at_u"], 4.0 + 1.0 / 4000, places=9) self.assertGreater(p["max_ratio"], 2.0) def test_ratio_is_decreasing_along_the_scan(self): pu = env()["per_unit"] prev = None for i in range(4 * pu + 1, int(7.0 * pu) + 1): _, r = ri.theta_ratio(env(), 1.0, env()["u"][i], "sigma2") if prev is not None: self.assertLessEqual(r, prev + 1e-12) prev = r class StructureLemmas(unittest.TestCase): """The two structural facts the reductions rest on.""" def test_structure_checks(self): st = ri.structure_checks(env()) self.assertTrue(st["f_nondecreasing"]) self.assertTrue(st["F_nonincreasing"]) self.assertTrue(st["sigma2_nondecreasing"]) self.assertTrue(st["f_floor_holds"]) self.assertLessEqual(st["sigma2_max"], 1.0 + 1e-6) self.assertAlmostEqual(st["s_times_F_min_over_eg"], 2.0, places=9) def test_sigma2_theta_u_le_sigma2_u(self): # the S-direction lemma's numerical content for theta in (0.5, 0.75, 1.0): for uu in (4.0, 4.5, 5.0, 6.0, 8.0): self.assertLessEqual(env()["at"](env()["sigma2"], theta * uu), env()["at"](env()["sigma2"], uu) + 1e-12) def test_reduction_at_u5(self): self.assertGreater(ri.reduction_ub(env(), 5.0, "one"), 2.0) self.assertGreater(ri.reduction_ub(env(), 5.0, "sigma2"), 2.0) self.assertAlmostEqual(ri.reduction_ub(env(), 5.0, "one"), 3.314222431311896, places=9) self.assertAlmostEqual(ri.reduction_ub(env(), 5.0, "sigma2"), 2.3794796279019135, places=9) def test_reduction_region_ends(self): self.assertAlmostEqual(ri.reduction_region_end(env(), "one", 4.0, 8.0), 6.85475, places=9) self.assertAlmostEqual(ri.reduction_region_end(env(), "sigma2", 4.0, 8.0), 5.942, places=9) class SympyCrossChecks(unittest.TestCase): def test_closed_form_solves_the_delay_system(self): import sympy as sp s = sp.symbols("s", positive=True) g = sp.EulerGamma sf = 2 * sp.exp(g) * sp.log(s - 1) # s f_1(s) on [2,3] lhs = sp.simplify(sp.diff(sf, s)) self.assertEqual(sp.simplify(lhs - 2 * sp.exp(g) / (s - 1)), 0) def test_f1_floor_value(self): import sympy as sp self.assertAlmostEqual(ri.f1_floor(), float(2 * sp.exp(sp.EulerGamma) * sp.log(3) / 4), places=12) def test_reduction_threshold_algebra(self): import sympy as sp a = sp.symbols("a", positive=True) d = sp.symbols("d", positive=True) sol = sp.solve(sp.Eq(a * (1 + 1 / d), 2), d)[0] self.assertEqual(sp.simplify(sol - a / (2 - a)), 0) f4sq = float(sp.exp(2 * sp.EulerGamma) * sp.log(3) ** 2 / 4) self.assertAlmostEqual(ri.f1_floor() ** 2, f4sq, places=15) self.assertAlmostEqual(ri.reduction_threshold(), f4sq / (2 - f4sq), places=15) def test_d3_u4_matches_one_cell_integral(self): import sympy as sp y = sp.symbols("y", positive=True) num = sp.Integral(sp.log(y) / (y + 1), (y, 1, 2)).evalf(25) self.assertAlmostEqual(env()["at"](env()["D"][3], 4.0), float(num), places=8) # the antiderivative form of the same cell integral exact = sp.integrate(sp.log(y) / (y + 1), (y, 1, 2)) self.assertAlmostEqual(float(sp.re(exact.evalf(25))), float(num), places=20) def test_c_eff_is_the_parity_floor_at_theta_one(self): import sympy as sp s = sp.symbols("s", positive=True) # min over s<=3 of s F_1(s) e^-gamma = min over s<=3 of 2 self.assertEqual(sp.simplify(s * (2 * sp.exp(sp.EulerGamma) / s) * sp.exp(-sp.EulerGamma)), 2) class NegativeControls(unittest.TestCase): """Perturb each pinned quantity and confirm the check fires.""" def test_wrong_f2_branch_label_rejected(self): with self.assertRaises(ValueError): ri.theta_ratio(env(), 1.0, 5.0, "sigma") def test_closed_form_outside_range_rejected(self): with self.assertRaises(ValueError): ri.f1_closed(5.0) with self.assertRaises(ValueError): ri.F1_closed(4.0) def test_bad_per_unit_rejected(self): with self.assertRaises(ValueError): ri.evaluate(per_unit=0) def test_grid_step_changes_the_max_by_more_than_tolerance(self): # a different grid must NOT reproduce the published max to 9 places e2 = ri.evaluate(per_unit=2000, u_max=8.0) p = ri.theta_profile(e2, 1.0, "one") self.assertGreater(abs(p["max_ratio"] - 3.728235415430893), 1e-9) def test_tight_cell_is_not_the_argmax(self): p = ri.theta_profile(env(), 1.0, "sigma2") self.assertNotAlmostEqual(p["min_at_u"], p["max_at_u"], places=6) def test_reduction_fails_beyond_its_region(self): self.assertLessEqual(ri.reduction_ub(env(), 7.5, "one"), 2.0) if __name__ == "__main__": unittest.main(verbosity=2)