Skip to content

interpolation

Differentiable piecewise-linear interpolation.

Replaces the SIMPLACE AFGEN/InterpolationTable lookup used throughout Lintul5. The implementation is a pure PyTorch piecewise-linear interpolator that is differentiable with respect to both the query x and the table y values.

interpolate(table, x)

Piecewise-linear interpolation of x against a breakpoint table.

Equivalent to the Lintul5 AFGEN function. Outside the support of the table the output is clamped to the first/last y-value (flat extrapolation), matching the SIMPLACE InterpolationTable behaviour.

Parameters:

Name Type Description Default
table torch.Tensor

Breakpoint table of shape [N, 2] with columns (x_k, y_k) sorted by increasing x_k. May also be of shape [B, N, 2] for batch-specific tables.

required
x torch.Tensor

Query values of shape [B] (or scalar).

required

Returns:

Type Description
torch.Tensor

Interpolated values with the broadcast shape of x.

Exceptions:

Type Description
ValueError

If table is neither 2-D nor 3-D.

Source code in torchcrop/functions/interpolation.py
def interpolate(
    table: torch.Tensor,
    x: torch.Tensor,
) -> torch.Tensor:
    """Piecewise-linear interpolation of ``x`` against a breakpoint table.

    Equivalent to the Lintul5 ``AFGEN`` function. Outside the support of the
    table the output is clamped to the first/last y-value (flat extrapolation),
    matching the SIMPLACE ``InterpolationTable`` behaviour.

    Args:
        table: Breakpoint table of shape ``[N, 2]`` with columns
            ``(x_k, y_k)`` sorted by increasing ``x_k``. May also be of
            shape ``[B, N, 2]`` for batch-specific tables.
        x: Query values of shape ``[B]`` (or scalar).

    Returns:
        Interpolated values with the broadcast shape of ``x``.

    Raises:
        ValueError: If ``table`` is neither 2-D nor 3-D.
    """
    if table.dim() == 2:
        xs = table[:, 0].contiguous()
        ys = table[:, 1].contiguous()
        return _interp_1d(xs, ys, x)
    if table.dim() == 3:
        return _interp_batched(table, x)
    raise ValueError(
        f"table must be of shape [N, 2] or [B, N, 2]; got {tuple(table.shape)}"
    )