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

fix: fix DiffEqArray constructors ambiguity #423

Merged
merged 3 commits into from
Feb 22, 2025
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
164 changes: 119 additions & 45 deletions src/vector_of_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,54 +174,77 @@ end

Base.parent(vec::VectorOfArray) = vec.u

function DiffEqArray(vec::AbstractVector{T},
ts::AbstractVector,
::NTuple{N, Int},
p = nothing,
sys = nothing; discretes = nothing) where {T, N}
DiffEqArray{
eltype(T), N, typeof(vec), typeof(ts), typeof(p), typeof(sys), typeof(discretes)}(
vec,
#### 2-argument

# first element representative
function DiffEqArray(vec::AbstractVector, ts::AbstractVector; discretes = nothing, variables = nothing, parameters = nothing, independent_variables = nothing)
sys = SymbolCache(something(variables, []),
something(parameters, []),
something(independent_variables, []))
_size = size(vec[1])
T = eltype(vec[1])
return DiffEqArray{
T,
length(_size) + 1,
typeof(vec),
typeof(ts),
Nothing,
typeof(sys),
typeof(discretes)
}(vec,
ts,
p,
nothing,
sys,
discretes)
end

# T and N from type
function DiffEqArray(vec::AbstractVector{VT}, ts::AbstractVector; discretes = nothing, variables = nothing, parameters = nothing, independent_variables = nothing) where {T, N, VT <: AbstractArray{T, N}}
sys = SymbolCache(something(variables, []),
something(parameters, []),
something(independent_variables, []))
return DiffEqArray{
eltype(eltype(vec)),
N + 1,
typeof(vec),
typeof(ts),
Nothing,
typeof(sys),
typeof(discretes)
}(vec,
ts,
nothing,
sys,
discretes)
end

# ambiguity resolution
function DiffEqArray(vec::AbstractVector{VT},
ts::AbstractVector,
::NTuple{N, Int}) where {T, N, VT <: AbstractArray{T, N}}
DiffEqArray{eltype(T), N, typeof(vec), typeof(ts), Nothing, Nothing, Nothing}(vec,
#### 3-argument

# NTuple, T from type
function DiffEqArray(vec::AbstractVector{T}, ts::AbstractVector, ::NTuple{N, Int}; discretes = nothing) where {T, N}
DiffEqArray{
eltype(T), N, typeof(vec), typeof(ts), Nothing, Nothing, typeof(discretes)}(
vec,
ts,
nothing,
nothing,
nothing)
discretes)
end
function DiffEqArray(vec::AbstractVector{VT},
ts::AbstractVector,
::NTuple{N, Int}, p; discretes = nothing) where {T, N, VT <: AbstractArray{T, N}}
DiffEqArray{
eltype(T), N, typeof(vec), typeof(ts), typeof(p), Nothing, typeof(discretes)}(vec,

# NTuple parameter
function DiffEqArray(vec::AbstractVector{VT}, ts::AbstractVector, p::NTuple{N2, Int}; discretes = nothing) where {T, N, VT <: AbstractArray{T, N}, N2}
DiffEqArray{eltype(T), N + 1, typeof(vec), typeof(ts), typeof(p), Nothing, typeof(discretes)}(vec,
ts,
p,
nothing,
discretes)
end
# Assume that the first element is representative of all other elements

function DiffEqArray(vec::AbstractVector,
ts::AbstractVector,
p = nothing,
sys = nothing;
variables = nothing,
parameters = nothing,
independent_variables = nothing,
discretes = nothing)
sys = something(sys,
SymbolCache(something(variables, []),
# first element representative
function DiffEqArray(vec::AbstractVector, ts::AbstractVector, p; discretes = nothing, variables = nothing, parameters = nothing, independent_variables = nothing)
sys = SymbolCache(something(variables, []),
something(parameters, []),
something(independent_variables, [])))
something(independent_variables, []))
_size = size(vec[1])
T = eltype(vec[1])
return DiffEqArray{
Expand All @@ -239,21 +262,50 @@ function DiffEqArray(vec::AbstractVector,
discretes)
end

function DiffEqArray(vec::AbstractVector{VT},
ts::AbstractVector,
p = nothing,
sys = nothing;
variables = nothing,
parameters = nothing,
independent_variables = nothing,
discretes = nothing) where {T, N, VT <: AbstractArray{T, N}}
sys = something(sys,
SymbolCache(something(variables, []),
# T and N from type
function DiffEqArray(vec::AbstractVector{VT}, ts::AbstractVector, p; discretes = nothing, variables = nothing, parameters = nothing, independent_variables = nothing) where {T, N, VT <: AbstractArray{T, N}}
sys = SymbolCache(something(variables, []),
something(parameters, []),
something(independent_variables, [])))
something(independent_variables, []))
DiffEqArray{eltype(T), N + 1, typeof(vec), typeof(ts), typeof(p), typeof(sys), typeof(discretes)}(vec,
ts,
p,
sys,
discretes)
end

#### 4-argument

# NTuple, T from type
function DiffEqArray(vec::AbstractVector{T}, ts::AbstractVector, ::NTuple{N, Int}, p; discretes = nothing) where {T, N}
DiffEqArray{
eltype(T), N, typeof(vec), typeof(ts), typeof(p), Nothing, typeof(discretes)}(
vec,
ts,
p,
nothing,
discretes)
end

# NTuple parameter
function DiffEqArray(vec::AbstractVector{VT}, ts::AbstractVector, p::NTuple{N2, Int}, sys; discretes = nothing) where {T, N, VT <: AbstractArray{T, N}, N2}
DiffEqArray{eltype(T), N + 1, typeof(vec), typeof(ts), typeof(p), typeof(sys), typeof(discretes)}(vec,
ts,
p,
sys,
discretes)
end

# first element representative
function DiffEqArray(vec::AbstractVector, ts::AbstractVector, p, sys; discretes = nothing, variables = nothing, parameters = nothing, independent_variables = nothing)
sys = SymbolCache(something(variables, []),
something(parameters, []),
something(independent_variables, []))
_size = size(vec[1])
T = eltype(vec[1])
return DiffEqArray{
eltype(eltype(vec)),
N + 1,
T,
length(_size) + 1,
typeof(vec),
typeof(ts),
typeof(p),
Expand All @@ -266,6 +318,28 @@ function DiffEqArray(vec::AbstractVector{VT},
discretes)
end

# T and N from type
function DiffEqArray(vec::AbstractVector{VT}, ts::AbstractVector, p, sys; discretes = nothing) where {T, N, VT <: AbstractArray{T, N}}
DiffEqArray{eltype(T), N + 1, typeof(vec), typeof(ts), typeof(p), typeof(sys), typeof(discretes)}(vec,
ts,
p,
sys,
discretes)
end

#### 5-argument

# NTuple, T from type
function DiffEqArray(vec::AbstractVector{T}, ts::AbstractVector, ::NTuple{N, Int}, p, sys; discretes = nothing) where {T, N}
DiffEqArray{
eltype(T), N, typeof(vec), typeof(ts), typeof(p), typeof(sys), typeof(discretes)}(
vec,
ts,
p,
sys,
discretes)
end

has_discretes(::T) where {T <: AbstractDiffEqArray} = hasfield(T, :discretes)
get_discretes(x) = getfield(x, :discretes)

Expand Down
2 changes: 1 addition & 1 deletion test/downstream/symbol_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sol_new = DiffEqArray(sol.u[1:10],
@test all(isequal.(all_variable_symbols(sol), all_variable_symbols(sol_new)))
@test all(isequal.(all_variable_symbols(sol), [x, RHS]))
@test all(isequal.(all_symbols(sol), all_symbols(sol_new)))
@test all(isequal.(all_symbols(sol), [x, RHS, τ, t]))
@test all([any(isequal(sym), all_symbols(sol)) for sym in [x, RHS, τ, t, Initial(x), Initial(RHS)]])
@test sol[solvedvariables] == sol[[x]]
@test sol_new[solvedvariables] == sol_new[[x]]
@test sol[allvariables] == sol[[x, RHS]]
Expand Down
19 changes: 19 additions & 0 deletions test/interface_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,22 @@ end
zvoa = zero(voa)
@test zvoa.u[1] == zvoa.u[2] == zeros(3)
end

@testset "Issue SciMLBase#889: `DiffEqArray` constructor ambiguity" begin
darr = DiffEqArray([[1.0, 1.0]], [1.0], ())
@test darr.p == ()
@test darr.sys === nothing
@test ndims(darr) == 2
darr = DiffEqArray([[1.0, 1.0]], [1.0], (), "A")
@test darr.p == ()
@test darr.sys == "A"
@test ndims(darr) == 2
darr = DiffEqArray([ones(2, 2)], [1.0], (1, 1, 1))
@test darr.p == (1, 1, 1)
@test darr.sys === nothing
@test ndims(darr) == 3
darr = DiffEqArray([ones(2, 2)], [1.0], (1, 1, 1), "A")
@test darr.p == (1, 1, 1)
@test darr.sys == "A"
@test ndims(darr) == 3
end
Loading