"""Exercise only the patched export function on small exact synthetic matrices.""" import argparse import ast from fractions import Fraction as Fr import json from pathlib import Path import time class Engine: zero_norm = False def __init__(self, *args): self.n = 2 def matrices(self): norm = Fr(0) if self.zero_norm else Fr(1) return [[norm, Fr(0)], [Fr(0), norm]], [[Fr(5), Fr(0)], [Fr(0), Fr(1)]] def main(): parser = argparse.ArgumentParser() parser.add_argument("patched_certificate", type=Path) args = parser.parse_args() tree = ast.parse(args.patched_certificate.read_text()) nodes = [node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name == "certify"] if len(nodes) != 1: raise ValueError("Expected exactly one certificate function") namespace = { "Fr": Fr, "time": time, "EvenEngine": Engine, "A_SRC": Fr(2583, 10000), "ritz_vector": lambda *args, **kwargs: (None, [0.5, 0.25], 4.2), } exec(compile(ast.Module(body=nodes, type_ignores=[]), "patched-export", "exec"), namespace) result = namespace["certify"](2, Fr(0), 1) assert [Fr(value) for value in result["coefficients"]] == [Fr(1, 2), Fr(1, 4)] assert Fr(result["gram_norm"]) == Fr(5, 16) assert Fr(result["quadratic_form"]) == Fr(21, 16) assert Fr(result["rayleigh_quotient"]) == Fr(21, 5) for name, threshold in (("tau_4", Fr(4)), ("tau_1_over_A", Fr(10000, 2583))): assert Fr(result[name]["value"]) == Fr(21, 16) - threshold * Fr(5, 16) Engine.zero_norm = True try: namespace["certify"](2, Fr(0), 1) except ValueError as error: assert "Nonpositive exact Gram norm" in str(error) else: raise AssertionError("Zero Gram norm must refuse a certificate") print(json.dumps({ "coefficient_roundtrip": True, "exact_gram_norm": "5/16", "exact_quadratic_form": "21/16", "rayleigh_quotient": "21/5", "zero_gram_norm_refused": True, "scope": "Synthetic export-function fixture; no original eigensolve or k=46 certificate rerun.", }, sort_keys=True, separators=(",", ":"))) if __name__ == "__main__": main()