"""Compare saved original-symbol quadratures with the exact generator.

Arithmetic balls on quadrature sums exclude quadrature truncation error.
Order agreement and exact controls measure that error heuristically.
"""
import numberdb.sage
from sage.rings.real_arb import RealBallField
from sage.rings.rational_field import QQ
from generate import CubicResistance
import json,re
from pathlib import Path
R=RealBallField(700);p=Path(__file__).resolve().parent;sets={}
for line in (p/'quadrature.log').read_text().splitlines():
 if line.startswith(('LOW64 ','LOW96 ','HIGH256 ','HIGH352 ')):
  tag,data=line.split(' ',1);sets[tag]=json.loads(data)
assert set(sets)=={'LOW64','LOW96','HIGH256','HIGH352'}
g=CubicResistance();failures=[];records=[];maxlow=R(0);maxhigh=QQ(0);maxorder=QQ(0)
for params in g.enumerate():
 key=','.join(str(params[k]) for k in g.parameters);expected=R(g.value(params,100));a=sets['LOW64'][key];b=sets['LOW96'][key]
 err=abs(R(str(b))-expected);maxlow=max(maxlow,err)
 if err>R('3e-12'):failures.append(key+' ordinary quadrature')
 record={'key':key,'low64':a,'low96':b,'low96_error':str(err)}
 if key in sets['HIGH352']:
  aa=R(sets['HIGH256'][key]);bb=R(sets['HIGH352'][key]);order=abs(aa-bb);err=abs(expected-bb)
  assert aa.is_finite() and bb.is_finite() and aa.rad().exact_rational()<QQ(10)**(-150) and bb.rad().exact_rational()<QQ(10)**(-150)
  maxhigh=max(maxhigh,err.upper().exact_rational());maxorder=max(maxorder,order.upper().exact_rational())
  if err>R('1e-110') or order>R('1e-110'):failures.append(key+' high quadrature')
  record.update(high256=sets['HIGH256'][key],high352=sets['HIGH352'][key],high352_error=str(err),order_difference=str(order))
 records.append(record)
known=all(abs(R(sets['HIGH352'][k])-R(QQ(1)/q))<R('1e-110') for k,q in [('sc,1,0,0',3),('bcc,1,1,1',4),('fcc,1,1,0',6)])
negative=all(abs(R(sets['HIGH352'][k])-wrong)>R('1e-3') for k,wrong in [('sc,1,0,0',R(QQ(2)/3)),('bcc,1,1,1',R(1)),('fcc,1,1,0',R(QQ(1)/3))])
assert known and negative
result={'table':'T751','checked_entries':87,'total_entries':87,'high_precision_checked_entries':41,'failures':failures,'known_answer_control':bool(known),'negative_control_rejected':bool(negative),'maximum_ordinary_error':str(maxlow),'maximum_high_error_upper_bound':str(R(maxhigh)),'maximum_high_order_difference_upper_bound':str(R(maxorder)),'quadrature_orders':[64,96,256,352],'high_precision_bits':560,'high_precision_sample':'every selected row of graph distance 1,2 or 6','method':'Original-symbol Fourier integral after analytic third-angle integration and eight Duffy triangle maps. Independent of rational recurrence coefficients and gamma products.','limits':'Gauss quadrature error is not certified; convergence is measured by order comparison, generator comparisons and known controls. Generator precision uses exact identities and Arb, not this heuristic convergence claim.','comparisons':records,'skill_manifest':'../20261118T114154Z-build-skills/manifest.json'}
print('QUADRATURE_RESULT',json.dumps(result),flush=True)
assert not failures,failures
