{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "approximatio-00",
   "metadata": {},
   "source": [
    "# Three Gaussian-weighted samples of a spectral response\n",
    "A dimensionless variable has probability density exp(−x²)/√π. Estimate its mean response to cos(ωx) by a three-node Hermite rule, then compare with the exact Gaussian Fourier transform. Vary the carrier and see whether exact low moments protect every observable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "approximatio-01",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "omega = 2.0\n",
    "assert 0 <= omega <= 10\n",
    "nodes=np.array([-np.sqrt(1.5),0,np.sqrt(1.5)])\n",
    "weights=np.array([1,4,1])/6\n",
    "def estimate(w):\n",
    "    return np.sum(weights*np.cos(np.asarray(w)[...,None]*nodes),axis=-1)\n",
    "for degree,value in enumerate([1.,0.,.5,0.,.75,0.]):\n",
    "    assert np.isclose(weights@(nodes**degree),value)\n",
    "exact=lambda w:np.exp(-np.asarray(w)**2/4)\n",
    "print(f\"Three samples {estimate(omega):.8f}, exact response {exact(omega):.8f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "approximatio-02",
   "metadata": {},
   "source": [
    "## Inspect what the nodes measure\n",
    "The curve on the left gives the density-weighted integrand. The vertical lines locate the Hermite sampling coordinates. The right panel shows the exact and discrete responses over a wider range of frequency."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "approximatio-03",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,(shape,response)=plt.subplots(1,2,figsize=(10,4),layout=\"constrained\")\n",
    "x=np.linspace(-3.2,3.2,450)\n",
    "shape.plot(x,np.exp(-x*x)*np.cos(omega*x)/np.sqrt(np.pi),color=\"#145c71\")\n",
    "for n in nodes:shape.axvline(n,color=\"#b94626\",ls=\":\",lw=1.5)\n",
    "shape.set(xlabel=\"x in Gaussian scale units\",ylabel=\"Weighted integrand\",title=f\"Three sampling locations at ω = {omega:g}\")\n",
    "ww=np.linspace(0,10,401)\n",
    "response.plot(ww,exact(ww),color=\"#145c71\",label=\"continuous Gaussian\")\n",
    "response.plot(ww,estimate(ww),color=\"#b94626\",ls=\"--\",label=\"three nodes\")\n",
    "response.plot(omega,estimate(omega),\"o\",color=\"#b94626\")\n",
    "response.set(xlabel=\"Carrier frequency ω\",ylabel=\"Mean cosine response\",title=\"Moment exactness has a frequency range\")\n",
    "response.legend()\n",
    "for ax in (shape,response):ax.grid(alpha=.2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "approximatio-04",
   "metadata": {},
   "source": [
    "## Independent reference\n",
    "Use NumPy's higher-order Gauss–Hermite rule and remove its unnormalised weight √π. At large frequencies three nodes cannot resolve phase cancellations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "approximatio-05",
   "metadata": {},
   "outputs": [],
   "source": [
    "xh,wh=np.polynomial.hermite.hermgauss(64)\n",
    "dense=np.sum(wh*np.cos(omega*xh))/np.sqrt(np.pi)\n",
    "assert abs(dense-exact(omega))<2e-12\n",
    "print(\"64-node check:\",dense)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
