Skip to content

Commit

Permalink
Capitalize extrapolation types
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Nov 26, 2024
1 parent e0011f2 commit 120e56f
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 232 deletions.
26 changes: 13 additions & 13 deletions docs/src/extrapolation_methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,63 @@ plot(A)

Extrapolation behavior can be set left and right of the data simultaneously with the `extension` keyword, or left and right separately with the `extrapolation_left` and `extrapolation_right` keywords respectively.

## `ExtrapolationType.none`
## `ExtrapolationType.None`

This extrapolation type will throw an error when the input `t` is beyond the data in the specified direction.

## `ExtrapolationType.constant`
## `ExtrapolationType.Constant`

This extrapolation type extends the interpolation with the boundary values of the data `u`.

```@example tutorial
A = QuadraticSpline(u, t; extrapolation = ExtrapolationType.constant)
A = QuadraticSpline(u, t; extrapolation = ExtrapolationType.Constant)
plot(A)
plot!(t_eval_left, A.(t_eval_left); label = "extrapolation left")
plot!(t_eval_right, A.(t_eval_right); label = "extrapolation right")
```

## `ExtrapolationType.linear`
## `ExtrapolationType.Linear`

This extrapolation type extends the interpolation with a linear continuation of the interpolation, making it $C^1$ smooth at the data boundaries.

```@example tutorial
A = QuadraticSpline(u, t; extrapolation = ExtrapolationType.linear)
A = QuadraticSpline(u, t; extrapolation = ExtrapolationType.Linear)
plot(A)
plot!(t_eval_left, A.(t_eval_left); label = "extrapolation left")
plot!(t_eval_right, A.(t_eval_right); label = "extrapolation right")
```

## `ExtrapolationType.extension`
## `ExtrapolationType.Extension`

This extrapolation type extends the interpolation with a continuation of the expression for the interpolation at the boundary intervals for maximum smoothness.

```@example tutorial
A = QuadraticSpline(u, t; extrapolation = ExtrapolationType.extension)
A = QuadraticSpline(u, t; extrapolation = ExtrapolationType.Extension)
plot(A)
plot!(t_eval_left, A.(t_eval_left); label = "extrapolation down")
plot!(t_eval_right, A.(t_eval_right); label = "extrapolation up")
```

## `ExtrapolationType.periodic`
## `ExtrapolationType.Periodic`

this extrapolation type extends the interpolation such that `A(t + T) == A(t)` for all `t`, where the period is given by `T = last(A.t) - first(A.t)`.

```@example tutorial
T = last(A.t) - first(A.t)
t_eval_left = range(first(t) - 2T, first(t), length = 100)
t_eval_right = range(last(t), last(t) + 2T, length = 100)
A = QuadraticSpline(u, t; extrapolation = ExtrapolationType.periodic)
A = QuadraticSpline(u, t; extrapolation = 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")
```

## `ExtrapolationType.reflective`
## `ExtrapolationType.Reflective`

this extrapolation type extends the interpolation such that `A(t_ + t) == A(t_ - t)` for all `t_, t` such that `(t_ - first(A.t)) % T == 0` and `0 < t < T`, where `T = last(A.t) - first(A.t)`.

```@example tutorial
A = QuadraticSpline(u, t; extrapolation = ExtrapolationType.reflective)
A = QuadraticSpline(u, t; extrapolation = ExtrapolationType.Reflective)
plot(A)
plot!(t_eval_left, A.(t_eval_left); label = "extrapolation down")
plot!(t_eval_right, A.(t_eval_right); label = "extrapolation up")
Expand All @@ -82,8 +82,8 @@ plot!(t_eval_right, A.(t_eval_right); label = "extrapolation up")
You can also have different extrapolation types left and right of the data.

```@example tutorial
A = QuadraticSpline(u, t; extrapolation_left = ExtrapolationType.reflective,
extrapolation_right = ExtrapolationType.periodic)
A = QuadraticSpline(u, t; extrapolation_left = ExtrapolationType.Reflective,
extrapolation_right = ExtrapolationType.Periodic)
plot(A)
plot!(t_eval_left, A.(t_eval_left); label = "extrapolation left")
plot!(t_eval_right, A.(t_eval_right); label = "extrapolation right")
Expand Down
4 changes: 2 additions & 2 deletions docs/src/interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ t = [0.0, 62.25, 109.66, 162.66, 205.8, 252.3]

