File size: 5,143 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
"""Independent arithmetic-engine tests and high-precision scalar diagnostics.

The lower-bound proof is analytic. Its strict numeric hypotheses are checked
by certify_disk_bound.py with exact rational interval arithmetic. This file
tests that engine's square-root enclosures directly against exact rational
squares and evaluates the final formulas independently with Decimal at two
precisions. Decimal evaluations are diagnostics, not the proof certificate.
"""

from decimal import Decimal as D, localcontext
from fractions import Fraction
from pathlib import Path
import json
import random

from certify_disk_bound import I


def engine_checks():
    rng = random.Random(319843)
    count = 0
    for digits in (20, 80, 120):
        for _ in range(100):
            q = Fraction(rng.randrange(0, 10**18), rng.randrange(1, 10**12))
            enclosure = I(q).sqrt(digits)
            assert enclosure.lo**2 <= q <= enclosure.hi**2
            assert enclosure.hi - enclosure.lo <= Fraction(1, 10**digits)
            count += 1
    for q in (Fraction(0), Fraction(1), Fraction(4), Fraction(9, 4),
              Fraction(1, 10**100)):
        enclosure = I(q).sqrt(120)
        assert enclosure.lo**2 <= q <= enclosure.hi**2
        count += 1
    try:
        I(-1).sqrt()
    except AssertionError:
        pass
    else:
        raise AssertionError("negative radical was accepted")
    try:
        I(1) / I(-1, 1)
    except AssertionError:
        pass
    else:
        raise AssertionError("division through zero was accepted")
    return count


def scalar_diagnostics(precision):
    with localcontext() as context:
        context.prec = precision
        r, t, delta = D('.00004057804'), D('.9661517984'), D('2.6864358e-6')
        a = 1 + r
        sqrt2 = D(2).sqrt()
        S, C = (2 - sqrt2).sqrt() / 2, (2 + sqrt2).sqrt() / 2
        w = (a*a - t*t).sqrt()
        eta0 = (1 - t) / 2
        omega = ((a**4 - t*t).sqrt() + w) / (a*a - 1)
        A, B = 2*(t*S - w*C), 2*(t*C + w*S)
        D_neighbor = sqrt2*(t + w)
        denominator = A*A + (eta0+B)**2
        q0 = eta0*eta0*A*A / (4*a*denominator)
        derivative = A*A*eta0*(A*A+B*B+B*eta0)/(2*a*denominator**2)
        def q(value):
            return value*value*A*A/(4*a*(A*A+(value+B)**2))
        def qp(value):
            return A*A*value*(A*A+B*B+B*value)/(2*a*(A*A+(value+B)**2)**2)
        maximum_flow = 2*eta0
        reward = q(4*eta0/3)+q(2*eta0/3)
        reward_derivative = 4*qp(4*eta0/3)/3+2*qp(2*eta0/3)/3
        values = {
            "packing_reward_contradiction_margin": reward-delta,
            "additive_monotonicity_slack": 1-omega*reward_derivative/2,
            "final_eta_positive_for_W_at_most_delta": eta0-omega*delta/2,
            "eta_positive_at_certified_initial_reward": eta0-omega*reward/2,
            "neighbor_branch_dominates_on_whole_eta_interval":
                2*a*(A*A+B*B)-D_neighbor*(A*A+(sqrt2*maximum_flow+B)**2),
            "opposed_second_derivative_positive_on_whole_eta_interval":
                (A*A+B*B)**2-3*(A*A+B*B)*maximum_flow**2-2*B*maximum_flow**3,
            "opposed_localized_projection_slack": 1-t*S-w*C,
            "neighbor_localized_projection_slack": 1-t*C-w*S,
            "near_coefficient_dominates_far": omega-sqrt2/(1-1/(sqrt2*t)),
        }
        assert min(values.values()) > 0
        return {"precision": precision,
                "positive_values": {key: str(value) for key, value in values.items()},
                "certified_initial_packing_reward": str(reward)}


def main():
    root = Path(__file__).parent
    certificate = json.loads((root / 'DISK_BOUND_CERTIFICATE.json').read_text())
    assert certificate['status'] == 'PASS'
    count = engine_checks()
    records = [scalar_diagnostics(80), scalar_diagnostics(120)]
    # Independently evaluate scalar values and compare with the certificate's
    # display strings. A tiny display tolerance is intentional: printed
    # decimals are rounded, while the certificate's interval signs are exact.
    with localcontext() as context:
        context.prec = 130
        for record in records:
            for key, scalar in record['positive_values'].items():
                lo, hi = map(D, certificate['positive_intervals'][key])
                value = D(scalar)
                tolerance = max(D('1e-80'), abs(value)*D('1e-43'))
                assert lo-tolerance <= value <= hi+tolerance, key
    output = {
        "status": "PASS",
        "scope": "Arithmetic-engine validation and independent high-precision scalar diagnostics of the complete disk-bound certificate.",
        "summary": {"all_assertions_passed": True, "exact_sqrt_enclosure_cases": count,
                    "decimal_precisions": [80, 120],
                    "negative_radical_rejected": True,
                    "division_through_zero_rejected": True,
                    "all_final_scalar_margins_positive": True},
        "diagnostics": records}
    (root / 'DISK_BOUND_VERIFICATION.json').write_text(json.dumps(output, indent=2)+'\n')
    print(json.dumps(output['summary'], indent=2))


if __name__ == '__main__':
    main()