File size: 3,351 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
"""Exact phase architecture for A(a,b)=[[0,a],[-b,0]], positive rectangles.

All numerical root solutions and quadratures are non-certified floats.
"""
from __future__ import annotations
from dataclasses import dataclass
import math
import numpy as np
from scipy.integrate import quad
from scipy.optimize import brentq

@dataclass(frozen=True)
class Rectangle:
    amin:float
    amax:float
    bmin:float
    bmax:float
    def __post_init__(self):
        if not all(map(math.isfinite,(self.amin,self.amax,self.bmin,self.bmax))):
            raise ValueError('Finite bounds required.')
        if not (0<self.amin<=self.amax and 0<self.bmin<=self.bmax):
            raise ValueError('Require positive ordered bounds.')
        if self.amin==self.amax and self.bmin==self.bmax:
            raise ValueError('Constant rectangle has zero growth; handle separately.')

@dataclass(frozen=True)
class RectangleCell:
    eta:float
    period:float
    log_gain:float
    # chronological angle arcs: (theta_start,theta_end,a,b,duration)
    arcs:tuple


def phase_speed(theta,a,b):
    return b*math.cos(theta)**2+a*math.sin(theta)**2


def generator_transfer(a:float,b:float,t:float)->np.ndarray:
    if min(a,b)<=0 or t<0:
        raise ValueError('a,b positive and t nonnegative required.')
    w=math.sqrt(a*b)
    c,s=math.cos(w*t),math.sin(w*t)
    return np.array([[c,a*s/w],[-b*s/w,c]])


def rectangle_cell(box:Rectangle,eta:float)->RectangleCell:
    am,ap,bm,bp=box.amin,box.amax,box.bmin,box.bmax
    if eta>0:
        cuts=[0,math.atan2(bp,eta),math.pi/2,math.pi-math.atan2(eta,ap),math.pi]
        states=[(am,bp),(ap,bp),(ap,bm),(ap,bp)]
    elif eta<0:
        e=-eta
        cuts=[0,math.atan2(e,am),math.pi/2,math.pi-math.atan2(bm,e),math.pi]
        states=[(am,bm),(am,bp),(am,bm),(ap,bm)]
    else:
        cuts=[0,math.pi/2,math.pi]
        states=[(am,bp),(ap,bm)]
    arcs=[]; T=0.; G=0.
    for x,y,(a,b) in zip(cuts[:-1],cuts[1:],states):
        h=quad(lambda t:1/phase_speed(t,a,b),x,y,epsabs=1e-12,epsrel=1e-12)[0]
        G+=0.5*math.log(phase_speed(x,a,b)/phase_speed(y,a,b))
        T+=h
        arcs.append((x,y,a,b,h))
    return RectangleCell(eta,T,G,tuple(arcs))


def rectangle_fixed(box:Rectangle,S:float):
    if not math.isfinite(S) or S<=0: raise ValueError('S must be positive and finite.')
    slow=math.sqrt(box.amin*box.bmin);fast=math.sqrt(box.amax*box.bmax)
    candidates=[]
    for n in range(1,math.floor(fast*S/math.pi)+1):
        tau=S/n
        if not math.pi/fast < tau < math.pi/slow:continue
        K=1.
        while rectangle_cell(box,-K).period < tau or rectangle_cell(box,K).period > tau:
            K*=2
            if K>1e12:raise ArithmeticError('Endpoint requires higher precision.')
        eta=brentq(lambda e:rectangle_cell(box,e).period-tau,-K,K,xtol=1e-12)
        cell=rectangle_cell(box,eta)
        candidates.append((n,cell))
    return max((n*c.log_gain for n,c in candidates),default=0.),candidates


def rectangle_free(box:Rectangle)->RectangleCell:
    hi=1.
    fun=lambda eta:rectangle_cell(box,eta).log_gain-eta*rectangle_cell(box,eta).period
    while fun(hi)>0:hi*=2
    root=brentq(fun,0,hi,xtol=1e-13)
    return rectangle_cell(box,root)


def cell_monodromy(cell:RectangleCell):
    P=np.eye(2)
    for _,_,a,b,t in cell.arcs:P=generator_transfer(a,b,t)@P
    return P