# `interpolate.polyint`¶

## Module Contents¶

### Classes¶

 `_Interpolator1D`(self,xi=None,yi=None,axis=None) Common features in univariate interpolation `_Interpolator1DWithDerivatives`() `KroghInterpolator`(self,xi,yi,axis=0) Interpolating polynomial for a set of points. `BarycentricInterpolator`(self,xi,yi=None,axis=0) The interpolating polynomial for a set of points

### Functions¶

 `_isscalar`(x) Check whether x is if a scalar type, or 0-dim `krogh_interpolate`(xi,yi,x,der=0,axis=0) Convenience function for polynomial interpolation. `approximate_taylor_polynomial`(f,x,degree,scale,order=None) Estimate the Taylor polynomial of f at x by polynomial fitting. `barycentric_interpolate`(xi,yi,x,axis=0) Convenience function for polynomial interpolation.
`_isscalar`(x)

Check whether x is if a scalar type, or 0-dim

class `_Interpolator1D`(xi=None, yi=None, axis=None)

Common features in univariate interpolation

Deal with input data type and interpolation axis rolling. The actual interpolator can assume the y-data is of shape (n, r) where n is the number of x-points, and r the number of variables, and use self.dtype as the y-data type.

_y_axis
Axis along which the interpolation goes in the original array
_y_extra_shape
Additional trailing shape of the input arrays, excluding the interpolation axis.
dtype
Dtype of the y-data arrays. Can be set via set_dtype, which forces it to be float or complex.

__call__ _prepare_x _finish_y _reshape_yi _set_yi _set_dtype _evaluate

`__init__`(xi=None, yi=None, axis=None)
`__call__`(x)

Evaluate the interpolant

x : array_like
Points to evaluate the interpolant at.
y : array_like
Interpolated values. Shape is determined by replacing the interpolation axis in the original array with the shape of x.
`_evaluate`(x)

Actually evaluate the value of the interpolator.

`_prepare_x`(x)

Reshape input x array to 1-D

`_finish_y`(y, x_shape)

Reshape interpolated y back to n-d array similar to initial y

`_reshape_yi`(yi, check=False)
`_set_yi`(yi, xi=None, axis=None)
`_set_dtype`(dtype, union=False)
class `_Interpolator1DWithDerivatives`
`derivatives`(x, der=None)

Evaluate many derivatives of the polynomial at the point x

Produce an array of all derivative values at the point x.

x : array_like
Point or points at which to evaluate the derivatives
der : int or None, optional
How many derivatives to extract; None for all potentially nonzero derivatives (that is a number equal to the number of points). This number includes the function value as 0th derivative.
d : ndarray
Array with derivatives; d[j] contains the j-th derivative. Shape of d[j] is determined by replacing the interpolation axis in the original array with the shape of x.
```>>> from scipy.interpolate import KroghInterpolator
>>> KroghInterpolator([0,0,0],[1,2,3]).derivatives(0)
array([1.0,2.0,3.0])
>>> KroghInterpolator([0,0,0],[1,2,3]).derivatives([0,0])
array([[1.0,1.0],
[2.0,2.0],
[3.0,3.0]])
```
`derivative`(x, der=1)

Evaluate one derivative of the polynomial at the point x

x : array_like
Point or points at which to evaluate the derivatives
der : integer, optional
Which derivative to extract. This number includes the function value as 0th derivative.
d : ndarray
Derivative interpolated at the x-points. Shape of d is determined by replacing the interpolation axis in the original array with the shape of x.

This is computed by evaluating all derivatives up to the desired one (using self.derivatives()) and then discarding the rest.

class `KroghInterpolator`(xi, yi, axis=0)

Interpolating polynomial for a set of points.

The polynomial passes through all the pairs (xi,yi). One may additionally specify a number of derivatives at each point xi; this is done by repeating the value xi and specifying the derivatives as successive yi values.

