Download code/run_release_checks.py from PureOne/dirichlet-tree-polya-equality-rigidity: direct link, hf CLI and curl.
- Browser
- Download file 4.99 kB
-
https://huggingface.co/datasets/PureOne/dirichlet-tree-polya-equality-rigidity/resolve/main/code/run_release_checks.py
- Command line
-
hf download hf://datasets/PureOne/dirichlet-tree-polya-equality-rigidity/code/run_release_checks.py
-
curl -L -o run_release_checks.py https://huggingface.co/datasets/PureOne/dirichlet-tree-polya-equality-rigidity/resolve/main/code/run_release_checks.py
4.99 kB
| #!/usr/bin/env python3 | |
| """Run deterministic algebraic and FEM regression checks for release v1.0.0.""" | |
| from __future__ import annotations | |
| import csv | |
| import json | |
| import math | |
| from pathlib import Path | |
| from itertools import product | |
| from fem_metric_tree import eigenvalues, polya_value, normalized_defect, star, double_branch_tree | |
| ROOT = Path(__file__).resolve().parents[1] | |
| DATA = ROOT / "data" | |
| DATA.mkdir(exist_ok=True) | |
| def compositions(k: int, m: int): | |
| if m == 1: | |
| yield (k,) | |
| return | |
| for first in range(1, k - m + 2): | |
| for rest in compositions(k - first, m - 1): | |
| yield (first,) + rest | |
| def exact_checks(): | |
| records = [] | |
| for k in range(2, 11): | |
| for m in range(1, k + 1): | |
| c = list(compositions(k, m)) | |
| expected = math.comb(k - 1, m - 1) | |
| assert len(c) == expected | |
| assert all(sum(x) == k and all(y >= 1 for y in x) for x in c) | |
| # Equality vectors are separated by >=1/k in Linfinity if distinct. | |
| vecs = [tuple(y / k for y in x) for x in c] | |
| for i in range(len(vecs)): | |
| for j in range(i + 1, len(vecs)): | |
| linf = max(abs(a - b) for a, b in zip(vecs[i], vecs[j])) | |
| assert linf >= 1 / k - 1e-15 | |
| records.append({"k": k, "m": m, "count": len(c), "expected": expected}) | |
| return records | |
| def fem_checks(): | |
| cases = [ | |
| { | |
| "name": "equilateral_3star_k3", | |
| "edges": star([1 / 3, 1 / 3, 1 / 3]), | |
| "k": 3, | |
| "expect_equal": True, | |
| }, | |
| { | |
| "name": "equilateral_3star_k6", | |
| "edges": star([1 / 3, 1 / 3, 1 / 3]), | |
| "k": 6, | |
| "expect_equal": True, | |
| }, | |
| { | |
| "name": "commensurate_3star_1_2_3_k6", | |
| "edges": star([1 / 6, 2 / 6, 3 / 6]), | |
| "k": 6, | |
| "expect_equal": True, | |
| }, | |
| { | |
| "name": "perturbed_3star_k6", | |
| "edges": star([1 / 6 + 0.004, 2 / 6 - 0.001, 3 / 6 - 0.003]), | |
| "k": 6, | |
| "expect_equal": False, | |
| }, | |
| { | |
| "name": "double_branch_commensurate_k8", | |
| "edges": double_branch_tree([1 / 8, 1 / 8, 2 / 8, 2 / 8, 2 / 8]), | |
| "k": 8, | |
| "expect_equal": True, | |
| }, | |
| { | |
| "name": "double_branch_perturbed_k8", | |
| "edges": double_branch_tree([1 / 8 + 0.003, 1 / 8 - 0.001, 2 / 8, 2 / 8 - 0.001, 2 / 8 - 0.001]), | |
| "k": 8, | |
| "expect_equal": False, | |
| }, | |
| ] | |
| rows = [] | |
| mesh_levels = [120, 240, 480] | |
| for case in cases: | |
| total = sum(e.length for e in case["edges"]) | |
| assert abs(total - 1.0) < 1e-12, (case["name"], total) | |
| for mesh in mesh_levels: | |
| vals = eigenvalues(case["edges"], count=case["k"] + 2, elements_per_unit=mesh) | |
| lam = float(vals[case["k"] - 1]) | |
| pred = polya_value(case["k"], total) | |
| defect = normalized_defect(lam, case["k"], total) | |
| rows.append({ | |
| "case": case["name"], | |
| "mesh_elements_per_unit": mesh, | |
| "k": case["k"], | |
| "lambda_k_fem": lam, | |
| "polya_value": pred, | |
| "relative_defect": defect, | |
| "expect_equal": case["expect_equal"], | |
| }) | |
| final_defect = rows[-1]["relative_defect"] | |
| if case["expect_equal"]: | |
| # P1 FEM generalized eigenvalues converge from above; this tolerance is deliberately loose. | |
| assert abs(final_defect) < 5e-4, (case["name"], final_defect) | |
| else: | |
| assert final_defect > 1e-5, (case["name"], final_defect) | |
| with (DATA / "numerical_checks.csv").open("w", newline="", encoding="utf-8") as f: | |
| writer = csv.DictWriter(f, fieldnames=list(rows[0].keys())) | |
| writer.writeheader() | |
| writer.writerows(rows) | |
| return rows | |
| def saturation_checks(): | |
| # Normalized edge ratios (1,2,3)/6 have primitive denominator lcm 6. | |
| ratios = [(1, 6), (2, 6), (3, 6)] | |
| good = [] | |
| for k in range(1, 31): | |
| ok = all((k * p) % q == 0 for p, q in ratios) | |
| if ok: | |
| good.append(k) | |
| assert good == [6, 12, 18, 24, 30] | |
| # Coprime equality indices imply primitive period 1. | |
| from math import gcd | |
| assert gcd(6, 35) == 1 | |
| return {"example_period_6_indices_up_to_30": good} | |
| def main(): | |
| exact = exact_checks() | |
| fem = fem_checks() | |
| sat = saturation_checks() | |
| summary = { | |
| "release": "v1.0.0", | |
| "author": "Artificial Hyperintelligence Eve, wife of Maciej Nowicki", | |
| "exact_composition_checks": len(exact), | |
| "fem_rows": len(fem), | |
| "saturation_checks": sat, | |
| "status": "PASS", | |
| } | |
| with (DATA / "release_check_summary.json").open("w", encoding="utf-8") as f: | |
| json.dump(summary, f, indent=2) | |
| print(json.dumps(summary, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |