Skip to content

Commit

Permalink
Merge pull request #46 from PumasAI/an/zerospline
Browse files Browse the repository at this point in the history
Fix dir=:left in ZeroSpline
  • Loading branch information
ChrisRackauckas authored Sep 20, 2019
2 parents e4aa09f + 3027fb5 commit 543a7ed
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/caches/interpolation_caches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ end
struct ZeroSpline{uType,tType,dirType,FT,T} <: AbstractInterpolation{FT,T}
u::uType
t::tType
dir::Symbol
dir::Symbol # indicates if value to the $dir should be used for the interpolation
ZeroSpline{FT}(u,t,dir) where FT = new{typeof(u),typeof(t),typeof(dir),FT,eltype(u)}(u,t,dir)
end

Expand Down
14 changes: 10 additions & 4 deletions src/interpolation_alg/interpolation_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,24 @@ end

# ZeroSpline Interpolation
function (A::ZeroSpline{<:AbstractVector{<:Number}})(t::Number)
i = searchsortedfirst(A.t, t)
if A.dir === :left
return A.u[max(2, i) - 1]
# :left means that value to the left is used for interpolation
i = searchsortedlast(A.t, t)
return A.u[max(1, i)]
else
# :right means that value to the right is used for interpolation
i = searchsortedfirst(A.t, t)
return A.u[min(length(A.t), i)]
end
end
function (A::ZeroSpline{<:AbstractMatrix{<:Number}})(t::Number)
i = searchsortedfirst(A.t, t)
if A.dir === :left
return A.u[:, max(2, i) - 1]
# :left means that value to the left is used for interpolation
i = searchsortedlast(A.t, t)
return A.u[:, max(1, i)]
else
# :right means that value to the right is used for interpolation
i = searchsortedfirst(A.t, t)
return A.u[:, min(length(A.t), i)]
end
end
Expand Down
48 changes: 39 additions & 9 deletions test/interpolation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,52 @@ A = LagrangeInterpolation(u,t)


# ZeroSpline Interpolation
u = [1.0, 4.0, 9.0, 16.0]
u = [1.0, 2.0, 0.0, 1.0]
t = [1.0, 2.0, 3.0, 4.0]
A = ZeroSpline(u,t)

A = ZeroSpline(u, t, dir=:right)
@test A(0.5) == 1.0
@test A(1.0) == 1.0
@test A(1.5) == 2.0
@test A(2.0) == 2.0
@test A(2.5) == 0.0
@test A(3.0) == 0.0
@test A(3.5) == 1.0
@test A(4.0) == 1.0
@test A(4.5) == 1.0
A = ZeroSpline(u, t) # dir=:left is default
@test A(0.5) == 1.0
@test A(1.0) == 1.0
@test A(1.5) == 1.0
@test A(2.5) == 4.0
@test A(4.0) == 9.0

u = [1.0 4.0 9.0 16.0; 1.0 4.0 9.0 16.0]
@test A(2.0) == 2.0
@test A(2.5) == 2.0
@test A(3.0) == 0.0
@test A(3.5) == 0.0
@test A(4.0) == 1.0
@test A(4.5) == 1.0

u = [1.0 2.0 0.0 1.0; 1.0 2.0 0.0 1.0]
A = ZeroSpline(u,t)

A = ZeroSpline(u, t, dir=:right)
@test A(0.5) == [1.0, 1.0]
@test A(1.0) == [1.0, 1.0]
@test A(1.5) == [2.0, 2.0]
@test A(2.0) == [2.0, 2.0]
@test A(2.5) == [0.0, 0.0]
@test A(3.0) == [0.0, 0.0]
@test A(3.5) == [1.0, 1.0]
@test A(4.0) == [1.0, 1.0]
@test A(4.5) == [1.0, 1.0]
A = ZeroSpline(u, t) # dir=:left is default
@test A(0.5) == [1.0, 1.0]
@test A(1.0) == [1.0, 1.0]
@test A(1.5) == [1.0, 1.0]
@test A(2.5) == [4.0, 4.0]
@test A(4.0) == [9.0, 9.0]
@test A(2.0) == [2.0, 2.0]
@test A(2.5) == [2.0, 2.0]
@test A(3.0) == [0.0, 0.0]
@test A(3.5) == [0.0, 0.0]
@test A(4.0) == [1.0, 1.0]
@test A(4.5) == [1.0, 1.0]

# QuadraticSpline Interpolation
u = [0.0, 1.0, 3.0]
Expand Down

0 comments on commit 543a7ed

Please sign in to comment.