Allows evaluation of the polynomial and all its derivatives. For reasons of numerical stability, this function does not compute the coefficients of the polynomial, although they can be obtained by evaluating all the derivatives.

xi : array_like, length N
Known x-coordinates. Must be sorted in increasing order.
yi : array_like
Known y-coordinates. When an xi occurs two or more times in a row, the corresponding yi’s represent derivative values.
axis : int, optional
Axis in the yi array corresponding to the x-coordinate values.

Be aware that the algorithms implemented here are not necessarily the most numerically stable known. Moreover, even in a world of exact computation, unless the x coordinates are chosen very carefully - Chebyshev zeros (e.g. cos(i*pi/n)) are a good choice - polynomial interpolation itself is a very ill-conditioned process due to the Runge phenomenon. In general, even with well-chosen x values, degrees higher than about thirty cause problems with numerical instability in this code.

Based on [1].

 [1] Krogh, “Efficient Algorithms for Polynomial Interpolation and Numerical Differentiation”, 1970.

To produce a polynomial that is zero at 0 and 1 and has derivative 2 at 0, call

```>>> from scipy.interpolate import KroghInterpolator
>>> KroghInterpolator([0,0,1],[0,2,0])
```

This constructs the quadratic 2*X**2-2*X. The derivative condition is indicated by the repeated zero in the xi array; the corresponding yi values are 0, the function value, and 2, the derivative value.

For another example, given xi, yi, and a derivative ypi for each point, appropriate arrays can be constructed as:

```>>> xi = np.linspace(0, 1, 5)
>>> yi, ypi = np.random.rand(2, 5)
>>> xi_k, yi_k = np.repeat(xi, 2), np.ravel(np.dstack((yi,ypi)))
>>> KroghInterpolator(xi_k, yi_k)
```

To produce a vector-valued polynomial, supply a higher-dimensional array for yi:

```>>> KroghInterpolator([0,1],[[2,3],[4,5]])
```

This constructs a linear polynomial giving (2,3) at 0 and (4,5) at 1.

`__init__`(xi, yi, axis=0)
`_evaluate`(x)
`_evaluate_derivatives`(x, der=None)
`krogh_interpolate`(xi, yi, x, der=0, axis=0)

Convenience function for polynomial interpolation.

See KroghInterpolator for more details.

xi : array_like
Known x-coordinates.
yi : array_like
Known y-coordinates, of shape `(xi.size, R)`. Interpreted as vectors of length R, or scalars if R=1.
x : array_like
Point or points at which to evaluate the derivatives.
der : int or list, optional
How many derivatives to extract; None for all potentially nonzero derivatives (that is a number equal to the number of points), or a list of derivatives to extract. This number includes the function value as 0th derivative.
axis : int, optional
Axis in the yi array corresponding to the x-coordinate values.
d : ndarray
If the interpolator’s values are R-dimensional then the returned array will be the number of derivatives by N by R. If x is a scalar, the middle dimension will be dropped; if the yi are scalars then the last dimension will be dropped.

KroghInterpolator

Construction of the interpolating polynomial is a relatively expensive process. If you want to evaluate it repeatedly consider using the class KroghInterpolator (which is what this function uses).

`approximate_taylor_polynomial`(f, x, degree, scale, order=None)

Estimate the Taylor polynomial of f at x by polynomial fitting.

f : callable
The function whose Taylor polynomial is sought. Should accept a vector of x values.
x : scalar
The point at which the polynomial is to be evaluated.
degree : int
The degree of the Taylor polynomial
scale : scalar
The width of the interval to use to evaluate the Taylor polynomial. Function values spread over a range this wide are used to fit the polynomial. Must be chosen carefully.
order : int or None, optional
The order of the polynomial to be used in the fitting; f will be evaluated `order+1` times. If None, use degree.
p : poly1d instance
The Taylor polynomial (translated to the origin, so that for example p(0)=f(x)).

