{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "8faa1fae",
            "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": "7294b007",
            "cell_type": "markdown",
            "source": "Suppose that we want to bilinearly interpolate an exponentially decaying\nfunction in 2 dimensions.\n",
            "metadata": {}
        },
        {
            "id": "d4c80778",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.interpolate import RectBivariateSpline\ndef f(x, y):\n    return np.exp(-np.sqrt((x / 2) ** 2 + y**2))",
            "outputs": []
        },
        {
            "id": "b636551b",
            "cell_type": "markdown",
            "source": "We sample the function on a coarse grid and set up the interpolator. Note that\nthe default ``indexing=\"xy\"`` of meshgrid would result in an unexpected\n(transposed) result after interpolation.\n",
            "metadata": {}
        },
        {
            "id": "514e3ba6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "xarr = np.linspace(-3, 3, 21)\nyarr = np.linspace(-3, 3, 21)\nxgrid, ygrid = np.meshgrid(xarr, yarr, indexing=\"ij\")\nzdata = f(xgrid, ygrid)\nrbs = RectBivariateSpline(xarr, yarr, zdata, kx=1, ky=1)",
            "outputs": []
        },
        {
            "id": "4b54f038",
            "cell_type": "markdown",
            "source": "Next we sample the function along a diagonal slice through the coordinate space\non a finer grid using interpolation.\n",
            "metadata": {}
        },
        {
            "id": "77e4a132",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "xinterp = np.linspace(-3, 3, 201)\nyinterp = np.linspace(3, -3, 201)\nzinterp = rbs.ev(xinterp, yinterp)",
            "outputs": []
        },
        {
            "id": "e8626eca",
            "cell_type": "markdown",
            "source": "And check that the interpolation passes through the function evaluations as a\nfunction of the distance from the origin along the slice.\n",
            "metadata": {}
        },
        {
            "id": "dddb27bc",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nfig = plt.figure()\nax1 = fig.add_subplot(1, 1, 1)\nax1.plot(np.sqrt(xarr**2 + yarr**2), np.diag(zdata), \"or\")\nax1.plot(np.sqrt(xinterp**2 + yinterp**2), zinterp, \"-b\")\nplt.show()",
            "outputs": []
        }
    ]
}