Skip to content

Commit

Permalink
Merge pull request #366 from Ickaser/nan_interp
Browse files Browse the repository at this point in the history
Make extrapolation with NaN Unitful-friendly
  • Loading branch information
ChrisRackauckas authored Dec 1, 2024
2 parents 0cc058d + 064aaa1 commit 7df4954
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ SafeTestsets = "0.1"
StableRNGs = "1"
Symbolics = "5.29, 6"
Test = "1"
Unitful = "1"
Zygote = "0.6.70"
julia = "1.10"

Expand All @@ -57,7 +58,8 @@ SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[targets]
test = ["Aqua", "BenchmarkTools", "SafeTestsets", "ChainRulesCore", "Optim", "RegularizationTools", "Test", "StableRNGs", "FiniteDifferences", "QuadGK", "ForwardDiff", "Symbolics", "Zygote"]
test = ["Aqua", "BenchmarkTools", "SafeTestsets", "ChainRulesCore", "Optim", "RegularizationTools", "Test", "StableRNGs", "FiniteDifferences", "QuadGK", "ForwardDiff", "Symbolics", "Unitful", "Zygote"]
6 changes: 3 additions & 3 deletions src/interpolation_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ function _interpolate(A::LinearInterpolation{<:AbstractVector}, t::Number, igues
if isnan(t)
# For correct derivative with NaN
idx = firstindex(A.u)
t1 = t2 = one(eltype(A.t))
u1 = u2 = one(eltype(A.u))
slope = t * get_parameters(A, idx)
t1 = t2 = oneunit(eltype(A.t))
u1 = u2 = oneunit(eltype(A.u))
slope = t/t * get_parameters(A, idx)
else
idx = get_idx(A, t, iguess)
t1, t2 = A.t[idx], A.t[idx + 1]
Expand Down
15 changes: 15 additions & 0 deletions test/interpolation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using FindFirstFunctions: searchsortedfirstcorrelated
using StableRNGs
using Optim, ForwardDiff
using BenchmarkTools
using Unitful

function test_interpolation_type(T)
@test T <: DataInterpolations.AbstractInterpolation
Expand Down Expand Up @@ -105,6 +106,14 @@ end
@test isnan(A(3.5))
@test isnan(A(4.0))

u = [0.0, 1.0, 2.0, NaN]
A = LinearInterpolation(u, t; extrapolate = true)
@test A(1.0) == 0.0
@test A(2.0) == 1.0
@test A(3.0) == 2.0
@test isnan(A(3.5))
@test isnan(A(4.0))

# Test type stability
u = Float32.(1:5)
t = Float32.(1:5)
Expand Down Expand Up @@ -134,6 +143,12 @@ end
@test @inferred(A(R64)) === A(R64)
end

# NaN time value for Unitful arrays: issue #365
t = (0:3)u"s" # Unitful quantities
u = [0, -2, -1, -2]u"m"
A = LinearInterpolation(u, t; extrapolate = true)
@test isnan(A(NaN*u"s"))

# Nan time value:
t = 0.0:3 # Floats
u = [0, -2, -1, -2]
Expand Down

0 comments on commit 7df4954

Please sign in to comment.