From 98077179d07ef7223673dc374a1998144174f3b7 Mon Sep 17 00:00:00 2001 From: Sathvik Bhagavan Date: Thu, 13 Jun 2024 07:14:09 +0000 Subject: [PATCH 1/2] refactor: add methods for computing with vector of timepoints for missing cases - vector of vectors and matrices --- src/DataInterpolations.jl | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/DataInterpolations.jl b/src/DataInterpolations.jl index 9ccaf31e..38cb387d 100644 --- a/src/DataInterpolations.jl +++ b/src/DataInterpolations.jl @@ -22,7 +22,30 @@ include("show.jl") (interp::AbstractInterpolation)(t::Number) = _interpolate(interp, t) (interp::AbstractInterpolation)(t::Number, i::Integer) = _interpolate(interp, t, i) function (interp::AbstractInterpolation)(t::AbstractVector) - interp(similar(t, promote_type(eltype(interp.u), eltype(t))), t) + u = get_u(interp.u, t) + interp(u, t) +end + +function get_u(u::AbstractVector, t) + return similar(t, promote_type(eltype(u), eltype(t))) +end + +function get_u(u::AbstractVector{<:AbstractVector}, t) + type = promote_type(eltype(eltype(u)), eltype(t)) + return [zeros(type, length(first(u))) for _ in eachindex(t)] +end + +function get_u(u::AbstractMatrix, t) + type = promote_type(eltype(u), eltype(t)) + return zeros(type, (size(u, 1), length(t))) +end + +function (interp::AbstractInterpolation)(u::AbstractMatrix, t::AbstractVector) + iguess = firstindex(interp.t) + @inbounds for i in eachindex(t) + u[:, i], iguess = interp(t[i], iguess) + end + u end function (interp::AbstractInterpolation)(u::AbstractVector, t::AbstractVector) iguess = firstindex(interp.t) From dfa2a978baf187663992137b2787a3f101565c97 Mon Sep 17 00:00:00 2001 From: Sathvik Bhagavan Date: Thu, 13 Jun 2024 07:16:29 +0000 Subject: [PATCH 2/2] test: add tests for computing with vector of timepoints --- test/interpolation_tests.jl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/interpolation_tests.jl b/test/interpolation_tests.jl index 24811680..a11d6ea4 100644 --- a/test/interpolation_tests.jl +++ b/test/interpolation_tests.jl @@ -636,6 +636,23 @@ end end end +@testset "Plugging vector timepoints" begin + # Issue https://github.com/SciML/DataInterpolations.jl/issues/267 + t = Float64[1.0, 2.0, 3.0, 4.0, 5.0] + @testset "utype - Vectors" begin + interp = LinearInterpolation(rand(5), t) + @test interp(t) isa Vector{Float64} + end + @testset "utype - Vector of Vectors" begin + interp = LinearInterpolation([rand(2) for _ in 1:5], t) + @test interp(t) isa Vector{Vector{Float64}} + end + @testset "utype - Matrix" begin + interp = LinearInterpolation(rand(2, 5), t) + @test interp(t) isa Matrix{Float64} + end +end + # missing values handling tests u = [1.0, 4.0, 9.0, 16.0, 25.0, missing, missing] t = [1.0, 2.0, 3.0, 4.0, missing, 6.0, missing]