"""Reproduce the exact proof schematic and its stated finite checks."""
from pathlib import Path
SVG='<svg xmlns="http://www.w3.org/2000/svg" width="1200" height="970" viewBox="0 0 1200 970" role="img" aria-labelledby="fp-title fp-desc">\n<title id="fp-title">Two sufficient routes to an exact finite whole-tunnel partition</title>\n<desc id="fp-desc">A selected finite square can be completed by a positive residual trace certificate or by a small cut and overlap certificate. The unrestricted input from amenability remains unproved. Boxes and arrows encode proof steps, not traces or geometry.</desc>\n<defs><marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="8" markerHeight="8" orient="auto-start-reverse"><path d="M0 0L10 5L0 10Z" fill="#334155"/></marker></defs>\n<rect width="1200" height="970" fill="#ffffff"/>\n<g font-family="Arial, DejaVu Sans, sans-serif" fill="#14263c">\n<text x="600" y="38" text-anchor="middle" font-size="25" font-weight="700">Two sufficient routes to a finite whole-tunnel partition</text>\n<text x="600" y="68" text-anchor="middle" font-size="17">Actual finite-index II₁ inclusion; finite targets Y; ε &gt; 0</text>\n<rect x="110" y="94" width="980" height="110" rx="14" fill="#f1f5f9" stroke="#64748b" stroke-width="2"/>\n<text x="600" y="122" text-anchor="middle" font-size="19" font-weight="700">76.4: selected whole-tunnel blocks and physical residual f</text>\n<text x="600" y="151" text-anchor="middle" font-size="17">P₀, Q₀; old physical supports are orthogonal; target error &lt; ε/2</text>\n<text x="600" y="180" text-anchor="middle" font-size="17">Each selected block has its complete actual finite whole-tunnel pair</text>\n<path d="M370 204L300 258" fill="none" stroke="#334155" stroke-width="2.5" marker-end="url(#arrow)"/>\n<path d="M830 204L900 258" fill="none" stroke="#334155" stroke-width="2.5" marker-end="url(#arrow)"/>\n<rect x="200" y="216" width="360" height="30" fill="#ffffff"/>\n<text x="220" y="235" font-size="15" font-weight="700">if the positive certificate is supplied</text>\n<rect x="680" y="216" width="450" height="30" fill="#ffffff"/>\n<text x="705" y="235" font-size="15" font-weight="700">if the small-cost certificate is supplied</text>\n<rect x="40" y="264" width="535" height="145" rx="14" fill="#eaf2ff" stroke="#3566a3" stroke-width="2"/>\n<text x="60" y="292" font-size="19" font-weight="700">Route A: exact positive residual certificate</text>\n<text x="60" y="322" font-size="17">τ(f) ∈ Γ₊ ⇔ finite positive trace certificate   [FP.9]</text>\n<text x="60" y="352" font-size="16">j ≥ m; z ∈ Z^(s_j); ωⱼ·z = 0; vⱼ+z ≥ 0</text>\n<text x="60" y="382" font-size="16">All-trace σ(vⱼ+z) &gt; 0 suffices [FP.10]; no upper test</text>\n<rect x="625" y="264" width="535" height="145" rx="14" fill="#e9f8ef" stroke="#327650" stroke-width="2"/>\n<text x="645" y="292" font-size="19" font-weight="700">Route B: small cut / ordered overlap certificate</text>\n<text x="645" y="322" font-size="17">Δⱼ ≤ ℰⱼ ≤ qΔⱼ   [FP.12]</text>\n<text x="645" y="352" font-size="17">ℰ &lt; [ε / (2(1+√2)R)]²   [FP.16]</text>\n<text x="645" y="382" font-size="16">R = max(1, max of target norms); overlap is ordered</text>\n<path d="M307 409V447" fill="none" stroke="#334155" stroke-width="2.5" marker-end="url(#arrow)"/>\n<path d="M893 409V447" fill="none" stroke="#334155" stroke-width="2.5" marker-end="url(#arrow)"/>\n<rect x="40" y="454" width="535" height="158" rx="14" fill="#f5f9ff" stroke="#3566a3" stroke-width="2"/>\n<text x="60" y="483" font-size="17" font-weight="700">FP.3: split ranks into finitely many bounded vectors</text>\n<text x="60" y="512" font-size="16">FP.5–FP.7: choose physical residual cells fₐ ≤ f</text>\n<text x="60" y="540" font-size="16">and place each in its own actual finite tunnel.</text>\n<text x="60" y="568" font-size="16">Old whole-stage blocks stay fixed; P₀ ⊂ P*.</text>\n<text x="60" y="595" font-size="16">Canonical certificate pieces need not be orthogonal.</text>\n<rect x="625" y="454" width="535" height="158" rx="14" fill="#f4fcf7" stroke="#327650" stroke-width="2"/>\n<text x="645" y="483" font-size="17" font-weight="700">80.3–80.4: optimal cuts remove total trace Δⱼ</text>\n<text x="645" y="512" font-size="16">Enlarge the physical residual by the removed pieces.</text>\n<text x="645" y="540" font-size="16">Complement ranks certify that new residual exactly.</text>\n<text x="645" y="568" font-size="16">Extra target error ≤ (1+√2)‖y‖√Δⱼ.</text>\n<text x="645" y="595" font-size="16">Cut old supports may change; targets remain fixed.</text>\n<path d="M307 612V635L480 666" fill="none" stroke="#334155" stroke-width="2.5" marker-end="url(#arrow)"/>\n<path d="M893 612V635L720 666" fill="none" stroke="#334155" stroke-width="2.5" marker-end="url(#arrow)"/>\n<rect x="75" y="675" width="1050" height="113" rx="14" fill="#eef2ff" stroke="#4f46a0" stroke-width="2"/>\n<text x="600" y="704" text-anchor="middle" font-size="20" font-weight="700">Finite full support partition: sum of physical supports = 1</text>\n<text x="600" y="734" text-anchor="middle" font-size="17">Every cell has its complete supported actual whole-tunnel pair</text>\n<text x="600" y="764" text-anchor="middle" font-size="17">E_N E_{P_*} = E_{P_*} E_N = E_{Q_*}; target error &lt; ε   [FP.6–FP.7 / FP.16]</text>\n<rect x="75" y="827" width="1050" height="115" rx="14" fill="#fff5e9" stroke="#b86b1f" stroke-width="2" stroke-dasharray="8 5"/>\n<text x="600" y="855" text-anchor="middle" font-size="20" font-weight="700">Remaining unrestricted input: not proved here</text>\n<text x="600" y="883" text-anchor="middle" font-size="17">Amenability must supply a positive certificate, a sufficiently cheap actual selection,</text>\n<text x="600" y="909" text-anchor="middle" font-size="17">or another complete construction of the original finite partition [FP.0–FP.1].</text>\n<text x="600" y="932" text-anchor="middle" font-size="14">Positions, areas and colors carry no trace, rank or geometric measurement.</text>\n</g></svg>'
Path(__file__).with_suffix(".svg").write_bytes(SVG.encode("utf-8"))
from fractions import Fraction
from itertools import product
import random
w = [Fraction(3, 15), Fraction(5, 15), Fraction(7, 15)]
T = {sum((w[i]*h[i] for i in range(3)), Fraction())
     for h in product(range(2), repeat=3)}
t = Fraction(2, 5)
assert t not in T and 2*w[0] == t and 3*w[0]+t == 1
assert 6*w[0] == 3*(2*w[0])
rng = random.Random(20261005)
cases = 0
for n in range(1, 13):
    for q in range(1, 8):
        for _ in range(20):
            h = [rng.randrange(n+1) for _ in range(q)]
            excess = max(sum(h)-n, 0)
            retained, remaining = h[:], excess
            for i in range(q):
                cut = min(remaining, retained[i])
                retained[i] -= cut
                remaining -= cut
            cursor, projections = 0, []
            for old_rank, new_rank in zip(h, retained):
                core = set(range(cursor, cursor+new_rank))
                cursor += new_rank
                outside = sorted(set(range(n))-core)
                projections.append(core | set(outside[:old_rank-new_rank]))
            energy = sum(len(projections[i] & projections[j])
                         for i in range(q) for j in range(q) if i != j)
            assert excess <= energy <= q*excess
            cases += 1
assert cases == 1680
