"""Fixed-period, fixed-mean exact-formula optimizer (floating-point evaluator). The proofs cover measurable controls. This code evaluates their finite formulas; it does not certify signs/ties extremely near resonance or supply interval proofs. """ from __future__ import annotations from dataclasses import dataclass import math import numpy as np from floquet import transfer, phase_speed @dataclass(frozen=True) class ResourceCandidate: winding: int high_time: float low_time: float discriminant: float log_gain: float def resource_optimum(R: float, S: float, mean: float) -> tuple[float, list[ResourceCandidate]]: if not all(map(math.isfinite, (R,S,mean))) or R <= 1 or S <= 0: raise ValueError('Require finite R > 1, S > 0 and mean in [1,R^2].') B = R*R if not 1 <= mean <= B: raise ValueError('Mean is outside the material interval.') if mean in (1.0,B): return 0.0, [] duty = (mean-1)/(B-1) candidates = [] for n in range(1, math.floor(R*S/math.pi)+1): tau = S/n if not math.pi/R < tau < math.pi: continue h,ell = duty*tau,(1-duty)*tau if R*h >= math.pi or ell >= math.pi: continue D = math.cos(R*h)*math.cos(ell) - (R+1/R)/2*math.sin(R*h)*math.sin(ell) if D < -1: candidates.append(ResourceCandidate(n,h,ell,D,n*math.acosh(-D))) return max((c.log_gain for c in candidates),default=0.0),candidates def lifted_phase_map(theta: float, w: float, inverse: bool=False) -> float: """Lift of atan(tan(theta)/w), or its inverse, modulo pi.""" j = math.floor((theta+math.pi/2)/math.pi) x = theta-j*math.pi factor = w if inverse else 1/w return math.atan(factor*math.tan(x))+j*math.pi def advance_phase(theta: float, u: float, duration: float) -> float: w = math.sqrt(u) return lifted_phase_map(lifted_phase_map(theta,w)+w*duration,w,True) def dual_parameters(R: float, candidate: ResourceCandidate) -> tuple[float,float,float,float]: """Return eta, xi, high-arc start/end angles from the expanding eigenline. Assumes a strictly hyperbolic candidate; conditioning degrades at gap edges. """ B = R*R P = transfer(1,candidate.low_time) @ transfer(B,candidate.high_time) vals,vecs = np.linalg.eig(P) x = np.real(vecs[:,int(np.argmax(np.abs(vals)))]) theta0 = math.atan2(-x[1],x[0]) % math.pi theta1 = advance_phase(theta0,B,candidate.high_time) rows = np.array([[math.cos(t)**2,-math.sin(t)**2] for t in (theta0,theta1)]) rhs = np.array([-math.sin(t)*math.cos(t) for t in (theta0,theta1)]) eta,xi = np.linalg.solve(rows,rhs) if 1+4*eta*xi <= 0: raise ArithmeticError('Unresolved/nonhyperbolic switching discriminant.') return float(eta),float(xi),theta0,theta1 def switching(theta: float, eta: float, xi: float) -> float: c,s = math.cos(theta),math.sin(theta) return s*c+eta*c*c-xi*s*s def selector(theta: float, R: float, eta: float, xi: float) -> float: return R*R if switching(theta,eta,xi)>0 else 1.0 def resource_defect(theta: float, u: float, R: float, eta: float, xi: float) -> float: b = selector(theta,R,eta,xi) return abs(switching(theta,eta,xi))*abs(u-b)/(phase_speed(theta,u)*phase_speed(theta,b)) def phase_stability_constant(R:float,candidate:ResourceCandidate) -> float: eta,xi,_,_ = dual_parameters(R,candidate) return 2*math.pi*R*R*math.sqrt(candidate.winding*(R*R-1))/(1+4*eta*xi)**0.25 def resource_gap_interval(R:float,mean:float)->tuple[float,float]: """Unique one-turn hyperbolic period interval (tau_minus,tau_plus).""" from scipy.optimize import brentq if not (R>1 and 1ResourceCandidate: """Unique maximum of G(tau)/tau. Strict concavity proves uniqueness.""" from scipy.optimize import brentq p=(mean-1)/(R*R-1) lo,hi=resource_gap_interval(R,mean) A=(R+1/R)/2 def q(t): x,y=R*p*t,(1-p)*t D=math.cos(x)*math.cos(y)-A*math.sin(x)*math.sin(y) Dp=-R*p*math.sin(x)*math.cos(y)-(1-p)*math.cos(x)*math.sin(y) \ -A*(R*p*math.cos(x)*math.sin(y)+(1-p)*math.sin(x)*math.cos(y)) G=math.acosh(-D) Gp=-Dp/math.sqrt(D*D-1) return t*Gp-G cut=(hi-lo)*1e-8 tau=brentq(q,lo+cut,hi-cut,xtol=1e-13) D=math.cos(R*p*tau)*math.cos((1-p)*tau)-A*math.sin(R*p*tau)*math.sin((1-p)*tau) return ResourceCandidate(1,p*tau,(1-p)*tau,D,math.acosh(-D))