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=LeftSide()) | With side mode (NearestSide(), LeftSide(), RightSide()) |
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=LeftSide()) | 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=DerivOp(1)) | First derivative (always 0) |
constant_interp(x, y, xq; deriv=DerivOp(2)) | Second derivative (always 0) |
deriv1(itp) | First derivative view |
deriv2(itp) | Second derivative view |
deriv3(itp) | Third derivative view (always 0) |
Functions
FastInterpolations.constant_interp — Function
constant_interp(x, y, x_targets; extrap=NoExtrap(), side=NearestSide(), deriv=EvalValue(), search=AutoSearch())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]
# Optimized for sorted queries
sorted_queries = sort(rand(1000))
vals = constant_interp(x, y, sorted_queries; search=LinearBinarySearch(linear_window=8))constant_interp(x, Y::AbstractMatrix; side=NearestSide(), extrap=NoExtrap())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)constant_interp(grids::NTuple{N,AbstractVector}, data::AbstractArray{<:Any,N}; kwargs...)Create an N-dimensional constant interpolant with tuple-grid API.
Arguments
grids: Tuple of grid vectors, one per dimension (e.g.,(x, y)or(x, y, z))data: N-dimensional data array wheresize(data, d) == length(grids[d])
Keyword Arguments
side=NearestSide(): Side selection mode (NearestSide(),LeftSide(),RightSide()) or per-axis tupleextrap=NoExtrap(): Extrapolation mode (NoExtrap(),ConstExtrap(),ExtendExtrap(),WrapExtrap()) or per-axis tuplesearch=AutoSearch(): Search policy or per-axis tuple
Returns
ConstantInterpolantND{Tg,Tv,N}: Callable interpolant object
Examples
# 2D constant interpolation
x = [0.0, 1.0, 2.0]
y = [0.0, 1.0, 2.0, 3.0]
data = rand(3, 4)
itp = constant_interp((x, y), data)
val = itp((0.5, 1.5)) # Scalar query
vals = itp((xs, ys)) # Batch SoA
vals = itp(points) # Batch AoS
# Per-axis configuration
itp = constant_interp((x, y), data;
side=(LeftSide(), RightSide()),
extrap=(NoExtrap(), WrapExtrap()),
search=(BinarySearch(), LinearBinarySearch())
)constant_interp(grids, data, query; kwargs...)One-shot N-dimensional constant interpolation (scalar query). Zero-allocation after warmup.
constant_interp(grids, data, queries::NTuple{N,AbstractVector}; kwargs...)One-shot N-dimensional constant interpolation (batch SoA query). Only allocates the output vector.
constant_interp(grids, data, queries::AbstractVector{<:NTuple}; kwargs...)One-shot N-dimensional constant interpolation (batch AoS query). Only allocates the output vector.
FastInterpolations.constant_interp! — Function
constant_interp!(output, x, y, x_targets; extrap=NoExtrap(), side=NearestSide(), deriv=EvalValue(), search=AutoSearch())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_interpsearch::AbstractSearchPolicy: Search algorithm for interval finding
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]
# Optimized for sorted queries
sorted_queries = sort(rand(1000))
output = zeros(1000)
constant_interp!(output, x, y, sorted_queries; search=LinearBinarySearch(linear_window=8))constant_interp!(output, grids, data, queries::NTuple{N,AbstractVector}; kwargs...)In-place one-shot N-dimensional constant interpolation (batch SoA query). Writes results into pre-allocated output vector.
constant_interp!(output, grids, data, queries::AbstractVector{<:NTuple}; kwargs...)In-place one-shot N-dimensional constant interpolation (batch AoS query). Writes results into pre-allocated output vector.
Interpolant Type
FastInterpolations.ConstantInterpolant — Type
ConstantInterpolant{Tg,Tv,X,Y,E,SD,P}Lightweight callable interpolant for constant (step) interpolation. Returned by constant_interp(x, y) (2-argument form).
Type Parameters
Tg<:AbstractFloat: Grid type (Float32, Float64) for x-coordinatesTv: Value type for y-values (can be Tg, Complex{Tg}, or other Number)X<:AbstractVector{Tg}: Type of x-coordinatesY<:AbstractVector{Tv}: Type of y-valuesE<:AbstractExtrap: Extrapolation mode type (compile-time specialized)SD<:AbstractSide: Side selection type (compile-time specialized)P<:AbstractSearchPolicy: Search policy type
Fields
x::X: x-coordinates (sorted)y::Y: y-valuesextrap::E: Extrapolation mode (NoExtrap(), ExtendExtrap(), ConstExtrap(), or WrapExtrap())side::SD: Side selection (NearestSide(), LeftSide(), RightSide())search_policy::P: Default search policy for interval lookup
Usage
itp = constant_interp(x, y) # default: extrap=NoExtrap(), side=NearestSide()
val = itp(0.5) # scalar evaluation
vals = itp.(query_points) # broadcast
# With Complex values
x = [0.0, 1.0, 2.0]
y = [1.0+2.0im, 3.0+4.0im, 5.0+6.0im]
itp = constant_interp(x, y)
val = itp(0.5) # returns ComplexF64
# With options
itp_left = constant_interp(x, y; side=LeftSide())
itp_wrap = constant_interp(x, y; extrap=WrapExtrap(), side=RightSide())
# Search policy: AutoSearch adapts to query type (scalar→BinarySearch, vector→LinearBinarySearch)
itp = constant_interp(x, y)
val = itp(0.5) # AutoSearch resolves to BinarySearch() for scalar
itp = constant_interp(x, y; search=LinearBinarySearch()) # explicit override
val = itp(0.5; search=BinarySearch()) # per-call overrideFastInterpolations.ConstantInterpolantND — Type
ConstantInterpolantND{Tg,Tv,N,G,S,E,SD,P}N-dimensional constant (step) interpolation with per-axis configuration.
Type Parameters
Tg<:AbstractFloat: Grid coordinate typeTv: Value type (can be Real or Complex)N: Number of dimensionsG<:NTuple{N, AbstractVector{Tg}}: Grid tuple typeS<:NTuple{N, AbstractGridSpacing{Tg}}: Spacing tuple typeE<:Tuple{Vararg{AbstractExtrap, N}}: Extrapolation mode tuple typeSD<:Tuple{Vararg{AbstractSide, N}}: Side selection tuple typeP<:NTuple{N, AbstractSearchPolicy}: Search policy tuple type
Fields
grids: Tuple of grid vectors, one per dimensionspacings: Tuple of spacing objects for efficient interval lookupdata: N-dimensional data arrayextraps: Per-axis extrapolation modessides: Per-axis side selection (NearestSide(), LeftSide(), RightSide())searches: Per-axis search policies
Usage
# 2D constant interpolation
x = [0.0, 1.0, 2.0]
y = [0.0, 1.0, 2.0, 3.0]
data = rand(3, 4)
itp = constant_interp((x, y), data) # Default: side=NearestSide(), extrap=NoExtrap()
val = itp((0.5, 1.5)) # Single query
# With configuration
itp = constant_interp((x, y), data; side=LeftSide(), extrap=ConstExtrap())
# Per-axis configuration
itp = constant_interp((x, y), data; side=(LeftSide(), RightSide()), extrap=(NoExtrap(), WrapExtrap()))Side Selection Types
FastInterpolations.AbstractSide — Type
AbstractSideAbstract type for side selection mode in constant interpolation. Determines which neighbor value to use at non-grid-point locations.
Concrete subtypes
NearestSide: Nearest neighbor with left tie-breaking at midpointLeftSide: Always use left (floor) valueRightSide: Use right (ceiling) value, except at grid points
FastInterpolations.NearestSide — Type
NearestSide <: AbstractSideNearest-neighbor side selection with left tie-breaking at midpoint. Returns left value if distance <= h/2, right value otherwise.
Example
itp = constant_interp(x, y; side=NearestSide())FastInterpolations.LeftSide — Type
LeftSide <: AbstractSideLeft-continuous (floor) side selection. Always returns the left boundary value.
Example
itp = constant_interp(x, y; side=LeftSide())FastInterpolations.RightSide — Type
RightSide <: AbstractSideRight-continuous (ceiling) side selection. Returns right value except at grid points (where it returns left value).
Example
itp = constant_interp(x, y; side=RightSide())Derivative Views
See Derivatives for deriv1, deriv2, deriv3 API reference.