torchgear.interpolate

N-dimensional B-spline interpolation with PyTorch.

class torchgear.interpolate.NdBSpline(t: Sequence[Tensor], c: Tensor, k: int | Sequence[int], extrapolate: bool = True)[source]

Bases: object

N-dimensional tensor-product B-spline.

In two dimensions, with \(\mathbf{x} = (x, y)\) and degrees \(k_x\), \(k_y\):

\[S(x, y) = \sum_{i=0}^{n_x - 1} \sum_{j=0}^{n_y - 1} c_{ij}\, B_{i, k_x}(x)\, B_{j, k_y}(y)\]

Each 1D basis function is defined by the Cox–de Boor recursion

\[B_{i, k}(x) = \frac{x - t_i}{t_{i+k} - t_i}\, B_{i, k-1}(x) + \frac{t_{i+k+1} - x}{t_{i+k+1} - t_{i+1}}\, B_{i+1, k-1}(x)\]

with \(B_{i, 0}(x) = 1\) on \([t_i, t_{i+1})\) and zero elsewhere. Higher dimensions are defined analogously as products of 1D basis functions.

Parameters:
  • t (Sequence[Tensor]) – Knot vectors, one per dimension; t[d] has shape (m_d,).

  • c (Tensor) – B-spline coefficients, shape (n_1, n_2, ..., n_N, *trailing) where n_d = t[d].numel() - k[d] - 1.

  • k (int | Sequence[int]) – Spline degree per dimension (scalar or length-N list).

  • extrapolate (bool) – Whether to extrapolate outside the knot span. Default: True.

c

B-spline coefficients; see class docstring.

evaluate_grid(grid_points: Sequence[Tensor], extrapolate: bool | None = None) → Tensor[source]

Evaluate on a rectilinear grid.

Parameters:
  • grid_points (Sequence[Tensor]) – 1D coordinate vectors per dimension; grid_points[d] has shape (m_d,).

  • extrapolate (bool) – Override extrapolate. Default: None.

Returns:

Values on the rectilinear grid, shape (m_1, m_2, ..., m_N, *trailing).

Return type:

Tensor

extrapolate

Whether to extrapolate outside the knot span.

classmethod from_grid(points: Sequence[Tensor], values: Tensor, k: int | Sequence[int] = 3, bc_type: str | tuple[str | Sequence[tuple[int, Tensor | float]], ...] | None = 'not-a-knot', extrapolate: bool = True) → NdBSpline[source]

Build an interpolating spline from rectilinear grid samples.

Parameters:
  • points (Sequence[Tensor]) – Strictly increasing 1D coordinate vectors; points[d] has shape (n_d,).

  • values (Tensor) – Sample values on the full grid, shape (n_1, n_2, ..., n_N, *trailing).

  • k (int | Sequence[int]) – Spline degree per dimension (scalar or length-N list). Default: 3.

  • bc_type – Boundary condition for all dimensions, per dimension, or (left, right) applied to every dimension. Supports 'not-a-knot', 'clamped', 'natural', and explicit derivative specs [(order, value), ...]. Default: 'not-a-knot'.

  • extrapolate (bool) – Whether to extrapolate outside the knot span. Default: True.

Returns:

Interpolating spline.

Return type:

NdBSpline

property k: list[int]

Spline degree per dimension.

property t: list[Tensor]

Knot vectors, one per dimension.