File size: 7,340 Bytes
868c741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
"""Independent exact checks for the adjacent-concavity negative direction.

The all-parameter conclusion rests on the written inequalities and analytic
proof. This file checks the endpoint arithmetic, polynomial identities by a
degree-certified grid, and a rational three-dimensional repaired transcript.
"""
from fractions import Fraction as Q
from itertools import product
import json
import argparse
from pathlib import Path


def dot(x, y):
    return sum((a * b for a, b in zip(x, y)), Q(0))


def sub(x, y):
    return tuple(a - b for a, b in zip(x, y))


def jet(t, A, B):
    u = [Q(0), -A, 1 - A, 1 - A - B]
    v = [
        Q(0),
        (-3*A*A*t - 3*A*A - A*t + 4*A - 2*B + 2*t - 1)/(6*t),
        (-A*A*t - A*A + A*t + 2*A - 1)/(2*t),
        Q(-10),
    ]
    w = [
        Q(0),
        A*A*t/2 + A*t/6 - 2*A/3 + B/3 - t/3 + Q(1,6),
        A*A*t/2 + A*A + A*t/6 - 5*A/3 + B/3 - t/3 + Q(2,3),
        A*A*t/2 + 2*A*A + 7*A*t/6 - 11*A/3 + B*B/2 + B/3 - 4*t/3 + Q(5,3),
    ]
    U = [sum(u[:i], Q(0)) for i in range(4)]
    V = [sum(v[:i], Q(0)) for i in range(4)]
    J, K = {}, {}
    for i, j in product(range(4), repeat=2):
        if i == j:
            continue
        J[i,j] = w[i]-w[j]+t*((i-j)*v[j]+V[i]-V[j]+u[j]*(U[i]-U[j]))-(u[i]-u[j])**2/2
        K[i,j] = t*v[j]*(V[i]-V[j])-(v[i]-v[j])**2/2
    return u, v, w, U, V, J, K


def leading_formulas(t, A, B):
    return {
        (0,1): Q(0), (0,2): Q(0), (2,0): Q(0), (3,1): Q(0), (3,2): Q(0),
        (1,0): (3*A*A*(t-1)+A*(t-4)+2*B-2*t+1)/6,
        (1,2): (2*A*(t-1)+B-t-1)/3,
        (2,1): (2*t-1-A*(t-1)-2*B)/3,
        (3,0): (A-1)*(2*t-1-A*(t-1)-2*B)/2,
        (0,3): -(3*A*A*(t+1)+4*A*B*t+2*A*B-3*A*t-6*A+2*B*B-2*B*t-2*B-60*t+3)/2,
        (1,3): -(3*A*A*(t+1)+6*A*B*t-5*A*t-4*A+3*B*B-3*B*t-4*B-59*t+4)/3,
        (2,3): -(A*A*(t+1)+2*A*B*t-A*t-2*A+2*B*B-2*B*t-20*t+1)/2,
    }


def kappa(t, A, B):
    return (
        -(3*A*A*t - 6*A*A + A*t + 2*A + 2*B - 2*t - 2)/6,
        (2*A*(t-1) + B*B - 2*t + 1)/2,
    )


def polynomial_identities():
    # Every J expression has individual degrees <= (1,2,2) in (t,A,B)
    # after t cancels the denominators of v. This tensor grid determines
    # those polynomial identities exactly.
    count = 0
    for t, A, B in product(map(Q,[1,2]), map(Q,[0,1,2]), map(Q,[0,1,2])):
        u, v, w, U, V, J, K = jet(t,A,B)
        assert J == leading_formulas(t,A,B)
        k0, k1 = kappa(t,A,B)
        assert w[0]-2*w[1]+w[2] == k0
        assert w[1]-2*w[2]+w[3] == k1
        C = 3*A*A*(t-2)+A*(7*t-4)-8*t+1
        assert k0-k1 == -(3*B*B+2*B+C)/6
        for r in map(Q,[0,1]):
            m00 = r*(1-t/2)
            m01 = r*(t+2)/12-(t-1)/2
            m11 = r*(t+1)/3-t+Q(1,2)
            m12 = r/6
            m22 = Q(1,2)
            form = m00*A*A-2*m01*A+m11-2*m12*B+m22*B*B
            assert form == r*k0+k1
        count += 1
    return count


