"""MQG test 21 predictor (frozen with the plan). Analytic inputs only; nothing is computed at a test point.
Prints, for every test point: x = sin(2 theta) sqrt(N/2), the choice-law prediction Phi(kappa x) with
kappa = sqrt(2/mu), the rival Phi(x), their difference and whether the point decides between them
(difference >= 0.025); the '+' direction phi_+(mu) and the slaving phase psi(mu) from linear theory.
Tolerances (from the non-test sandbox, sandbox_bias.py): delta_K = 0.02, delta_C = 0.01, slaving 0.03 and 6 deg.
Usage: python -I mqg_test21_predict.py"""
import numpy as np
from math import erf, sqrt
Phi = lambda z: 0.5 * (1 + erf(z / sqrt(2)))
THETAS, NS, MUS = (0.005, 0.015, 0.03), (640, 1280, 2560, 5120), (1.5, 2.0, 2.5)
DECIDE = 0.025
print("linear theory: kappa = sqrt(2/mu); '+' direction phi_+ = arg[-(1+i lam)/(lam - i alpha)]; psi = atan2(lam, alpha)")
for mu in MUS:
    lam, al = np.sqrt(mu - 1), 1 - mu / 2
    ph = np.degrees(np.angle(-(1 + 1j * lam) / (lam - 1j * al)))
    print(f"  mu {mu}: lambda {lam:.4f}  kappa {np.sqrt(2/mu):.4f}  phi_+ {ph:.3f} deg  psi {np.degrees(np.arctan2(lam, al)):.3f} deg")
print("row  theta   N     mu   x       Phi(kx)  Phi(x)   diff     decides")
k = 0; ndec = 0
for th in THETAS:
    for N in NS:
        for mu in MUS:
            k += 1; x = np.sin(2 * th) * np.sqrt(N / 2); pk, p1 = Phi(np.sqrt(2 / mu) * x), Phi(x)
            dec = abs(pk - p1) >= DECIDE; ndec += dec
            print(f"MQG21-{k:02d} {th:<6} {N:5d} {mu:4} {x:7.4f}  {pk:.4f}   {p1:.4f}   {pk-p1:+.4f}  {'yes' if dec else '-'}")
print(f"deciding points for rival E: {ndec}")
print("collapse pairs (same x): (0.015,2560)~(0.03,640) and (0.015,5120)~(0.03,1280), at each mu")
