"""Sharp uniform finite-angle opacity audits for the unit disk. The construction consists of alternate edges of an inscribed regular 2n-gon. It blocks every required line at normals k*pi/n and has the exact optimal sampled length 2n*sin(pi/(2n)). It is NOT a fully opaque barrier. Its centered opaque radius is zero, and its missed required-line measure tends to pi/2. The formulas are analytic mathematical identities; this implementation evaluates them and geometric tests in ordinary double precision. """ from __future__ import annotations import math import numpy as np PI = math.pi def _validate_n(n): if isinstance(n, bool) or int(n) != n or n < 2: raise ValueError("n must be an integer at least two") return int(n) def alternating_polygon(n): """Return n selected segments, array shape (n,2,2), inside the unit disk.""" n = _validate_n(n) angle = np.arange(2 * n) * PI / n vertices = np.column_stack((np.cos(angle), np.sin(angle))) return np.stack((vertices[::2], vertices[1::2]), axis=1) def sampled_length(n): n = _validate_n(n) return 2 * n * math.sin(PI / (2 * n)) def missed_line_measure(n): """Integral over normal angles of missed offsets in [-1,1].""" n = _validate_n(n) a = PI / (2 * n) return 2 * PI - 2 * n * (math.sin(a) + math.tan(a / 2)) def covered_line_measure(n): n = _validate_n(n) a = PI / (2 * n) return 2 * n * (math.sin(a) + math.tan(a / 2)) def exact_pair_overlap_sum(n): n = _validate_n(n) a = PI / (2 * n) return 2 * n * (math.sin(a) - math.tan(a / 2)) def projection_intervals(segments, theta): segments = np.asarray(segments, dtype=float) if segments.ndim != 3 or segments.shape[1:] != (2, 2): raise ValueError("segments must have shape (n,2,2)") normal = np.array([math.cos(theta), math.sin(theta)]) projected = segments @ normal return np.sort(projected, axis=1) def interval_union(intervals): """Closed interval union; degenerate intervals are retained.""" intervals = sorted((float(a), float(b)) for a, b in intervals) if not intervals: return [] merged = [list(intervals[0])] for a, b in intervals[1:]: if a <= merged[-1][1]: merged[-1][1] = max(merged[-1][1], b) else: merged.append([a, b]) return merged def missed_length_at_angle(segments, theta): intervals = projection_intervals(segments, theta) clipped = [(max(-1.0, a), min(1.0, b)) for a, b in intervals if b >= -1.0 and a <= 1.0] covered = math.fsum(b - a for a, b in interval_union(clipped)) return max(0.0, 2.0 - covered) def maximal_required_gap(segments, theta): """Largest interval of required offsets not covered (end gaps included).""" union = interval_union(projection_intervals(segments, theta)) right = -1.0 gap = 0.0 for a, b in union: if b < -1.0 or a > 1.0: continue gap = max(gap, max(0.0, min(1.0, a) - right)) right = max(right, min(1.0, b)) return max(gap, max(0.0, 1.0 - right)) def sampled_cosine_maximum(n): """Independent analytic-cell evaluation of max_phi sum_k |cos(phi-k*pi/n)|. Sign-change cells determine vectors (C,S); on each cell the sum is C*cos(phi)+S*sin(phi). Its amplitude bounds that entire cell. A supplied maximizing angle verifies attainment of the largest amplitude. """ n = _validate_n(n) normals = np.arange(n) * PI / n raw_events = np.sort(np.r_[0.0, (normals + PI / 2) % PI, PI]) events = [float(raw_events[0])] for event in raw_events[1:]: if event - events[-1] > 1e-12: events.append(float(event)) amplitudes = [] for a, b in zip(events[:-1], events[1:]): midpoint = (a + b) / 2 signs = np.sign(np.cos(midpoint - normals)) coefficient_c = float(signs @ np.cos(normals)) coefficient_s = float(signs @ np.sin(normals)) amplitudes.append(math.hypot(coefficient_c, coefficient_s)) witness = 0.0 if n % 2 else PI / (2 * n) achieved = float(np.abs(np.cos(witness - normals)).sum()) return {"upper_bound": max(amplitudes), "achieved": achieved, "witness_angle": witness, "formula": 1 / math.sin(PI / (2 * n))} def missed_line_witness(n): """An explicit required line which misses every selected segment. Even n: a central line through the midpoint direction of unselected edge1. Odd n: a horizontal line a small distance below the central diameter. A line is represented by unit-normal angle theta and signed offset p. """ n = _validate_n(n) if n % 2 == 0: theta = (3 * PI / (2 * n) + PI / 2) % PI offset = 0.0 else: theta = PI / 2 offset = -0.5 * math.sin(PI / n) projected = projection_intervals(alternating_polygon(n), theta) distances = np.maximum(projected[:, 0] - offset, offset - projected[:, 1]) return {"normal_angle": theta, "offset": offset, "min_distance_to_any_segment": float(distances.min()), "expected_clearance": (math.sin(PI / (2 * n)) if n % 2 == 0 else 0.5 * math.sin(PI / n)), "meets_unit_disk": abs(offset) <= 1, "hits_selected_segment": bool(np.any( (projected[:, 0] <= offset) & (offset <= projected[:, 1])))} def component_defect_bound(sample_count, component_count, containing_radius=1.0): """Necessary bound if all sampled-angle coverage conditions hold. The set must be a union of at most component_count connected compact pieces inside a disk of the specified radius. This function evaluates the bound; it does not establish those hypotheses for arbitrary inputs. """ sample_count = _validate_n(sample_count) if component_count < 1 or containing_radius < 0: raise ValueError("Positive component count and nonnegative radius required") return min(2 * PI, component_count * containing_radius * PI**2 / (2 * sample_count))