Skip to content

Commit

Permalink
refactor: integral function to correctly compute second index and rem…
Browse files Browse the repository at this point in the history
…ove `samples`
  • Loading branch information
sathvikbhagavan committed Jun 7, 2024
1 parent daebb52 commit d68068e
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/integrals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ end
function integral(A::AbstractInterpolation, t1::Number, t2::Number)
((t1 < A.t[1] || t1 > A.t[end]) && !A.extrapolate) && throw(ExtrapolationError())
((t2 < A.t[1] || t2 > A.t[end]) && !A.extrapolate) && throw(ExtrapolationError())
bw, fw = samples(A)
# the index less than or equal to t1
idx1 = max(1 + bw, min(searchsortedlast(A.t, t1), length(A.t) - fw))
idx1 = max(1, min(searchsortedlast(A.t, t1), length(A.t) - 1))
# the index less than t2
idx2 = max(2 + bw, min(searchsortedlast(A.t, t2), length(A.t) - fw))
idx2 = max(1, min(searchsortedlast(A.t, t2), length(A.t) - 1))
if A.t[idx2] == t2
idx2 -= 1
end
Expand All @@ -23,7 +22,6 @@ function integral(A::AbstractInterpolation, t1::Number, t2::Number)
total
end

samples(A::LinearInterpolation{<:AbstractVector}) = (0, 1)
function _integral(A::LinearInterpolation{<:AbstractVector{<:Number}},
idx::Number,
t::Number)
Expand All @@ -34,7 +32,6 @@ function _integral(A::LinearInterpolation{<:AbstractVector{<:Number}},
t^2 * (u1 - u2) / (2 * t1 - 2 * t2) + t * (t1 * u2 - t2 * u1) / (t1 - t2)
end

samples(A::ConstantInterpolation{<:AbstractVector}) = (0, 1)
function _integral(A::ConstantInterpolation{<:AbstractVector}, idx::Number, t::Number)
if A.dir === :left
# :left means that value to the left is used for interpolation
Expand All @@ -45,7 +42,6 @@ function _integral(A::ConstantInterpolation{<:AbstractVector}, idx::Number, t::N
end
end

samples(A::QuadraticInterpolation{<:AbstractVector}) = (0, 1)
function _integral(A::QuadraticInterpolation{<:AbstractVector{<:Number}},
idx::Number,
t::Number)
Expand All @@ -69,7 +65,6 @@ function _integral(A::QuadraticInterpolation{<:AbstractVector{<:Number}},
(t1^2 * t2 - t1^2 * t3 - t1 * t2^2 + t1 * t3^2 + t2^2 * t3 - t2 * t3^2))
end

samples(A::QuadraticSpline{<:AbstractVector{<:Number}}) = (0, 1)
function _integral(A::QuadraticSpline{<:AbstractVector{<:Number}}, idx::Number, t::Number)
t1 = A.t[idx]
t2 = A.t[idx + 1]
Expand All @@ -81,7 +76,6 @@ function _integral(A::QuadraticSpline{<:AbstractVector{<:Number}}, idx::Number,
(2 * t1 - 2 * t2)
end

samples(A::CubicSpline{<:AbstractVector{<:Number}}) = (0, 1)
function _integral(A::CubicSpline{<:AbstractVector{<:Number}}, idx::Number, t::Number)
t1 = A.t[idx]
t2 = A.t[idx + 1]
Expand All @@ -98,7 +92,6 @@ function _integral(A::CubicSpline{<:AbstractVector{<:Number}}, idx::Number, t::N
(6 * h2))
end

samples(A::AkimaInterpolation{<:AbstractVector{<:Number}}) = (0, 1)
function _integral(A::AkimaInterpolation{<:AbstractVector{<:Number}},
idx::Number,
t::Number)
Expand Down

0 comments on commit d68068e

Please sign in to comment.