DataInterpolations.jl
DataInterpolations.jl copied to clipboard
Interpolation error for monotonically decreasing t
Describe the bug 🐞
In v8.0.1, creating an interpolation now checks if the t array is sorted, but it uses the issorted function which, by default, make sure that the values are monotonically increasing. For monotonically decreasing t, this throws an error.
Expected behavior
DataInterpolations should work properly for monotonically decreasing t, as it did before v8.0.1.
Minimal Reproducible Example 👇
using DataInterpolations
N = 11
x = range(1, 0, N)
y = rand(N)
DataInterpolations.LinearInterpolation(y, x)
Error & Stacktrace ⚠️
ArgumentError: The second argument (`t`), which is used for the interpolation domain, is not sorted.
Stacktrace:
[1] munge_data(u::Vector{Float64}, t::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}; check_sorted::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, sorted_arg_name::Tuple{String, String})
@ DataInterpolations [~/.julia/packages/DataInterpolations/CuJMO/src/interpolation_utils.jl:106](http://localhost:8888/lab/tree/FUSE/playground/~/.julia/packages/DataInterpolations/CuJMO/src/interpolation_utils.jl#line=105)
[2] munge_data
@ [~/.julia/packages/DataInterpolations/CuJMO/src/interpolation_utils.jl:94](http://localhost:8888/lab/tree/FUSE/playground/~/.julia/packages/DataInterpolations/CuJMO/src/interpolation_utils.jl#line=93) [inlined]
[3] LinearInterpolation(u::Vector{Float64}, t::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}; extrapolation::DataInterpolations.ExtrapolationType.T, extrapolation_left::DataInterpolations.ExtrapolationType.T, extrapolation_right::DataInterpolations.ExtrapolationType.T, cache_parameters::Bool, assume_linear_t::Float64)
@ DataInterpolations [~/.julia/packages/DataInterpolations/CuJMO/src/interpolation_caches.jl:55](http://localhost:8888/lab/tree/FUSE/playground/~/.julia/packages/DataInterpolations/CuJMO/src/interpolation_caches.jl#line=54)
[4] LinearInterpolation(u::Vector{Float64}, t::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64})
@ DataInterpolations [~/.julia/packages/DataInterpolations/CuJMO/src/interpolation_caches.jl:49](http://localhost:8888/lab/tree/FUSE/playground/~/.julia/packages/DataInterpolations/CuJMO/src/interpolation_caches.jl#line=48)
[5] top-level scope
@ In[8]:5
Environment (please complete the following information):
- Output of
using Pkg; Pkg.status()
Status `/private/var/folders/nt/ct_lf2n94_1c21908mmbx55w0000gq/T/jl_fUWBJg/Project.toml`
[82cc6244] DataInterpolations v8.0.1
- Output of
using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
Status `/private/var/folders/nt/ct_lf2n94_1c21908mmbx55w0000gq/T/jl_fUWBJg/Manifest.toml`
[bbf7d656] CommonSubexpressions v0.3.1
[a8cc5b0e] Crayons v4.1.1
[9a962f9c] DataAPI v1.16.0
[82cc6244] DataInterpolations v8.0.1
[e2d170a0] DataValueInterfaces v1.0.0
[163ba53b] DiffResults v1.1.0
[b552c78f] DiffRules v1.15.1
[ffbed154] DocStringExtensions v0.9.4
[4e289a0a] EnumX v1.0.5
[64ca27bc] FindFirstFunctions v1.4.1
⌅ [f6369f11] ForwardDiff v0.10.38
[92d709cd] IrrationalConstants v0.2.4
[82899510] IteratorInterfaceExtensions v1.0.0
[692b3bcd] JLLWrappers v1.7.0
[b964fa9f] LaTeXStrings v1.4.0
[2ab3a3ac] LogExpFunctions v0.3.29
[1914dd2f] MacroTools v0.5.16
[77ba4419] NaNMath v1.1.3
[bac558e1] OrderedCollections v1.8.0
⌅ [aea7be01] PrecompileTools v1.2.1
[21216c6a] Preferences v1.4.3
[08abe8d2] PrettyTables v2.4.0
[3cdcf5f2] RecipesBase v1.3.4
[189a3867] Reexport v1.2.2
[276daf66] SpecialFunctions v2.5.1
[1e83bf80] StaticArraysCore v1.4.3
[892a3eda] StringManipulation v0.4.1
[3783bdb8] TableTraits v1.0.1
[bd369af6] Tables v1.12.0
[efe28fd5] OpenSpecFun_jll v0.5.6+0
[56f22d72] Artifacts v1.11.0
[2a0f44e3] Base64 v1.11.0
[ade2ca70] Dates v1.11.0
[8f399da3] Libdl v1.11.0
[37e2e46d] LinearAlgebra v1.11.0
[d6f4376e] Markdown v1.11.0
[de0858da] Printf v1.11.0
[9a3f8284] Random v1.11.0
[ea8e919c] SHA v0.7.0
[fa267f1f] TOML v1.0.3
[4ec0a83e] Unicode v1.11.0
[e66e0078] CompilerSupportLibraries_jll v1.1.1+0
[4536629a] OpenBLAS_jll v0.3.27+1
[05823500] OpenLibm_jll v0.8.5+0
[8e850b90] libblastrampoline_jll v5.11.0+0
Info Packages marked with ⌅ have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated -m`
- Output of
versioninfo()
Julia Version 1.11.5
Commit 760b2e5b739 (2025-04-14 06:53 UTC)
Build Info:
Official https://julialang.org/ release
Platform Info:
OS: macOS (arm64-apple-darwin24.0.0)
CPU: 12 × Apple M4 Pro
WORD_SIZE: 64
LLVM: libLLVM-16.0.6 (ORCJIT, apple-m1)
Threads: 10 default, 0 interactive, 5 GC (on 8 virtual cores)
Environment:
JULIA_NUM_THREADS = 10
Oh that's a bad regression. @SebastianM-C https://github.com/SciML/DataInterpolations.jl/pull/421 should have covered this case.
As @SouthEndMusic pointed out some interpolation methods do not allow that: https://github.com/SciML/DataInterpolations.jl/pull/421#discussion_r2036103489
Is there any case beyond LinearInterpolation that actually supports this?
Interesting, it turns out that not even LinearInterpolation supports this. If y is linear I think it might be fine, but for nonlinear y, you get bizarre results with v8.0.0. It fails entirely if you use ExtrapolationType.None, but if you allow extrapolation, it gives a nonsensical result. For example:
using DataInterpolations
N = 1001
x = range(0, 1, N)
y = x .^ 2
println("**Test increasing**")
itp = DataInterpolations.LinearInterpolation(y, x)
xx = 1.0 / π
@show xx^2, itp(xx), (1.0 - itp(xx) / xx^2)
println()
println("**Test decreasing (no extrap)**")
try
itp = DataInterpolations.LinearInterpolation(reverse(y), reverse(x))
@show xx^2, itp(xx), (1.0 - itp(xx) / xx^2)
catch err
display(err)
end
println()
println("**Test decreasing (with extrap)**")
itp = DataInterpolations.LinearInterpolation(reverse(y), reverse(x); extrapolation=ExtrapolationType.Extension)
@show xx^2, itp(xx), (1.0 - itp(xx) / xx^2)
Gives
**Test increasing**
(xx ^ 2, itp(xx), 1.0 - itp(xx) / xx ^ 2) = (0.10132118364233779, 0.10132139749907468, -2.110681391664926e-6)
**Test decreasing (no extrap)**
DataInterpolations.LeftExtrapolationError()
**Test decreasing (with extrap)**
(xx ^ 2, itp(xx), 1.0 - itp(xx) / xx ^ 2) = (0.10132118364233779, -0.3626985375185827, 4.579691082162077)
(0.10132118364233779, -0.3626985375185827, 4.579691082162077)
So in one way, it's really good that we get an explicit error thrown for such unsupported use.
On the other hand, it seems trivial to support this. The t abscissa can be normalized such that tnorm = (t - t[1]) / (t[end]-t[1]). Then as long as it's sorted, regardless of direction, all interpolation types should work properly.
To make that work it also needs to be taken into account in differentiation and integration, and there's also the (undocumented?) feature of adding data to an existing interpolation: https://github.com/SciML/DataInterpolations.jl/blob/master/src/online.jl.
So in one way, it's really good that we get an explicit error thrown for such unsupported use.
I'm glad this helped :D
To make that work it also needs to be taken into account in differentiation and integration
Once we add support for reverse ordering we just need to change the condition to something like
!(issorted(check_sorted) || allows_reverse_order && issorted(check_sorted, rev=true))