File size: 4,829 Bytes
9755170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""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 1<mean<R*R):raise ValueError('Strict contrast and interior mean required.')
    p=(mean-1)/(R*R-1)
    end=min(math.pi/(R*p),math.pi/(1-p))
    def f(t):return math.tan(R*p*t/2)*math.tan((1-p)*t/2)
    hi=np.nextafter(end,0.)
    lo=brentq(lambda t:f(t)-1/R,0,hi,xtol=1e-13)
    up=brentq(lambda t:f(t)-R,0,hi,xtol=1e-13)
    return lo,up


def resource_free(R:float,mean:float)->ResourceCandidate:
    """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))