#!/usr/bin/env python3 """Verify the exceptional mutation sequences using exact arithmetic.""" from __future__ import annotations import argparse import json from fractions import Fraction from math import gcd, lcm from pathlib import Path from verify_affine_lifts import finite_roots, grading_equations, mutate, recover_matrix, require def inverse(matrix): n = len(matrix) a = [[Fraction(x) for x in row] + [Fraction(i == j) for j in range(n)] for i, row in enumerate(matrix)] for j in range(n): pivot = next((i for i in range(j, n) if a[i][j]), None) require(pivot is not None, "Singular matrix in exact inversion") a[j], a[pivot] = a[pivot], a[j] scale = a[j][j] a[j] = [x / scale for x in a[j]] for i in range(n): if i != j and a[i][j]: scale = a[i][j] a[i] = [x - scale * y for x, y in zip(a[i], a[j])] return [row[n:] for row in a] def rank(matrix): a = [[Fraction(x) for x in row] for row in matrix] p = 0 for j in range(len(a[0])): pivot = next((i for i in range(p, len(a)) if a[i][j]), None) if pivot is None: continue a[p], a[pivot] = a[pivot], a[p] scale = a[p][j] a[p] = [x / scale for x in a[p]] for i in range(p + 1, len(a)): if a[i][j]: scale = a[i][j] a[i] = [x - scale * y for x, y in zip(a[i], a[p])] p += 1 if p == len(a): break return p def root_data(name, framing): if name == "F4": a = [[2, -1, 0, 0], [-1, 2, -1, 0], [0, -2, 2, -1], [0, 0, -1, 2]] t = [1, 1, 2, 2] elif name == "G2": a = [[2, -3], [-1, 2]] t = [3, 1] else: raise ValueError("Expected F4 or G2") n = len(a) require(1 <= framing <= n, "Invalid fundamental node") at_inverse = inverse([list(row) for row in zip(*a)]) d = [row[framing - 1] for row in at_inverse] require(all(x.denominator == 1 and x > 0 for x in d), "Invalid dimension vector") d = [int(x) for x in d] parent = [None] * n visited = {framing - 1} queue = [framing - 1] for i in queue: for j in range(n): if a[i][j] and j != i and j not in visited: visited.add(j) parent[j] = i queue.append(j) require(len(visited) == n, "Disconnected root system") roots = finite_roots(a) highest = max((b for b in roots if all(x >= 0 for x in b)), key=sum) coroot = [Fraction(b, ti) for b, ti in zip(highest, t)] require(all(x.denominator == 1 for x in coroot), "Invalid highest coroot") return a, t, d, parent, [1] + [int(x) for x in coroot], roots def initial_construction(seed): a, t, d, parent, comarks, roots = root_data(seed["type"], seed["fundamental_node"]) n, ell = sum(d), len(d) require(seed["rank"] == ell, "Rank mismatch") total = 2 * n + 1 root = seed["fundamental_node"] - 1 length = t[root] labels = [] for i, size in enumerate(d): for h in range(1, size): labels.extend([["s", i + 1, h], ["z", i + 1, h]]) labels.extend(["edge", parent[i] + 1, i + 1] for i in range(ell) if parent[i] is not None) labels.append(["root", root + 1]) mutable = len(labels) labels.append(["c"]) labels.extend(["principal", i + 1] for i in range(ell)) require(len(labels) == total, "Incorrect number of initial variables") require(labels == seed["initial"]["labels"], "Variable labels do not match root data") offset, s = [], 0 for size in d: offset.append(s) s += size def unit(j): return [Fraction(k == j) for k in range(total)] def add(*terms): return [sum(row[j] for row in terms) for j in range(total)] def scale(c, row): return [c * x for x in row] def p(i, h): return unit(offset[i] + h - 1) def x(i, h): return unit(n + offset[i] + h - 1) def psum(i, h): return add(*(p(i, k) for k in range(1, h + 1))) def xsum(i, h): return add(*(x(i, k) for k in range(1, h + 1))) def descendants(i): return [i] + [k for j in range(ell) if parent[j] == i for k in descendants(j)] trees = [descendants(i) for i in range(ell)] tree_vectors = [add(*(scale(Fraction(t[j], t[i]), xsum(j, d[j])) for j in trees[i])) for i in range(ell)] tree_degrees = [int(length > 1 and t[i] == length and any(t[j] == 1 for j in trees[i])) for i in range(ell)] rho = [add(tree_vectors[i], scale(-1, xsum(i, d[i]))) for i in range(ell)] characters, directions, raw_degrees, symmetrizers = [], [], [], [] for label in labels: kind = label[0] if kind in ("s", "z"): i, h = label[1] - 1, label[2] row = add(xsum(i, h), rho[i]) raw_degree = tree_degrees[i] if kind == "s": row = add(row, psum(i, h)) direction = add(x(i, h + 1), scale(-1, x(i, h))) else: direction = add(p(i, h), scale(-1, p(i, h + 1)), x(i, h), scale(-1, x(i, h + 1))) directions.append(direction) symmetrizers.append(t[i]) elif kind == "edge": i, j = label[1] - 1, label[2] - 1 row = scale(Fraction(t[j], lcm(t[i], t[j])), tree_vectors[j]) raw_degree = (1 if length > 1 and t[i] == length and t[j] == 1 else tree_degrees[j]) directions.append(add(p(j, d[j]), scale(-1, p(i, 1)))) symmetrizers.append(lcm(t[i], t[j])) elif kind == "root": row = tree_vectors[root][:] raw_degree = int(length > 1) directions.append(p(root, d[root])) symmetrizers.append(length) elif kind == "c": row = unit(total - 1) raw_degree = length elif kind == "principal": i = label[1] - 1 row = psum(i, d[i]) raw_degree = 0 else: raise ValueError("Unknown variable label") if kind != "c": row[-1] += Fraction(raw_degree, length) characters.append(row) raw_degrees.append(raw_degree) ci = inverse(characters) matrix = [[sum(u * v for u, v in zip(direction, column)) for column in zip(*ci)] for direction in directions] require(all(x.denominator == 1 for row in matrix for x in row), "Nonintegral exchange matrix") raw_matrix = [[int(x) for x in row] for row in matrix] words = {name: chart["word"] for name, chart in seed["charts"].items()} matrix, affine_position = recover_matrix( raw_matrix, labels, symmetrizers, t, d, parent, roots, root, words) equations, targets = grading_equations(matrix, symmetrizers, t, root, words) degrees = [sum(x * value for x, value in zip(row, targets)) for row in inverse(equations)] require(all(v.denominator == 1 and v > 0 for v in degrees), "Invalid geometric degree") degrees = [int(v) for v in degrees] powers = [] for degree, original in zip(degrees, raw_degrees): power, remainder = divmod(degree - original, length) require(remainder == 0, "Nonintegral geometric lift") powers.append(power) c = mutable require(powers[c] == 0, "The homogeneous parameter must not be rescaled by its own power") for raw, actual in zip(raw_matrix, matrix): lifted_entry = raw[c] - sum(raw[j] * powers[j] for j in range(total) if j != c) require(lifted_entry == actual[c], "Primitive lifts disagree with the geometric column") recorded_powers = seed["initial"]["c_powers"] require(all(type(v) is int for v in recorded_powers) and recorded_powers == powers, "Recorded c_powers disagree with the reconstructed geometric lifts") boundaries = {str(i): mutable + i + 1 for i in range(ell + 1)} require(seed["cartan_matrix"] == a, "Cartan matrix mismatch") require(seed["root_length_factors"] == t, "Root lengths mismatch") require(seed["dimension_vector"] == d, "Dimension vector mismatch") require(seed["mutable_count"] == mutable, "Mutable count mismatch") require(seed["variable_count"] == total, "Variable count mismatch") require(seed["boundary_indices"] == boundaries, "Boundary indexing mismatch") require(seed["initial"]["degrees_before_lift"] == raw_degrees, "Degrees before lifting mismatch") require(seed["initial"]["degrees"] == degrees, "Initial degrees mismatch") require(seed["initial"]["matrix"] == matrix, "Initial exchange matrix mismatch") require(seed["symmetrizers"] == symmetrizers, "Symmetrizer mismatch") require(all(degrees[boundaries[str(i)] - 1] == length * comarks[i] for i in range(ell + 1)), "Frozen degrees do not equal root length times comark") require(rank(matrix) == mutable, "Initial extended matrix does not have full row rank") require(all(gcd(*row) == 1 for row in matrix), "A mutable row is not primitive") phase = seed["initial"]["phase_exponents"] order = seed["initial"]["phase_order"] require(order % 2 == 0 and len(phase) == total and phase[c] % order == 0, "Invalid phase normalization") require(all((sum(b * q for b, q in zip(row, phase)) - order // 2) % order == 0 for row in matrix), "The phases do not give the stated exchange normalization") return a, t, d, parent, matrix, degrees, affine_position def finite_profile(labels, a, d, parent, color, sign): i = color - 1 beta = sum(-a[j][i] * d[j] for j in range(len(d)) if parent[j] == i) values = [] for label in labels: kind, value = label[0], 0 if kind in ("s", "z"): j, h = label[1] - 1, label[2] if i == j: value = (max(d[i] - h + int(kind == "s"), beta - 2 * h) if sign == "E" else max(d[i] + h + int(kind == "s") - beta, 0)) elif sign == "E" and parent[j] == i: value = -a[i][j] * h elif sign == "F" and parent[i] == j: value = -a[i][j] * (d[j] - h) elif kind == "edge": source, target = label[1] - 1, label[2] - 1 if sign == "E" and source == i: value = d[target] elif sign == "F" and target == i: value = d[source] elif kind == "principal" and label[1] - 1 == i: value = 1 elif kind == "root" and label[1] - 1 == i and sign == "F": value = 1 values.append(value) return values def validate_state(matrix, degrees, symmetrizers): m, n = len(matrix), len(degrees) require(all(type(v) is int and v > 0 for v in degrees), "Nonpositive or nonintegral degree") for i, row in enumerate(matrix): require(len(row) == n and row[i] == 0, "Invalid exchange matrix") require(sum(b * degree for b, degree in zip(row, degrees)) == 0, "An exchange is not homogeneous") for j in range(m): require(symmetrizers[i] * row[j] == -symmetrizers[j] * matrix[j][i], "Principal part is not skew-symmetrized by the stated factors") def verify_seed(seed): a, t, d, parent, initial_matrix, initial_degrees, affine_position = initial_construction(seed) mutable, total = seed["mutable_count"], seed["variable_count"] delta = seed["symmetrizers"] validate_state(initial_matrix, initial_degrees, delta) expected_names = {f"{sign}{i}" for i in range(len(t) + 1) for sign in ("E", "F")} require(set(seed["charts"]) == expected_names, "Missing or duplicate current chart") mutations, profiles = 0, 0 for name, chart in seed["charts"].items(): sign, color = name[0], int(name[1:]) boundary = seed["boundary_indices"][str(color)] - 1 factor = 1 if color == 0 else t[color - 1] column_sign = 1 if sign == "E" else -1 require(chart["boundary_index"] == boundary + 1, f"{name}: wrong boundary") require(chart["root_length_factor"] == factor, f"{name}: wrong root length") require(chart["column_sign"] == column_sign, f"{name}: wrong column sign") matrix, degrees = [row[:] for row in initial_matrix], initial_degrees[:] profile = None if color: profile = finite_profile(seed["initial"]["labels"], a, d, parent, color, sign) require(chart["initial_current_degrees"] == profile, f"{name}: initial current degrees mismatch") profiles += 1 for step, position in enumerate(chart["word"], 1): require(type(position) is int and 1 <= position <= mutable, f"{name}: invalid pivot at step {step}") k = position - 1 row = matrix[k] positive = sum(max(b, 0) * v for b, v in zip(row, degrees)) negative = sum(max(-b, 0) * v for b, v in zip(row, degrees)) require(positive == negative, f"{name}: degree mismatch at step {step}") degrees[k] = positive - degrees[k] if profile is not None: positive = sum(max(b, 0) * v for b, v in zip(row, profile)) negative = sum(max(-b, 0) * v for b, v in zip(row, profile)) profile[k] = max(positive, negative) - profile[k] require(all(v >= 0 for v in profile), f"{name}: negative current degree at step {step}") matrix = mutate(matrix, k) validate_state(matrix, degrees, delta) require(matrix == chart["endpoint_matrix"], f"{name}: endpoint matrix mismatch") require(degrees == chart["endpoint_degrees"], f"{name}: endpoint degrees mismatch") exponents = [Fraction(column_sign * delta[j] * matrix[j][boundary], factor) for j in range(mutable)] require(any(exponents) and all(v >= 0 and v.denominator == 1 for v in exponents), f"{name}: incorrect sign or nonintegral coefficient exponents") exponents = [int(v) for v in exponents] + [0] * (total - mutable) require(exponents == chart["coefficient_exponents"], f"{name}: coefficient exponents mismatch") require(sum(v * degree for v, degree in zip(exponents, degrees)) == degrees[boundary], f"{name}: coefficient has the wrong degree") if profile is not None: expected = [int(j == boundary) for j in range(total)] require(profile == expected, f"{name}: endpoint current is not adapted") require(chart["endpoint_current_degrees"] == expected, f"{name}: stored current degrees mismatch") mutations += len(chart["word"]) if seed["type"] == "G2" and seed["fundamental_node"] == 2: require(seed["charts"]["F0"]["word"] == [], "The long-node G2 negative affine word must be empty") require(seed["charts"]["F0"]["coefficient_exponents"] == [0, 0, 0, 1, 0, 0, 0], "The long-node G2 negative affine coefficient must be P") return len(seed["charts"]), mutations, profiles, affine_position def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("data", nargs="?", type=Path, default=Path(__file__).with_name("exceptional_mutations.json")) args = parser.parse_args() data = json.loads(args.data.read_text()) cases = data["seeds"] require({(s["type"], s["fundamental_node"]) for s in cases} == {("F4", i) for i in range(1, 5)} | {("G2", i) for i in range(1, 3)}, "The six exceptional fundamental cases are required") require(len(cases) == 6, "Duplicate fundamental case") charts = mutations = profiles = 0 for seed in cases: counts = verify_seed(seed) charts += counts[0] mutations += counts[1] profiles += counts[2] print(f"{seed['type']}, node {seed['fundamental_node']}: " f"geometric E0 coefficient at position {counts[3]}; " f"{counts[0]} charts, {counts[1]} mutations, {counts[2]} finite current profiles") print(f"Verified {len(cases)} seeds, {charts} charts, {mutations} mutations " f"and {profiles} finite current profiles; reconstructed all geometric columns and lift powers.") if __name__ == "__main__": main()