#!/usr/bin/env python3 """Independent exact audit of finite-group tropical quotient convolution. Standard library only. This intentionally simple implementation is independent of the research prototype and is a correctness check, not a speed benchmark. Run: python3 audit_algebra.py --output audit_algebra_results.json """ import argparse import hashlib import json import math import random from pathlib import Path class Dihedral: """D_m of order 2m: (i,b) denotes r**i s**b.""" def __init__(self, m): self.m = m self.elements = [(i, b) for b in range(2) for i in range(m)] def mul(self, a, b): return ((a[0] + (-1 if a[1] else 1) * b[0]) % self.m, a[1] ^ b[1]) def inv(self, a): return ((-(-1 if a[1] else 1) * a[0]) % self.m, a[1]) def stabilizers(self, f): left = [a for a in self.elements if all(f[self.mul(a, x)] == f[x] for x in self.elements)] right = [a for a in self.elements if all(f[self.mul(x, a)] == f[x] for x in self.elements)] return left, right def dense(self, f, g): return {z: min(f[a] + g[self.mul(self.inv(a), z)] for a in self.elements) for z in self.elements} def reduced(self, f, g): """Simple exhaustive coset construction; all cost witnesses checked.""" lf, rf = self.stabilizers(f) lg, rg = self.stabilizers(g) common = set(rf) & set(lg) remaining = set(self.elements) argument_representatives = [] while remaining: a = min(remaining) argument_representatives.append(a) remaining -= {self.mul(a, h) for h in common} remaining = set(self.elements) output = {} pair_evaluations = 0 witness_checks = 0 output_orbits = 0 while remaining: z = min(remaining) values = [(f[a] + g[self.mul(self.inv(a), z)], a, self.mul(self.inv(a), z)) for a in argument_representatives] value, a, b = min(values) pair_evaluations += len(values) output_orbits += 1 for l in lf: for r in rg: zz = self.mul(self.mul(l, z), r) aa, bb = self.mul(l, a), self.mul(b, r) assert self.mul(aa, bb) == zz assert f[aa] + g[bb] == value witness_checks += 1 if zz in output: assert output[zz] == value output[zz] = value remaining.discard(zz) assert len(output) == len(self.elements) assert pair_evaluations == output_orbits * len(argument_representatives) return output, { "objective_pair_evaluations": pair_evaluations, "output_double_cosets": output_orbits, "inner_left_cosets": len(argument_representatives), "witness_factor_checks": witness_checks, } def original_500_case_audit(): """Same seed, sample order, cases and assertions as the initial audit.""" rng = random.Random(713) checks = 0 examples = [] witness_checks = 0 for m in (3, 4, 5, 6, 8): group = Dihedral(m) f = {x: (0 if x[1] == 0 and x[0] <= m // 2 else 1 if x == (0, 1) else 9) for x in group.elements} assert all(len(s) == 1 for s in group.stabilizers(f)) k = group.dense(f, f) assert all(k[x] == x[1] for x in group.elements) output, work = group.reduced(k, k) assert output == k assert work["objective_pair_evaluations"] == 4 examples.append({ "rotation_order_m": m, "group_order": 2 * m, "input_left_right_stabilizer_orders": list(map(len, group.stabilizers(f))), "composite_left_right_stabilizer_orders": list(map(len, group.stabilizers(k))), "dense_objective_pairs": (2 * m) ** 2, **work, }) for trial in range(100): f = {x: rng.randrange(5) for x in group.elements} g = {x: rng.randrange(5) for x in group.elements} # Keep the original zero-range RNG calls: they affect later samples. if trial % 4 == 0: f = {x: x[1] + rng.randrange(1) for x in group.elements} if trial % 5 == 0: g = {x: x[1] for x in group.elements} output, work = group.reduced(f, g) assert output == group.dense(f, g) checks += 1 witness_checks += work["witness_factor_checks"] assert checks == 500 return { "seed": 713, "cases_per_group": 100, "rotation_orders": [3, 4, 5, 6, 8], "cases_passed": checks, "witness_factor_checks": witness_checks, "emergent_examples": examples, } def ladder_audit(): """Check the nonconstant subgroup ladder in D_(2**k), independently.""" records = [] transitions = 0 for exponent in (3, 4, 5, 6): m = 2 ** exponent group = Dihedral(m) cost, high = 1, 9 previous = None previous_zero_set = None for j in range(1, exponent + 1): subgroup_size = 2 ** j stride = m // subgroup_size subgroup = {(a * stride % m, 0) for a in range(subgroup_size)} reflected = {group.mul(x, (0, 1)) for x in subgroup} zero_half = {(a * stride % m, 0) for a in range(subgroup_size // 2 + 1)} f = {x: 0 if x in zero_half else cost if x == (0, 1) else high for x in group.elements} assert all(len(s) == 1 for s in group.stabilizers(f)) expected = {x: 0 if x in subgroup else cost if x in reflected else high for x in group.elements} squared = group.dense(f, f) assert squared == expected left, right = group.stabilizers(squared) assert set(left) == subgroup assert set(right) == subgroup record = { "group_rotation_exponent": exponent, "group_order": 2 * m, "level_j": j, "subgroup_order": subgroup_size, "primitive_stabilizers_trivial": True, "square_matches_exact_formula": True, "composite_left_right_stabilizer_orders": [len(left), len(right)], "composite_distinct_values": sorted(set(squared.values())), } if previous is not None: composed = group.dense(previous, f) assert composed == expected assert previous != composed assert previous_zero_set < subgroup reduced, work = group.reduced(previous, f) assert reduced == expected # The freshly learned left stabilizer has 2**(j-1) elements. assert work["objective_pair_evaluations"] == (2 * m) ** 2 // (2 ** (j - 1)) record["ladder_transition"] = { "previous_times_next_primitive_equals_new_square": True, "target_kernel_is_new_and_nonconstant": True, "dense_objective_pairs": (2 * m) ** 2, **work, } transitions += 1 previous = squared previous_zero_set = subgroup records.append(record) return {"transitions_passed": transitions, "levels": records} def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--output", default="audit_algebra_results.json") args = parser.parse_args() result = { "audit": "independent finite-group tropical convolution identities", "status": "all assertions passed", "standard_library_only": True, "timing_or_speedup_claim": False, "work_count_scope": "objective-pair evaluations only; not total runtime", "script_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "original_audit": original_500_case_audit(), "nonconstant_subgroup_ladder": ladder_audit(), } target = Path(args.output) target.parent.mkdir(parents=True, exist_ok=True) target.write_text(json.dumps(result, indent=2) + "\n", encoding="utf-8") print(json.dumps({ "status": result["status"], "random_structured_cases": result["original_audit"]["cases_passed"], "ladder_transitions": result["nonconstant_subgroup_ladder"]["transitions_passed"], "output": str(target.resolve()), })) if __name__ == "__main__": main()