File size: 4,989 Bytes
4fd2269 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | #!/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()
|