{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "560cf69a",
            "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": "eb93dd0c",
            "cell_type": "markdown",
            "source": "Use cubic interpolation on Chebyshev nodes:\n",
            "metadata": {}
        },
        {
            "id": "7c729982",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport matplotlib.pyplot as plt\ndef cheb_nodes(N):\n    jj = 2.*np.arange(N) + 1\n    x = np.cos(np.pi * jj / 2 / N)[::-1]\n    return x",
            "outputs": []
        },
        {
            "id": "f04b0d8a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = cheb_nodes(20)\ny = np.sqrt(1 - x**2)",
            "outputs": []
        },
        {
            "id": "c96b7fb9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.interpolate import BSpline, make_interp_spline\nb = make_interp_spline(x, y)\nnp.allclose(b(x), y)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "50886643",
            "cell_type": "markdown",
            "source": "Note that the default is a cubic spline with a not-a-knot boundary condition\n",
            "metadata": {}
        },
        {
            "id": "b9950f29",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "b.k",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "3"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "0058f790",
            "cell_type": "markdown",
            "source": "Here we use a 'natural' spline, with zero 2nd derivatives at edges:\n",
            "metadata": {}
        },
        {
            "id": "176e47cc",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "l, r = [(2, 0.0)], [(2, 0.0)]\nb_n = make_interp_spline(x, y, bc_type=(l, r))  # or, bc_type=\"natural\"\nnp.allclose(b_n(x), y)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a1b32e20",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x0, x1 = x[0], x[-1]\nnp.allclose([b_n(x0, 2), b_n(x1, 2)], [0, 0])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "32fc8130",
            "cell_type": "markdown",
            "source": "Interpolation of parametric curves is also supported. As an example, we\ncompute a discretization of a snail curve in polar coordinates\n",
            "metadata": {}
        },
        {
            "id": "463b0cb8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "phi = np.linspace(0, 2.*np.pi, 40)\nr = 0.3 + np.cos(phi)\nx, y = r*np.cos(phi), r*np.sin(phi)  # convert to Cartesian coordinates",
            "outputs": []
        },
        {
            "id": "273a109e",
            "cell_type": "markdown",
            "source": "Build an interpolating curve, parameterizing it by the angle\n",
            "metadata": {}
        },
        {
            "id": "1c08a7f7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "spl = make_interp_spline(phi, np.c_[x, y])",
            "outputs": []
        },
        {
            "id": "a6e6d387",
            "cell_type": "markdown",
            "source": "Evaluate the interpolant on a finer grid (note that we transpose the result\nto unpack it into a pair of x- and y-arrays)\n",
            "metadata": {}
        },
        {
            "id": "5c97a10f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "phi_new = np.linspace(0, 2.*np.pi, 100)\nx_new, y_new = spl(phi_new).T",
            "outputs": []
        },
        {
            "id": "561c208c",
            "cell_type": "markdown",
            "source": "Plot the result\n",
            "metadata": {}
        },
        {
            "id": "6ebdaf26",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.plot(x, y, 'o')\nplt.plot(x_new, y_new, '-')\nplt.show()",
            "outputs": []
        },
        {
            "id": "74dbb28a",
            "cell_type": "markdown",
            "source": "Build a B-spline curve with 2 dimensional y\n",
            "metadata": {}
        },
        {
            "id": "41c91632",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = np.linspace(0, 2*np.pi, 10)\ny = np.array([np.sin(x), np.cos(x)])",
            "outputs": []
        },
        {
            "id": "5bf03580",
            "cell_type": "markdown",
            "source": "Periodic condition is satisfied because y coordinates of points on the ends\nare equivalent\n",
            "metadata": {}
        },
        {
            "id": "c5c2587b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ax = plt.axes(projection='3d')\nxx = np.linspace(0, 2*np.pi, 100)\nbspl = make_interp_spline(x, y, k=5, bc_type='periodic', axis=1)\nax.plot3D(xx, *bspl(xx))\nax.scatter3D(x, *y, color='red')\nplt.show()",
            "outputs": []
        }
    ]
}