{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {},
  "_ordered_sections": [],
  "item_file": null,
  "item_line": null,
  "item_type": null,
  "aliases": [],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [],
  "signature": null,
  "references": null,
  "qa": "tutorial:interpolate:extrapolation_examples",
  "arbitrary": [
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Handling of extrapolation---evaluation of the interpolators on query points outside of the domain of interpolated data---is not fully consistent among different routines in "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "scipy.interpolate",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "*",
                "kind": "api",
                "path": "scipy.interpolate"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". Different interpolators use different sets of keyword arguments to control the behavior outside of the data domain: some use "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "extrapolate=True/False/None"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", some allow the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "fill_value"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " keyword. Refer to the API documentation for details for each specific interpolation routine."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Depending on a particular problem, the available keywords may or may not be sufficient. Special attention needs to be paid to extrapolation of non-linear interpolants. Very often the extrapolated results make less sense with increasing distance from the data domain. This is of course to be expected: an interpolant only knows the data within the data domain."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "When the default extrapolated results are not adequate, users need to implement the desired extrapolation mode themselves."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "In this tutorial, we consider several worked examples where we demonstrate both the use of available keywords and manual implementation of desired extrapolation modes. These examples may or may not be applicable to your particular problem; they are not necessarily best practices; and they are deliberately pared down to bare essentials needed to demonstrate the main ideas, in a hope that they serve as an inspiration for your handling of your particular problem."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Extrapolation tips and tricks"
        }
      ],
      "level": 0,
      "target": "tutorial-extrapolation"
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "TL;DR: Use "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "fill_value=(left, right)"
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "numpy.interp",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "numpy",
                "version": "*",
                "kind": "api",
                "path": "numpy:interp"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " uses constant extrapolation, and defaults to extending the first and last values of the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "y"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " array in the interpolation interval: the output of "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "np.interp(xnew, x, y)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "y[0]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " for "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "xnew < x[0]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "y[-1]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " for "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "xnew > x[-1]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "By default, "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "interp1d",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " refuses to extrapolate, and raises a "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "ValueError"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " when evaluated on a data point outside of the interpolation range. This can be switched off by the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "bounds_error=False"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " argument: then "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "interp1d"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " sets the out-of-range values with the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "fill_value"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", which is "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "nan"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " by default."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "To mimic the behavior of "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "numpy.interp",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "numpy",
                "version": "*",
                "kind": "api",
                "path": "numpy:interp"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " with "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "interp1d",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", you can use the fact that it supports a 2-tuple as the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "fill_value"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". The tuple elements are then used to fill for "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "xnew < min(x)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "x > max(x)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", respectively. For multidimensional "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "y"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", these elements must have the same shape as "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "y"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " or be broadcastable to it."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "To illustrate:"
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.interpolate import interp1d\n\nx = np.linspace(0, 1.5*np.pi, 11)\ny = np.column_stack((np.cos(x), np.sin(x)))   # y.shape is (11, 2)\n\nfunc = interp1d(x, y,\n                axis=0,  # interpolate along columns\n                bounds_error=False,\n                kind='linear',\n                fill_value=(y[0], y[-1]))\nxnew = np.linspace(-np.pi, 2.5*np.pi, 51)\nynew = func(xnew)\n\nfix, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))\nax1.plot(xnew, ynew[:, 0])\nax1.plot(x, y[:, 0], 'o')\n\nax2.plot(xnew, ynew[:, 1])\nax2.plot(x, y[:, 1], 'o')\nplt.tight_layout()",
          "execution_status": null
        },
        {
          "__type": "Figure",
          "__tag": 4024,
          "value": {
            "__type": "RefInfo",
            "__tag": 4000,
            "module": "scipy",
            "version": "1.17.1",
            "kind": "assets",
            "path": "fig-plot-2.png"
          }
        }
      ],
      "title": [
        {
          "__type": "InlineRole",
          "__tag": 4003,
          "value": "interp1d",
          "domain": null,
          "role": null,
          "inventory": null
        },
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "replicate "
        },
        {
          "__type": "InlineRole",
          "__tag": 4003,
          "value": "numpy.interp",
          "domain": null,
          "role": null,
          "inventory": null
        },
        {
          "__type": "Text",
          "__tag": 4046,
          "value": " left and right fill values"
        }
      ],
      "level": 1,
      "target": "tutorial-extrapolation-left-right"
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "CubicSpline",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " needs two extra boundary conditions, which are controlled by the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "bc_type"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " parameter. This parameter can either list explicit values of derivatives at the edges, or use helpful aliases. For instance, "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "bc_type=\"clamped\""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " sets the first derivatives to zero, "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "bc_type=\"natural\""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " sets the second derivatives to zero (two other recognized string values are \"periodic\" and \"not-a-knot\")"
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "While the extrapolation is controlled by the boundary condition, the relation is not very intuitive. For instance, one can expect that for "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "bc_type=\"natural\""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", the extrapolation is linear. This expectation is too strong: each boundary condition sets the derivatives at a single point, "
            },
            {
              "__type": "Emphasis",
              "__tag": 4047,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "at the boundary"
                }
              ]
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " only. Extrapolation is done from the first and last polynomial pieces, which — for a natural spline — is a cubic with a zero second derivative at a given point."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "One other way of seeing why this expectation is too strong is to consider a dataset with only three data points, where the spline has two polynomial pieces. To extrapolate linearly, this expectation implies that both of these pieces are linear. But then, two linear pieces cannot match at a middle point with a continuous 2nd derivative! (Unless of course, if all three data points actually lie on a single straight line)."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "To illustrate the behavior we consider a synthetic dataset and compare several boundary conditions:"
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.interpolate import CubicSpline\n\nxs = [1, 2, 3, 4, 5, 6, 7, 8]\nys = [4.5, 3.6, 1.6, 0.0, -3.3, -3.1, -1.8, -1.7]\n\nnotaknot = CubicSpline(xs, ys, bc_type='not-a-knot')\nnatural = CubicSpline(xs, ys, bc_type='natural')\nclamped = CubicSpline(xs, ys, bc_type='clamped')\nxnew = np.linspace(min(xs) - 4, max(xs) + 4, 101)\n\nsplines = [notaknot, natural, clamped]\ntitles = ['not-a-knot', 'natural', 'clamped']\n\nfig, axs = plt.subplots(3, 3, figsize=(12, 12))\nfor i in [0, 1, 2]:\n    for j, spline, title in zip(range(3), splines, titles):\n        axs[i, j].plot(xs, spline(xs, nu=i),'o')\n        axs[i, j].plot(xnew, spline(xnew, nu=i),'-')\n        axs[i, j].set_title(f'{title}, deriv={i}')\n\nplt.tight_layout()\nplt.show()",
          "execution_status": null
        },
        {
          "__type": "Figure",
          "__tag": 4024,
          "value": {
            "__type": "RefInfo",
            "__tag": 4000,
            "module": "scipy",
            "version": "1.17.1",
            "kind": "assets",
            "path": "fig-plot-3.png"
          }
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "It is clearly seen that the natural spline does have the zero second derivative at the boundaries, but extrapolation is non-linear. "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "bc_type=\"clamped\""
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " shows a similar behavior: first derivatives are only equal to zero exactly at the boundary. In all cases, extrapolation is done by extending the first and last polynomial pieces of the spline, whatever they happen to be."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "One possible way to force the extrapolation is to extend the interpolation domain to add first and last polynomial pieces which have desired properties."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Here we use "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "extend"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " method of the "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "CubicSpline",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " superclass, "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "PPoly",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", to add two extra breakpoints and to make sure that the additional polynomial pieces maintain the values of the derivatives. Then the extrapolation proceeds using these two additional intervals."
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.interpolate import CubicSpline\n\ndef add_boundary_knots(spline):\n    \"\"\"\n    Add knots infinitesimally to the left and right.\n\n    Additional intervals are added to have zero 2nd and 3rd derivatives,\n    and to maintain the first derivative from whatever boundary condition\n    was selected. The spline is modified in place.\n    \"\"\"\n    # determine the slope at the left edge\n    leftx = spline.x[0]\n    lefty = spline(leftx)\n    leftslope = spline(leftx, nu=1)\n\n    # add a new breakpoint just to the left and use the\n    # known slope to construct the PPoly coefficients.\n    leftxnext = np.nextafter(leftx, leftx - 1)\n    leftynext = lefty + leftslope*(leftxnext - leftx)\n    leftcoeffs = np.array([0, 0, leftslope, leftynext])\n    spline.extend(leftcoeffs[..., None], np.r_[leftxnext])\n\n    # repeat with additional knots to the right\n    rightx = spline.x[-1]\n    righty = spline(rightx)\n    rightslope = spline(rightx,nu=1)\n    rightxnext = np.nextafter(rightx, rightx + 1)\n    rightynext = righty + rightslope * (rightxnext - rightx)\n    rightcoeffs = np.array([0, 0, rightslope, rightynext])\n    spline.extend(rightcoeffs[..., None], np.r_[rightxnext])\n\nxs = [1, 2, 3, 4, 5, 6, 7, 8]\nys = [4.5, 3.6, 1.6, 0.0, -3.3, -3.1, -1.8, -1.7]\n\nnotaknot = CubicSpline(xs,ys, bc_type='not-a-knot')\n# not-a-knot does not require additional intervals\n\nnatural = CubicSpline(xs,ys, bc_type='natural')\n# extend the natural natural spline with linear extrapolating knots\nadd_boundary_knots(natural)\n\nclamped = CubicSpline(xs,ys, bc_type='clamped')\n# extend the clamped spline with constant extrapolating knots\nadd_boundary_knots(clamped)\n\nxnew = np.linspace(min(xs) - 5, max(xs) + 5, 201)\n\nfig, axs = plt.subplots(3, 3,figsize=(12,12))\n\nsplines = [notaknot, natural, clamped]\ntitles = ['not-a-knot', 'natural', 'clamped']\n\nfor i in [0, 1, 2]:\n    for j, spline, title in zip(range(3), splines, titles):\n        axs[i, j].plot(xs, spline(xs, nu=i),'o')\n        axs[i, j].plot(xnew, spline(xnew, nu=i),'-')\n        axs[i, j].set_title(f'{title}, deriv={i}')\n\nplt.tight_layout()\nplt.show()",
          "execution_status": null
        },
        {
          "__type": "Figure",
          "__tag": 4024,
          "value": {
            "__type": "RefInfo",
            "__tag": 4000,
            "module": "scipy",
            "version": "1.17.1",
            "kind": "assets",
            "path": "fig-plot-4.png"
          }
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "CubicSpline extend the boundary conditions"
        }
      ],
      "level": 1,
      "target": "tutorial-extrapolation-cubicspline-extend"
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The previous trick of extending the interpolation domain relies on the "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "CubicSpline.extend",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " method. A somewhat more general alternative is to implement a wrapper which handles the out-of-bounds behavior explicitly. Let us consider a worked example."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Manually implement the asymptotics"
        }
      ],
      "level": 1,
      "target": "tutorial-extrapolation-asymptotics"
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Suppose we want to solve at a given value of "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "a"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " the equation"
            }
          ]
        },
        {
          "__type": "Math",
          "__tag": 4058,
          "value": "a x = 1/\\tan{x}\\;."
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "(One application where these kinds of equations appear is solving for energy levels of a quantum particle). For simplicity, let’s only consider "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "x\\in (0, \\pi/2)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Solving this equation "
            },
            {
              "__type": "Emphasis",
              "__tag": 4047,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "once"
                }
              ]
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is straightforward:"
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.optimize import brentq\n\ndef f(x, a):\n    return a*x - 1/np.tan(x)\n\na = 3\nx0 = brentq(f, 1e-16, np.pi/2, args=(a,))   # here we shift the left edge\n                                            # by a machine epsilon to avoid\n                                            # a division by zero at x=0\nxx = np.linspace(0.2, np.pi/2, 101)\nplt.plot(xx, a*xx, '--')\nplt.plot(xx, 1/np.tan(xx), '--')\nplt.plot(x0, a*x0, 'o', ms=12)\nplt.text(0.1, 0.9, fr'$x_0 = {x0:.3f}$',\n               transform=plt.gca().transAxes, fontsize=16)\nplt.show()",
          "execution_status": null
        },
        {
          "__type": "Figure",
          "__tag": 4024,
          "value": {
            "__type": "RefInfo",
            "__tag": 4000,
            "module": "scipy",
            "version": "1.17.1",
            "kind": "assets",
            "path": "fig-plot-5.png"
          }
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "However, if we need to solve it multiple times (e.g. to find "
            },
            {
              "__type": "Emphasis",
              "__tag": 4047,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "a series"
                }
              ]
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " of roots due to periodicity of the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "tan"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " function), repeated calls to "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "scipy.optimize.brentq",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "*",
                "kind": "api",
                "path": "scipy.optimize._zeros_py:brentq"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " become prohibitively expensive."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "To circumvent this difficulty, we tabulate "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "y = ax - 1/\\tan{x}"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and interpolate it on the tabulated grid. In fact, we will use the "
            },
            {
              "__type": "Emphasis",
              "__tag": 4047,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "inverse"
                }
              ]
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " interpolation: we interpolate the values of "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "x"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " versus "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "у"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". This way, solving the original equation becomes simply an evaluation of the interpolated function at zero "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "y"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " argument."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "To improve the interpolation accuracy we will use the knowledge of the derivatives of the tabulated function. We will use "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "BPoly.from_derivatives",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to construct a cubic interpolant (equivalently, we could have used "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "CubicHermiteSpline",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ")"
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.interpolate import BPoly\n\ndef f(x, a):\n    return a*x - 1/np.tan(x)\n\nxleft, xright = 0.2, np.pi/2\nx = np.linspace(xleft, xright, 11)\n\nfig, ax = plt.subplots(1, 2, figsize=(12, 4))\n\nfor j, a in enumerate([3, 93]):\n    y = f(x, a)\n    dydx = a + 1./np.sin(x)**2    # d(ax - 1/tan(x)) / dx\n    dxdy = 1 / dydx               # dx/dy = 1 / (dy/dx)\n\n    xdx = np.c_[x, dxdy]\n    spl = BPoly.from_derivatives(y, xdx)   # inverse interpolation\n\n    yy = np.linspace(f(xleft, a), f(xright, a), 51)\n    ax[j].plot(yy, spl(yy), '--')\n    ax[j].plot(y, x, 'o')\n    ax[j].set_xlabel(r'$y$')\n    ax[j].set_ylabel(r'$x$')\n    ax[j].set_title(rf'$a = {a}$')\n\n    ax[j].plot(0, spl(0), 'o', ms=12)\n    ax[j].text(0.1, 0.85, fr'$x_0 = {spl(0):.3f}$',\n               transform=ax[j].transAxes, fontsize=18)\n    ax[j].grid(True)\nplt.tight_layout()\nplt.show()",
          "execution_status": null
        },
        {
          "__type": "Figure",
          "__tag": 4024,
          "value": {
            "__type": "RefInfo",
            "__tag": 4000,
            "module": "scipy",
            "version": "1.17.1",
            "kind": "assets",
            "path": "fig-plot-6.png"
          }
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Note that for "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "a=3"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "spl(0)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " agrees with the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "brentq"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " call above, while for "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "a = 93"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", the difference is substantial. The reason the procedure starts failing for large "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "a"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is that the straight line "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "y = ax"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " tends towards the vertical axis, and the root of the original equation tends towards "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "x=0"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". Since we tabulated the original function at a finite grid, "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "spl(0)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " involves extrapolation for too-large values of "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "a"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". Relying on extrapolation is prone to losing accuracy and is best avoided."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "The setup"
        }
      ],
      "level": 2,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Looking at the original equation, we note that for "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "x\\to 0"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "\\tan(x) = x + O(x^3)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", and the original equation becomes"
            }
          ]
        },
        {
          "__type": "Math",
          "__tag": 4058,
          "value": "ax = 1/x  \\;,"
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "so that "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "x_0 \\approx 1/\\sqrt{a}"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " for "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "a \\gg 1"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "We will use this to cook up a class which switches from interpolation to using this known asymptotic behavior for out-of-range data. A bare-bones implementation may look like this"
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "class RootWithAsymptotics:\n   def __init__(self, a):\n\n       # construct the interpolant\n       xleft, xright = 0.2, np.pi/2\n       x = np.linspace(xleft, xright, 11)\n\n       y = f(x, a)\n       dydx = a + 1./np.sin(x)**2    # d(ax - 1/tan(x)) / dx\n       dxdy = 1 / dydx               # dx/dy = 1 / (dy/dx)\n\n       # inverse interpolation\n       self.spl = BPoly.from_derivatives(y, np.c_[x, dxdy])\n       self.a = a\n\n   def root(self):\n       out = self.spl(0)\n       asympt = 1./np.sqrt(self.a)\n       return np.where(spl.x.min() < asympt, out, asympt)",
          "execution_status": null
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "And then"
            }
          ]
        },
        {
          "__type": "Blockquote",
          "__tag": 4059,
          "children": [
            {
              "__type": "Code",
              "__tag": 4050,
              "value": ">>> r = RootWithAsymptotics(93)\n>>> r.root()\narray(0.10369517)",
              "execution_status": null
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "which differs from the extrapolated result and agrees with the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "brentq"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " call."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Note that this implementation is intentionally pared down. From the API perspective, you may want to instead implement the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "__call__"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " method so that the full dependence of "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "x"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " on "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "y"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is available. From the numerical perspective, more work is needed to make sure that the switch between interpolation and asymptotics occurs deep enough into the asymptotic regime, so that the resulting function is smooth enough at the switch-over point."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Also in this example we artificially limited the problem to only consider a single periodicity interval of the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "tan"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " function, and only dealt with "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "a > 0"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". For negative values of "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "a"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", we would need to implement the other asymptotics, for "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "x\\to \\pi"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "However the basic idea is the same."
            }
          ]
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Use the known asymptotics"
        }
      ],
      "level": 2,
      "target": null
    },
    {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The basic idea of implementing extrapolation manually in a wrapper class or function can be easily generalized to higher dimensions. As an example, we consider a C1-smooth interpolation problem of 2D data using "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "CloughTocher2DInterpolator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". By default, it fills the out of bounds values with "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "nan"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "s, and we want to instead use for each query point the value of its nearest neighbor."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Since "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "CloughTocher2DInterpolator",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " accepts either 2D data or a Delaunay triangulation of the data points, the efficient way of finding nearest neighbors of query points would be to construct the triangulation (using "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "scipy.spatial",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "*",
                "kind": "api",
                "path": "scipy.spatial"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " tools) and use it to find nearest neighbors on the convex hull of the data."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "We will instead use a simpler, naive method and rely on looping over the whole dataset using NumPy broadcasting."
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.interpolate import CloughTocher2DInterpolator as CT\n\ndef my_CT(xy, z):\n    \"\"\"CT interpolator + nearest-neighbor extrapolation.\n\n    Parameters\n    ----------\n    xy : ndarray, shape (npoints, ndim)\n        Coordinates of data points\n    z : ndarray, shape (npoints)\n        Values at data points\n\n    Returns\n    -------\n    func : callable\n        A callable object which mirrors the CT behavior,\n        with an additional neareast-neighbor extrapolation\n        outside of the data range.\n    \"\"\"\n    x = xy[:, 0]\n    y = xy[:, 1]\n    f = CT(xy, z)\n\n    # this inner function will be returned to a user\n    def new_f(xx, yy):\n        # evaluate the CT interpolator. Out-of-bounds values are nan.\n        zz = f(xx, yy)\n        nans = np.isnan(zz)\n\n        if nans.any():\n            # for each nan point, find its nearest neighbor\n            inds = np.argmin(\n                (x[:, None] - xx[nans])**2 +\n                (y[:, None] - yy[nans])**2\n                , axis=0)\n            # ... and use its value\n            zz[nans] = z[inds]\n        return zz\n\n    return new_f\n\n# Now illustrate the difference between the original ``CT`` interpolant\n# and ``my_CT`` on a small example:\n\nx = np.array([1, 1, 1, 2, 2, 2, 4, 4, 4])\ny = np.array([1, 2, 3, 1, 2, 3, 1, 2, 3])\nz = np.array([0, 7, 8, 3, 4, 7, 1, 3, 4])\n\nxy = np.c_[x, y]\nlut = CT(xy, z)\nlut2 = my_CT(xy, z)\n\nX = np.linspace(min(x) - 0.5, max(x) + 0.5, 71)\nY = np.linspace(min(y) - 0.5, max(y) + 0.5, 71)\nX, Y = np.meshgrid(X, Y)\n\nfig = plt.figure()\nax = fig.add_subplot(projection='3d')\n\nax.plot_wireframe(X, Y, lut(X, Y), label='CT')\nax.plot_wireframe(X, Y, lut2(X, Y), color='m',\n                  cstride=10, rstride=10, alpha=0.7, label='CT + n.n.')\n\nax.scatter(x, y, z,  'o', color='k', s=48, label='data')\nax.legend()\nplt.tight_layout()",
          "execution_status": null
        },
        {
          "__type": "Figure",
          "__tag": 4024,
          "value": {
            "__type": "RefInfo",
            "__tag": 4000,
            "module": "scipy",
            "version": "1.17.1",
            "kind": "assets",
            "path": "fig-plot-7.png"
          }
        }
      ],
      "title": [
        {
          "__type": "Text",
          "__tag": 4046,
          "value": "Extrapolation in "
        },
        {
          "__type": "InlineCode",
          "__tag": 4051,
          "value": "D > 1"
        }
      ],
      "level": 1,
      "target": "tutorial-extrapolation-CT_NN"
    }
  ],
  "local_refs": []
}