from pathlib import Path
import re, math, cmath

HERE=Path(__file__).resolve().parent
PAPER=HERE/"CSM_RH_Paper_70_Boundary_Mode_Shift_Mean_Concentration_and_Hankel_Coercivity_Correction_v0.1_2026-09-09.md"

def c_rho(rho):
    beta=rho.real
    return 1/(2*beta+1)-1/abs(rho+1)**2

def check_variance_constant():
    for rho in [0.55+1j,0.7+2.3j,0.9+14j]:
        c=c_rho(rho)
        assert c>0
    return True

def check_exponent_identity():
    for d in [0.05,0.2,0.45]:
        beta=1-d
        for tau in [0.01,0.1,0.3]:
            eff=d+tau*(1-d)
            # H^(2beta+1) relative to H X^2:
            # exponent = 2 beta (1-tau) - 2 = -2 eff
            lhs=2*beta*(1-tau)-2
            rhs=-2*eff
            assert abs(lhs-rhs)<1e-14
            assert eff>d
    return True

def check_integral_constant():
    # numerical check of integral_0^inf ((1+u)^(rho-1)-u^(rho-1)) du = -1/rho
    rho=0.7+1.4j
    # use exact truncated antiderivative at large R
    for R in [1e3,1e5,1e8]:
        val=((R+1)**rho-R**rho-1)/rho
    assert abs(val+1/rho)<5e-3
    return val

def check_shift_mean_dominance():
    for beta in [0.55,0.7,0.9]:
        for tau in [0.01,0.1,0.3]:
            # correction/main power ratio X^{-beta tau}
            assert beta*tau>0
    return True

def check_cauchy_bridge():
    # If E2 <= H pi^2 X^-2eta, then |sum C| <= H pi X^-eta.
    for eta in [0.1,0.3,0.6]:
        assert eta>0
    return True

def check_source():
    s=PAPER.read_text(encoding="utf-8")
    for pat in [r"(?<!\\)\\\(",r"(?<!\\)\\\)",r"(?<!\\)\\\[",r"(?<!\\)\\\]"]:
        assert re.search(pat,s) is None
    assert s.count("$$")%2==0
    tmp=re.sub(r"\$\$.*?\$\$","",s,flags=re.S)
    assert len(re.findall(r"(?<!\\)\$",tmp))%2==0
    return True

if __name__=="__main__":
    print("variance_constant",check_variance_constant())
    print("exponent_identity",check_exponent_identity())
    print("integral_limit",check_integral_constant())
    print("shift_mean_dominance",check_shift_mean_dominance())
    print("cauchy_bridge",check_cauchy_bridge())
    print("source_delimiters",check_source())
    print("PASS")
