-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from SouthEndMusic/invert_integrals
Invert interpolation integrals
- Loading branch information
Showing
9 changed files
with
310 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Inverting integrals | ||
|
||
Solving implicit integral problems of the form: | ||
|
||
```math | ||
\begin{equation} | ||
\text{find $t$ such that } \int_{t_1}^t f(\tau)\text{d}\tau = V \ge 0 | ||
\end{equation} | ||
``` | ||
|
||
is supported for interpolations $f$ that are strictly positive and of one of these types: | ||
|
||
- `ConstantInterpolation` | ||
- `LinearInterpolation` | ||
|
||
This is done by creating an 'integral inverse' interpolation object which can efficiently compute $t$ for a given value of $V$, see the example below. | ||
|
||
```@example inverting_integrals | ||
using Random #hide | ||
Random.seed!(1234) # hide | ||
using DataInterpolations | ||
using Plots | ||
# Create LinearInterpolation object from the | ||
u = sqrt.(1:25) + (2.0 * rand(25) .- 1.0) / 3 | ||
t = cumsum(rand(25)) | ||
A = LinearInterpolation(u, t) | ||
# Create LinearInterpolationIntInv object | ||
# from the LinearInterpolation object | ||
A_intinv = DataInterpolations.invert_integral(A) | ||
# Get the t values up to and including the | ||
# solution to the integral problem | ||
V = 25.0 | ||
t_ = A_intinv(V) | ||
ts = t[t .<= t_] | ||
push!(ts, t_) | ||
# Plot results | ||
plot(A; label = "Linear Interpolation") | ||
plot!(ts, A.(ts), fillrange = 0.0, fillalpha = 0.75, | ||
fc = :blues, lw = 0, label = "Area of $V") | ||
``` | ||
|
||
## Docstrings | ||
|
||
```@docs | ||
DataInterpolations.invert_integral | ||
ConstantInterpolationIntInv | ||
LinearInterpolationIntInv | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
abstract type AbstractIntegralInverseInterpolation{T} <: AbstractInterpolation{T} end | ||
|
||
""" | ||
invert_integral(A::AbstractInterpolation)::AbstractIntegralInverseInterpolation | ||
Creates the inverted integral interpolation object from the given interpolation. Conditions: | ||
- The range of `A` must be strictly positive | ||
- There must be an ordering defined on the data type of `A.u` | ||
- This is currently only supported for ConstantInterpolation and LinearInterpolation | ||
## Arguments | ||
- `A`: interpolation object satisfying the above requirements | ||
""" | ||
invert_integral(A::AbstractInterpolation) = throw(IntegralInverseNotFoundError()) | ||
|
||
_integral(A::AbstractIntegralInverseInterpolation, idx, t) = throw(IntegralNotFoundError()) | ||
|
||
function _derivative(A::AbstractIntegralInverseInterpolation, t::Number, iguess) | ||
inv(A.itp(A(t))), A.idx_prev[] | ||
end | ||
|
||
""" | ||
some stuff | ||
""" | ||
struct LinearInterpolationIntInv{uType, tType, itpType, T} <: | ||
AbstractIntegralInverseInterpolation{T} | ||
u::uType | ||
t::tType | ||
extrapolate::Bool | ||
idx_prev::Base.RefValue{Int} | ||
itp::itpType | ||
function LinearInterpolationIntInv(u, t, A) | ||
new{typeof(u), typeof(t), typeof(A), eltype(u)}( | ||
u, t, A.extrapolate, Ref(1), A) | ||
end | ||
end | ||
|
||
function invertible_integral(A::LinearInterpolation{<:AbstractVector{<:Number}}) | ||
return all(A.u .> 0) | ||
end | ||
|
||
function invert_integral(A::LinearInterpolation{<:AbstractVector{<:Number}}) | ||
!invertible_integral(A) && throw(IntegralNotInvertibleError()) | ||
return LinearInterpolationIntInv(A.t, A.I, A) | ||
end | ||
|
||
function _interpolate( | ||
A::LinearInterpolationIntInv{<:AbstractVector{<:Number}}, t::Number, iguess) | ||
idx = get_idx(A.t, t, iguess) | ||
Δt = t - A.t[idx] | ||
x = A.itp.u[idx] | ||
u = A.u[idx] + 2Δt / (x + sqrt(x^2 + A.itp.p.slope[idx] * 2Δt)) | ||
u, idx | ||
end | ||
|
||
""" | ||
some stuff | ||
""" | ||
struct ConstantInterpolationIntInv{uType, tType, itpType, T} <: | ||
AbstractIntegralInverseInterpolation{T} | ||
u::uType | ||
t::tType | ||
extrapolate::Bool | ||
idx_prev::Base.RefValue{Int} | ||
itp::itpType | ||
function ConstantInterpolationIntInv(u, t, A) | ||
new{typeof(u), typeof(t), typeof(A), eltype(u)}( | ||
u, t, A.extrapolate, Ref(1), A | ||
) | ||
end | ||
end | ||
|
||
function invertible_integral(A::ConstantInterpolation{<:AbstractVector{<:Number}}) | ||
return all(A.u .> 0) | ||
end | ||
|
||
function invert_integral(A::ConstantInterpolation{<:AbstractVector{<:Number}}) | ||
!invertible_integral(A) && throw(IntegralNotInvertibleError()) | ||
return ConstantInterpolationIntInv(A.t, A.I, A) | ||
end | ||
|
||
function _interpolate( | ||
A::ConstantInterpolationIntInv{<:AbstractVector{<:Number}}, t::Number, iguess) | ||
idx = get_idx(A.t, t, iguess; ub_shift = 0) | ||
if A.itp.dir === :left | ||
# :left means that value to the left is used for interpolation | ||
idx_ = get_idx(A.t, t, idx; lb = 1, ub_shift = 0) | ||
else | ||
# :right means that value to the right is used for interpolation | ||
idx_ = get_idx(A.t, t, idx; side = :first, lb = 1, ub_shift = 0) | ||
end | ||
A.u[idx] + (t - A.t[idx]) / A.itp.u[idx_], idx | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.