{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "a630d576",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "1e951e37",
            "cell_type": "markdown",
            "source": "This example is the adapted version of ``fftlogtest.f`` which is provided\nin [2]. It evaluates the integral\n\n$$ \\int^\\infty_0 r^{\\mu+1} \\exp(-r^2/2) J_\\mu(kr) k dr = k^{\\mu+1} \\exp(-k^2/2) . $$",
            "metadata": {}
        },
        {
            "id": "189195bd",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import fft\nimport matplotlib.pyplot as plt",
            "outputs": []
        },
        {
            "id": "f779d2ff",
            "cell_type": "markdown",
            "source": "Parameters for the transform.\n",
            "metadata": {}
        },
        {
            "id": "618da8d7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "mu = 0.0                     # Order mu of Bessel function\nr = np.logspace(-7, 1, 128)  # Input evaluation points\ndln = np.log(r[1]/r[0])      # Step size\noffset = fft.fhtoffset(dln, initial=-6*np.log(10), mu=mu)\nk = np.exp(offset)/r[::-1]   # Output evaluation points",
            "outputs": []
        },
        {
            "id": "9d93c01e",
            "cell_type": "markdown",
            "source": "Define the analytical function.\n",
            "metadata": {}
        },
        {
            "id": "a96862b4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "def f(x, mu):\n    \"\"\"Analytical function: x^(mu+1) exp(-x^2/2).\"\"\"\n    return x**(mu + 1)*np.exp(-x**2/2)",
            "outputs": []
        },
        {
            "id": "5f6b0d8e",
            "cell_type": "markdown",
            "source": "Evaluate the function at ``r`` and compute the corresponding values at\n``k`` using FFTLog.\n",
            "metadata": {}
        },
        {
            "id": "1076e8ad",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a_r = f(r, mu)\nfht = fft.fht(a_r, dln, mu=mu, offset=offset)",
            "outputs": []
        },
        {
            "id": "523b5c30",
            "cell_type": "markdown",
            "source": "For this example we can actually compute the analytical response (which in\nthis case is the same as the input function) for comparison and compute the\nrelative error.\n",
            "metadata": {}
        },
        {
            "id": "3f687c10",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a_k = f(k, mu)\nrel_err = abs((fht-a_k)/a_k)",
            "outputs": []
        },
        {
            "id": "493a8740",
            "cell_type": "markdown",
            "source": "Plot the result.\n",
            "metadata": {}
        },
        {
            "id": "19878dcd",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "figargs = {'sharex': True, 'sharey': True, 'constrained_layout': True}\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4), **figargs)\nax1.set_title(r'$r^{\\mu+1}\\ \\exp(-r^2/2)$')\nax1.loglog(r, a_r, 'k', lw=2)\nax1.set_xlabel('r')\nax2.set_title(r'$k^{\\mu+1} \\exp(-k^2/2)$')\nax2.loglog(k, a_k, 'k', lw=2, label='Analytical')\nax2.loglog(k, fht, 'C3--', lw=2, label='FFTLog')\nax2.set_xlabel('k')\nax2.legend(loc=3, framealpha=1)\nax2.set_ylim([1e-10, 1e1])\nax2b = ax2.twinx()\nax2b.loglog(k, rel_err, 'C0', label='Rel. Error (-)')\nax2b.set_ylabel('Rel. Error (-)', color='C0')\nax2b.tick_params(axis='y', labelcolor='C0')\nax2b.legend(loc=4, framealpha=1)\nax2b.set_ylim([1e-9, 1e-3])\nplt.show()",
            "outputs": []
        }
    ]
}