Download code/maxwell_rectangle.py from PureOne/eve-floquet-defect-rigidity: direct link, hf CLI and curl.
- Browser
- Download file 3.35 kB
-
https://huggingface.co/datasets/PureOne/eve-floquet-defect-rigidity/resolve/main/code/maxwell_rectangle.py
- Command line
-
hf download hf://datasets/PureOne/eve-floquet-defect-rigidity/code/maxwell_rectangle.py
-
curl -L -o maxwell_rectangle.py https://huggingface.co/datasets/PureOne/eve-floquet-defect-rigidity/resolve/main/code/maxwell_rectangle.py
3.35 kB
| """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 | |
| 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.') | |
| 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 | |