Interpolation
The CTBase.Interpolation submodule provides lightweight, dependency-free interpolation for one-dimensional data. Two methods are available:
Linear interpolation with flat extrapolation outside the node range.
Piecewise-constant (right-continuous steppost) interpolation.
Both produce a callable Interpolant — a Function subtype whose evaluation rule is selected at compile time via a trait parameter.
Linear interpolation
ctinterpolate builds a linear interpolant from nodes x and values f. Outside [x[1], x[end]] the value is held flat (clamped to the nearest endpoint):
julia> using CTBase
julia> x = [0.0, 1.0, 2.0, 3.0]
4-element Vector{Float64}:
0.0
1.0
2.0
3.0
julia> f = [0.0, 1.0, 0.0, -1.0]
4-element Vector{Float64}:
0.0
1.0
0.0
-1.0
julia> interp = CTBase.Interpolation.ctinterpolate(x, f)
Interpolant (linear): 4 nodes
julia> interp(0.5) # linear between x[1] and x[2]
0.5
julia> interp(2.5) # linear between x[3] and x[4]
-0.5
julia> interp(-1.0) # flat extrapolation → f[1]
0.0
julia> interp(5.0) # flat extrapolation → f[end]
-1.0Piecewise-constant interpolation
ctinterpolate_constant builds a right-continuous steppost interpolant: the value at node x[i] is held on [x[i], x[i+1]).
julia> const_interp = CTBase.Interpolation.ctinterpolate_constant(x, f)
Interpolant (piecewise-constant): 4 nodes
julia> const_interp(0.0) # f[1]
0.0
julia> const_interp(0.5) # still f[1] (held on [0, 1))
0.0
julia> const_interp(1.0) # f[2] (right-continuous jump)
1.0
julia> const_interp(1.5) # still f[2]
1.0This is the standard representation for piecewise-constant control functions in optimal control.
The Interpolant type
An Interpolant is parametrised by its method trait (Linear or Constant), which makes the call type-stable:
julia> typeof(interp)
CTBase.Interpolation.Interpolant{CTBase.Interpolation.Linear, Vector{Float64}, Vector{Float64}}The method can be recovered with method:
julia> CTBase.Interpolation.method(interp)
CTBase.Interpolation.Linear
julia> CTBase.Interpolation.method(const_interp)
CTBase.Interpolation.ConstantDisplay
julia> interp
Interpolant (linear): 4 nodes
julia> const_interp
Interpolant (piecewise-constant): 4 nodesFunction Reference
| Function | Purpose |
|---|---|
ctinterpolate | Linear interpolation with flat extrapolation |
ctinterpolate_constant | Piecewise-constant (steppost) interpolation |
method | Return the interpolation method trait of an Interpolant |
See Also
- Traits guide — the trait-parameter pattern used by
Interpolant.