File size: 4,324 Bytes
3b80460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Exact one-arc cosine-transform mass-budget relaxation.

Coordinates are on R/(pi Z).  The shifted orientation measure mu obeys
H'' + H = mu; equivalently H(t) = .5 integral |sin(t-u)| dmu(u).
The support function H is at least 1, and its integral is total mass L.
The numerical routines illustrate a proved continuum theorem.  They do not
test geometric opacity or construct an opaque barrier.
"""
from dataclasses import dataclass
import math
import numpy as np
from scipy.optimize import brentq


@dataclass(frozen=True)
class SoftGapSolution:
    beta: float
    eta: float
    regime: str
    value: float
    critical_budget: float
    plateau_halfwidth: float
    cap_width: float
    dual_multiplier: float


def solve_soft_gap(beta, eta):
    """Return the exact formula, with one scalar root in the middle regime."""
    beta, eta = float(beta), float(eta)
    if not 0.0 < beta < math.pi / 2:
        raise ValueError("beta must lie strictly between 0 and pi/2")
    if eta < 0 or not math.isfinite(eta):
        raise ValueError("eta must be finite and nonnegative")
    critical = 2 * math.tan(beta / 2)
    if eta <= critical:
        b = 2*math.sin(beta/2)**2 / math.cos(beta)
        value = math.pi + 2 * (math.tan(beta) - beta) - b * eta
        return SoftGapSolution(beta, eta, "central atom", value, critical,
                               0.0, beta, b)
    if eta < 2 * beta:
        s = brentq(lambda x: 2*x + 2*math.tan((beta-x)/2) - eta,
                   0.0, beta, xtol=5e-15)
        d = beta - s
        value = math.pi + 4 * math.tan(d / 2) - 2 * d
        return SoftGapSolution(beta, eta, "contact plateau", value, critical,
                               s, d, 2*math.sin(d/2)**2 / math.cos(d))
    return SoftGapSolution(beta, eta, "uniform", math.pi, critical,
                           beta, 0.0, 0.0)


def circle_coordinate(t):
    return (np.asarray(t, dtype=float) + math.pi/2) % math.pi - math.pi/2


def extremizer_h(t, solution):
    """The primal continuum extremizer H, including all regimes."""
    a = np.abs(circle_coordinate(t))
    z = np.full_like(a, 1.0)
    p = solution
    if p.regime == "central atom":
        mask = a < p.beta
        A = (2 - p.eta*math.sin(p.beta)) / math.cos(p.beta)
        z = np.where(mask, .5*(A*np.cos(a) + p.eta*np.sin(a)), z)
    elif p.regime == "contact plateau":
        mask = (a > p.plateau_halfwidth) & (a < p.beta)
        cap = np.cos(a-(p.plateau_halfwidth+p.beta)/2) / math.cos(p.cap_width/2)
        z = np.where(mask, cap, z)
    return z


def dual_w(t, solution):
    """The supported dual test function 0 <= w <= b*1_I."""
    a = np.abs(circle_coordinate(t))
    p = solution
    if p.regime == "uniform":
        return np.zeros_like(a)
    d, s = p.cap_width, p.plateau_halfwidth
    numerator = np.cos(np.maximum(a-s, 0.0)) - math.cos(d)
    denominator = math.cos(d)
    return np.where(a >= p.beta, 0.0,
                    np.where(a <= s, p.dual_multiplier, numerator/denominator))


def measure_decomposition(solution):
    """Return uniform-density intervals and (position, mass) atoms of mu.

    Arc endpoints +/-beta are excluded from the budget.  Zero-width
    intervals and coincident zero atoms have no effect.
    """
    p = solution
    if p.regime == "uniform":
        return [(-math.pi/2, math.pi/2)], []
    intervals = [(-math.pi/2, -p.beta), (p.beta, math.pi/2)]
    if p.regime == "central atom":
        edge = math.tan(p.beta) - p.eta/(2*math.cos(p.beta))
        atoms = [(-p.beta, edge), (0.0, p.eta), (p.beta, edge)]
    else:
        s = p.plateau_halfwidth
        if s > 0:
            intervals.append((-s, s))
        mass = math.tan(p.cap_width/2)
        atoms = [(-p.beta, mass), (-s, mass), (s, mass), (p.beta, mass)]
    return intervals, atoms


def mass_in_open_arc(solution):
    intervals, atoms = measure_decomposition(solution)
    beta = solution.beta
    diffuse = sum(max(0.0, min(b,beta)-max(a,-beta)) for a,b in intervals)
    atomic = sum(m for x,m in atoms if -beta < x < beta)
    return diffuse + atomic


def total_mass(solution):
    intervals, atoms = measure_decomposition(solution)
    return sum(b-a for a,b in intervals) + sum(m for x,m in atoms)


def value_curve(beta, budgets):
    return np.array([solve_soft_gap(beta, eta).value for eta in budgets])