|
| 1 | +# ============================================================================= |
| 2 | +# Call methods |
| 3 | +# ============================================================================= |
| 4 | + |
| 5 | +# Linear interpolation with flat extrapolation outside [x[1], x[end]]. |
| 6 | +function (interp::Interpolant{Linear})(t) |
| 7 | + x, f = interp.x, interp.f |
| 8 | + if t < x[1] |
| 9 | + return f[1] |
| 10 | + elseif t >= x[end] |
| 11 | + return f[end] |
| 12 | + else |
| 13 | + i = searchsortedlast(x, t) |
| 14 | + i == length(x) && return f[end] |
| 15 | + α = (t - x[i]) / (x[i + 1] - x[i]) |
| 16 | + return f[i] + α * (f[i + 1] - f[i]) |
| 17 | + end |
| 18 | +end |
| 19 | + |
| 20 | +# Right-continuous piecewise-constant (steppost) interpolation. |
| 21 | +function (interp::Interpolant{Constant})(t) |
| 22 | + x, f = interp.x, interp.f |
| 23 | + if t < x[1] |
| 24 | + return f[1] |
| 25 | + elseif t >= x[end] |
| 26 | + return f[end] |
| 27 | + else |
| 28 | + return f[searchsortedlast(x, t)] |
| 29 | + end |
| 30 | +end |
| 31 | + |
| 32 | +# ============================================================================= |
| 33 | +# Factories (public API) |
| 34 | +# ============================================================================= |
| 35 | + |
| 36 | +""" |
| 37 | +$(TYPEDSIGNATURES) |
| 38 | +
|
| 39 | +Return a linear interpolation function for the data `f` defined at points `x`. |
| 40 | +
|
| 41 | +This creates a one-dimensional linear [`Interpolant`](@ref) with flat extrapolation beyond |
| 42 | +the bounds of `x` (returns `f[1]` for `t < x[1]` and `f[end]` for `t >= x[end]`). |
| 43 | +
|
| 44 | +# Arguments |
| 45 | +- `x`: A vector of points at which the values `f` are defined. |
| 46 | +- `f`: A vector of values to interpolate. |
| 47 | +
|
| 48 | +# Returns |
| 49 | +A callable [`LinearInterpolant`](@ref) that can be evaluated at new points. |
| 50 | +
|
| 51 | +# Example |
| 52 | +```julia-repl |
| 53 | +julia> x = [0.0, 1.0, 2.0] |
| 54 | +julia> f = [1.0, 2.0, 3.0] |
| 55 | +julia> interp = ctinterpolate(x, f) |
| 56 | +julia> interp(0.5) # Returns 1.5 (linear interpolation) |
| 57 | +julia> interp(-1.0) # Returns 1.0 (flat extrapolation) |
| 58 | +julia> interp(3.0) # Returns 3.0 (flat extrapolation) |
| 59 | +``` |
| 60 | +""" |
| 61 | +ctinterpolate(x, f) = Interpolant{Linear}(x, f) |
| 62 | + |
| 63 | +""" |
| 64 | +$(TYPEDSIGNATURES) |
| 65 | +
|
| 66 | +Return a piecewise-constant interpolation function for the data `f` defined at points `x`. |
| 67 | +
|
| 68 | +This creates a right-continuous piecewise-constant [`Interpolant`](@ref): the value at knot |
| 69 | +`x[i]` is held constant on the interval `[x[i], x[i+1})`. |
| 70 | +
|
| 71 | +This implements the standard steppost behavior for optimal control: |
| 72 | +- `u(t_i) = u_i` (value at the knot) |
| 73 | +- `u(t) = u_i` for all `t ∈ [t_i, t_{i+1})` |
| 74 | +- Right-continuous: `lim_{t→t_i^+} u(t) = u(t_i)` |
| 75 | +
|
| 76 | +# Arguments |
| 77 | +- `x`: A vector of points at which the values `f` are defined. |
| 78 | +- `f`: A vector of values to interpolate. |
| 79 | +
|
| 80 | +# Returns |
| 81 | +A callable [`ConstantInterpolant`](@ref) that can be evaluated at new points. |
| 82 | +
|
| 83 | +# Example |
| 84 | +```julia-repl |
| 85 | +julia> x = [0.0, 1.0, 2.0] |
| 86 | +julia> f = [1.0, 2.0, 3.0] |
| 87 | +julia> interp = ctinterpolate_constant(x, f) |
| 88 | +julia> interp(0.0) # Returns 1.0 (value at x[1]) |
| 89 | +julia> interp(0.5) # Returns 1.0 (held from x[1] on [0.0, 1.0)) |
| 90 | +julia> interp(1.0) # Returns 2.0 (value at x[2], right-continuous) |
| 91 | +julia> interp(1.5) # Returns 2.0 (held from x[2] on [1.0, 2.0)) |
| 92 | +``` |
| 93 | +""" |
| 94 | +ctinterpolate_constant(x, f) = Interpolant{Constant}(x, f) |
0 commit comments