diff --git a/src/interpolation_utils.jl b/src/interpolation_utils.jl index 86a9dfbb..3d484cdc 100644 --- a/src/interpolation_utils.jl +++ b/src/interpolation_utils.jl @@ -328,6 +328,13 @@ function munge_data(U::AbstractMatrix, t::AbstractVector) TU = nonmissingtype(eltype(U)) Tt = nonmissingtype(eltype(t)) if TU === eltype(U) && Tt === eltype(t) + if !issorted(t; by = ForwardDiff.value) + throw( + ArgumentError( + "The second argument (`t`), which is used for the interpolation domain, is not sorted." + ) + ) + end return U, t end @@ -347,6 +354,13 @@ function munge_data(U::AbstractArray{T, N}, t) where {T, N} TU = nonmissingtype(eltype(U)) Tt = nonmissingtype(eltype(t)) if TU === eltype(U) && Tt === eltype(t) + if !issorted(t; by = ForwardDiff.value) + throw( + ArgumentError( + "The second argument (`t`), which is used for the interpolation domain, is not sorted." + ) + ) + end return U, t end diff --git a/test/interpolation_tests.jl b/test/interpolation_tests.jl index 3efde54d..4f4a43e9 100644 --- a/test/interpolation_tests.jl +++ b/test/interpolation_tests.jl @@ -2214,6 +2214,16 @@ f_cubic_spline = c -> square(CubicSpline, c) end end +@testset "munge_data rejects unsorted t for matrix/N-d u" begin + # The vector `munge_data` method already rejects unsorted `t`; the matrix and + # N-d methods silently accepted it and returned wrong values + # (https://github.com/SciML/DataInterpolations.jl, candidate 5). + us = [1.0, 2.0, 3.0, 4.0] + ts = [1.0, 3.0, 2.0, 4.0] + @test_throws ArgumentError LinearInterpolation([us'; us'], ts) + @test_throws ArgumentError LinearInterpolation(reshape(vcat(us, us), 2, 1, 4), ts) +end + @testset "user error" begin @test_throws ArgumentError LinearInterpolation(rand(10), rand(10)) @test_throws ArgumentError LinearInterpolation(1:10, rand(10))