{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "0334391d",
            "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": "e32d78c7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.special import roots_legendre, eval_legendre\nroots, weights = roots_legendre(9)",
            "outputs": []
        },
        {
            "id": "a1d35df6",
            "cell_type": "markdown",
            "source": "``roots`` holds the roots, and ``weights`` holds the weights for\nGauss-Legendre quadrature.\n",
            "metadata": {}
        },
        {
            "id": "a61e25ba",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "roots",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([-0.96816024, -0.83603111, -0.61337143, -0.32425342,  0.        ,\n        0.32425342,  0.61337143,  0.83603111,  0.96816024])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2f8d589b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "weights",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.08127439, 0.18064816, 0.2606107 , 0.31234708, 0.33023936,\n       0.31234708, 0.2606107 , 0.18064816, 0.08127439])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "360954b1",
            "cell_type": "markdown",
            "source": "Verify that we have the roots by evaluating the degree 9 Legendre\npolynomial at ``roots``.  All the values are approximately zero:\n",
            "metadata": {}
        },
        {
            "id": "cb3803ce",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "eval_legendre(9, roots)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([-8.88178420e-16, -2.22044605e-16,  1.11022302e-16,  1.11022302e-16,\n        0.00000000e+00, -5.55111512e-17, -1.94289029e-16,  1.38777878e-16,\n       -8.32667268e-17])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "b05fb95b",
            "cell_type": "markdown",
            "source": "Here we'll show how the above values can be used to estimate the\nintegral from 1 to 2 of f(t) = t + 1/t with Gauss-Legendre\nquadrature [Rf56b2625d086-GL]_.  First define the function and the integration\nlimits.\n",
            "metadata": {}
        },
        {
            "id": "4735536e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "def f(t):\n   return t + 1/t\n\na = 1\nb = 2",
            "outputs": []
        },
        {
            "id": "4edca92c",
            "cell_type": "markdown",
            "source": "We'll use ``integral(f(t), t=a, t=b)`` to denote the definite integral\nof f from t=a to t=b.  The sample points in ``roots`` are from the\ninterval [-1, 1], so we'll rewrite the integral with the simple change\nof variable\n```\n\nx = 2/(b - a) * t - (a + b)/(b - a)\n\n```\nwith inverse\n```\n\nt = (b - a)/2 * x + (a + b)/2\n\n```\nThen\n```\n\nintegral(f(t), a, b) =\n(b - a)/2 * integral(f((b-a)/2*x + (a+b)/2), x=-1, x=1)\n\n```\nWe can approximate the latter integral with the values returned\nby `roots_legendre`.\n\nMap the roots computed above from [-1, 1] to [a, b].\n",
            "metadata": {}
        },
        {
            "id": "3b6d32b7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "t = (b - a)/2 * roots + (a + b)/2",
            "outputs": []
        },
        {
            "id": "1f7e9431",
            "cell_type": "markdown",
            "source": "Approximate the integral as the weighted sum of the function values.\n",
            "metadata": {}
        },
        {
            "id": "977460c2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "(b - a)/2 * f(t).dot(weights)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "2.1931471805599276"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "b7706643",
            "cell_type": "markdown",
            "source": "Compare that to the exact result, which is 3/2 + log(2):\n",
            "metadata": {}
        },
        {
            "id": "896dd062",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "1.5 + np.log(2)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "2.1931471805599454"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}