Unxei-XII-Resolution-Actuation-Law / src /verify_boundary_completion.py
PureOne's picture
Upload research package with Eve Uploader
a5568e0 verified
Raw History Blame Contribute Delete
12 kB
"""Exact and numerical checks for tolerance-threshold completion.
At an exact polynomial minimax tolerance, a vanishing higher-degree admixture
creates a strict margin that dominates the finite-gap Hermite remainder,
without changing the lower-degree energy exponent. The two examples below
use entirely rational polynomials and exact band-error certificates.
"""
from pathlib import Path
import json
import mpmath as mp
import sympy as sp
from spectral_control import MomentProblem, polynomial_jets, polyval, real
ROOT = Path(__file__).resolve().parents[1]
DPS = 160
def number(value):
return mp.nstr(value, 75)
def rational(value):
value = sp.Rational(value)
return mp.mpf(str(value.p))/mp.mpf(str(value.q))
def coefficients(poly, x, degree):
return [rational(sp.expand(poly).coeff(x, k)) for k in range(degree+1)]
def confluent_limit(degree, scaled_coefficients):
"""Use one-node derivative kernels, not a colliding-node extrapolation."""
p = MomentProblem([2], degree, [[1]], [[0]*(degree+1)], impulse=0)
data = mp.matrix(len(p.rows), 1)
for k, c in enumerate(scaled_coefficients):
# Derivative kernels are k! times the Taylor-coefficient kernels.
data[1+2*k] = mp.factorial(k)*rational(c)
return (data.T*mp.lu_solve(p.G, data))[0]
def realize_mix(low_coefficients, high_coefficients, n, N, gap, dps=DPS):
mp.mp.dps = dps
gap = real(gap)
alpha = gap**(N-n)
coeffs = [(1-alpha)*(low_coefficients[k] if k < len(low_coefficients) else 0)
+ alpha*high_coefficients[k] for k in range(N+1)]
m = (N-1)//2
jets = polynomial_jets(coeffs, [-mp.mpf("0.5"), mp.mpf("0.5")], gap, m)
p = MomentProblem([2-gap/2, 2+gap/2], m, [[1], [1]], jets, impulse=0)
return p, coeffs
def run():
mp.mp.dps = DPS
x, delta = sp.symbols("x delta", real=True)
a, r = sp.Rational(1, 2), sp.Rational(1, 4)
P1 = -2*x
P3 = (32*x**3-26*x)/9
# The standard integrated polynomial with A=a+r has error 113/301,
# exceeding 1/3. This rational monotone degree-five substitute does better.
P5 = (-810*x+1920*x**3-2048*x**5)/229
original_P5 = (-810*x+960*x**3-512*x**5)/301
checks = []
def check(name, condition, **details):
record = {"name": name, "passed": bool(condition), **details}
checks.append(record)
if not condition:
raise AssertionError(json.dumps(record, indent=2))
for name, poly in [("P1", P1), ("P3", P3), ("P5", P5)]:
check(f"{name} exact oddness and nominal values",
sp.expand(poly.subs(x, -x)+poly) == 0 and
sp.simplify(poly.subs(x, -a)-1) == 0 and
sp.simplify(poly.subs(x, a)+1) == 0)
check("linear exact band error is 1/2",
abs(P1.subs(x, a-r)+1) == sp.Rational(1, 2) and
abs(P1.subs(x, a+r)+1) == sp.Rational(1, 2))
check("cubic endpoints have error 1/3",
P3.subs(x, a-r)+1 == sp.Rational(1, 3) and
P3.subs(x, a+r)+1 == sp.Rational(1, 3))
# On the positive interval, Q3=-P3 is concave. Its only stationary point
# is sqrt(39)/12 and its value is 13 sqrt(39)/81; the squared inequality
# below proves the interior overshoot is no larger than 1/3.
stationary = sp.sqrt(39)/12
check("cubic interior critical value respects global 1/3 error",
sp.simplify(sp.diff(P3, x).subs(x, stationary)) == 0 and
sp.simplify((-P3).subs(x, stationary)-13*sp.sqrt(39)/81) == 0 and
sp.Integer(13)**2*39 <= sp.Integer(108)**2)
check("quintic is globally monotone by an exact square factorization",
sp.factor(sp.diff(P5, x)+10*(9-32*x**2)**2/229) == 0)
e5 = sp.Rational(109, 458)
check("quintic exact uniform band error is 109/458 below 1/3",
P5.subs(x, a-r) == -sp.Rational(349, 458) and
P5.subs(x, a+r) == -sp.Rational(567, 458) and e5 < sp.Rational(1, 3))
check("original A=3/4 quintic is correctly rejected at this threshold",
1+original_P5.subs(x, a-r) == sp.Rational(113, 301) and
sp.Rational(113, 301) > sp.Rational(1, 3))
specs = [
{"name": "linear_threshold", "n": 1, "N": 3,
"low": P1, "high": P3, "tau": sp.Rational(1, 2),
"higher_error": sp.Rational(1, 3),
"gaps": ["0.01", "0.003", "0.001", "0.0003", "0.0001"],
"expected_vector": [0, -2, 0, sp.Rational(32, 9)]},
{"name": "cubic_threshold", "n": 3, "N": 5,
"low": P3, "high": P5, "tau": sp.Rational(1, 3),
"higher_error": e5,
"gaps": ["0.001", "0.0001", "0.00001", "0.000003", "0.000001"],
"expected_vector": [0, 0, 0, sp.Rational(32, 9), 0, -sp.Rational(2048, 229)]},
]
cases, all_residuals = [], []
for spec in specs:
n, N = spec["n"], spec["N"]
mix = sp.expand((1-delta**(N-n))*spec["low"]+delta**(N-n)*spec["high"])
scaled_coefficients = [sp.cancel(delta**(n-k)*mix.coeff(x, k)) for k in range(N+1)]
limits = [sp.limit(c, delta, 0, dir="+") for c in scaled_coefficients]
check(f"{spec['name']} exact finite scaled coefficient vector",
limits == spec["expected_vector"] and
all(sp.denom(c).subs(delta, 0) != 0 for c in scaled_coefficients),
exact_limit_vector=[str(c) for c in limits])
check(f"{spec['name']} mixture preserves nominal identities exactly",
sp.simplify(mix.subs(x, -a)-1) == 0 and
sp.simplify(mix.subs(x, a)+1) == 0)
lo = coefficients(spec["low"], x, n)
hi = coefficients(spec["high"], x, N)
expected = confluent_limit(N, limits)
tau = rational(spec["tau"])
strict_difference = rational(spec["tau"]-spec["higher_error"])
m, R = (N-1)//2, N+1
max_product = (mp.mpf(5)/16)**(m+1)
points, energy_errors, band_checks, remainder_checks = [], [], [], []
# The two outer and two inner band endpoints and nominal centers.
sample_x = [-mp.mpf(3)/4, -mp.mpf(1)/2, -mp.mpf(1)/4,
mp.mpf(1)/4, mp.mpf(1)/2, mp.mpf(3)/4]
for gap_string in spec["gaps"]:
gap = real(gap_string)
p, coeffs = realize_mix(lo, hi, n, N, gap)
all_residuals.append(p.solve_residual)
scaled_energy = gap**(2*n)*p.energy
energy_error = abs(scaled_energy/expected-1)
energy_errors.append(energy_error)
margin = gap**(N-n)*strict_difference
remainder = gap**R*max_product*p.moment_norm(R)*mp.sqrt(p.energy)/mp.factorial(R)
polynomial_bound = tau-margin
analytic_bound = polynomial_bound+remainder
max_sample_error, max_profile_difference = mp.mpf(0), mp.mpf(0)
for xx in sample_x:
target = mp.mpf(1) if xx < 0 else -mp.mpf(1)
response = p.response(2+gap*xx)
max_sample_error = max(max_sample_error, abs(response-target))
max_profile_difference = max(max_profile_difference,
abs(response-polyval(coeffs, xx)))
band_checks.append(max_sample_error <= analytic_bound+mp.mpf("1e-70"))
remainder_checks.append(max_profile_difference <= remainder+mp.mpf("1e-70"))
points.append({"gap": gap_string, "energy": number(p.energy),
"scaled_energy": number(scaled_energy),
"relative_error_to_confluent_limit": number(energy_error),
"strict_polynomial_margin": number(margin),
"uniform_Hermite_remainder_bound": number(remainder),
"remainder_to_margin_ratio": number(remainder/margin),
"analytic_uniform_band_error_bound": number(analytic_bound),
"analytically_certified_below_tolerance": bool(analytic_bound < tau),
"sampled_maximum_actual_error": number(max_sample_error),
"sampled_maximum_polynomial_difference": number(max_profile_difference),
"solve_residual": number(p.solve_residual)})
check(f"{spec['name']} energy retains exponent 2n at exact tolerance",
energy_errors[-1] < mp.mpf("1e-6") and
energy_errors[-1] < energy_errors[0]/10 and
energy_errors[-1] < energy_errors[-2],
predicted_exponent=2*n, final_relative_error=number(energy_errors[-1]))
check(f"{spec['name']} strict margin dominates the whole-band remainder",
all(p["analytically_certified_below_tolerance"] for p in points[-2:]),
final_remainder_to_margin=points[-1]["remainder_to_margin_ratio"])
check(f"{spec['name']} direct frequency responses respect the analytic bounds",
all(band_checks) and all(remainder_checks))
cases.append({"name": spec["name"], "radius": "1/4", "tau": str(spec["tau"]),
"n": n, "N": N, "energy_exponent": 2*n,
"lower_polynomial": str(spec["low"]),
"higher_polynomial": str(spec["high"]),
"higher_polynomial_exact_band_error": str(spec["higher_error"]),
"mixture": str(mix),
"strict_margin_coefficient": str(spec["tau"]-spec["higher_error"]),
"exact_scaled_coefficient_limit": [str(c) for c in limits],
"predicted_exact_energy_limit": number(expected),
"points": points})
# Precision sensitivity of the smallest-gap, cubic-threshold example.
spec = specs[-1]
mp.mp.dps = 160
lo = coefficients(spec["low"], x, spec["n"])
hi = coefficients(spec["high"], x, spec["N"])
lower, _ = realize_mix(lo, hi, spec["n"], spec["N"], spec["gaps"][-1], dps=160)
lower_energy = lower.energy
mp.mp.dps = 180
lo = coefficients(spec["low"], x, spec["n"])
hi = coefficients(spec["high"], x, spec["N"])
higher, _ = realize_mix(lo, hi, spec["n"], spec["N"], spec["gaps"][-1], dps=180)
discrepancy = abs(lower_energy/higher.energy-1)
check("160-to-180-digit threshold energy agreement at gap 1e-6",
discrepancy < mp.mpf("1e-60"), relative_discrepancy=number(discrepancy))
max_residual = max(all_residuals)
check("all boundary systems meet a strict nominal moment residual",
max_residual < mp.mpf("1e-70"), maximum_residual=number(max_residual))
report = {
"scope": "Exact nominal targets and uniform interval-frequency tolerance, fixed T=6, Omega=2, polynomial weight q=4",
"claim": "Threshold tolerances retain exponent 2 n_tau by a vanishing higher-degree polynomial admixture",
"important_distinction": "The analytic whole-band bound is global; sampled actual errors are only corroboration",
"working_decimal_precision": DPS,
"cases": cases,
"precision_comparison_relative_discrepancy": number(discrepancy),
"maximum_nominal_moment_residual": number(max_residual),
"checks": checks, "check_count": len(checks),
"passed": all(c["passed"] for c in checks),
}
path = ROOT / "results" / "boundary_completion.json"
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(report, indent=2)+"\n")
print(json.dumps({"passed": report["passed"], "check_count": len(checks),
"report": str(path), "precision_discrepancy": number(discrepancy),
"maximum_residual": number(max_residual),
"final_certificates": [{"case": c["name"], "tau": c["tau"],
"gap": c["points"][-1]["gap"],
"bound": c["points"][-1]["analytic_uniform_band_error_bound"]}
for c in cases]}))
if __name__ == "__main__":
run()