phytorch.interpolate

Classes for univariate and multidimensional interpolation. Batched, of course.

Splines

class phytorch.interpolate.splines.Spline

Interpolating univariate spline.

x0

locations of the knots along the last dimension. Must be sorted!

Type:

Tensor (batch_shape..., N)

degree

degree of the spline. Only degree = 3 works flawlessly for now.

Type:

int

npoints

= N: number of knots

Type:

int

npolys

= N-1: number of interpolating polynomials

Type:

int

nvarsp

= degree + 1: number of coefficients per polynomial

Type:

int

nvars

= nvarsp * npolys: total number of coefficients

Type:

int

batch_shape

= x0.shape[:-1]: batch shape of the knots

Type:

torch.Size

Y0(y0)

Right-hand side of all constraint equations in the appropriate order.

Parameters:

y0 (Tensor (batch..., npoints)) – Values of the interpolated function.

Returns:

Vector(s) of length nvars consisting of

  • the first end-point,

  • duplicated internal points,

  • the last end-point,

  • zero padding.

Return type:

Tensor (batch..., nvars)

coeffs(y0)

Coefficients for the interpolating polynomials.

Parameters:

y0 (Tensor (batch..., npoints)) – Values of the interpolated function. Must broadcast with x0.

Returns:

Solution to \(X_0 c = Y_0\), where \(X_0\) and \(Y_0\) are constructed from \(x_0\) and \(y_0\), respectively.

Return type:

Tensor (batch..., npolys, nvarsp)

evaluate(x, y0)

Evaluate the spline (that interpolates y0 from x0) at x.

Parameters:
  • x (Tensor (batch..., M)) – Points at which interpolation is to be performed. Can have a last dimension of arbitrary length. The rest need to broadcast with the batch_shape.

  • y0 (Tensor (batch..., npoints)) – Values of the interpolated function. Must broadcast with x0.

Returns:

The spline which passes through (x0, y0) evaluated at x.

Return type:

Tensor (batch..., M)

pick_coeffs(x, y0)

Coefficients of the interpolating polynomials, corresponding to each point in x.

Parameters:
  • x (Tensor (batch..., M)) – Points at which interpolation is to be performed. Can have a last dimension of arbitrary length. The rest need to broadcast with the batch_shape.

  • y0 (Tensor (batch..., npoints)) – Values of the interpolated function. Must broadcast with x0.

Returns:

Coefficients (in the first dimension) corresponding to the particular polynomial that covers each point in x. The spline can then be evaluated by using polyval:

polyval(Spline(x0).pick_coeffs(x, y0), x)

which is exactly the code of evaluate.

Return type:

Tensor (nvarsp, batch..., M)

pick_poly(x)

Index of the polynomial that each element of x corresponds to.

Parameters:

x (Tensor (batch..., M)) – Points at which interpolation is to be performed. Can have a last dimension of arbitrary length. The rest need to broadcast with the batch_shape.

Returns:

LongTensor of the (broadcasted) shape of x which indexes into the polynomials that comprise the spline. Points outside the range of x0’s are extrapolated, i.e. assigned to the boundary polynomials.

Return type:

Tensor (additional_batch..., batch_shape..., M)

weights(x)

Weights that directly combine \(\{y_{0, i}\}\) into \(y(x)\).

Parameters:

x (Tensor (batch..., M)) – Points at which interpolation is to be performed. Can have a last dimension of arbitrary length. The rest need to broadcast with the batch_shape.

Returns:

Weights (in the last dimension) that multiply a given \(y_0\) to give \(y(x)\) at each given x. This leads to an alternative way to evaluate the spline:

\[y(x) = w(x_0, x)^T y_0.\]

Return type:

Tensor (batch..., M, npoints)

property A: Tensor

Linear operator, which multiplies the values to get the coefficients:

It is a nvars-by-npoints matrix calculated as a linear combination of the first 2 * npolys columns of the inverse of X0.

Return type:

Tensor (batch_shape..., nvars, npoints)

property X0: Tensor

Linear operator representing all constraint equations.

Return type:

Tensor (batch_shape..., nvars, nvars)

class phytorch.interpolate.splines.SplineNd

N-dimensional spline as the product of univariate Splines.

Parameters:
  • x0s[(batch_shape_1..., npoints_1), (batch_shape_2..., npoints_2)), ...] knots in each dimension. The batch shapes should broadcast.

  • degree – degree of the spline. Passed to the univariate Splines.

splines

the individual univariate Splines

Type:

list[Spline]

grid_shape

shape of the grid: (npoints_1, npoints_2, ...)

Type:

torch.Size

batch_shape

broadcasted batch_shape of all the 1D grids.

Type:

torch.Size

ndim

= len(x0s) = len(splines) = len(grid_shape): number of dimensions

Type:

int

evaluate(y0, *xs)

Evaluate the n-dimensional spline as a multilinear “product” of 1-dim splines:

\[y(x_1, x_2, ...) = \sum_{i=1, j=1, \ldots}^{N_1, N_2, \ldots} y_{0, ij\ldots} w_{1,i}(x_1) w_{2,i}(x_2) \ldots\]
Parameters:
  • y0 (Tensor (batch..., npoints_1, npoints_2, ...)) – Values of the interpolated function as a grid in the last ndim dimensions. The rest must broadcast with batch_shape and with the batch shapes of xs.

  • xs[(batch..., M), (batch..., M), ...] Coordinates of the points where the spline is to be evaluated. Last dimension must match, and the batch dimensions must broadcast among each other, with y0, and with batch_shape.

Returns:

The spline evaluated at xs = (x_1, x_2, ...).

Return type:

Tensor (batch..., M)