{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "75f68490",
            "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": "8d3cd5b0",
            "cell_type": "markdown",
            "source": "To calculate the derivatives of a B-spline there are several aproaches. \nIn this example, we will demonstrate that `spalde` is equivalent to\ncalling `splev` and `splder`.\n",
            "metadata": {}
        },
        {
            "id": "ebe9f152",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.interpolate import BSpline, spalde, splder, splev",
            "outputs": []
        },
        {
            "id": "237f7ba5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "# Store characteristic parameters of a B-spline\ntck = ((-2, -2, -2, -2, -1, 0, 1, 2, 2, 2, 2),  # knots\n       (0, 0, 0, 6, 0, 0, 0),  # coefficients\n       3)  # degree (cubic)\n# Instance a B-spline object\n# `BSpline` objects are preferred, except for spalde()\nbspl = BSpline(tck[0], tck[1], tck[2])\n# Generate extra points to get a smooth curve\nx = np.linspace(min(tck[0]), max(tck[0]), 100)",
            "outputs": []
        },
        {
            "id": "dc177d75",
            "cell_type": "markdown",
            "source": "Evaluate the curve and all derivatives\n",
            "metadata": {}
        },
        {
            "id": "73a3748e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "# The order of derivative must be less or equal to k, the degree of the spline\n# Method 1: spalde()\nf1_y_bsplin = [spalde(i, tck)[0] for i in x ]  # The B-spline itself\nf1_y_deriv1 = [spalde(i, tck)[1] for i in x ]  # 1st derivative\nf1_y_deriv2 = [spalde(i, tck)[2] for i in x ]  # 2nd derivative\nf1_y_deriv3 = [spalde(i, tck)[3] for i in x ]  # 3rd derivative\n# You can reach the same result by using `splev`and `splder`\nf2_y_deriv3 = splev(x, bspl, der=3)\nf3_y_deriv3 = splder(bspl, n=3)(x)",
            "outputs": []
        },
        {
            "id": "91a5f0e3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "# Generate a figure with three axes for graphic comparison\nfig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(16, 5))\nsuptitle = fig.suptitle(f'Evaluate a B-spline and all derivatives')\n# Plot B-spline and all derivatives using the three methods\norders = range(4)\nlinetypes = ['-', '--', '-.', ':']\nlabels = ['B-Spline', '1st deriv.', '2nd deriv.', '3rd deriv.']\nfunctions = ['splev()', 'splder()', 'spalde()']\nfor order, linetype, label in zip(orders, linetypes, labels):\n    ax1.plot(x, splev(x, bspl, der=order), linetype, label=label)\n    ax2.plot(x, splder(bspl, n=order)(x), linetype, label=label)\n    ax3.plot(x, [spalde(i, tck)[order] for i in x], linetype, label=label)\nfor ax, function in zip((ax1, ax2, ax3), functions):\n    ax.set_title(function)\n    ax.legend()\nplt.tight_layout()\nplt.show()",
            "outputs": []
        }
    ]
}