Skip to content

Commit 69f70a7

Browse files
Fixed DiffEqArray constructors, added tests
- DiffEqArray constructs SymbolCache taking into account if `indepsym` is `nothing`, a single symbol or an `AbstractArray` of symbols - Test symbolic indexing functionality
1 parent 16de275 commit 69f70a7

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/vector_of_array.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,23 @@ VectorOfArray(vec::AbstractVector{T}, ::NTuple{N}) where {T, N} = VectorOfArray{
9393
VectorOfArray(vec::AbstractVector) = VectorOfArray(vec, (size(vec[1])..., length(vec)))
9494
VectorOfArray(vec::AbstractVector{VT}) where {T, N, VT<:AbstractArray{T, N}} = VectorOfArray{T, N+1, typeof(vec)}(vec)
9595

96-
DiffEqArray(vec::AbstractVector{T}, ts, ::NTuple{N}, syms=nothing, indepsym=nothing, observed=nothing, p=nothing) where {T, N} = DiffEqArray{eltype(T), N, typeof(vec), typeof(ts), SymbolCache{typeof(syms), Vector{typeof(indepsym)}, Nothing}, typeof(observed), typeof(p)}(vec, ts, SymbolCache(syms, [indepsym], nothing), observed, p)
96+
function DiffEqArray(vec::AbstractVector{T}, ts, ::NTuple{N}, syms=nothing, indepsym=nothing, observed=nothing, p=nothing) where {T, N}
97+
sc = if isnothing(indepsym) || indepsym isa AbstractArray
98+
SymbolCache{typeof(syms),typeof(indepsym),Nothing}(syms, indepsym, nothing)
99+
else
100+
SymbolCache{typeof(syms),Vector{typeof(indepsym)},Nothing}(syms, [indepsym], nothing)
101+
end
102+
DiffEqArray{eltype(T), N, typeof(vec), typeof(ts), typeof(sc), typeof(observed), typeof(p)}(vec, ts, sc, observed, p)
103+
end
97104
# Assume that the first element is representative of all other elements
98105
DiffEqArray(vec::AbstractVector,ts::AbstractVector, syms=nothing, indepsym=nothing, observed=nothing, p=nothing) = DiffEqArray(vec, ts, (size(vec[1])..., length(vec)), syms, indepsym, observed, p)
99106
function DiffEqArray(vec::AbstractVector{VT},ts::AbstractVector, syms=nothing, indepsym=nothing, observed=nothing, p=nothing) where {T, N, VT<:AbstractArray{T, N}}
100-
DiffEqArray{T, N+1, typeof(vec), typeof(ts), SymbolCache{typeof(syms), Vector{typeof(indepsym)}, Nothing}, typeof(observed), typeof(p)}(vec, ts, SymbolCache(syms, [indepsym], nothing), observed, p)
107+
sc = if isnothing(indepsym) || indepsym isa AbstractArray
108+
SymbolCache{typeof(syms),typeof(indepsym),Nothing}(syms, indepsym, nothing)
109+
else
110+
SymbolCache{typeof(syms),Vector{typeof(indepsym)},Nothing}(syms, [indepsym], nothing)
111+
end
112+
DiffEqArray{T, N+1, typeof(vec), typeof(ts), typeof(sc), typeof(observed), typeof(p)}(vec, ts, sc, observed, p)
101113
end
102114

103115
# Interface for the linear indexing. This is just a view of the underlying nested structure

test/symbolic_indexing_interface_test.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ sc = SymbolCache([:a, :b], [:t], [:c, :d])
2626
@test isnothing(param_sym_to_index(sc, :a))
2727
@test all(is_param_sym.((sc,), [:c, :d]))
2828
@test !is_param_sym(sc, :b)
29+
30+
t = 0.0:0.1:1.0
31+
f(x) = 2x
32+
f2(x) = 3x
33+
34+
dx = DiffEqArray([[f(x), f2(x)] for x in t], t, [:a, :b], :t)
35+
@test dx[:t] == t
36+
@test dx[:a] == [f(x) for x in t]
37+
@test dx[:b] == [f2(x) for x in t]
38+
39+
dx = DiffEqArray([[f(x), f2(x)] for x in t], t, [:a, :b], [:t])
40+
@test dx[:t] == t
41+
dx = DiffEqArray([[f(x), f2(x)] for x in t], t, [:a, :b])
42+
@test_throws Exception dx[nothing] # make sure it isn't storing [nothing] as indepsym

0 commit comments

Comments
 (0)