-
-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extrapolation options #356
base: master
Are you sure you want to change the base?
Conversation
This looks like a great idea. It should be an EnumX enum though instead of just symbols in order to autocomplete and spellcheck properly. |
Derivative extrapolation now also looking good: using DataInterpolations
using CairoMakie
using Random
Random.seed!(2)
t = cumsum(rand(5))
u = rand(5)
t_eval = range(-1, 3.5, length = 1000)
fig = Figure()
ax_itp = Axis(fig[1,1]; title = "(inter/extra)polation")
ax_deriv = Axis(fig[2,1]; title = "(inter/extra)polation first derivative")
ax_deriv2 = Axis(fig[3,1]; title = "(inter/extra)polation second derivative")
scatter!(ax_itp, t,u; label = "data")
for extrapolation_type in [ExtrapolationType.constant, ExtrapolationType.linear, ExtrapolationType.extension]
A = QuadraticSpline(u,t; extrapolation_up = extrapolation_type, extrapolation_down = extrapolation_type)
lines!(ax_itp, t_eval, A.(t_eval); label = string(extrapolation_type))
lines!(ax_deriv, t_eval, DataInterpolations.derivative.(Ref(A), t_eval); label = string(extrapolation_type))
lines!(ax_deriv2, t_eval, DataInterpolations.derivative.(Ref(A), t_eval, 2); label = string(extrapolation_type))
end
axislegend(ax_itp)
axislegend(ax_deriv)
axislegend(ax_deriv2)
fig |
And the integral: using DataInterpolations
using CairoMakie
using Random
Random.seed!(2)
t = cumsum(rand(5))
u = rand(5)
t_eval = range(-2, 5.5, length = 1000)
fig = Figure()
ax_itp = Axis(fig[1,1]; title = "(inter/extra)polation")
ax_antideriv = Axis(fig[2,1]; title = "(inter/extra)polation antiderivative")
scatter!(ax_itp, t,u; label = "data")
for extrapolation_type in [ExtrapolationType.constant, ExtrapolationType.linear, ExtrapolationType.extension]
A = QuadraticSpline(u,t; extrapolation_up = extrapolation_type, extrapolation_down = extrapolation_type)
lines!(ax_itp, t_eval, A.(t_eval); label = string(extrapolation_type))
lines!(ax_antideriv, t_eval, DataInterpolations.integral.(Ref(A), t_eval); label = string(extrapolation_type))
end
axislegend(ax_itp)
axislegend(ax_antideriv)
fig |
I've found myself in a refactoring rabbit hole again 😅
|
Can you do the refactors in different PRs? |
Not really, or I have to do them before this one. Is it fine by you if I wrap up what I'm doing here and make some follow-up issues? |
Thats alright, refactors that are not strictly needed in this one can have follow up issues/PRs. |
@sathvikbhagavan sorry for the spam but just to let you know that this PR is ready for review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there. A few minor comments.
return if order == 1 | ||
_derivative(A, t, iguess) | ||
elseif order == 2 | ||
function _extrapolate_derivative_down(A, t, order) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function _extrapolate_derivative_down(A, t, order) | |
function _extrapolate_derivative_left(A, t, order) |
end | ||
end | ||
|
||
function _extrapolate_derivative_up(A, t, order) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function _extrapolate_derivative_up(A, t, order) | |
function _extrapolate_derivative_right(A, t, order) |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slightly knit picky, but I think checking this one line above would prevent a few wasteful computation of typed_zero
return total | ||
end | ||
|
||
function _extrapolate_integral_down(A, t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function _extrapolate_integral_down(A, t) | |
function _extrapolate_integral_left(A, t) |
end | ||
end | ||
|
||
function _extrapolate_integral_up(A, t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function _extrapolate_integral_up(A, t) | |
function _extrapolate_integral_right(A, t) |
end | ||
end | ||
|
||
function _extrapolate_down(A, t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function _extrapolate_down(A, t) | |
function _extrapolate_left(A, t) |
end | ||
end | ||
|
||
function _extrapolate_up(A, t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function _extrapolate_up(A, t) | |
function _extrapolate_right(A, t) |
|
||
function _extrapolate_down(A, t) | ||
(; extrapolation_left) = A | ||
if extrapolation_left == ExtrapolationType.none |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as derivative/integral comment
test_constant_extrapolation(LinearInterpolation, u, t) | ||
|
||
for extrapolation_type in [ExtrapolationType.linear, ExtrapolationType.extension] | ||
# Down extrapolation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Down extrapolation | |
# Left extrapolation |
@test DataInterpolations.integral(A, t_eval) == -0.5 | ||
t_eval = 3.0 | ||
|
||
# Up extrapolation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Up extrapolation | |
# Right extrapolation |
Fixes #355.
This is what I have in mind:
Let me know whether this is a good approach before I apply it to all interpolation types, integrals, derivatives.