checks.py

back to table · edit · history · where entries came from · files · download

11491 bytes, as of the version from 2026-10-28 05:42 (current). Recorded here, not run.

"""Independent checks for Bures rank volumes -- numberdb.org/T572.

    sage -python checks.py                 # offline, all 270 generated rows
    sage -python checks.py --stored        # API readback; owner key for draft

Checks include: exact Pfaffian integration of the complex Bures eigenvalue
weight, all rank-two integrals in both fields, pure projective spaces,
full-rank hemispherical volumes, the complex boundary/interior ratio, and
exact containment in the serialized 100-digit intervals. The direct 560-bit
gamma evaluation tests implementation and serialization, not independence.
The Pfaffian method is Sarkar-Kumar arXiv:1901.09587, (21)-(23) and (37), with
alpha=N-r-1/2. No closed product for that Pfaffian is used by this checker.
See numerical-evidence.md for the rank-two integral derivation and scope.
"""
import json
import os
import sys
from fractions import Fraction
from math import factorial
from pathlib import Path
import numberdb.sage as numberdb
from numberdb._compare import _claim, digits_of
from numberdb._write import to_text
from sage.rings.rational_field import QQ
from sage.rings.real_arb import RealBallField
from sage.env import SAGE_VERSION
import generate
from program import bures_volume
from check import agrees_with, exactness, measure, names_its_rings

R = RealBallField(560)


def unwrap(x):
    return x['number'] if isinstance(x, dict) else x


def bounds(text):
    center, radius = _claim(text)
    c, r = QQ(Fraction(center)), QQ(Fraction(radius))
    return c-r, c+r


def contains(text, ball):
    assert ball.is_finite() and ball > 0
    assert ball.accuracy() >= 360, 'wide comparison ball'
    lo, hi = bounds(text)
    assert lo <= QQ(ball.lower()) and QQ(ball.upper()) <= hi, ('enclosure', text, ball)


def same_ball(a, b):
    assert a.is_finite() and b.is_finite()
    assert a.accuracy() >= 360 and b.accuracy() >= 360
    assert a.overlaps(b), (a, b)


def direct_gamma(field, N, r):
    b = 1 if field == 'real' else 2
    d = N-r
    D = r-1 + b*r*(2*N-r-1)//2
    x = R(2)**(-D)*R.pi()**(QQ(D+1)/2)/R(QQ(D+1)/2).gamma()
    for j in range(1, r+1):
        x *= R(QQ(j*b)/2).gamma()*R(1+QQ((2*d+j-1)*b)/2).gamma()
        x /= R(QQ((d+j)*b)/2).gamma()*R(1+QQ((d+j-1)*b)/2).gamma()
    return x


def pfaffian(M):
    """Exact elimination, with simultaneous row/column pivoting."""
    A = [list(row) for row in M]
    out = QQ(1)
    while A:
        n = len(A)
        assert n % 2 == 0
        pivot = next((j for j in range(1,n) if A[0][j]), None)
        if pivot is None:
            return QQ(0)
        if pivot != 1:
            A[1], A[pivot] = A[pivot], A[1]
            for row in A:
                row[1], row[pivot] = row[pivot], row[1]
            out = -out
        z = A[0][1]
        out *= z
        A = [[A[i][j]-(A[0][i]*A[1][j]-A[0][j]*A[1][i])/z
              for j in range(2,n)] for i in range(2,n)]
    return out


def recurrence_gamma(twice_x):
    """Independent recurrence: q*pi^(p/2), without factorial reduction."""
    m = int(twice_x)
    if m % 2:
        q, p, start = QQ(1), 1, 1
    else:
        q, p, start = QQ(1), 0, 2
    for t in range(start, m, 2):
        q *= QQ(t)/2
    return q, p


def complex_integral_coeff(N, r):
    d = N-r
    a = QQ(2*d+1)/2
    size = r + r%2
    M = [[QQ(0) for _ in range(size)] for _ in range(size)]
    for i in range(r):
        for j in range(r):
            M[i][j] = QQ(j-i)/(2*a+i+j)
    if r%2:
        for i in range(r):
            M[i][r], M[r][i] = QQ(1), QQ(-1)
    q = pfaffian(M)/QQ(2)**(r-1)
    p = 0
    for i in range(r):
        g, e = recurrence_gamma(2*d+1+2*i)
        q *= g
        p += e
    for k in range(d+1, N+1):
        q /= QQ(factorial(k-1))
        p += 2*(k-1)
    # Radial integral, lambda_i = t*x_i: Gamma(r*(2*N-r)/2).
    g, e = recurrence_gamma(r*(2*N-r))
    q /= g
    p -= e
    assert p%2 == 0
    return q, p//2


