Skip to content

Commit

Permalink
Comments adressed
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Nov 21, 2024
1 parent 3c54932 commit c1f6f7e
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 86 deletions.
14 changes: 13 additions & 1 deletion docs/src/extrapolation_methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A = QuadraticSpline(u, t)
plot(A)
```

Extrapolation behavior can be set left and right of the data simultaneously with the `extension` keyword, or left and right separately with the `extension_left` and `extension_right` keywords respectively.
Extrapolation behavior can be set left and right of the data simultaneously with the `extension` keyword, or left and right separately with the `extrapolation_left` and `extrapolation_right` keywords respectively.

## `ExtrapolationType.none`

Expand Down Expand Up @@ -51,3 +51,15 @@ plot(A)
plot!(t_eval_down, A.(t_eval_down); label = "extrapolation down")
plot!(t_eval_up, A.(t_eval_up); label = "extrapolation up")
```

## Mixed extrapolation

You can also have different extrapolation types left and right of the data.

```@example tutorial
A = QuadraticSpline(u, t; extrapolation_left = ExtrapolationType.constant,
extrapolation_right = ExtrapolationType.linear)
plot(A)
plot!(t_eval_down, A.(t_eval_down); label = "extrapolation down")
plot!(t_eval_up, A.(t_eval_up); label = "extrapolation up")
```
16 changes: 8 additions & 8 deletions src/DataInterpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ function Base.showerror(io::IO, ::ExtrapolationError)
print(io, EXTRAPOLATION_ERROR)
end

const DOWN_EXTRAPOLATION_ERROR = "Cannot extrapolate down as `extrapolation_left` keyword passed was `none`"
struct DownExtrapolationError <: Exception end
function Base.showerror(io::IO, ::DownExtrapolationError)
print(io, DOWN_EXTRAPOLATION_ERROR)
const LEFT_EXTRAPOLATION_ERROR = "Cannot extrapolate for t < first(A.t) as the `extrapolation_left` kwarg passed was `ExtrapolationType.none`"
struct LeftExtrapolationError <: Exception end
function Base.showerror(io::IO, ::LeftExtrapolationError)
print(io, LEFT_EXTRAPOLATION_ERROR)
end

const UP_EXTRAPOLATION_ERROR = "Cannot extrapolate up as `extrapolation_right` keyword passed was `none`"
struct UpExtrapolationError <: Exception end
function Base.showerror(io::IO, ::UpExtrapolationError)
print(io, UP_EXTRAPOLATION_ERROR)
const RIGHT_EXTRAPOLATION_ERROR = "Cannot extrapolate for t > last(A.t) as the `extrapolation_tight` kwarg passed was `ExtrapolationType.none`"
struct RightExtrapolationError <: Exception end
function Base.showerror(io::IO, ::RightExtrapolationError)
print(io, RIGHT_EXTRAPOLATION_ERROR)
end

