check_values.py

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

6324 bytes, as of the version from 2026-11-26 12:33 (current). Recorded here, not run.

"""Check every selected coefficient using Theorem 4.11, not the generator recurrence.
Run: sage -python check_values.py [stored-document.json]
The original inverse-q theorem is additionally evaluated in exact fraction fields.
All supplementary tests are exact. This is builder evidence, not a critic verdict.
"""
import json, sys, hashlib
from pathlib import Path
import numberdb.sage as numberdb
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.combinat.q_analogues import q_binomial
from generate import TypeRIQLommel, polynomial, P, x, c, q

WORK=Path(__file__).resolve().parent

def choose(n,k,t):
    if k<0 or k>n: return t.parent().zero()
    return q_binomial(n,k,q=t)

def poch(t,b,n):
    result=t.parent().one()
    for j in range(n): result *= 1-t*b**j
    return result

def explicit(n):
    result=P.zero()
    for k in range(n+1):
        for a in range(n-k+1):
            result += (-1)**(n+k)*c**(n-k-a)*q**((n-k)*(n-k-1)//2)*choose(k+a,a,q)*choose(n-a,k,q)*x**(n-a)
    return result

def valid(n,v):
    return (v.parent() == P and all(type(t).__name__=='Integer' for t in v.coefficients()) and v==explicit(n))

def expected_rows():
    return {str(p['n']): polynomial(p['n']) for p in TypeRIQLommel().enumerate()}

def read_rows(path):
    # Preserve live identities; do not silently drop additional entries.
    sys.path.insert(0,str(WORK.parents[2]))
    try:
        from numberdb_app.flatten import to_records
        records=to_records(json.loads(Path(path).read_text()))
    except ImportError:
        raise RuntimeError('For standalone use supply stored-rows.json through --rows')
    return {str(r['params']['n']): P(str(r['number']).replace('^','**')) for r in records}

def check(rows):
    assert list(rows)==[str(n) for n in range(6)], 'missing, duplicate, extra or reordered n'
    for n,v in rows.items(): assert valid(int(n),v), 'coefficient mismatch at n='+n

rows=expected_rows()
if len(sys.argv)>1:
    if sys.argv[1]=='--rows':
        rows={r['n']:P(r['number']) for r in json.loads(Path(sys.argv[2]).read_text())}
    else: rows=read_rows(sys.argv[1])
check(rows)
assert explicit(0)==1
assert explicit(1)==(1-c)*x-1
assert explicit(2)==(1-c)*(1-c*q)*x**2-(1+q-c*q)*x+q
known=True
negative=[]
for name,n,v in [('changed coefficient',3,rows['3']+1),('wrong sign',2,-rows['2']),('ordinary q family',2,(1-c)*(1-c*q)*x**2-c),('even-odd family',2,(1-c)*(1-c*q)*x**2-1)]:
    assert not valid(n,v); negative.append(name)
wrong=dict(rows); del wrong['3']
try: check(wrong)
except AssertionError: negative.append('missing n')
else: raise AssertionError('missing-n control accepted')
from sage.rings.real_mpfr import RealField
RP=PolynomialRing(RealField(53),names=('x','c','q'))
assert not valid(2,RP(rows['2'])); negative.append('inexact polynomial rejected by ring and native-type predicates')
K=P.fraction_field()
assert K(rows['2'])/K((1-c)*(1-c*q)) != K(explicit(2)); negative.append('extra monic division')
# Check the transformed double sum against the original theorem with q^-1.
R=PolynomialRing(QQ,'t'); t=R.gen(); Fr=R.fraction_field(); t=Fr(t)
original_checks=0; determinant_checks=0; connection_checks=0; constant_checks=0
for cv,qv in [(QQ(1)/3,QQ(1)/2),(QQ(2)/5,QQ(2)/3),(QQ(-2),QQ(3)/2)]:
    rr=[]; ee=[]
    for n in range(11):
        d=QQ(1)
        for j in range(n): d*=1-cv*qv**j
        # Independent even-odd family needed only for the connection check.
        if n==0: ev=Fr.one()
        elif n==1: ev=t
        else:
            j=n-1
            lam=(cv*qv**(3*(j//2)-1) if j%2==0 else qv**((j-1)//2))/((1-cv*qv**(j-1))*(1-cv*qv**j))
            ev=t*ee[-1]-lam*ee[-2]
        ee.append(ev)
        if n>5: continue
        value=Fr(rows[str(n)](t,cv,qv))
        rn=value/d; rr.append(rn)
        den=QQ(1)
        for j in range(n): den*=1-(1/cv)*(1/qv)**j
        original=Fr.zero()
        for k in range(n+1):
            for a in range(n-k+1):
                original+=(-t/cv)**k*qv**(-k*(k-1)//2)*q_binomial(k+a,a,q=1/qv)*cv**(-a)*q_binomial(n-a,k,q=1/qv)*t**(n-k-a)
        assert rn==original/den; original_checks+=1
        # Continuant determinant with coefficient list from source Definition 2.4.
        from sage.matrix.constructor import matrix
        mat=matrix(Fr,n,n)
        for j in range(n):
            mat[j,j]=t-qv**j/(1-cv*qv**j)
            if j:
                mat[j-1,j]=1
                mat[j,j-1]=t*cv*qv**(2*j-1)/((1-cv*qv**(j-1))*(1-cv*qv**j))
        assert rn==mat.det(); determinant_checks+=1
    for n in range(6):
        # Proposition 4.12, with all D_n factors restored by using rn=F_n/D_n.
        rhs=Fr.zero()
        for k in range(n+1):
            den=QQ(1)
            for j in range(k): den*=(1-cv*qv**(n-1-j))*(1-cv*qv**(2*n-k-j))
            rhs+=q_binomial(n,k,q=qv)*cv**k*qv**(n*n-(n-k)**2)/den*ee[2*n-2*k]
        assert rr[n](t**2)==rhs; connection_checks+=1
for n,v in rows.items():
    n=int(n)
    assert v(0,c,q)==(-1)**n*q**(n*(n-1)//2)
    assert P(v).coefficient({x:n})==poch(c,q,n)
    constant_checks+=1
# Exact c=0 specialization, separately multiplied linear factors.
for n in range(6):
    product=P.one()
    for j in range(n): product*=x-q**j
    assert rows[str(n)](x,0,q)==product
result={'checked_entries':len(rows),'total_entries':6,'failures':[], 'known_answer_control':known,'negative_control_rejected':True,'negative_controls':negative,'full_coefficient_method':'Polynomial double sum derived from Kim--Stanton Theorem 4.11','checked_indices':list(range(6)),'exact_test_pairs':[['1/3','1/2'],['2/5','2/3'],['-2','3/2']],'original_theorem_exact_specializations':original_checks,'continuant_determinants':determinant_checks,'even_odd_connection_exact_specializations':connection_checks,'constant_and_leading_coefficient_checks':constant_checks,'c_zero_product_checks':6,'independence':'Finite sum differs from recurrence; same source conventions. Determinants share recurrence coefficients. Even-odd polynomial recurrence independently constructed, not read from a sibling.','skill_manifest':'../20261126T113843Z-build-skills/manifest.json','skill_bundle_sha256':'b5c3c7985b7c9e178b4899d083a9e3bc4284989dff76f05e5b0e52835d47eed2'}
print(json.dumps(result,indent=2))