Skip to content

Commit

Permalink
Merge branch 'extrapolation_options' into more_extrapolation_options
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Nov 25, 2024
2 parents 6023946 + 2af4705 commit 5ed2379
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
12 changes: 6 additions & 6 deletions docs/src/extrapolation_methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ This extrapolation type extends the interpolation with the boundary values of th
```@example tutorial
A = QuadraticSpline(u, t; extrapolation = ExtrapolationType.constant)
plot(A)
plot!(t_eval_left, A.(t_eval_left); label = "extrapolation down")
plot!(t_eval_right, A.(t_eval_right); label = "extrapolation up")
plot!(t_eval_left, A.(t_eval_left); label = "extrapolation left")
plot!(t_eval_right, A.(t_eval_right); label = "extrapolation right")
```

## `ExtrapolationType.linear`
Expand All @@ -37,8 +37,8 @@ This extrapolation type extends the interpolation with a linear continuation of
```@example tutorial
A = QuadraticSpline(u, t; extrapolation = ExtrapolationType.linear)
plot(A)
plot!(t_eval_left, A.(t_eval_left); label = "extrapolation down")
plot!(t_eval_right, A.(t_eval_right); label = "extrapolation up")
plot!(t_eval_left, A.(t_eval_left); label = "extrapolation left")
plot!(t_eval_right, A.(t_eval_right); label = "extrapolation right")
```