The appropriate choice of “scale” is a trade-off; too large and the function differs from its Taylor polynomial too much to get a good answer, too small and round-off errors overwhelm the higher-order terms. The algorithm used becomes numerically unstable around order 30 even under ideal circumstances.

Choosing order somewhat larger than degree may improve the higher-order terms.

class `BarycentricInterpolator`(xi, yi=None, axis=0)

The interpolating polynomial for a set of points

Constructs a polynomial that passes through a given set of points. Allows evaluation of the polynomial, efficient changing of the y values to be interpolated, and updating by adding more x values. For reasons of numerical stability, this function does not compute the coefficients of the polynomial.

The values yi need to be provided before the function is evaluated, but none of the preprocessing depends on them, so rapid updates are possible.

xi : array_like
1-d array of x coordinates of the points the polynomial should pass through
yi : array_like, optional
The y coordinates of the points the polynomial should pass through. If None, the y values will be supplied later via the set_y method.
axis : int, optional
Axis in the yi array corresponding to the x-coordinate values.

This class uses a “barycentric interpolation” method that treats the problem as a special case of rational function interpolation. This algorithm is quite stable, numerically, but even in a world of exact computation, unless the x coordinates are chosen very carefully - Chebyshev zeros (e.g. cos(i*pi/n)) are a good choice - polynomial interpolation itself is a very ill-conditioned process due to the Runge phenomenon.

Based on Berrut and Trefethen 2004, “Barycentric Lagrange Interpolation”.

`__init__`(xi, yi=None, axis=0)
`set_yi`(yi, axis=None)

Update the y values to be interpolated

The barycentric interpolation algorithm requires the calculation of weights, but these depend only on the xi. The yi can be changed at any time.

yi : array_like
The y coordinates of the points the polynomial should pass through. If None, the y values will be supplied later.
axis : int, optional
Axis in the yi array corresponding to the x-coordinate values.
`add_xi`(xi, yi=None)

Add more x values to the set to be interpolated

The barycentric interpolation algorithm allows easy updating by adding more points for the polynomial to pass through.

xi : array_like
The x coordinates of the points that the polynomial should pass through.
yi : array_like, optional
The y coordinates of the points the polynomial should pass through. Should have shape `(xi.size, R)`; if R > 1 then the polynomial is vector-valued. If yi is not given, the y values will be supplied later. yi should be given if and only if the interpolator has y values specified.
`__call__`(x)

Evaluate the interpolating polynomial at the points x

x : array_like
Points to evaluate the interpolant at.
y : array_like
Interpolated values. Shape is determined by replacing the interpolation axis in the original array with the shape of x.

Currently the code computes an outer product between x and the weights, that is, it constructs an intermediate array of size N by len(x), where N is the degree of the polynomial.

`_evaluate`(x)
`barycentric_interpolate`(xi, yi, x, axis=0)

Convenience function for polynomial interpolation.

Constructs a polynomial that passes through a given set of points, then evaluates the polynomial. For reasons of numerical stability, this function does not compute the coefficients of the polynomial.

This function uses a “barycentric interpolation” method that treats the problem as a special case of rational function interpolation. This algorithm is quite stable, numerically, but even in a world of exact computation, unless the x coordinates are chosen very carefully - Chebyshev zeros (e.g. cos(i*pi/n)) are a good choice - polynomial interpolation itself is a very ill-conditioned process due to the Runge phenomenon.

xi : array_like
1-d array of x coordinates of the points the polynomial should pass through
yi : array_like
The y coordinates of the points the polynomial should pass through.
x : scalar or array_like
Points to evaluate the interpolator at.
axis : int, optional
Axis in the yi array corresponding to the x-coordinate values.
y : scalar or array_like
Interpolated values. Shape is determined by replacing the interpolation axis in the original array with the shape of x.

BarycentricInterpolator

Construction of the interpolation weights is a relatively slow process. If you want to call this many times with the same xi (but possibly varying yi or x) you should use the class BarycentricInterpolator. This is what this function uses internally.