Download src/verify_resolution.py from PureOne/Unxei-XII-Resolution-Actuation-Law: direct link, hf CLI and curl.
- Browser
- Download file 6.83 kB
-
https://huggingface.co/datasets/PureOne/Unxei-XII-Resolution-Actuation-Law/resolve/main/src/verify_resolution.py
- Command line
-
hf download hf://datasets/PureOne/Unxei-XII-Resolution-Actuation-Law/src/verify_resolution.py
-
curl -L -o verify_resolution.py https://huggingface.co/datasets/PureOne/Unxei-XII-Resolution-Actuation-Law/resolve/main/src/verify_resolution.py
6.83 kB
| """High-precision checks of interval response, minimax degree and collision cost.""" | |
| import json | |
| from pathlib import Path | |
| import mpmath as mp | |
| from spectral_control import (two_mode, MomentProblem, polyval, flat_polynomial_coeffs, | |
| flat_error, interval_error, polynomial_jets) | |
| ROOT=Path(__file__).resolve().parents[1] | |
| def s(x): | |
| return mp.nstr(x,45) | |
| def main(): | |
| mp.mp.dps=110 | |
| checks=[] | |
| def check(name,condition,detail=None): | |
| if not condition: | |
| raise AssertionError(name+': '+str(detail)) | |
| checks.append({'name':name,'passed':True,'detail':detail}) | |
| # Exact polynomial profiles, including high-order extrapolation. | |
| rc=(mp.sqrt(2)-1)/2 | |
| profile=[] | |
| for radius in [mp.mpf('.1'),rc,mp.mpf('.25'),mp.mpf('.4')]: | |
| for m in [0,1,2,4,8,16,32,64]: | |
| A=flat_error(m,radius) | |
| B=interval_error(m,radius) | |
| profile.append({'m':m,'radius':s(radius),'flat_error':s(A),'interval_error':s(B)}) | |
| for radius in [mp.mpf('.1'),rc,mp.mpf('.25'),mp.mpf('.4')]: | |
| m=512 | |
| g=radius*(1+radius) | |
| asymp=4*g/(mp.sqrt(mp.pi)*(1+2*radius)*mp.sqrt(m))*(4*g)**m | |
| ratio=flat_error(m,radius)/asymp | |
| check('Laplace_asymptotic_r'+s(radius),abs(ratio-1)<mp.mpf('.006'),s(ratio)) | |
| # Fixed-m response tends to the Hermite polynomial; remainder is a bound | |
| # at every sampled point (the proof, not sampling, gives the continuum bound). | |
| convergence=[] | |
| points=[mp.mpf(k)/8 for k in range(-2,11)] # y=-.25,...,1.25 | |
| for m in [0,1,2]: | |
| H=flat_polynomial_coeffs(m) | |
| previous=None | |
| for gap in ['.08','.04','.02','.01']: | |
| problem=two_mode(m,gap,dps=110) | |
| delta=mp.mpf(gap) | |
| errors=[]; bound_ratios=[] | |
| for y in points: | |
| F=problem.response(2+delta*(y-mp.mpf('.5'))) | |
| err=abs(F-polyval(H,y)) | |
| bound=problem.hermite_remainder_bound(y,[0,1],delta) | |
| if bound: | |
| check(f'Hermite_bound_m{m}_gap{gap}_y{s(y)}',err<=bound*(1+mp.mpf('1e-65')),s(err/bound)) | |
| bound_ratios.append(err/bound) | |
| else: | |
| check(f'nodal_value_m{m}_gap{gap}_y{s(y)}',err<mp.mpf('1e-65'),s(err)) | |
| errors.append(err) | |
| maxerr=max(errors) | |
| if previous is not None: | |
| check(f'profile_convergence_m{m}_gap{gap}',maxerr<previous,s(maxerr/previous)) | |
| previous=maxerr | |
| check(f'linear_residual_m{m}_gap{gap}',problem.solve_residual<mp.mpf('1e-65'),s(problem.solve_residual)) | |
| convergence.append({'m':m,'gap':gap,'energy':s(problem.energy), | |
| 'scaled_energy':s(problem.energy*delta**(4*m+2)), | |
| 'profile_max_error':s(maxerr),'max_bound_fraction':s(max(bound_ratios))}) | |
| # Analytically optimal cubic at radius1/4. It is not the flat cubic. | |
| P=[0,-mp.mpf(26)/9,0,mp.mpf(32)/9] | |
| cubic=[] | |
| for gap in ['.08','.04','.02','.01','.001','.0001']: | |
| delta=mp.mpf(gap) | |
| jets=polynomial_jets(P,[-mp.mpf('.5'),mp.mpf('.5')],delta,1) | |
| p=MomentProblem([2-delta/2,2+delta/2],1,jets=jets) | |
| f=two_mode(1,gap,dps=110) | |
| # Uniform bound: |(x+.5)^2(x-.5)^2| is maximal at the outer edge. | |
| maxprod=(mp.mpf('.75')**2-mp.mpf('.5')**2)**2 | |
| bound=delta**4*maxprod*p.moment_norm(4)*mp.sqrt(p.energy)/mp.factorial(4) | |
| certified=mp.mpf(1)/3+bound | |
| samples=[-mp.mpf('.75')+mp.mpf(k)/200 for k in range(101)] | |
| samples+= [mp.mpf('.25')+mp.mpf(k)/200 for k in range(101)] | |
| worst=max(abs(p.response(2+delta*x)-(1 if x<0 else -1)) for x in samples) | |
| check('cubic_finite_gap_bound_'+gap,worst<=certified,s(certified)) | |
| cubic.append({'gap':gap,'energy':s(p.energy),'flat_energy':s(f.energy), | |
| 'energy_ratio':s(p.energy/f.energy),'sampled_worst_error':s(worst), | |
| 'uniform_error_upper_bound':s(certified),'uniform_remainder_bound':s(bound), | |
| 'tolerance_0_4_certified':bool(certified<mp.mpf('.4'))}) | |
| check('cubic_asymptotic_energy_ratio',abs(mp.mpf(cubic[-1]['energy_ratio'])-mp.mpf(64)/81)<mp.mpf('1e-5')) | |
| check('cubic_continuum_tolerance_certificate',any(x['tolerance_0_4_certified'] for x in cubic)) | |
| # Leading coefficient constants, independent of extrapolation tests. | |
| constants=[] | |
| for m in [0,1,2]: | |
| n=2*m+1 | |
| # Leading coefficient in centered x is unchanged by the shift y=x+.5. | |
| hn=(-1)**(m+1)*2*mp.binomial(2*m,m) | |
| jets=[[0]*(n+1)] | |
| jets[0][n]=mp.factorial(n)*hn | |
| limit=MomentProblem([2],n,jets=jets).energy | |
| final=[v for v in convergence if v['m']==m][-1] | |
| ratio=mp.mpf(final['scaled_energy'])/limit | |
| check(f'leading_constant_m{m}',abs(ratio-1)<mp.mpf('.003'),s(ratio)) | |
| constants.append({'m':m,'limit_constant':s(limit),'finite_gap_ratio':s(ratio)}) | |
| # Unequal collision speeds: orders4 and6 instead of one universal order4. | |
| multiscale=[] | |
| for target,exponent in [([0,0,1],6),([1,0,0],4),([1,1,1],0)]: | |
| vals=[] | |
| for gap in ['.08','.04','.02','.01']: | |
| eps=mp.mpf(gap) | |
| p=MomentProblem([2,2+eps,2+eps+eps**2],0,jets=[[v] for v in target]) | |
| vals.append({'epsilon':gap,'energy':s(p.energy),'scaled_energy':s(p.energy*eps**exponent)}) | |
| slope=mp.log(mp.mpf(vals[-1]['energy'])/mp.mpf(vals[-2]['energy']))/mp.log(mp.mpf('.5')) | |
| check('multiscale_slope_'+str(target),abs(slope+exponent)<mp.mpf('.12'),s(slope)) | |
| multiscale.append({'target':target,'expected_exponent':exponent,'slope':s(slope),'data':vals}) | |
| # One independently integrated difficult case and a precision replication. | |
| p=two_mode(1,'.04',dps=90) | |
| quadrature=p.direct_residual() | |
| check('independent_adaptive_quadrature',quadrature<mp.mpf('1e-55'),s(quadrature)) | |
| E90=p.energy | |
| p=two_mode(1,'.04',dps=140) | |
| difference=abs(E90/p.energy-1) | |
| check('precision_90_vs_140',difference<mp.mpf('1e-60'),s(difference)) | |
| report={'status':'PASS','precision_digits':110,'assertions':len(checks),'checks':checks, | |
| 'critical_radius':s(rc),'polynomial_profiles':profile,'response_convergence':convergence, | |
| 'cubic_comparison':cubic,'leading_constants':constants,'multiscale':multiscale, | |
| 'independent_quadrature_residual':s(quadrature),'precision_relative_difference':s(difference), | |
| 'limitations':'High precision is not directed-rounding interval arithmetic. Continuum bounds follow from proved formulas; response grids are illustrations.'} | |
| path=ROOT/'results/resolution_report.json' | |
| path.write_text(json.dumps(report,indent=2),encoding='utf-8') | |
| print(f'PASS: {len(checks)} numerical assertions; {path.name}') | |
| if __name__=='__main__': | |
| main() | |