## `ExtrapolationType.extension`
Expand Down Expand Up @@ -85,6 +85,6 @@ You can also have different extrapolation types left and right of the data.
A = QuadraticSpline(u, t; extrapolation_left = ExtrapolationType.reflective,
extrapolation_right = ExtrapolationType.periodic)
plot(A)
plot!(t_eval_left, A.(t_eval_left); label = "extrapolation down")
plot!(t_eval_right, A.(t_eval_right); label = "extrapolation up")
plot!(t_eval_left, A.(t_eval_left); label = "extrapolation left")
plot!(t_eval_right, A.(t_eval_right); label = "extrapolation right")
```
24 changes: 12 additions & 12 deletions src/derivatives.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function derivative(A, t, order = 1)
(order (1, 2)) && throw(DerivativeNotFoundError())
if t < first(A.t)
_extrapolate_derivative_down(A, t, order)
_extrapolate_derivative_left(A, t, order)
elseif t > last(A.t)
_extrapolate_derivative_up(A, t, order)
_extrapolate_derivative_right(A, t, order)
else
iguess = A.iguesser
(order == 1) ? _derivative(A, t, iguess) :
Expand All @@ -13,16 +13,16 @@ function derivative(A, t, order = 1)
end
end

function _extrapolate_derivative_down(A, t, order)
function _extrapolate_derivative_left(A, t, order)
(; extrapolation_left) = A
typed_zero = zero(first(A.u) / one(A.t[1]))
if extrapolation_left == ExtrapolationType.none
throw(LeftExtrapolationError())
elseif extrapolation_left == ExtrapolationType.constant
typed_zero
zero(first(A.u) / one(A.t[1]))
elseif extrapolation_left == ExtrapolationType.linear
(order == 1) ? derivative(A, first(A.t)) : typed_zero
elseif extrapolation_left == ExtrapolationType.extension
(order == 1) ? derivative(A, first(A.t)) : zero(first(A.u) / one(A.t[1]))
else
# extrapolation_left == ExtrapolationType.extension
iguess = A.iguesser
(order == 1) ? _derivative(A, t, iguess) :
ForwardDiff.derivative(t -> begin
Expand All @@ -38,16 +38,16 @@ function _extrapolate_derivative_down(A, t, order)
end
end

function _extrapolate_derivative_up(A, t, order)
function _extrapolate_derivative_right(A, t, order)
(; extrapolation_right) = A
typed_zero = zero(first(A.u) / one(A.t[1]))
if extrapolation_right == ExtrapolationType.none
throw(RightExtrapolationError())
elseif extrapolation_right == ExtrapolationType.constant
typed_zero
zero(first(A.u) / one(A.t[1]))
elseif extrapolation_right == ExtrapolationType.linear
(order == 1) ? derivative(A, last(A.t)) : typed_zero
elseif extrapolation_right == ExtrapolationType.extension
(order == 1) ? derivative(A, last(A.t)) : zero(first(A.u) / one(A.t[1]))
else
# extrapolation_right == ExtrapolationType.extension
iguess = A.iguesser
(order == 1) ? _derivative(A, t, iguess) :
ForwardDiff.derivative(t -> begin
Expand Down
12 changes: 6 additions & 6 deletions src/integrals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ function integral(A::AbstractInterpolation, t1::Number, t2::Number)
if t1 < first(A.t)
if t2 < first(A.t)
# If interval is entirely below data
return _extrapolate_integral_down(A, t2) - extrapolate_integral_down(A.t1)
return _extrapolate_integral_left(A, t2) - extrapolate_integral_left(A.t1)
end

idx1 -= 1 # Make sure lowest complete interval is included
total += _extrapolate_integral_down(A, t1)
total += _extrapolate_integral_left(A, t1)
else
total += _integral(A, idx1, t1, A.t[idx1 + 1])
end
Expand All @@ -37,11 +37,11 @@ function integral(A::AbstractInterpolation, t1::Number, t2::Number)
if t2 > last(A.t)
if t1 > last(A.t)
# If interval is entirely above data
return _extrapolate_integral_up(A, t2) - extrapolate_integral_up(A.t, t1)
return _extrapolate_integral_right(A, t2) - extrapolate_integral_right(A.t, t1)
end

idx2 += 1 # Make sure highest complete interval is included
total += _extrapolate_integral_up(A, t2)
total += _extrapolate_integral_right(A, t2)
else
total += _integral(A, idx2, A.t[idx2], t2)
end
Expand All @@ -65,7 +65,7 @@ function integral(A::AbstractInterpolation, t1::Number, t2::Number)
return total
end

function _extrapolate_integral_down(A, t)
function _extrapolate_integral_left(A, t)
(; extrapolation_left) = A
if extrapolation_left == ExtrapolationType.none
throw(LeftExtrapolationError())
Expand Down Expand Up @@ -99,7 +99,7 @@ function _extrapolate_integral_down(A, t)
end
end

function _extrapolate_integral_up(A, t)
function _extrapolate_integral_right(A, t)
(; extrapolation_right) = A
if extrapolation_right == ExtrapolationType.none
throw(RightExtrapolationError())
Expand Down
8 changes: 4 additions & 4 deletions src/interpolation_methods.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function _interpolate(A, t)
if t < first(A.t)
_extrapolate_down(A, t)
_extrapolate_left(A, t)
elseif t > last(A.t)
_extrapolate_up(A, t)
_extrapolate_right(A, t)
else
_interpolate(A, t, A.iguesser)
end
end

function _extrapolate_down(A, t)
function _extrapolate_left(A, t)
(; extrapolation_left) = A
if extrapolation_left == ExtrapolationType.none
throw(LeftExtrapolationError())
Expand All @@ -30,7 +30,7 @@ function _extrapolate_down(A, t)
end
end

function _extrapolate_up(A, t)
function _extrapolate_right(A, t)
(; extrapolation_right) = A
if extrapolation_right == ExtrapolationType.none
throw(RightExtrapolationError())
Expand Down
12 changes: 6 additions & 6 deletions test/extrapolation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ end
test_extrapolation(LinearInterpolation, u, t)

for extrapolation_type in [ExtrapolationType.linear, ExtrapolationType.extension]
# Down extrapolation
# Left extrapolation
A = LinearInterpolation(u, t; extrapolation_left = extrapolation_type)
t_eval = 0.0
@test A(t_eval) == 0.0
Expand All @@ -59,7 +59,7 @@ end
@test DataInterpolations.integral(A, t_eval) == -0.5
t_eval = 3.0

# Up extrapolation
# Right extrapolation
A = LinearInterpolation(u, t; extrapolation_right = extrapolation_type)
t_eval = 3.0
@test A(t_eval) == 3.0
Expand All @@ -76,23 +76,23 @@ end

test_extrapolation(QuadraticInterpolation, u, t)

# Linear down extrapolation
# Linear left extrapolation
A = QuadraticInterpolation(u, t; extrapolation_left = ExtrapolationType.linear)
t_eval = 0.0
@test A(t_eval) -2.5
@test DataInterpolations.derivative(A, t_eval) == 3.5
@test DataInterpolations.derivative(A, t_eval, 2) == 0.0
@test DataInterpolations.integral(A, t_eval) 0.75

# Linear up extrapolation
# Linear right extrapolation
A = QuadraticInterpolation(u, t; extrapolation_right = ExtrapolationType.linear)
t_eval = 4.0
@test A(t_eval) -0.5
@test DataInterpolations.derivative(A, t_eval) == -2.5
@test DataInterpolations.derivative(A, t_eval, 2) == 0.0
@test DataInterpolations.integral(A, t[end], t_eval) 0.75

# Extension down extrapolation
# Extension left extrapolation
f = t -> (-3t^2 + 13t - 8) / 2
df = t -> (-6t + 13) / 2
A = QuadraticInterpolation(u, t; extrapolation_left = ExtrapolationType.extension)
Expand All @@ -102,7 +102,7 @@ end
@test DataInterpolations.derivative(A, t_eval, 2) == -3
@test DataInterpolations.integral(A, t_eval) 1.25

# Extension up extrapolation
# Extension right extrapolation
A = QuadraticInterpolation(u, t; extrapolation_right = ExtrapolationType.extension)
t_eval = 4.0
@test A(t_eval) -2.0
Expand Down

0 comments on commit 5ed2379

Please sign in to comment.