"""Full two-resource rectangle checks. Random testing does not prove theorems.""" from __future__ import annotations import json,math,time,platform from pathlib import Path import numpy as np from scipy.optimize import brentq from scipy.integrate import quad from maxwell_rectangle import Rectangle,generator_transfer,phase_speed from maxwell_resource import * from floquet import log_spectral_radius from resource_floquet import lifted_phase_map ROOT=Path(__file__).resolve().parents[1] rng=np.random.default_rng(260926);start=time.time() report={'seed':260926,'python':platform.python_version()} boxes=[Rectangle(1,2,1,3),Rectangle(1,2,1,2),Rectangle(.7,2.5,.4,4)] canonical=0;gainerr=0.;timeerr=0.;dualerr=0.;switcherr=0.;balanceerr=0.;signerr=0;tests=[] for box in boxes: for j in range(600): eta,xi,zeta=rng.normal(size=3)*float(rng.choice([.15,.6,2.])) if j%7==0:eta=0. cell=calibrated_resource_cell(box,eta,xi,zeta) am,ap,bm,bp=box.amin,box.amax,box.bmin,box.bmax if min(cell.mean_a-am,ap-cell.mean_a,cell.mean_b-bm,bp-cell.mean_b)<1e-7 or cell.log_gain<1e-7:continue explicit=explicit_resource_cell(box,cell.period,cell.mean_a,cell.mean_b) assert cell_winding(explicit)==1 gainerr=max(gainerr,abs(cell.log_gain-explicit.log_gain)) dual,res,sw=resource_dual_parameters(box,explicit) dualerr=max(dualerr,max(abs(a-b) for a,b in zip(dual,(eta,xi,zeta)))) switcherr=max(switcherr,res) P=(cell.mean_a-am)/(ap-am);Q=(cell.mean_b-bm)/(bp-bm);w=P+Q-1 if abs(w)>1e-10:assert eta*w>0 # Cyclically merge an origin-split diagonal piece before comparing visits. st=list(cell.states) if st[0][:2]==st[-1][:2] and len(st)>1: a,b,t=st.pop();aa,bb,tt=st[0];st[0]=(aa,bb,t+tt) diagonal=(ap,bp) if eta>0 else (am,bm) visits=[t for a,b,t in st if (a,b)==diagonal] if eta: assert len(visits)==2 balanceerr=max(balanceerr,abs(visits[0]-visits[1])) else:assert len(st)==2 for i in range(20): th=float(rng.uniform(0,math.pi));c,s=math.cos(th),math.sin(th) K=s*c+xi*c*c-zeta*s*s if eta>0:ab=(am,bp) if K>eta*s*s/bp else ((ap,bm) if K<-eta*c*c/ap else (ap,bp)) elif eta<0:ab=(am,bp) if K>-eta*c*c/am else ((ap,bm) if K0 else (ap,bm) H=lambda a,b:((b-a)*s*c-eta-xi*a-zeta*b)/(b*c*c+a*s*s) assert H(*ab)>=max(H(a,b) for a in [am,ap] for b in [bm,bp])-1e-10 canonical+=1 assert gainerr<1e-8 and dualerr<1e-7 and switcherr<1e-10 and balanceerr<1e-9 report['canonical']={'calibrated_cells':canonical,'pointwise_corner_checks':canonical*20,'max_gain_error':gainerr,'max_dual_error':dualerr,'max_switch_equation_residual':switcherr,'max_diagonal_visit_mismatch':balanceerr} count=0;hyper=0;worst=-math.inf;meanerr=0.;defect_cases=[] for box in boxes: am,ap,bm,bp=box.amin,box.amax,box.bmin,box.bmax for p,q in [(.2,.25),(.3,.7),(.7,.7),(.8,.1),(.2,.9)]: abar=am+(ap-am)*p;bbar=bm+(bp-bm)*q for S in np.linspace(.6,8,14): F,cs=rectangle_resource_optimum(box,float(S),abar,bbar) for j in range(16): if j%2: z=float(rng.uniform(max(0,p+q-1),min(p,q))) fracs=[z,q-z,p-z,1-p-q+z];corners=[(ap,bp),(am,bp),(ap,bm),(am,bm)] states=[] for (a,b),f in zip(corners,fracs): for t in rng.dirichlet(np.ones(3))*f*S:states.append((a,b,float(t))) rng.shuffle(states) else: N=int(rng.integers(2,15));ts=rng.dirichlet(np.ones(N))*S raw1=rng.uniform(-1,1,N);raw2=rng.uniform(-1,1,N) sh1=brentq(lambda x:np.dot(np.clip(raw1+x,0,1),ts)/S-p,-3,3,xtol=1e-14) sh2=brentq(lambda x:np.dot(np.clip(raw2+x,0,1),ts)/S-q,-3,3,xtol=1e-14) aa=am+(ap-am)*np.clip(raw1+sh1,0,1);bb=bm+(bp-bm)*np.clip(raw2+sh2,0,1) states=list(zip(aa,bb,ts)) M=np.eye(2) for a,b,t in states:M=generator_transfer(a,b,t)@M L=log_spectral_radius(M);worst=max(worst,L-F);count+=1 meanerr=max(meanerr,abs(sum(a*t for a,b,t in states)/S-abar),abs(sum(b*t for a,b,t in states)/S-bbar)) assert L<=F+2e-8,(box,S,p,q,L,F) if L>1e-6: hyper+=1 if len(defect_cases)<50 and j%3==0:defect_cases.append((box,S,abar,bbar,states,M,L,cs)) report['fixed_resource_random']={'profiles':count,'hyperbolic':hyper,'max_bound_excess':worst,'max_mean_error':meanerr} errors=[];mindef=math.inf for box,S,abar,bbar,states,M,L,cs in defect_cases: vals,vecs=np.linalg.eig(M);x=np.real(vecs[:,np.argmax(np.abs(vals))]);theta0=math.atan2(-x[1],x[0]);theta=theta0 for a,b,t in states: w=math.sqrt(b/a);theta=lifted_phase_map(lifted_phase_map(theta,w)+math.sqrt(a*b)*t,w,True) n=round((theta-theta0)/math.pi) cells=[c for j,c in cs if j==n];assert cells opt=cells[0];dual,_,switches=resource_dual_parameters(box,opt);eta,xi,zeta=dual corners=[(a,b) for a in [box.amin,box.amax] for b in [box.bmin,box.bmax]] def defect(th,a,b): c,s=math.cos(th),math.sin(th) H=lambda aa,bb:((bb-aa)*s*c-eta-xi*aa-zeta*bb)/(bb*c*c+aa*s*s) return max(H(*ab) for ab in corners)-H(a,b) integral=0.;theta=theta0 for a,b,t in states: w=math.sqrt(b/a);end=lifted_phase_map(lifted_phase_map(theta,w)+math.sqrt(a*b)*t,w,True) points=[] for z in switches: z%=math.pi for j in range(math.floor(theta/math.pi)-1,math.ceil(end/math.pi)+2): zz=z+j*math.pi if theta+1e-12