Skip to content

Commit 1ea6bba

Browse files
committed
Ensure that the interpolation domain is sorted
1 parent 44f2343 commit 1ea6bba

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/interpolation_utils.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,22 @@ end
9494
function munge_data(u::AbstractVector, t::AbstractVector)
9595
Tu = nonmissingtype(eltype(u))
9696
Tt = nonmissingtype(eltype(t))
97+
9798
if Tu === eltype(u) && Tt === eltype(t)
99+
if !(issorted(t) || issorted(t, rev = true))
100+
# there is likely an user error
101+
msg = "The second argument, which is used for the interpolation domain, is not sorted."
102+
if (issorted(u) || issorted(u, rev = true))
103+
msg *= "\nIt looks like the arguments were inversed, make sure you used the arguments in the correct order."
104+
end
105+
throw(ArgumentError(msg))
106+
end
107+
98108
return u, t
99109
end
100110

101111
@assert length(t) == length(u)
112+
102113
non_missing_mask = map((ui, ti) -> !ismissing(ui) && !ismissing(ti), u, t)
103114
u = convert(AbstractVector{Tu}, u[non_missing_mask])
104115
t = convert(AbstractVector{Tt}, t[non_missing_mask])

test/interpolation_tests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,3 +1105,8 @@ f_cubic_spline = c -> square(CubicSpline, c)
11051105
end
11061106
end
11071107
end
1108+
1109+
@testset "user error" begin
1110+
@test_throws ArgumentError LinearInterpolation(rand(10), rand(10))
1111+
@test_throws ArgumentError LinearInterpolation(0:10, rand(10))
1112+
end

0 commit comments

Comments
 (0)