from pathlib import Path
import re, math, cmath

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

def check_weighted_degree():
    delta=0.23
    theta=[1.0,0.7,1.3]
    n=[2,1,3]
    W=sum(a*b for a,b in zip(theta,n))
    X1=1e5
    X2=1e8
    # pure amplitudes; ratio determines slope
    A1=math.prod((X1**(-theta[j]*delta))**n[j] for j in range(3))
    A2=math.prod((X2**(-theta[j]*delta))**n[j] for j in range(3))
    slope=-math.log(A2/A1)/math.log(X2/X1)
    assert abs(slope-W*delta)<1e-12,(slope,W*delta)
    return W,slope

def check_log_derivative():
    delta=0.17
    gamma=3.4
    X=1e6
    y=X**(-delta)*cmath.exp(1j*gamma*math.log(X))
    # D Y exact multiplier for mode
    Dy=(-delta+1j*gamma)*y
    assert abs(abs(Dy)/abs(y)-abs(-delta+1j*gamma))<1e-12
    return complex(Dy/y)

def check_scale_difference():
    delta=0.19
    gamma=2.7
    c=1.7
    coeff=c**(-delta+1j*gamma)-1
    X1=1e5; X2=1e9
    def diff_amp(X):
        y=(c*X)**(-delta)*cmath.exp(1j*gamma*math.log(c*X))
        z=X**(-delta)*cmath.exp(1j*gamma*math.log(X))
        return abs(y-z)
    slope=-math.log(diff_amp(X2)/diff_amp(X1))/math.log(X2/X1)
    assert abs(slope-delta)<1e-12
    return complex(coeff),slope

def check_analytic_leading_degree():
    # Phi(z)=exp(z)-1-z has leading degree 2.
    delta=0.11
    # use high precision-esque small but not too small real values
    X1=1e3; X2=1e5
    def phi(X):
        z=X**(-delta)
        return math.expm1(z)-z
    slope=-math.log(phi(X2)/phi(X1))/math.log(X2/X1)
    # finite-X slope approaches 2 delta; allow correction
    assert abs(slope-2*delta)<0.015,(slope,2*delta)
    return slope,2*delta

def check_ratio():
    delta=0.21
    c1=2.3
    c2=-0.7
    for X in [1e3,1e6,1e12]:
        y1=c1*X**(-delta)
        y2=c2*X**(-delta)
        assert abs(y1/y2-c1/c2)<1e-12
    return c1/c2

def check_adaptive_threshold():
    delta=0.18
    d=0.24
    # single sinusoid. Fraction above threshold tends to 1 except near phase zeros
    # once amplitude ratio X^(d-delta) is large.
    X=1e12
    R=X**(d-delta)
    threshold_ratio=1/R
    # fraction of t on [0,2pi] with |cos t| > threshold_ratio
    frac=1-(2/math.pi)*math.asin(min(1,threshold_ratio))
    assert frac>0.8
    return R,frac

def check_exponent_map():
    for beta in [0.51,0.63,0.8,0.95]:
        delta=1-beta
        for W in [0.7,1,2,4.3]:
            s=W*delta
            recovered=1-s/W
            assert abs(recovered-beta)<1e-14
            kappa=2*delta
            assert abs(2*s/W-kappa)<1e-14
    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("weighted_degree",check_weighted_degree())
    print("log_derivative",check_log_derivative())
    print("scale_difference",check_scale_difference())
    print("analytic_leading_degree",check_analytic_leading_degree())
    print("ratio",check_ratio())
    print("adaptive_threshold",check_adaptive_threshold())
    print("exponent_map",check_exponent_map())
    print("source_delimiters",check_source())
    print("PASS")
