Skip to content

Commit d598d0f

Browse files
committed
Add Interpolation types and display files
1 parent d6d98bd commit d598d0f

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

src/Interpolation/display.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
$(TYPEDSIGNATURES)
3+
4+
Display a compact representation of an [`Interpolant`](@ref).
5+
6+
# Example
7+
```julia-repl
8+
julia> ctinterpolate([0.0, 1.0, 2.0], [0.0, 1.0, 0.0])
9+
Interpolant (linear): 3 nodes
10+
```
11+
"""
12+
function Base.show(io::IO, interp::Interpolant{M}) where {M}
13+
label = M === Linear ? "linear" : "piecewise-constant"
14+
print(io, "Interpolant ($label): $(length(interp.x)) nodes")
15+
end
16+
17+
"""
18+
$(TYPEDSIGNATURES)
19+
20+
Display an [`Interpolant`](@ref) in the REPL with the same format as `Base.show(io, interp)`.
21+
"""
22+
function Base.show(io::IO, ::MIME"text/plain", interp::Interpolant)
23+
return show(io, interp)
24+
end

src/Interpolation/types.jl

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
$(TYPEDEF)
3+
4+
Abstract interpolation method (trait).
5+
6+
Concrete subtypes (`Linear`, `Constant`) are singleton trait values carried as a type
7+
parameter of [`Interpolant`](@ref), so the call method is resolved statically.
8+
"""
9+
abstract type AbstractInterpolation end
10+
11+
"""
12+
$(TYPEDEF)
13+
14+
Linear interpolation method (with flat extrapolation outside the node range).
15+
"""
16+
struct Linear <: AbstractInterpolation end
17+
18+
"""
19+
$(TYPEDEF)
20+
21+
Piecewise-constant (right-continuous steppost) interpolation method.
22+
"""
23+
struct Constant <: AbstractInterpolation end
24+
25+
"""
26+
$(TYPEDEF)
27+
28+
Callable interpolant of method `M` over nodes `x` with values `f`.
29+
30+
`Interpolant` subtypes `Function`: an instance `interp` is evaluated as `interp(t)`. The
31+
method `M` (a subtype of [`AbstractInterpolation`](@ref)) is a compile-time trait parameter
32+
that selects the evaluation rule, so the call is type-stable.
33+
34+
# Fields
35+
- `x::TX`: nodes at which the values are defined.
36+
- `f::TF`: values to interpolate.
37+
38+
# Construction
39+
Build instances through the factories [`ctinterpolate`](@ref) (linear) and
40+
[`ctinterpolate_constant`](@ref) (piecewise-constant), or directly via the aliases
41+
[`LinearInterpolant`](@ref) / [`ConstantInterpolant`](@ref).
42+
"""
43+
struct Interpolant{M<:AbstractInterpolation,TX,TF} <: Function
44+
x::TX
45+
f::TF
46+
end
47+
48+
# Outer constructor: infer TX, TF from the arguments.
49+
function Interpolant{M}(x::TX, f::TF) where {M<:AbstractInterpolation,TX,TF}
50+
return Interpolant{M,TX,TF}(x, f)
51+
end
52+
53+
"""
54+
Alias for a linear [`Interpolant`](@ref).
55+
"""
56+
const LinearInterpolant = Interpolant{Linear}
57+
58+
"""
59+
Alias for a piecewise-constant [`Interpolant`](@ref).
60+
"""
61+
const ConstantInterpolant = Interpolant{Constant}
62+
63+
"""
64+
$(TYPEDSIGNATURES)
65+
66+
Return the interpolation method (a subtype of [`AbstractInterpolation`](@ref)) of `interp`.
67+
"""
68+
method(::Interpolant{M}) where {M} = M

0 commit comments

Comments
 (0)