Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make extrapolation with NaN Unitful-friendly #366

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading