Skip to content

Commit

Permalink
Merge pull request #268 from sathvikbhagavan/sb/vector_timepoints
Browse files Browse the repository at this point in the history
refactor: add methods for computing with vector of timepoints vector of vectors and matrices
  • Loading branch information
ChrisRackauckas authored Jun 13, 2024
2 parents ca7ff72 + dfa2a97 commit 03cc960
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/DataInterpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions test/interpolation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 03cc960

Please sign in to comment.