Constant Interpolation API
Overview
One-shot (construction + evaluation)
| Function | Description |
|---|---|
constant_interp(x, y, xq) | Constant interpolation at point(s) xq |
constant_interp(x, y, xq; side=:left) | With side mode (:nearest, :left, :right) |
constant_interp!(out, x, y, xq) | In-place constant interpolation |
constant_interp!(out, x, y, xq; side) | In-place with side mode |
Re-usable interpolant
| Function | Description |
|---|---|
itp = constant_interp(x, y) | Create constant interpolant |
itp = constant_interp(x, y; side=:left) | Create with side mode |
itp(xq) | Evaluate at point(s) xq |
itp(out, xq) | Evaluate at xq, store result in out |
Derivatives
| Function | Description |
|---|---|
constant_interp(x, y, xq; deriv=1) | First derivative (always 0) |
deriv1(itp) | First derivative view |
deriv2(itp) | Second derivative view |
Functions
FastInterpolations.constant_interp — Function
constant_interp(x, y, xi; extrap=:none, side=:nearest, deriv=0)Constant (step/piecewise constant) interpolation at a single point.
Arguments
x::AbstractVector: x-coordinates (sorted, length ≥ 2)y::AbstractVector: y-values (same length as x)xi::Real: Query pointextrap::Symbol: Extrapolation mode:none(default): throws DomainError if outside domain:constant: clamp to boundary values:extension: same as :constant (slope=0):wrap: wrap to [xmin, xmax)
side::Symbol: Side selection:nearest(default): nearest neighbor (left tie-breaking at midpoint):left: always use left value:right: use right value (except at grid points)
deriv::Int: Derivative order (0, 1, or 2). Derivatives are always 0.
Returns
- Interpolated value (Float type)
Example
x = [0.0, 1.0, 2.0, 3.0]
y = [10.0, 20.0, 30.0, 40.0]
constant_interp(x, y, 0.5) # 10.0 (nearest to left)
constant_interp(x, y, 0.5; side=:left) # 10.0
constant_interp(x, y, 0.5; side=:right) # 20.0
constant_interp(x, y, 1.0) # 20.0 (grid point)
constant_interp(x, y, -1.0; extrap=:constant) # 10.0 (clamped)constant_interp(x, y, x_targets; extrap=:none, side=:nearest, deriv=0)Constant interpolation for multiple query points (allocating version).
Example
x = [0.0, 1.0, 2.0, 3.0]
y = [10.0, 20.0, 30.0, 40.0]
result = constant_interp(x, y, [0.5, 1.5, 2.5])
# result == [10.0, 20.0, 30.0]constant_interp(x, y; extrap=:none, side=:nearest) -> ConstantInterpolantCreate a callable interpolant for broadcast fusion and reuse.
Arguments
x::AbstractVector: x-coordinates (sorted, length ≥ 2)y::AbstractVector: y-valuesextrap::Symbol: Extrapolation modeside::Symbol: Side selection
Returns
ConstantInterpolant object for scalar/broadcast evaluation.
Example
x = [0.0, 1.0, 2.0, 3.0]
y = [10.0, 20.0, 30.0, 40.0]
itp = constant_interp(x, y)
itp(0.5) # 10.0
itp.([0.5, 1.5]) # [10.0, 20.0]
# Fused broadcast (optimal)
result = @. coef * itp(query)constant_interp(x, ys::AbstractVector{<:AbstractVector}; side=:nearest, extrap=:none)Create a multi-Y constant interpolant for multiple y-data series sharing the same x-grid.
Arguments
x::AbstractVector: x-coordinates (sorted, length ≥ 2)ys: Vector of y-value vectors (all same length as x)side: Side for discontinuities (:left, :right, :nearest)extrap: Extrapolation mode (:none, :constant, :extension, :wrap)
Returns
ConstantSeriesInterpolant object with matrix storage.
Example
x = collect(range(0.0, 1.0, 101))
y1 = sin.(2π .* x)
y2 = cos.(2π .* x)
y3 = exp.(-x)
sitp = constant_interp(x, [y1, y2, y3])
vals = sitp(0.5)constant_interp(x, Y::AbstractMatrix; side=:nearest, extrap=:none)Create a multi-Y constant interpolant from a matrix where each column is a y-series.
Arguments
x::AbstractVector: x-coordinates (length n)Y::AbstractMatrix: n×m matrix, each column is a y-seriesside,extrap: Same as vector form
Example
x = collect(range(0.0, 1.0, 101))
Y = hcat(sin.(2π .* x), cos.(2π .* x)) # 101×2 matrix
sitp = constant_interp(x, Y)FastInterpolations.constant_interp! — Function
constant_interp!(output, x, y, x_targets; extrap=:none, side=:nearest, deriv=0)Zero-allocation constant interpolation for multiple query points.
Arguments
output: Pre-allocated output vectorx, y, x_targets: Grid and query pointsextrap, side, deriv: Same asconstant_interp
Example
x = [0.0, 1.0, 2.0, 3.0]
y = [10.0, 20.0, 30.0, 40.0]
out = zeros(3)
constant_interp!(out, x, y, [0.5, 1.5, 2.5])
# out == [10.0, 20.0, 30.0]Interpolant Type
FastInterpolations.ConstantInterpolant — Type
ConstantInterpolant{T,X,Y}Lightweight callable interpolant for constant (step) interpolation. Returned by constant_interp(x, y) (2-argument form).
Fields
x::X: x-coordinates (sorted)y::Y: y-valuesmode::ExtrapVal: Extrapolation modeside::SideVal: Side selection (:nearest, :left, :right)
Usage
itp = constant_interp(x, y) # default: extrap=:none, side=:nearest
val = itp(0.5) # scalar evaluation
vals = itp.(query_points) # broadcast
# With options
itp_left = constant_interp(x, y; side=:left)
itp_wrap = constant_interp(x, y; extrap=:wrap, side=:right)