Interp1D

class xitorch.interpolate.Interp1D(x: torch.Tensor, y: Optional[torch.Tensor] = None, method: Optional[Union[str, Callable]] = None, assume_sorted: bool = False, **fwd_options)[source]

1D interpolation class. When initializing the class, the x must be specified and y can be specified during initialization or later.

Parameters
  • x (torch.Tensor) – The position of known values in tensor with shape (..., nr)

  • y (torch.Tensor or None) – The values at the given position with shape (..., nr). If None, it must be supplied during __call__

  • method (str or callable or None) – Interpolation method. If None, it will choose "cspline".

  • assume_sorted (bool) – Assume x is sorted monotonically increasing. If False, then it sorts the input x and y first before doing the interpolation.

  • **fwd_options – Method-specific options (see method section below)

Note

Batched x and xq is only implemented if there is no extrapolation involved.

method="cspline"
Interp1D(..., method="cspline", *, y=None, bc_type=None, extrap=None)

Perform 1D cubic spline interpolation for non-uniform x 1 2.

Keyword Arguments
  • bc_type (str or None) –

    Boundary condition:

    • "not-a-knot": The first and second segments are the same polynomial

    • "natural": 2nd grad at the boundaries are 0

    • "clamped": 1st grad at the boundaries are 0

    • "periodic": periodic boundary condition (new in version 0.2)

    If None, it will choose "not-a-knot"

  • extrap (int, float, 1-element torch.Tensor, str, or None) –

    Extrapolation option:

    • int, float, or 1-element torch.Tensor: it will pad the extrapolated values with the specified values

    • "mirror": the extrapolation values are mirrored

    • "periodic": periodic boundary condition. y[...,0] == y[...,-1] must be fulfilled for this condition.

    • "bound": fill in the extrapolated values with the left or right bound values.

    • "nan": fill the extrapolated values with nan

    • callable: apply this extrapolation function with the extrapolated positions and use the output as the values

    • None: choose the extrapolation based on the bc_type. These are the pairs:

      • "clamped": "mirror"

      • other: "nan"

    Default: None

References

1

SplineInterpolation on Wikipedia, https://en.wikipedia.org/wiki/Spline_interpolation#Algorithm_to_find_the_interpolating_cubic_spline)

2

Carl de Boor, “A Practical Guide to Splines”, Springer-Verlag, 1978.

method="linear"
Interp1D(..., method="linear", *, y=None, extrap=None)

Perform 1D linear interpolation for non-uniform x.

Keyword Arguments

extrap (int, float, 1-element torch.Tensor, str, or None) –

Extrapolation option:

  • int, float, or 1-element torch.Tensor: it will pad the extrapolated values with the specified values

  • "mirror": the extrapolation values are mirrored

  • "periodic": periodic boundary condition. y[...,0] == y[...,-1] must be fulfilled for this condition.

  • "bound": fill in the extrapolated values with the left or right bound values.

  • "nan": fill the extrapolated values with nan

  • callable: apply this extrapolation function with the extrapolated positions and use the output as the values

  • None: choose the extrapolation based on the bc_type. These are the pairs:

    • "clamped": "mirror"

    • other: "nan"

Default: None

__call__(xq: torch.Tensor, y: Optional[torch.Tensor] = None) → torch.Tensor[source]
Parameters
  • xq (torch.Tensor) – The position of query points with shape (..., nrq).

  • y (torch.Tensor or None) – The values at the given position with shape (..., nr). If y has been specified during __init__ and also specified here, the value of y given here will be ignored. If no y ever specified, then it will raise an error.

Returns

The interpolated values with shape (..., nrq).

Return type

torch.Tensor