def projective(field, N):
    if field == 'real':
        return R.pi()**(QQ(N)/2)/R(QQ(N)/2).gamma()
    return R.pi()**(N-1)/R(factorial(N-1))


def rank_two(field, N):
    b = 1 if field == 'real' else 2
    d = N-2
    orbit = R(1)
    for k in (N-1, N):
        orbit *= R.pi()**(QQ((k-1)*b)/2)*R(QQ(b)/2).gamma()/R(QQ(k*b)/2).gamma()
    u, v = QQ(b+1)/2, QQ(b*d+1)/2
    beta_integral = R(2)**(-b*d)*R(u).gamma()*R(v).gamma()/R(u+v).gamma()
    # 1/2 from the diagonal metric, 1/2! from unordered eigenvalues.
    return orbit*beta_integral/4


def controls():
    assert pfaffian([[QQ(0),QQ(7)],[-QQ(7),QQ(0)]]) == 7
    A = [[QQ(0),QQ(2),QQ(3),QQ(5)],[-QQ(2),QQ(0),QQ(7),QQ(11)],
         [-QQ(3),-QQ(7),QQ(0),QQ(13)],[-QQ(5),-QQ(11),-QQ(13),QQ(0)]]
    assert pfaffian(A) == 2*13-3*11+5*7 == 28
    # Pivoting and negative Pfaffians must also work.
    assert pfaffian([[QQ(0),QQ(0),QQ(1),QQ(0)], [QQ(0),QQ(0),QQ(0),QQ(1)],
                     [-QQ(1),QQ(0),QQ(0),QQ(0)], [QQ(0),-QQ(1),QQ(0),QQ(0)]]) == -1
    assert recurrence_gamma(1) == (QQ(1),1)
    assert recurrence_gamma(6) == (QQ(2),0)
    assert recurrence_gamma(7) == (QQ(15)/8,1)
    same_ball(rank_two('real',2),R.pi()/2)
    same_ball(rank_two('complex',2),R.pi()**2/8)
    same_ball(projective('real',2),R.pi())
    same_ball(projective('complex',2),R.pi())
    assert complex_integral_coeff(2,1) == (QQ(1),1)
    assert complex_integral_coeff(2,2) == (QQ(1)/8,2)
    # The containment check must reject a known shifted decimal.
    rejected = False
    try:
        contains('2.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', R.pi())
    except AssertionError:
        rejected = True
    assert rejected


def records_of(doc):
    records = doc['Numbers']
    if isinstance(records,list):
        return records
    # numberdb API returns the legacy nested form if that is how it was written.
    result = []
    for f, ns in records.items():
        for N, rs in ns.items():
            for r, entry in rs.items():
                result.append(dict(entry if isinstance(entry,dict) else {'number':entry}, params={'field':f,'N':N,'rank':r}))
    return result


