{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "c900110f",
            "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": "ef2bb34c",
            "cell_type": "markdown",
            "source": "Construct an interpolating spline and convert it to a `PPoly` instance\n",
            "metadata": {}
        },
        {
            "id": "177582f4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.interpolate import splrep, PPoly\nx = np.linspace(0, 1, 11)\ny = np.sin(2*np.pi*x)\ntck = splrep(x, y, s=0)\np = PPoly.from_spline(tck)\nisinstance(p, PPoly)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "0f726efe",
            "cell_type": "markdown",
            "source": "Note that this function only supports 1D splines out of the box.\n\nIf the ``tck`` object represents a parametric spline (e.g. constructed\nby `splprep` or a `BSpline` with ``c.ndim > 1``), you will need to loop\nover the dimensions manually.\n",
            "metadata": {}
        },
        {
            "id": "84c2b302",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.interpolate import splprep, splev\nt = np.linspace(0, 1, 11)\nx = np.sin(2*np.pi*t)\ny = np.cos(2*np.pi*t)\n(t, c, k), u = splprep([x, y], s=0)",
            "outputs": []
        },
        {
            "id": "a3faaa42",
            "cell_type": "markdown",
            "source": "Note that ``c`` is a list of two arrays of length 11.\n",
            "metadata": {}
        },
        {
            "id": "e778e0a3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "unew = np.arange(0, 1.01, 0.01)\nout = splev(unew, (t, c, k))",
            "outputs": []
        },
        {
            "id": "c21b2984",
            "cell_type": "markdown",
            "source": "To convert this spline to the power basis, we convert each\ncomponent of the list of b-spline coefficients, ``c``, into the\ncorresponding cubic polynomial.\n",
            "metadata": {}
        },
        {
            "id": "fa07e4f0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "polys = [PPoly.from_spline((t, cj, k)) for cj in c]\npolys[0].c.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(4, 14)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c900d0aa",
            "cell_type": "markdown",
            "source": "Note that the coefficients of the polynomials `polys` are in the\npower basis and their dimensions reflect just that: here 4 is the order\n(degree+1), and 14 is the number of intervals---which is nothing but\nthe length of the knot array of the original `tck` minus one.\n\nOptionally, we can stack the components into a single `PPoly` along\nthe third dimension:\n",
            "metadata": {}
        },
        {
            "id": "3bb47ab7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "cc = np.dstack([p.c for p in polys])    # has shape = (4, 14, 2)\npoly = PPoly(cc, polys[0].x)\nnp.allclose(poly(unew).T,     # note the transpose to match `splev`\n            out, atol=1e-15)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}