Skip to content

Commit

Permalink
refactor: extrapolate the last spline in AkimaInterpolation instead o…
Browse files Browse the repository at this point in the history
…f constant
  • Loading branch information
sathvikbhagavan committed Nov 18, 2023
1 parent eb697fc commit c213eb7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/interpolation_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/interpolation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c213eb7

Please sign in to comment.