{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "32ecfa7e",
            "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": "defe204f",
            "cell_type": "markdown",
            "source": "Construct a design matrix for a B-spline\n",
            "metadata": {}
        },
        {
            "id": "0af2225b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.interpolate import make_interp_spline, BSpline\nimport numpy as np\nx = np.linspace(0, np.pi * 2, 4)\ny = np.sin(x)\nk = 3\nbspl = make_interp_spline(x, y, k=k)\ndesign_matrix = bspl.design_matrix(x, bspl.t, k)\ndesign_matrix.toarray()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "[[1.        , 0.        , 0.        , 0.        ],\n[0.2962963 , 0.44444444, 0.22222222, 0.03703704],\n[0.03703704, 0.22222222, 0.44444444, 0.2962963 ],\n[0.        , 0.        , 0.        , 1.        ]]"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "535b5a27",
            "cell_type": "markdown",
            "source": "Construct a design matrix for some vector of knots\n",
            "metadata": {}
        },
        {
            "id": "ac6a4fb3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "k = 2\nt = [-1, 0, 1, 2, 3, 4, 5, 6]\nx = [1, 2, 3, 4]\ndesign_matrix = BSpline.design_matrix(x, t, k).toarray()\ndesign_matrix",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "[[0.5, 0.5, 0. , 0. , 0. ],\n[0. , 0.5, 0.5, 0. , 0. ],\n[0. , 0. , 0.5, 0.5, 0. ],\n[0. , 0. , 0. , 0.5, 0.5]]"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "44237788",
            "cell_type": "markdown",
            "source": "This result is equivalent to the one created in the sparse format\n",
            "metadata": {}
        },
        {
            "id": "77017274",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "c = np.eye(len(t) - k - 1)\ndesign_matrix_gh = BSpline(t, c, k)(x)\nnp.allclose(design_matrix, design_matrix_gh, atol=1e-14)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}