const INTEGRAL_NOT_FOUND_ERROR = "Cannot integrate it analytically. Please use Numerical Integration methods."
Expand Down
4 changes: 2 additions & 2 deletions src/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function _extrapolate_derivative_down(A, t, order)
(; extrapolation_left) = A
typed_zero = zero(first(A.u) / one(A.t[1]))
if extrapolation_left == ExtrapolationType.none
throw(DownExtrapolationError())
throw(LeftExtrapolationError())
elseif extrapolation_left == ExtrapolationType.constant
typed_zero
elseif extrapolation_left == ExtrapolationType.linear
Expand All @@ -36,7 +36,7 @@ function _extrapolate_derivative_up(A, t, order)
(; extrapolation_right) = A
typed_zero = zero(first(A.u) / one(A.t[1]))
if extrapolation_right == ExtrapolationType.none
throw(UpExtrapolationError())
throw(RightExtrapolationError())
elseif extrapolation_right == ExtrapolationType.constant
typed_zero
elseif extrapolation_right == ExtrapolationType.linear
Expand Down
4 changes: 2 additions & 2 deletions src/integrals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ end
function _extrapolate_integral_down(A, t)
(; extrapolation_left) = A
if extrapolation_left == ExtrapolationType.none
throw(DownExtrapolationError())
throw(LeftExtrapolationError())
elseif extrapolation_left == ExtrapolationType.constant
first(A.u) * (first(A.t) - t)
elseif extrapolation_left == ExtrapolationType.linear
Expand All @@ -80,7 +80,7 @@ end
function _extrapolate_integral_up(A, t)
(; extrapolation_right) = A
if extrapolation_right == ExtrapolationType.none
throw(UpExtrapolationError())
throw(RightExtrapolationError())
elseif extrapolation_right == ExtrapolationType.constant
last(A.u) * (t - last(A.t))
elseif extrapolation_right == ExtrapolationType.linear
Expand Down
84 changes: 43 additions & 41 deletions src/interpolation_caches.jl

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/interpolation_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ end
function _extrapolate_down(A, t)
(; extrapolation_left) = A
if extrapolation_left == ExtrapolationType.none
throw(DownExtrapolationError())
throw(LeftExtrapolationError())
elseif extrapolation_left == ExtrapolationType.constant
slope = derivative(A, first(A.t))
first(A.u) + slope * zero(t)
Expand All @@ -27,7 +27,7 @@ end
function _extrapolate_up(A, t)
(; extrapolation_right) = A
if extrapolation_right == ExtrapolationType.none
throw(UpExtrapolationError())
throw(RightExtrapolationError())
elseif extrapolation_right == ExtrapolationType.constant
slope = derivative(A, last(A.t))
last(A.u) + slope * zero(t)
Expand Down
5 changes: 3 additions & 2 deletions test/derivative_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ function test_derivatives(method; args = [], kwargs = [], name::String)
@test_throws DataInterpolations.ExtrapolationError derivative(func, t[1] - 1.0)
@test_throws DataInterpolations.ExtrapolationError derivative(func, t[end] + 1.0)
else
@test_throws DataInterpolations.DownExtrapolationError derivative(func, t[1] - 1.0)
@test_throws DataInterpolations.UpExtrapolationError derivative(func, t[end] + 1.0)
@test_throws DataInterpolations.LeftExtrapolationError derivative(func, t[1] - 1.0)
@test_throws DataInterpolations.RightExtrapolationError derivative(
func, t[end] + 1.0)
end
@test_throws DataInterpolations.DerivativeNotFoundError derivative(
func, t[1], 3)
Expand Down
4 changes: 2 additions & 2 deletions test/extrapolation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ function test_extrapolation_errors(method, u, t)
@test A.extrapolation_right == ExtrapolationType.none
@test A.extrapolation_left == ExtrapolationType.none
for (error_type, t_eval) in zip(
(DataInterpolations.DownExtrapolationError,
DataInterpolations.UpExtrapolationError),
(DataInterpolations.LeftExtrapolationError,
DataInterpolations.RightExtrapolationError),
(first(t) - 1, last(t) + 1))
@test_throws error_type A(t_eval)
@test_throws error_type DataInterpolations.derivative(
Expand Down
9 changes: 5 additions & 4 deletions test/integral_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ function test_integral(method; args = [], kwargs = [], name::String)
@test isapprox(qint, aint, atol = 1e-6, rtol = 1e-8)
end
func = method(args...; kwargs...)
@test_throws DataInterpolations.DownExtrapolationError integral(func, t[1] - 1.0)
@test_throws DataInterpolations.UpExtrapolationError integral(func, t[end] + 1.0)
@test_throws DataInterpolations.DownExtrapolationError integral(func, t[1] - 1.0, t[2])
@test_throws DataInterpolations.UpExtrapolationError integral(func, t[1], t[end] + 1.0)
@test_throws DataInterpolations.LeftExtrapolationError integral(func, t[1] - 1.0)
@test_throws DataInterpolations.RightExtrapolationError integral(func, t[end] + 1.0)
@test_throws DataInterpolations.LeftExtrapolationError integral(func, t[1] - 1.0, t[2])
@test_throws DataInterpolations.RightExtrapolationError integral(
func, t[1], t[end] + 1.0)
end

@testset "LinearInterpolation" begin
Expand Down
42 changes: 21 additions & 21 deletions test/interpolation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ end
@test A(-1.0) == -2.0
@test A(11.0) == 22.0
A = LinearInterpolation(u, t)
@test_throws DataInterpolations.DownExtrapolationError A(-1.0)
@test_throws DataInterpolations.UpExtrapolationError A(11.0)
@test_throws DataInterpolations.DownExtrapolationError A([-1.0, 11.0])
@test_throws DataInterpolations.LeftExtrapolationError A(-1.0)
@test_throws DataInterpolations.RightExtrapolationError A(11.0)
@test_throws DataInterpolations.LeftExtrapolationError A([-1.0, 11.0])
end

@testset "Quadratic Interpolation" begin
Expand Down Expand Up @@ -275,8 +275,8 @@ end
@test A(0.0) == -4.5
@test A(5.0) == -7.5
A = QuadraticInterpolation(u, t)
@test_throws DataInterpolations.DownExtrapolationError A(0.0)
@test_throws DataInterpolations.UpExtrapolationError A(5.0)
@test_throws DataInterpolations.LeftExtrapolationError A(0.0)
@test_throws DataInterpolations.RightExtrapolationError A(5.0)
end

@testset "Lagrange Interpolation" begin
Expand Down Expand Up @@ -335,8 +335,8 @@ end
@test A(0.0) == 0.0
@test A(4.0) == 16.0
A = LagrangeInterpolation(u, t)
@test_throws DataInterpolations.DownExtrapolationError A(-1.0)
@test_throws DataInterpolations.UpExtrapolationError A(4.0)
@test_throws DataInterpolations.LeftExtrapolationError A(-1.0)
@test_throws DataInterpolations.RightExtrapolationError A(4.0)
end

@testset "Akima Interpolation" begin
Expand Down Expand Up @@ -366,8 +366,8 @@ end
@test A(-1.0) -5.0
@test A(11.0) -3.924742268041234
A = AkimaInterpolation(u, t)
@test_throws DataInterpolations.DownExtrapolationError A(-1.0)
@test_throws DataInterpolations.UpExtrapolationError A(11.0)
@test_throws DataInterpolations.LeftExtrapolationError A(-1.0)
@test_throws DataInterpolations.RightExtrapolationError A(11.0)
end

@testset "ConstantInterpolation" begin
Expand Down Expand Up @@ -496,8 +496,8 @@ end
@test A(-1.0) == 1.0
@test A(11.0) == 1.0
A = ConstantInterpolation(u, t)
@test_throws DataInterpolations.DownExtrapolationError A(-1.0)
@test_throws DataInterpolations.UpExtrapolationError A(11.0)
@test_throws DataInterpolations.LeftExtrapolationError A(-1.0)
@test_throws DataInterpolations.RightExtrapolationError A(11.0)

# Test extrapolation with infs with regularly spaced t
u = [1.67e7, 1.6867e7, 1.7034e7, 1.7201e7, 1.7368e7]
Expand Down Expand Up @@ -549,8 +549,8 @@ end
@test A(-2.0) == 0.0
@test A(2.0) == 6.0
A = QuadraticSpline(u, t)
@test_throws DataInterpolations.DownExtrapolationError A(-2.0)
@test_throws DataInterpolations.UpExtrapolationError A(2.0)
@test_throws DataInterpolations.LeftExtrapolationError A(-2.0)
@test_throws DataInterpolations.RightExtrapolationError A(2.0)
end

@testset "CubicSpline Interpolation" begin
Expand Down Expand Up @@ -602,8 +602,8 @@ end
@test A(-2.0) -1.0
@test A(2.0) 5.0
A = CubicSpline(u, t)
@test_throws DataInterpolations.DownExtrapolationError A(-2.0)
@test_throws DataInterpolations.UpExtrapolationError A(2.0)
@test_throws DataInterpolations.LeftExtrapolationError A(-2.0)
@test_throws DataInterpolations.RightExtrapolationError A(2.0)

@testset "AbstractMatrix" begin
t = 0.1:0.1:1.0
Expand Down Expand Up @@ -647,8 +647,8 @@ end
@test A(-1.0) == u[1]
@test A(300.0) == u[end]
A = BSplineInterpolation(u, t, 2, :Uniform, :Uniform)
@test_throws DataInterpolations.DownExtrapolationError A(-1.0)
@test_throws DataInterpolations.UpExtrapolationError A(300.0)
@test_throws DataInterpolations.LeftExtrapolationError A(-1.0)
@test_throws DataInterpolations.RightExtrapolationError A(300.0)

A = BSplineInterpolation(u, t, 2, :ArcLen, :Average)

Expand All @@ -668,8 +668,8 @@ end
@test A(-1.0) == u[1]
@test A(300.0) == u[end]
A = BSplineInterpolation(u, t, 2, :ArcLen, :Average)
@test_throws DataInterpolations.DownExtrapolationError A(-1.0)
@test_throws DataInterpolations.UpExtrapolationError A(300.0)
@test_throws DataInterpolations.LeftExtrapolationError A(-1.0)
@test_throws DataInterpolations.RightExtrapolationError A(300.0)

@testset "AbstractMatrix" begin
t = 0.1:0.1:1.0
Expand Down Expand Up @@ -726,8 +726,8 @@ end
@test A(-1.0) == u[1]
@test A(300.0) == u[end]
A = BSplineApprox(u, t, 2, 4, :Uniform, :Uniform)
@test_throws DataInterpolations.DownExtrapolationError A(-1.0)
@test_throws DataInterpolations.UpExtrapolationError A(300.0)
@test_throws DataInterpolations.LeftExtrapolationError A(-1.0)
@test_throws DataInterpolations.RightExtrapolationError A(300.0)

@testset "AbstractMatrix" begin
t = 0.1:0.1:1.0
Expand Down
2 changes: 1 addition & 1 deletion test/regularization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ end
uₒ, tₒ; alg = :fixed, extrapolation_right = ExtrapolationType.extension)
@test A(10.0) == A.Aitp(10.0)
A = RegularizationSmooth(uₒ, tₒ; alg = :fixed)
@test_throws DataInterpolations.UpExtrapolationError A(10.0)
@test_throws DataInterpolations.RightExtrapolationError A(10.0)
end

@testset "Type inference" begin
Expand Down

0 comments on commit c1f6f7e

Please sign in to comment.