{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e264944c6f01",
   "metadata": {},
   "source": [
    "# Evaluating trial states at a contact attraction\n",
    "Two normalisable trial families compete for the ground state of a one-dimensional attractive contact potential. Each can vary its width. Predict which family can satisfy the derivative jump before inspecting its lowest energy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2804e82362dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "strength=1.         # units in which hbar = mass = 1\n",
    "scale=1.0           # common multiplier applied to optimal inverse decay lengths\n",
    "assert strength>0 and scale>0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b8af474faf4",
   "metadata": {},
   "source": [
    "## Compute the energy functional\n",
    "Use the quadratic-form kinetic energy ∫|ψ'|²/2 and the contact expectation −g|ψ(0)|². The Gaussian parameter b is defined by exp(−b² x²/2); the cusp parameter a appears in exp(−a|x|)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4f2b9a2f275",
   "metadata": {},
   "outputs": [],
   "source": [
    "b=2*strength/np.sqrt(np.pi)*scale; a=strength*scale\n",
    "def gaussian_energy(b):return b*b/4-strength*b/np.sqrt(np.pi)\n",
    "def cusp_energy(a):return a*a/2-strength*a\n",
    "print(\"Gaussian, cusp, and exact ground energies:\",gaussian_energy(b),cusp_energy(a),-.5*strength**2)\n",
    "assert gaussian_energy(b)>=-.5*strength**2-1e-12 and cusp_energy(a)>=-.5*strength**2-1e-12"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18cd3c57e1dc",
   "metadata": {},
   "source": [
    "## Compare shapes and upper bounds\n",
    "Plot probability densities and energies versus the same scaled inverse-length parameter. Inspect where the smooth trial loses contact probability as it spreads its kinetic cost."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2271f4b6afb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax=plt.subplots(1,2,figsize=(9,3.9),layout=\"constrained\")\n",
    "x=np.linspace(-4/strength,4/strength,401); v=np.linspace(.03,2.75,180)*strength\n",
    "ax[0].plot(x,b/np.sqrt(np.pi)*np.exp(-(b*x)**2),color=\"#375b9a\",label=\"Gaussian\")\n",
    "ax[0].plot(x,a*np.exp(-2*a*abs(x)),color=\"#a23d21\",label=\"Cusp trial\")\n",
    "ax[0].set(xlabel=\"Position (model units)\",ylabel=\"Probability per unit length\",title=\"Behaviour at the contact\");ax[0].legend(fontsize=8)\n",
    "ax[1].plot(v/strength,gaussian_energy(v)/strength**2,color=\"#375b9a\",label=\"Gaussian\")\n",
    "ax[1].plot(v/strength,cusp_energy(v)/strength**2,color=\"#a23d21\",label=\"Cusp trial\")\n",
    "ax[1].axhline(-.5,color=\"#175868\",ls=\":\",label=\"Exact ground\")\n",
    "ax[1].set(xlabel=\"Inverse length / coupling\",ylabel=\"Energy / coupling squared\",title=\"Optimising one length scale\");ax[1].legend(fontsize=8)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
