"""Finite Hermite intensity in its bulk and edge scaling; Python 3, NumPy, SciPy."""
import numpy as np
from scipy.integrate import quad, dblquad
from scipy.special import airy


def intensity(n, x):
    """K_n(x,x) for Gaussian weight exp(-x*x) with respect to dx."""
    prev = 0.0
    current = np.pi**(-0.25)*np.exp(-x*x/2)
    value = current*current
    for j in range(n-1):
        nxt = np.sqrt(2/(j+1))*x*current - np.sqrt(j/(j+1))*prev
        prev, current = current, nxt
        value += current*current
    return value


def main():
    mass, _ = quad(lambda x: intensity(2,x), -np.inf, np.inf, epsabs=1e-12)
    assert abs(mass-2) < 2e-13
    for a in (0, 0.25, 0.5, 0.8, 1):
        overlap = np.array([[a, np.sqrt(3)*(a*a-a)],
                            [np.sqrt(3)*(a*a-a), 4*a**3-6*a*a+3*a]])
        gap, _ = dblquad(lambda y,x: 6*(x-y)**2,a,1,a,1)
        assert abs(np.linalg.det(np.eye(2)-overlap)-gap)<1e-14
        assert abs(gap-(1-a)**4)<1e-14
    print("edge positions s = -1, 0, 1; bulk density ratio at the origin")
    for n in (16,64,256,512):
        edge = np.sqrt(2*n)
        scale = 1/(np.sqrt(2)*n**(1/6))
        vals = [scale*intensity(n,edge+scale*s) for s in (-1,0,1)]
        print(n, *(f"{v:.10f}" for v in vals), f"{np.pi*intensity(n,0)/edge:.10f}")
    for s in (-1,0,1):
        ai, aip, _, _ = airy(s)
        print("Airy intensity",s,f"{aip*aip-s*ai*ai:.10f}")


if __name__ == "__main__":
    main()
