diff --git a/docs/src/extrapolation_methods.md b/docs/src/extrapolation_methods.md index fbce5d0f..488746e8 100644 --- a/docs/src/extrapolation_methods.md +++ b/docs/src/extrapolation_methods.md @@ -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` @@ -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` @@ -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") ``` diff --git a/src/derivatives.jl b/src/derivatives.jl index 7ee8fe2e..e7ed9cb3 100644 --- a/src/derivatives.jl +++ b/src/derivatives.jl @@ -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) : @@ -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 @@ -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 diff --git a/src/integrals.jl b/src/integrals.jl index 6948f8f9..baabc634 100644 --- a/src/integrals.jl +++ b/src/integrals.jl @@ -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 @@ -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 @@ -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()) @@ -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()) diff --git a/src/interpolation_methods.jl b/src/interpolation_methods.jl index a0437b02..d7cab62a 100644 --- a/src/interpolation_methods.jl +++ b/src/interpolation_methods.jl @@ -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()) @@ -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()) diff --git a/test/extrapolation_tests.jl b/test/extrapolation_tests.jl index fa12be1b..12f7b2bb 100644 --- a/test/extrapolation_tests.jl +++ b/test/extrapolation_tests.jl @@ -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 @@ -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 @@ -76,7 +76,7 @@ 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 @@ -84,7 +84,7 @@ end @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 @@ -92,7 +92,7 @@ end @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) @@ -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