{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "5e0c26c0",
            "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": "36388e56",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy import integrate\nimport numpy as np\nimport matplotlib.pyplot as plt\nx = np.linspace(-2, 2, num=20)\ny = x**2\ny_int = integrate.cumulative_simpson(y, x=x, initial=0)\nfig, ax = plt.subplots()\nax.plot(x, y_int, 'ro', x, x**3/3 - (x[0])**3/3, 'b-')\nax.grid()\nplt.show()",
            "outputs": []
        },
        {
            "id": "6c83502d",
            "cell_type": "markdown",
            "source": "The output of `cumulative_simpson` is similar to that of iteratively\ncalling `simpson` with successively higher upper limits of integration, but\nnot identical.\n",
            "metadata": {}
        },
        {
            "id": "6a888b92",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "def cumulative_simpson_reference(y, x):\n    return np.asarray([integrate.simpson(y[:i], x=x[:i])\n                       for i in range(2, len(y) + 1)])\n\nrng = np.random.default_rng()\nx, y = rng.random(size=(2, 10))\nx.sort()\n\nres = integrate.cumulative_simpson(y, x=x)\nref = cumulative_simpson_reference(y, x)\nequal = np.abs(res - ref) < 1e-15\nequal  # not equal when `simpson` has even number of subintervals",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([False,  True, False,  True, False,  True, False,  True,  True])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "4f1f85cf",
            "cell_type": "markdown",
            "source": "This is expected: because `cumulative_simpson` has access to more\ninformation than `simpson`, it can typically produce more accurate\nestimates of the underlying integral over subintervals.\n",
            "metadata": {}
        }
    ]
}