def run(stored=False):
    controls()
    print('Controls passed: Pfaffians, gamma recurrence, geometric base cases, failed-enclosure control.')
    g = generate.BuresVolumes()
    pars = list(g.enumerate())
    vals = {tuple(p.values()):g.value(p,100) for p in pars}
    assert len(vals) == 270
    assert not exactness(vals) and not names_its_rings(Path(generate.__file__))
    expected = {(f,N,r) for f in ('real','complex') for N in range(2,17) for r in range(1,N+1)}
    assert set(vals) == expected
    complex_coeffs = {(N,r):generate.coefficient('complex',N,r)
                      for N in range(2,17) for r in range(1,N+1)}
    mismatches = agrees_with(complex_coeffs,lambda key:complex_integral_coeff(*key))
    assert not mismatches, mismatches[:3]
    print('Exact Pfaffian integration agrees on all 135 complex entries.')
    rows = []
    generated_text = {}
    for p in pars:
        key = tuple(p.values())
        v = vals[key]
        text = to_text(unwrap(v),100)
        generated_text[key] = text
        row = {'params':p,'number':text}
        if isinstance(v,dict) and 'comment' in v:row['comment']=v['comment']
        rows.append(row)
    if stored:
        doc = numberdb.table('T572')
        dbrows = records_of(doc)
        texts = {(str(e['params']['field']),int(e['params']['N']),int(e['params']['rank'])):e['number'] for e in dbrows}
        assert set(texts)==expected and len(dbrows)==270
        assert list(texts)==list(generated_text), 'stored enumeration differs'
        assert texts==generated_text, 'stored text differs from dry run'
    else:
        texts = generated_text
    counts = {'direct_gamma':0,'serialized_enclosure':0,'pure_state':0,
              'full_rank':0,'rank_two':0,'complex_pfaffian':0,'complex_boundary_ratio':0}
    min_bits = 10000
    for (field,N,r), text in texts.items():
        b = 1 if field=='real' else 2
        D = r-1+b*r*(2*N-r-1)//2
        v = unwrap(vals[(field,N,r)])
        min_bits = min(min_bits,int(v.accuracy()))
        assert digits_of(text)>=100
        contains(text,v)
        counts['serialized_enclosure']+=1
        other = direct_gamma(field,N,r)
        contains(text,other)
        same_ball(v,other)
        counts['direct_gamma']+=1
        contains(text,bures_volume(field,N,r))
        if field=='complex':
            q,k=complex_integral_coeff(N,r)
            contains(text,R(q)*R.pi()**k)
            counts['complex_pfaffian']+=1
        if r==1:
            contains(text,projective(field,N))
            counts['pure_state']+=1
        if r==N:
            contains(text,R.pi()**(QQ(D+1)/2)/(R(2)**D*R(QQ(D+1)/2).gamma()))
            counts['full_rank']+=1
        if r==2:
            contains(text,rank_two(field,N))
            counts['rank_two']+=1
    # Test the boundary ratio on serialized intervals for every N.
    for N in range(2,17):
        a,b=bounds(texts[('complex',N,N-1)])
        c,d=bounds(texts[('complex',N,N)])
        ratio=2*N/R.pi().sqrt()*R(QQ(N*N)/2).gamma()/R(QQ(N*N-1)/2).gamma()
        assert ratio.is_finite() and ratio.accuracy()>500
        assert a/d <= QQ(ratio.lower()) and QQ(ratio.upper()) <= b/c
        counts['complex_boundary_ratio']+=1
    counts['program_snippet']=270
    if stored:
        hs=numberdb.table('T571')['Numbers']
        spheres=numberdb.table('T28')['Numbers']
        balls=numberdb.table('T27')['Numbers']
        for field in ('real','complex'):
            beta=1 if field=='real' else 2
            for N in range(2,17):
                contains(unwrap(hs[field][str(N)]['1']),
                         projective(field,N)*R(2)**(QQ(beta*(N-1))/2))
                if field=='real':
                    contains(unwrap(spheres[str(N-1)]),2*projective(field,N))
                else:
                    contains(unwrap(balls[str(2*(N-1))]),projective(field,N))
        counts['linked_hilbert_schmidt_pure']=30
        counts['linked_sphere_pure']=15
        counts['linked_ball_pure']=15
    print('Enclosures and independent geometrical identities:',json.dumps(counts,sort_keys=True))
    print('Minimum generating-ball relative accuracy:',min_bits,'bits')
    summary={'table':'T572','stored':stored,'entries':270,'checks':counts,
             'exact_complex_pfaffian_coefficients':135,'controls':'passed',
             'minimum_relative_accuracy_bits':min_bits,'working_bits':numberdb.bits(100,losing=64),
             'independent_bits':560,'raw_ball_measure':measure(vals),
             'serialized_measure':measure({k:v for k,v in texts.items()}),
             'serialized_max_characters':max(map(len,texts.values())),
             'skill_manifest':'agents/runs/20261028T051113Z-build-skills/manifest.json',
             'skill_sha256':'e37efb7e2ededf6c1d7134c673f152f648f26e0267c556aebdf3ec65765d8b4c'}
    print('EVIDENCE_JSON '+json.dumps(summary,sort_keys=True))
    print('VERSIONS_JSON '+json.dumps({'sage':SAGE_VERSION,'numberdb':numberdb.__version__,'python':sys.version}))
    print('ROWS_JSON '+json.dumps(rows))
    return summary


if __name__=='__main__':
    if os.environ.get('NUMBERDB_KEY_FROM_STDIN')=='1':
        key=sys.stdin.read().strip()
        if '=' in key and key.split('=',1)[0].replace('_','').isalnum():key=key.split('=',1)[1].strip().strip('\"\'')
        os.environ['NUMBERDB_API_KEY']=key
    run('--stored' in sys.argv or os.environ.get('NUMBERDB_CHECK_STORED')=='1')