From c213eb7feb59f0b3f0fd87760ce6200162e10fba Mon Sep 17 00:00:00 2001 From: Sathvik Bhagavan Date: Sat, 18 Nov 2023 14:53:03 +0000 Subject: [PATCH] refactor: extrapolate the last spline in AkimaInterpolation instead of constant --- src/derivatives.jl | 7 ++++--- src/interpolation_methods.jl | 4 +--- test/interpolation_tests.jl | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/derivatives.jl b/src/derivatives.jl index fb6a62c2..fe6b1a53 100644 --- a/src/derivatives.jl +++ b/src/derivatives.jl @@ -107,9 +107,10 @@ derivative(A::LagrangeInterpolation{<:AbstractVector}, t::Number, i) = derivativ derivative(A::LagrangeInterpolation{<:AbstractMatrix}, t::Number, i) = derivative(A, t), i function derivative(A::AkimaInterpolation{<:AbstractVector}, t::Number, iguess) - t < A.t[1] && return zero(A.u[1]), 1 - t > A.t[end] && return zero(A.u[end]), lastindex(t) - i = searchsortedlastcorrelated(A.t, t, iguess) + i = searchsortedfirstcorrelated(A.t, t, iguess) + i > length(A.t) ? i -= 1 : nothing + i -= 1 + i == 0 ? i += 1 : nothing j = min(i, length(A.c)) # for smooth derivative at A.t[end] wj = t - A.t[i] (@evalpoly wj A.b[i] 2A.c[j] 3A.d[j]), i diff --git a/src/interpolation_methods.jl b/src/interpolation_methods.jl index 328c9208..334ce030 100644 --- a/src/interpolation_methods.jl +++ b/src/interpolation_methods.jl @@ -123,9 +123,7 @@ function _interpolate(A::LagrangeInterpolation{<:AbstractMatrix}, t::Number, i) end function _interpolate(A::AkimaInterpolation{<:AbstractVector}, t::Number, iguess) - i = searchsortedlastcorrelated(A.t, t, iguess) - i == 0 && return A.u[1], i - i == length(A.t) && return A.u[end], i + i = max(1, min(searchsortedlastcorrelated(A.t, t, iguess), length(A.t) - 1)) wj = t - A.t[i] (@evalpoly wj A.u[i] A.b[i] A.c[i] A.d[i]), i end diff --git a/test/interpolation_tests.jl b/test/interpolation_tests.jl index a10c1bde..c8416e14 100644 --- a/test/interpolation_tests.jl +++ b/test/interpolation_tests.jl @@ -324,8 +324,8 @@ end # Test extrapolation A = AkimaInterpolation(u, t; extrapolate = true) - @test A(-1.0) == 0.0 - @test A(11.0) == 3.0 + @test A(-1.0) ≈ -5.0 + @test A(11.0) ≈ -3.924742268041234 A = AkimaInterpolation(u, t) @test_throws DataInterpolations.ExtrapolationError A(-1.0) @test_throws DataInterpolations.ExtrapolationError A(11.0)