All interpolation methods return an object from which we can compute the value of the dependent variable at any time point.

We will use the `CubicSpline` method for demonstration, but the API is the same for all the methods. We can also pass the `extrapolation = ExtrapolationType.extension` keyword if we want to allow the interpolation to go beyond the range of the timepoints in the positive `t` direction. The default value is `extrapolation = ExtrapolationType.none`. For more information on extrapolation see [Extrapolation methods](extrapolation_methods.md).
We will use the `CubicSpline` method for demonstration, but the API is the same for all the methods. We can also pass the `extrapolation = ExtrapolationType.Extension` keyword if we want to allow the interpolation to go beyond the range of the timepoints in the positive `t` direction. The default value is `extrapolation = ExtrapolationType.None`. For more information on extrapolation see [Extrapolation methods](extrapolation_methods.md).

```@example interface
A1 = CubicSpline(u, t)
# For interpolation do, A(t)
A1(100.0)
A2 = CubicSpline(u, t; extrapolation = ExtrapolationType.extension)
A2 = CubicSpline(u, t; extrapolation = ExtrapolationType.Extension)
# Extrapolation
A2(300.0)
Expand Down
28 changes: 14 additions & 14 deletions ext/DataInterpolationsRegularizationToolsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ A = RegularizationSmooth(u, t, t̂, wls, wr, d; λ = 1.0, alg = :gcv_svd)
function RegularizationSmooth(u::AbstractVector, t::AbstractVector, t̂::AbstractVector,
wls::AbstractVector, wr::AbstractVector, d::Int = 2;
λ::Real = 1.0, alg::Symbol = :gcv_svd,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.none,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.none)
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None)
u, t = munge_data(u, t)
M = _mapping_matrix(t̂, t)
Wls½ = LA.diagm(sqrt.(wls))
Expand All @@ -90,8 +90,8 @@ A = RegularizationSmooth(u, t, d; λ = 1.0, alg = :gcv_svd, extrapolate = false)
"""
function RegularizationSmooth(u::AbstractVector, t::AbstractVector, d::Int = 2;
λ::Real = 1.0,
alg::Symbol = :gcv_svd, extrapolation_left::ExtrapolationType.T = ExtrapolationType.none,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.none)
alg::Symbol = :gcv_svd, extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None)
u, t = munge_data(u, t)
= t
N = length(t)
Expand Down Expand Up @@ -122,8 +122,8 @@ A = RegularizationSmooth(u, t, t̂, d; λ = 1.0, alg = :gcv_svd, extrapolate = f
"""
function RegularizationSmooth(u::AbstractVector, t::AbstractVector, t̂::AbstractVector,
d::Int = 2; λ::Real = 1.0, alg::Symbol = :gcv_svd,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.none,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.none)
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None)
u, t = munge_data(u, t)
N, N̂ = length(t), length(t̂)
M = _mapping_matrix(t̂, t)
Expand Down Expand Up @@ -153,8 +153,8 @@ A = RegularizationSmooth(u, t, t̂, wls, d; λ = 1.0, alg = :gcv_svd, extrapolat
"""
function RegularizationSmooth(u::AbstractVector, t::AbstractVector, t̂::AbstractVector,
wls::AbstractVector, d::Int = 2; λ::Real = 1.0,
alg::Symbol = :gcv_svd, extrapolation_left::ExtrapolationType.T = ExtrapolationType.none,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.none)
alg::Symbol = :gcv_svd, extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None)
u, t = munge_data(u, t)
N, N̂ = length(t), length(t̂)
M = _mapping_matrix(t̂, t)
Expand Down Expand Up @@ -185,8 +185,8 @@ A = RegularizationSmooth(
"""
function RegularizationSmooth(u::AbstractVector, t::AbstractVector, t̂::Nothing,
wls::AbstractVector, d::Int = 2; λ::Real = 1.0,
alg::Symbol = :gcv_svd, extrapolation_left::ExtrapolationType.T = ExtrapolationType.none,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.none)
alg::Symbol = :gcv_svd, extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None)
u, t = munge_data(u, t)
= t
N = length(t)
Expand Down Expand Up @@ -219,8 +219,8 @@ A = RegularizationSmooth(
function RegularizationSmooth(u::AbstractVector, t::AbstractVector, t̂::Nothing,
wls::AbstractVector, wr::AbstractVector, d::Int = 2;
λ::Real = 1.0, alg::Symbol = :gcv_svd,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.none,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.none)
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None)
u, t = munge_data(u, t)
= t
N = length(t)
Expand Down Expand Up @@ -252,8 +252,8 @@ A = RegularizationSmooth(
"""
function RegularizationSmooth(u::AbstractVector, t::AbstractVector, t̂::Nothing,
wls::Symbol, d::Int = 2; λ::Real = 1.0, alg::Symbol = :gcv_svd,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.none,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.none)
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None)
u, t = munge_data(u, t)
= t
N = length(t)
Expand Down
6 changes: 3 additions & 3 deletions src/DataInterpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using EnumX
import FindFirstFunctions: searchsortedfirstcorrelated, searchsortedlastcorrelated,
Guesser

@enumx ExtrapolationType none constant linear extension periodic reflective
@enumx ExtrapolationType None Constant Linear Extension Periodic Reflective

include("parameter_caches.jl")
include("interpolation_caches.jl")
Expand Down Expand Up @@ -64,13 +64,13 @@ function Base.showerror(io::IO, ::ExtrapolationError)
print(io, EXTRAPOLATION_ERROR)
end

const LEFT_EXTRAPOLATION_ERROR = "Cannot extrapolate for t < first(A.t) as the `extrapolation_left` kwarg passed was `ExtrapolationType.none`"
const LEFT_EXTRAPOLATION_ERROR = "Cannot extrapolate for t < first(A.t) as the `extrapolation_left` kwarg passed was `ExtrapolationType.None`"
struct LeftExtrapolationError <: Exception end
function Base.showerror(io::IO, ::LeftExtrapolationError)
print(io, LEFT_EXTRAPOLATION_ERROR)
end

const RIGHT_EXTRAPOLATION_ERROR = "Cannot extrapolate for t > last(A.t) as the `extrapolation_tight` kwarg passed was `ExtrapolationType.none`"
const RIGHT_EXTRAPOLATION_ERROR = "Cannot extrapolate for t > last(A.t) as the `extrapolation_tight` kwarg passed was `ExtrapolationType.None`"
struct RightExtrapolationError <: Exception end
function Base.showerror(io::IO, ::RightExtrapolationError)
print(io, RIGHT_EXTRAPOLATION_ERROR)
Expand Down
24 changes: 12 additions & 12 deletions src/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,47 @@ end

function _extrapolate_derivative_left(A, t, order)
(; extrapolation_left) = A
if extrapolation_left == ExtrapolationType.none
if extrapolation_left == ExtrapolationType.None
throw(LeftExtrapolationError())
elseif extrapolation_left == ExtrapolationType.constant
elseif extrapolation_left == ExtrapolationType.Constant
zero(first(A.u) / one(A.t[1]))
elseif extrapolation_left == ExtrapolationType.linear
elseif extrapolation_left == ExtrapolationType.Linear
(order == 1) ? derivative(A, first(A.t)) : zero(first(A.u) / one(A.t[1]))
elseif extrapolation_left == ExtrapolationType.extension
elseif extrapolation_left == ExtrapolationType.Extension
iguess = A.iguesser
(order == 1) ? _derivative(A, t, iguess) :
ForwardDiff.derivative(t -> begin
_derivative(A, t, iguess)
end, t)
elseif extrapolation_left == ExtrapolationType.periodic
elseif extrapolation_left == ExtrapolationType.Periodic
t_, _ = transformation_periodic(A, t)
derivative(A, t_, order)
else
# extrapolation_left == ExtrapolationType.reflective
# extrapolation_left == ExtrapolationType.Reflective
t_, n = transformation_reflective(A, t)
isodd(n) ? -derivative(A, t_, order) : derivative(A, t_, order)
end
end

function _extrapolate_derivative_right(A, t, order)
(; extrapolation_right) = A
if extrapolation_right == ExtrapolationType.none
if extrapolation_right == ExtrapolationType.None
throw(RightExtrapolationError())
elseif extrapolation_right == ExtrapolationType.constant
elseif extrapolation_right == ExtrapolationType.Constant
zero(first(A.u) / one(A.t[1]))
elseif extrapolation_right == ExtrapolationType.linear
elseif extrapolation_right == ExtrapolationType.Linear
(order == 1) ? derivative(A, last(A.t)) : zero(first(A.u) / one(A.t[1]))
elseif extrapolation_right == ExtrapolationType.extension
elseif extrapolation_right == ExtrapolationType.Extension
iguess = A.iguesser
(order == 1) ? _derivative(A, t, iguess) :
ForwardDiff.derivative(t -> begin
_derivative(A, t, iguess)
end, t)
elseif extrapolation_right == ExtrapolationType.periodic
elseif extrapolation_right == ExtrapolationType.Periodic
t_, _ = transformation_periodic(A, t)
derivative(A, t_, order)
else
# extrapolation_right == ExtrapolationType.reflective
# extrapolation_right == ExtrapolationType.Reflective
t_, n = transformation_reflective(A, t)
iseven(n) ? -derivative(A, t_, order) : derivative(A, t_, order)
end
Expand Down
24 changes: 12 additions & 12 deletions src/integrals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,25 @@ end

function _extrapolate_integral_left(A, t)
(; extrapolation_left) = A
if extrapolation_left == ExtrapolationType.none
if extrapolation_left == ExtrapolationType.None
throw(LeftExtrapolationError())
elseif extrapolation_left == ExtrapolationType.constant
elseif extrapolation_left == ExtrapolationType.Constant
first(A.u) * (first(A.t) - t)
elseif extrapolation_left == ExtrapolationType.linear
elseif extrapolation_left == ExtrapolationType.Linear
slope = derivative(A, first(A.t))
Δt = first(A.t) - t
(first(A.u) - slope * Δt / 2) * Δt
elseif extrapolation_left == ExtrapolationType.extension
elseif extrapolation_left == ExtrapolationType.Extension
_integral(A, 1, t, first(A.t))
elseif extrapolation_left == ExtrapolationType.periodic
elseif extrapolation_left == ExtrapolationType.Periodic
t_, n = transformation_periodic(A, t)
out = -integral(A, t_)
if !iszero(n)
out -= n * integral(A, first(A.t), last(A.t))
end
out
else
# extrapolation_left == ExtrapolationType.reflective
# extrapolation_left == ExtrapolationType.Reflective
t_, n = transformation_reflective(A, t)
out = if isodd(n)
-integral(A, t_, last(A.t))
Expand All @@ -101,25 +101,25 @@ end

function _extrapolate_integral_right(A, t)
(; extrapolation_right) = A
if extrapolation_right == ExtrapolationType.none
if extrapolation_right == ExtrapolationType.None
throw(RightExtrapolationError())
elseif extrapolation_right == ExtrapolationType.constant
elseif extrapolation_right == ExtrapolationType.Constant
last(A.u) * (t - last(A.t))
elseif extrapolation_right == ExtrapolationType.linear
elseif extrapolation_right == ExtrapolationType.Linear
slope = derivative(A, last(A.t))
Δt = t - last(A.t)
(last(A.u) + slope * Δt / 2) * Δt
elseif extrapolation_right == ExtrapolationType.extension
elseif extrapolation_right == ExtrapolationType.Extension
_integral(A, length(A.t) - 1, last(A.t), t)
elseif extrapolation_right == ExtrapolationType.periodic
elseif extrapolation_right == ExtrapolationType.Periodic
t_, n = transformation_periodic(A, t)
out = integral(A, first(A.t), t_)
if !iszero(n)
out += n * integral(A, first(A.t), last(A.t))
end
out
else
# extrapolation_right == ExtrapolationType.reflective
# extrapolation_right == ExtrapolationType.Reflective
t_, n = transformation_reflective(A, t)
out = if iseven(n)
integral(A, t_, last(A.t))
Expand Down
Loading

0 comments on commit 120e56f

Please sign in to comment.