File size: 7,504 Bytes
4aa7bc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Run deterministic exact and numerical release checks; write JSON/CSV results."""
from __future__ import annotations
from fractions import Fraction as F
from pathlib import Path
import random, json, csv
import numpy as np
import networkx as nx
from scipy.linalg import null_space, eigh
from exact_graph import Edge, graph, parameters, classify, exact_halfcell_spectrum, threshold_multiplicity
from port_bound import theta_bound
from fem_check import eigenvalues

ROOT=Path(__file__).resolve().parents[1]
rng=random.Random(260925)
cases=[]
def add(name, data, D=()):
    es=[Edge(a,b,F(str(l))) for a,b,l in data]
    cases.append((name,es,tuple(D)))

add('DD_interval',[(0,1,2)],(0,1))
add('DN_interval',[(0,1,2.5)],(0,))
add('NN_interval',[(0,1,2)])
add('mixed_star',[(0,1,1.5),(0,2,1),(0,3,1)],(2,3))
add('theta_odd',[(0,1,1),(0,1,1),(0,1,3)])
add('theta_even',[(0,1,2),(0,1,2),(0,1,4)])
add('theta_wrong_parity',[(0,1,1),(0,1,1),(0,1,2)])
add('figure_eight',[(0,0,2),(0,0,4)])
add('figure_eight_bad',[(0,0,1),(0,0,3)])
add('Dirichlet_lasso',[(0,0,2),(0,1,1.5)],(1,))
add('Neumann_lasso',[(0,0,2),(0,1,1)])
add('barbell',[(0,0,2),(0,1,1),(1,1,2)])
add('branch_lasso_tree',[(0,1,1),(0,2,.5),(0,3,2.5),(2,2,2)],(1,))
add('loop_at_degree_four',[(0,0,2.5),(0,1,.5),(0,2,1.5)])
for p in range(3,7):
    add(f'bouquet_{p}',[(0,0,2)]*p)
for p in range(2,8):
    add(f'pumpkin_{p}',[(0,1,1)]*p)

# Connected simple graph atlas, with all degree-two labels harmlessly retained.
atlas=[g for g in nx.graph_atlas_g() if 2<=len(g)<=6 and nx.is_connected(g)]
for j,g in enumerate(atlas):
    for repetition in range(3):
        leaves=[v for v in g if g.degree(v)==1]
        D=[v for v in leaves if rng.random()<.5]
        es=[(a,b,F(rng.randint(1,4),2)) for a,b in g.edges()]
        add(f'atlas_{j}_{repetition}',es,D)

# Explicit compatible stars and loop-decorated trees guarantee substantial
# positive coverage, rather than a suite consisting mostly of strict cases.
for i in range(60):
    branches=rng.randint(3,6)
    D=[]; es=[]
    for v in range(1,branches+1):
        virtualN=rng.random()<.6
        m=rng.randint(0,1) if virtualN else rng.randint(1,2)
        es.append((0,v,F(m)+F(int(virtualN),2)))
        if virtualN and rng.random()<.5:
            es.append((v,v,2))
        elif not virtualN:
            D.append(v)
    add(f'compatible_star_{i}',es,D)

# Compatible nonsymmetric tree skeletons, including multiple branch vertices.
for order in range(2,9):
    for tree_index,tree in enumerate(nx.nonisomorphic_trees(order)):
        for rep in range(2):
            leaves=[v for v in tree if tree.degree(v)==1]
            virtualN={v for v in leaves if rng.random()<.55}
            D=[v for v in leaves if v not in virtualN]
            es=[]
            for a,b in tree.edges():
                nu=int(a in virtualN)+int(b in virtualN)
                m=rng.randint(0,1) if nu else rng.randint(1,2)
                es.append((a,b,F(m)+F(nu,2)))
            for v in virtualN:
                if rng.random()<.5:
                    es.append((v,v,2))
            add(f'compatible_tree_{order}_{tree_index}_{rep}',es,D)

results=[]
for name,es,D in cases:
    g=graph(es,D)
    d,n,beta,L=parameters(g); B=n+beta
    candidate=L+F(B,2)
    inregime=candidate.denominator==1 and int(candidate)>=max(B,1 if d else 2)
    # Circle is excluded from the inequality and classifier.
    circle=all(g.degree(v)==2 for v in g)
    certificate=exact_halfcell_spectrum(es,D)
    classification=classify(es,D)
    exact_sharp=bool(inregime and not circle and certificate['multiplicity']>0 and certificate['top_index']==int(candidate))
    assert exact_sharp == classification['saturated'], (name,classification,certificate)
    ode_multiplicity=threshold_multiplicity(es,D)
    assert certificate['multiplicity']==ode_multiplicity,(name,certificate,ode_multiplicity)
    if exact_sharp:
        assert certificate['multiplicity']==d+n+2*beta-1,(name,certificate)
    results.append(dict(name=name,edges=[[e.u,e.v,str(e.length)] for e in es],Dirichlet=list(D),classification=classification,exact=certificate,ODE_multiplicity=ode_multiplicity,passed=True))

# General abstract quantitative inheritance: independent random matrix tests.
np_rng=np.random.default_rng(260925)
abstract=[]
for case in range(200):
    size=12; k=5; m=int(np_rng.integers(1,4)); lower=k-m
    lam=2.0; g=float(np_rng.uniform(.2,2.0))
    values=np.r_[np.linspace(.3,1.3,lower),np.full(m,lam),lam+g,lam+g+np.arange(1,size-k)]
    A=np.diag(values)
    ports=int(np_rng.integers(1,4))
    C=np_rng.normal(size=(ports,size))
    S=C@np.diag(1/values)@C.T
    R=C[:,lower:k]@C[:,lower:k].T
    rho=max(0.,float(eigh(R,S,eigvals_only=True)[-1]))
    bound=g*rho/(lam+g+rho)
    Q=null_space(C)
    delta=float(eigh(Q.T@A@Q,eigvals_only=True)[k-1]-lam)
    assert delta+1e-10>=bound,(delta,bound)
    abstract.append(dict(case=case,delta=delta,bound=bound,passed=True))

# Numerical convergence and the concrete strict theta gap certificate.
numerical=[]
for name in ['theta_odd','theta_wrong_parity','figure_eight','Dirichlet_lasso','branch_lasso_tree','barbell']:
    _,es,D=next(c for c in cases if c[0]==name)
    d,n,beta,L=parameters(graph(es,D)); k=int(L+F(n+beta,2))
    for density in (30,60,120):
        vals=eigenvalues(es,D,count=max(k+3,12),density=density)
        row=dict(name=name,density=density,k=k,lambda_k=float(vals[k-1]),excess=float(vals[k-1]-np.pi**2))
        numerical.append(row)
strict=theta_bound((1,1,2),5)
strict['exact_excess_expression']='4*(pi-atan(sqrt(5)))**2-pi**2'
strict['exact_excess_decimal']=float(4*(np.pi-np.arctan(np.sqrt(5)))**2-np.pi**2)
strict['numerical_finest_excess']=next(x['excess'] for x in numerical if x['name']=='theta_wrong_parity' and x['density']==120)
assert strict['exact_excess_decimal']>strict['gap_lower_bound']
theta_fem=[x['excess'] for x in numerical if x['name']=='theta_wrong_parity']
assert all(x>strict['exact_excess_decimal'] for x in theta_fem)
assert all(a>b for a,b in zip(theta_fem,theta_fem[1:]))
assert strict['numerical_finest_excess']>strict['gap_lower_bound']
# Port-coordinate invariance is structural; verify a nonorthogonal example.
U=np.array([[2.,1.],[0.,3.]])
R=np.array(strict['residue']);S=np.array(strict['path_gram'])
assert np.allclose(eigh(U@R@U.T,U@S@U.T,eigvals_only=True),eigh(R,S,eigvals_only=True))

out=ROOT/'data';out.mkdir(exist_ok=True)
(out/'exact_cases.json').write_text(json.dumps(results,indent=2))
(out/'abstract_bound_checks.json').write_text(json.dumps(abstract,indent=2))
(out/'theta_gap_certificate.json').write_text(json.dumps(strict,indent=2))
with (out/'fem_convergence.csv').open('w',newline='') as f:
    writer=csv.DictWriter(f,fieldnames=numerical[0].keys());writer.writeheader();writer.writerows(numerical)
summary=dict(exact_graph_cases=len(results),exact_graph_passed=len(results),exact_saturated_cases=sum(r['classification']['saturated'] for r in results),exact_ODE_nullity_crosschecks=len(results),abstract_inequality_cases=len(abstract),abstract_inequality_passed=len(abstract),FEM_runs=len(numerical),failures=0,seed=260925,proof_role='Regression and finite-instance certificates only; the general theorem is proved in the manuscript.',theta_gap_bound=strict['gap_lower_bound'],theta_actual_excess_FEM=strict['numerical_finest_excess'],theta_exact_excess_decimal=strict['exact_excess_decimal'])
(out/'check_summary.json').write_text(json.dumps(summary,indent=2))
print(json.dumps(summary,indent=2))