File size: 8,201 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""Sharp small-inflation cost when a circle must be retained.

The old component is C=boundary(r D).  An admissible finite segment network A
must make C union A connected and its convex hull contain (r+delta) D.
Only the length of A outside the old circle is charged.  This is an
augmentation problem: it does not concern replacement of the old component.

Analytic conclusions (proved in tmp/sharp_augmentation_proof.tex):
    S >= 2*sqrt(2*r*delta),
    inf S / (pi*sqrt(2*r*delta)) -> 1 as delta/r -> 0.
The scalar lower bound below and radial-spoke upper bound sandwich inf S.
Floating-point values are diagnostics, not interval-certified optima over all A.
"""

from __future__ import annotations

import math
import numpy as np
from scipy.optimize import brentq

PI = math.pi


def _parameters(radius, inflation):
    radius, inflation = float(radius), float(inflation)
    if not math.isfinite(radius) or radius <= 0:
        raise ValueError("radius must be finite and positive")
    if not math.isfinite(inflation) or inflation <= 0:
        raise ValueError("inflation must be finite and positive")
    return radius, inflation


def covered_normal_width(length, radius=1.0, inflation=0.01):
    """Upper bound on normals served by one anchored exterior component.

    A connected exterior piece of length ell has a circle anchor q; every x
    in that piece has |x-q|<=ell.  Thus u.x>=r+delta requires
    u.q>=r+delta-ell.  The resulting arc width is this scalar expression.
    """
    radius, inflation = _parameters(radius, inflation)
    length = float(length)
    if not math.isfinite(length) or length < 0:
        raise ValueError("length must be finite and nonnegative")
    if length <= inflation:
        return 0.0
    if length >= inflation + 2 * radius:
        return 2 * PI
    return 2 * math.acos(1 - (length - inflation) / radius)


def elementary_lower_bound(radius=1.0, inflation=0.01):
    radius, inflation = _parameters(radius, inflation)
    return 2 * math.sqrt(2 * radius * inflation)


def scalar_lower_bound(radius=1.0, inflation=0.01):
    """Evaluate the exact one-variable lower-bound formula numerically.

    M=sup_{ell>0} w(ell)/ell and S>=2*pi/M.  Writing
    ell=r*(a+1-cos(phi)) reduces this to
        pi*r*min_{0<phi<=pi} (a+1-cos(phi))/phi.
    Its only possible interior minimum is the first root of
    phi*sin(phi)+cos(phi)-1=a in (0,pi/2]; the endpoint pi is also checked.
    The global reduction is analytic; the reported root/value use doubles.
    """
    radius, inflation = _parameters(radius, inflation)
    a = inflation / radius
    candidates = [(PI, a + 2)]
    if a < PI / 2 - 1:
        def equation(phi):
            return phi * math.sin(phi) - 2 * math.sin(phi / 2) ** 2 - a
        phi = brentq(equation, 0.0, PI / 2, xtol=1e-15, rtol=1e-14)
        candidates.append((phi, a + 2 * math.sin(phi / 2) ** 2))
    phi, denominator = min(candidates, key=lambda z: z[1] / z[0])
    value = PI * radius * denominator / phi
    return {"lower_bound": value, "max_width_per_length": 2 * PI / value,
            "maximizing_component_length": radius * denominator,
            "maximizing_phi": phi,
            "evaluation": "analytic scalar reduction; floating-point root"}


def spoke_cost(count, radius=1.0, inflation=0.01):
    radius, inflation = _parameters(radius, inflation)
    if isinstance(count, bool) or int(count) != count or count < 3:
        raise ValueError("count must be an integer at least three")
    count = int(count)
    x = PI / count
    sec_minus_one = 2 * math.sin(x / 2) ** 2 / math.cos(x)
    return count * (inflation + (radius + inflation) * sec_minus_one)


def optimal_spoke_count(radius=1.0, inflation=0.01):
    """Integer optimum within equal radial-arm constructions only.

    The continuous cost N*((r+delta)*sec(pi/N)-r) is strictly convex for N>=3.
    A floating-point derivative root brackets the two candidate integers.
    """
    radius, inflation = _parameters(radius, inflation)

    def derivative(count):
        x = PI / count
        sec_minus_one = 2 * math.sin(x / 2) ** 2 / math.cos(x)
        return inflation + (radius + inflation) * (
            sec_minus_one - x * math.tan(x) / math.cos(x))

    upper = 6.0
    while derivative(upper) < 0:
        upper *= 2
    continuous = brentq(derivative, 3.0, upper, xtol=1e-10, rtol=1e-13)
    candidates = sorted({3, max(3, math.floor(continuous)), math.ceil(continuous)})
    count = min(candidates, key=lambda n: spoke_cost(n, radius, inflation))
    return {"count": count, "cost": spoke_cost(count, radius, inflation),
            "continuous_count": continuous,
            "scope": "equal radial-arm constructions"}


def radial_arms(count, radius=1.0, inflation=0.01, rotation=0.0):
    """Finite connected augmentation with an enclosing regular tip polygon."""
    radius, inflation = _parameters(radius, inflation)
    spoke_cost(count, radius, inflation)  # validate the integer count
    count = int(count)
    theta = float(rotation) + 2 * PI * np.arange(count) / count
    directions = np.column_stack((np.cos(theta), np.sin(theta)))
    tip_radius = (radius + inflation) / math.cos(PI / count)
    roots, tips = radius * directions, tip_radius * directions
    return {"segments": np.stack((roots, tips), axis=1),
            "roots": roots, "tips": tips, "tip_radius": tip_radius,
            "count": count, "radius": radius, "inflation": inflation,
            "cost_formula": spoke_cost(count, radius, inflation)}


def make_figure(path_png, path_pdf):
    import matplotlib
    matplotlib.use("Agg")
    import matplotlib.pyplot as plt

    plt.rcParams.update({"font.size": 10, "axes.spines.top": False,
                         "axes.spines.right": False, "figure.dpi": 160})
    fig, axes = plt.subplots(1, 3, figsize=(12.1, 3.5), layout="constrained")
    ax = axes[0]
    delta = 0.025
    optimum = optimal_spoke_count(1, delta)
    data = radial_arms(optimum["count"], 1, delta)
    theta = np.linspace(0, 2 * PI, 800)
    ax.plot(np.cos(theta), np.sin(theta), color="#283b59", lw=1.6,
            label="retained circle")
    ax.plot((1 + delta) * np.cos(theta), (1 + delta) * np.sin(theta),
            color="#cc6545", lw=1, ls="--", label="inflated disk")
    for segment in data["segments"]:
        ax.plot(segment[:, 0], segment[:, 1], color="#167f86", lw=2)
    tips = np.vstack((data["tips"], data["tips"][0]))
    ax.plot(tips[:, 0], tips[:, 1], color="#167f86", lw=.7, alpha=.7)
    ax.set_aspect("equal")
    ax.set_title("A  Retain the circle; add radial arms", loc="left", fontsize=10)
    ax.set_xlim(-1.13, 1.13); ax.set_ylim(-1.13, 1.13)
    ax.set_xticks([-1, 0, 1]); ax.set_yticks([-1, 0, 1])
    ax.legend(frameon=False, loc="lower left", fontsize=8)

    ratios = np.geomspace(1e-6, .1, 100)
    scale = PI * np.sqrt(2 * ratios)
    lower = np.array([scalar_lower_bound(1, d)["lower_bound"] for d in ratios])
    spoke = [optimal_spoke_count(1, d) for d in ratios]
    upper = np.array([row["cost"] for row in spoke])
    ax = axes[1]
    ax.semilogx(ratios, lower / scale, color="#283b59", lw=2,
                label="scalar lower bound")
    ax.semilogx(ratios, upper / scale, color="#167f86", lw=2,
                label="radial-arm upper bound")
    ax.axhline(1, color=".5", ls=":", lw=1)
    ax.set_xlabel(r"relative inflation $\delta/r$")
    ax.set_ylabel(r"length $/\,\pi\sqrt{2r\delta}$")
    ax.set_title("B  Matching sharp asymptotic", loc="left", fontsize=10)
    ax.legend(frameon=False, fontsize=8)
    ax.grid(alpha=.15)

    ax = axes[2]
    counts = np.array([row["count"] for row in spoke])
    ax.semilogx(ratios, counts * np.sqrt(ratios), color="#167f86", lw=1.7,
                label=r"integer-optimal $N\sqrt{\delta/r}$")
    ax.axhline(PI / math.sqrt(2), color="#283b59", ls="--", lw=1.2,
               label=r"limit $\pi/\sqrt{2}$")
    ax.set_xlabel(r"relative inflation $\delta/r$")
    ax.set_ylabel("scaled arm count")
    ax.set_title("C  Arm count grows as inverse square root", loc="left", fontsize=10)
    ax.legend(frameon=False, fontsize=8)
    ax.grid(alpha=.15)
    fig.savefig(path_png, dpi=200)
    fig.savefig(path_pdf)
    plt.close(fig)