Skip to content

Commit

Permalink
Update integral values when adding data
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Jul 6, 2024
1 parent 947aefb commit a4c2988
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/online.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import Base: append!, push!

function add_integral_values!(A)
integral_values = cumsum([_integral(A, idx, A.t[idx + 1]) - _integral(A, idx, A.t[idx])
for idx in (length(A.I) - 1):(length(A.t) - 1)])
pop!(A.I)
integral_values .+= last(A.I)
append!(A.I, integral_values)
end

function push!(A::LinearInterpolation{U, T}, u::eltype(U), t::eltype(T)) where {U, T}
push!(A.u.parent, u)
push!(A.t.parent, t)
slope = linear_interpolation_parameters(A.u, A.t, length(A.t) - 1)
push!(A.p.slope, slope)
add_integral_values!(A)
A
end

Expand All @@ -15,12 +24,14 @@ function push!(A::QuadraticInterpolation{U, T}, u::eltype(U), t::eltype(T)) wher
push!(A.p.l₀, l₀)
push!(A.p.l₁, l₁)
push!(A.p.l₂, l₂)
add_integral_values!(A)
A
end

function push!(A::ConstantInterpolation{U, T}, u::eltype(U), t::eltype(T)) where {U, T}
push!(A.u.parent, u)
push!(A.t.parent, t)
add_integral_values!(A)
A
end

Expand All @@ -34,6 +45,7 @@ function append!(
slope = linear_interpolation_parameters.(
Ref(A.u), Ref(A.t), length_old:(length(A.t) - 1))
append!(A.p.slope, slope)
add_integral_values!(A)
A
end

Expand All @@ -43,6 +55,7 @@ function append!(
u, t = munge_data(u, t, true)
append!(A.u.parent, u)
append!(A.t.parent, t)
add_integral_values!(A)
A
end

Expand All @@ -59,5 +72,6 @@ function append!(
append!(A.p.l₀, l₀)
append!(A.p.l₁, l₁)
append!(A.p.l₂, l₂)
add_integral_values!(A)
A
end
2 changes: 2 additions & 0 deletions test/online_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ for method in [LinearInterpolation, QuadraticInterpolation, ConstantInterpolatio
@test getfield(func1.p, name) == getfield(func2.p, name)
end
@test func1(ts) == func2(ts)
@test func1.I == func2.I

func1 = method(u1, t1)
push!(func1, 1.0, 4.0)
Expand All @@ -28,4 +29,5 @@ for method in [LinearInterpolation, QuadraticInterpolation, ConstantInterpolatio
@test getfield(func1.p, name) == getfield(func2.p, name)
end
@test func1(ts) == func2(ts)
@test func1.I == func2.I
end

0 comments on commit a4c2988

Please sign in to comment.