def endpoint_checks():
    alo, ahi = Q(13517,10000), Q(6759,5000)
    hlo, hhi = Q(1863,1000), Q(233,125)
    blo, bhi = Q(5,8), Q(157,250)
    def C(A,h):
        return 3*A*A*(h-2)+A*(7*h-4)-8*h+1
    assert 6*ahi*(hlo-2)+7*hlo-4 > 0
    assert 3*alo*alo+7*alo-8 > 0
    cmin, cmax = C(alo,hlo), C(ahi,hhi)
    assert cmin == -Q(243421547779,100000000000)
    assert cmax == -Q(7582743131,3125000000)
    assert 3*blo*blo+2*blo+cmax == -Q(3595939,781250000)
    assert 3*bhi*bhi+2*bhi+cmin == Q(493652221,100000000000)
    slope = ahi-1-blo*(3*alo*alo+7*alo-8)/(6*bhi+2)
    assert slope == -Q(369648683,922880000) < -Q(3,8)
    bounds = [
        (3*alo*alo*(hlo-1)+ahi*(hlo-4)+2*blo-2*hhi+1)/6,
        (2*alo*(hlo-1)+blo-hhi-1)/3,
        (2*hlo-1-ahi*(hhi-1)-2*bhi)/3,
        (alo-1)*(2*hlo-1-ahi*(hhi-1)-2*bhi)/2,
        (60*hlo-(3*ahi*ahi*(hhi+1)+4*ahi*bhi*hhi+2*ahi*bhi+2*bhi*bhi+3))/2,
        (59*hlo-(3*ahi*ahi*(hhi+1)+6*ahi*bhi*hhi+3*bhi*bhi+4))/3,
        (20*hlo-(ahi*ahi*(hhi+1)+2*ahi*bhi*hhi+2*bhi*bhi+1))/2,
    ]
    claimed = [Q(3,50), Q(3,100), Q(1,10), Q(1,20), Q(42), Q(26), Q(13)]
    assert all(a>b for a,b in zip(bounds,claimed))
    return {"slope_upper":str(slope),"leading_slack_lower_bounds":[str(x) for x in bounds]}


def rational_repair():
    t, A, B = Q(233,125), Q(13517,10000), Q(313,500)
    u,v,w,U,V,J,K = jet(t,A,B)
    assert min(J.values()) == 0
    k0,k1 = kappa(t,A,B)
    assert max(k0,k1)<0
    rho=1-t/2
    z=[2*rho**i for i in range(4)]
    b=[rho**i for i in range(4)]
    aa=[rho**(2*i) for i in range(4)]
    P={ij:(rho**ij[0]-rho**ij[1])**2/2 for ij in J}
    # Both square-root scales are rational in this concrete audit case.
    c=Q(10**8)
    e=Q(1,2*10**6)
    e2=e*e
    assert c>max(abs(K[ij])/P[ij] for ij in J)
    q0=(1-rho*rho)**2
    assert c*e2*q0<=-max(k0,k1)/2
    # Scaled coordinates are represented without square roots:
    # x=(x0, sqrt(e2)*x1, sqrt(c)*e2*x2), likewise for g.
    X=[(-t*i-t*e2*V[i],-t*U[i],z[i]) for i in range(4)]
    G=[(1+e2*v[i],u[i],b[i]) for i in range(4)]
    f=[-t*i+e2*w[i]+c*e2*e2*aa[i] for i in range(4)]
    weights=(Q(1),e2,c*e2*e2)
    def wdot(x,y):
        return sum((ww*xx*yy for ww,xx,yy in zip(weights,x,y)),Q(0))
    for i in range(3):
        assert all(X[i+1][j]==X[i][j]-t*G[i][j] for j in range(3))
    residuals={}
    for i,j in J:
        dx,dg=sub(X[i],X[j]),sub(G[i],G[j])
        I=f[i]-f[j]-wdot(G[j],dx)-wdot(dg,dg)/2
        assert I==e2*J[i,j]+e2*e2*(K[i,j]+c*P[i,j])>0
        residuals[i,j]=I
    curves=[f[i]-2*f[i+1]+f[i+2] for i in range(2)]
    assert max(curves)<=e2*max(k0,k1)/2<0
    amin=min(residuals.values())
    bmax=max(wdot(sub(sub(X[i],X[j]),sub(G[i],G[j])),
                  sub(sub(X[i],X[j]),sub(G[i],G[j]))) for i,j in J)
    mu=amin/(amin+bmax+1)
    assert 0<mu<1
    assert all(I>mu*bmax/(2*(1-mu)) for I in residuals.values())
    root_c=Q(10**4)
    actual_x=[(row[0],e*row[1],root_c*e2*row[2]) for row in X]
    actual_g=[(row[0],e*row[1],root_c*e2*row[2]) for row in G]
    assert root_c*root_c==c
    M=1+max(wdot(row,row) for row in G)
    delta=-max(curves)
    C=16*M*t+6+27*t*t
    sigma=min(Q(1),delta/(2*C))
    assert C*sigma<=delta/2
    return {
        "step":str(t),"A":str(A),"B":str(B),
        "rational_points":[[str(z)for z in row]for row in actual_x],
        "rational_gradients":[[str(z)for z in row]for row in actual_g],
        "rational_values":[str(z)for z in f],
        "exact_interpolation_slacks":{str(ij):str(v)for ij,v in residuals.items()},
        "exact_repaired_curvatures":[str(v)for v in curves],
        "mu":str(mu),"analytic_sigma":str(sigma),
        "leading_curvatures":[str(k0),str(k1)],
        "repaired_curvatures_display":[float(v) for v in curves],
        "strong_convexity_display":float(mu),
        "strict_interpolation_pairs":len(residuals),
        "all_exact_checks_passed":True,
    }


if __name__=="__main__":
    parser=argparse.ArgumentParser()
    parser.add_argument("--output",type=Path)
    args=parser.parse_args()
    result={
        "polynomial_identity_grid_cases":polynomial_identities(),
        "endpoint_checks":endpoint_checks(),
        "rational_repair":rational_repair(),
        "all_exact_checks_passed":True,
    }
    out=json.dumps(result,indent=2)+"\n"
    if args.output:
        args.output.parent.mkdir(parents=True,exist_ok=True)
        args.output.write_text(out)
    print(out)