Skip to content

Commit

Permalink
Added Linear() extrapolation method (#93)
Browse files Browse the repository at this point in the history
* linear extrapolation

* Update SplineExtrapolations.jl

* finite diffs -> AD forward diff

* Revert "finite diffs -> AD forward diff"

This reverts commit aa9b33e.

* interpolation example now working, sorted out Project.toml issue

* using ForwardDiff in extrapolations module

hopefully this fixes the CI error

* edits

removed unnecessary ForwardDiff inclusions, added Linear to extrapolation.md, removed CairoMakie from Project.toml
  • Loading branch information
fintzij authored Jul 8, 2024
1 parent f74498b commit 32575ff
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "0.17.3"
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
FastGaussQuadrature = "442a2c76-b920-505d-bb47-c5924d526838"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -20,6 +21,7 @@ StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
ArrayLayouts = "1"
BandedMatrices = "0.17, 1"
FastGaussQuadrature = "0.5, 1"
ForwardDiff = "0.10"
LinearAlgebra = "1.8"
PrecompileTools = "1.0"
Random = "1.8"
Expand Down
1 change: 1 addition & 0 deletions docs/src/extrapolation.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extrapolate

```@docs
Flat
Linear
Smooth
```

Expand Down
4 changes: 3 additions & 1 deletion examples/interpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ current_figure() # hide
#
# First, we generate and interpolate new data:

xs = 0:0.2:1
xs = 0.2:0.2:1.2
ys = 2 * cospi.(xs)
S = interpolate(xs, ys, BSplineOrder(4))

Expand All @@ -110,6 +110,7 @@ S(-0.32)
# Here we compare both [`Flat`](@ref) and [`Smooth`](@ref) methods:

E_flat = extrapolate(S, Flat())
E_linear = extrapolate(S, Linear())
E_smooth = extrapolate(S, Smooth())

#
Expand All @@ -119,6 +120,7 @@ ax = Axis(fig[1, 1])
scatter!(ax, xs, ys; label = "Data", color = :black)
lines!(ax, -0.5..1.5, S; label = "No extrapolation", linewidth = 2)
lines!(ax, -0.5..1.5, E_smooth; label = "Smooth", linestyle = :dash, linewidth = 2)
lines!(ax, -0.5..1.5, E_linear; label = "Linear", linestyle = :dashdot, linewidth = 2)
lines!(ax, -0.5..1.5, E_flat; label = "Flat", linestyle = :dot, linewidth = 2)
axislegend(ax)
fig
Expand Down
2 changes: 1 addition & 1 deletion src/BSplineKit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module BSplineKit

using Reexport
using PrecompileTools
using LinearAlgebra: LinearAlgebra # needed for docs
using LinearAlgebra: LinearAlgebra # needed for docs

include("BandedTensors/BandedTensors.jl")
@reexport using .BandedTensors
Expand Down
22 changes: 22 additions & 0 deletions src/SplineExtrapolations/SplineExtrapolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export
SplineExtrapolation,
extrapolate,
Flat,
Linear,
Smooth

using ..BSplines
using ..Splines
using ForwardDiff

"""
AbstractExtrapolationMethod
Expand All @@ -30,6 +32,26 @@ function extrapolate_at_point(::Flat, S::Spline, x)
S(x′)
end

"""
Linear <: AbstractExtrapolationMethod
Represents a linear extrapolation: splines values extend linearly beyond the left and right boundaries.
"""
struct Linear <: AbstractExtrapolationMethod end

function extrapolate_at_point(::Linear, S::Spline, x)
a, b = boundaries(basis(S))
if x < a
slope = ForwardDiff.derivative(S, a)
S(a) + slope * (x - a)
elseif x > b
slope = ForwardDiff.derivative(S, b)
S(b) + slope * (x - b)
else
S(x)
end
end

"""
Smooth <: AbstractExtrapolationMethod
Expand Down

0 comments on commit 32575ff

Please sign in to comment.