@@ -3,11 +3,30 @@ mutable struct VectorOfArray{T, N, A} <: AbstractVectorOfArray{T, N, A}
33 u:: A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
44end
55# VectorOfArray with an added series for time
6- mutable struct DiffEqArray{T, N, A, B} <: AbstractDiffEqArray{T, N, A}
6+ mutable struct DiffEqArray{T, N, A, B, C, D, E, F } <: AbstractDiffEqArray{T, N, A}
77 u:: A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
88 t:: B
9+ syms:: C
10+ indepsym:: D
11+ observed:: E
12+ p:: F
913end
1014
15+ Base. @pure __parameterless_type (T) = Base. typename (T). wrapper
16+ parameterless_type (x) = parameterless_type (typeof (x))
17+ parameterless_type (x:: Type ) = __parameterless_type (x)
18+
19+ # ## Abstract Interface
20+ struct AllObserved
21+ end
22+ issymbollike (x) = x isa Symbol ||
23+ x isa AllObserved ||
24+ Symbol (parameterless_type (typeof (x))) == :Operation ||
25+ Symbol (parameterless_type (typeof (x))) == :Variable ||
26+ Symbol (parameterless_type (typeof (x))) == :Sym ||
27+ Symbol (parameterless_type (typeof (x))) == :Num ||
28+ Symbol (parameterless_type (typeof (x))) == :Term
29+
1130Base. Array (VA:: AbstractVectorOfArray{T,N,A} ) where {T,N,A <: AbstractVector{<:AbstractVector} } = reduce (hcat,VA. u)
1231Base. Array (VA:: AbstractVectorOfArray{T,N,A} ) where {T,N,A <: AbstractVector{<:Number} } = VA. u
1332function Base. Array (VA:: AbstractVectorOfArray )
@@ -20,10 +39,11 @@ VectorOfArray(vec::AbstractVector{T}, ::NTuple{N}) where {T, N} = VectorOfArray{
2039VectorOfArray (vec:: AbstractVector ) = VectorOfArray (vec, (size (vec[1 ])... , length (vec)))
2140VectorOfArray (vec:: AbstractVector{VT} ) where {T, N, VT<: AbstractArray{T, N} } = VectorOfArray {T, N+1, typeof(vec)} (vec)
2241
23- DiffEqArray (vec:: AbstractVector{T} , ts, :: NTuple{N} ) where {T, N} = DiffEqArray {eltype(T), N, typeof(vec), typeof(ts)} (vec, ts)
42+ DiffEqArray (vec:: AbstractVector{T} , ts, :: NTuple{N} ) where {T, N} = DiffEqArray {eltype(T), N, typeof(vec), typeof(ts), Nothing, Nothing, Nothing, Nothing } (vec, ts, nothing , nothing , nothing , nothing )
2443# Assume that the first element is representative of all other elements
2544DiffEqArray (vec:: AbstractVector ,ts:: AbstractVector ) = DiffEqArray (vec, ts, (size (vec[1 ])... , length (vec)))
26- DiffEqArray (vec:: AbstractVector{VT} ,ts:: AbstractVector ) where {T, N, VT<: AbstractArray{T, N} } = DiffEqArray {T, N+1, typeof(vec), typeof(ts)} (vec, ts)
45+ DiffEqArray (vec:: AbstractVector{VT} ,ts:: AbstractVector ) where {T, N, VT<: AbstractArray{T, N} } = DiffEqArray {T, N+1, typeof(vec), typeof(ts), Nothing, Nothing, Nothing, Nothing} (vec, ts, nothing , nothing , nothing , nothing )
46+ DiffEqArray (vec:: AbstractVector{VT} ,ts:: AbstractVector , syms:: Vector{Symbol} , indepsym:: Symbol , observed:: Function , p) where {T, N, VT<: AbstractArray{T, N} } = DiffEqArray {T, N+1, typeof(vec), typeof(ts), typeof(syms), typeof(indepsym), typeof(observed), typeof(p)} (vec, ts, syms, indepsym, observed, p)
2747
2848# Interface for the linear indexing. This is just a view of the underlying nested structure
2949@inline Base. firstindex (VA:: AbstractVectorOfArray ) = firstindex (VA. u)
@@ -38,6 +58,52 @@ Base.@propagate_inbounds Base.getindex(VA::AbstractVectorOfArray{T, N}, I::Int)
3858Base. @propagate_inbounds Base. getindex (VA:: AbstractVectorOfArray{T, N} , I:: Colon ) where {T, N} = VA. u[I]
3959Base. @propagate_inbounds Base. getindex (VA:: AbstractVectorOfArray{T, N} , I:: AbstractArray{Int} ) where {T, N} = VectorOfArray (VA. u[I])
4060Base. @propagate_inbounds Base. getindex (VA:: AbstractDiffEqArray{T, N} , I:: AbstractArray{Int} ) where {T, N} = DiffEqArray (VA. u[I],VA. t[I])
61+ Base. @propagate_inbounds function Base. getindex (A:: AbstractDiffEqArray{T, N} ,sym) where {T, N}
62+ if issymbollike (sym) && A. syms != = nothing
63+ i = findfirst (isequal (Symbol (sym)),A. syms)
64+ else
65+ i = sym
66+ end
67+
68+ if i === nothing
69+ if issymbollike (i) && A. indepsym != = nothing && Symbol (i) == A. indepsym
70+ A. t
71+ else
72+ observed (A,sym,:)
73+ end
74+ else
75+ Base. getindex .(A. u, i)
76+ end
77+ end
78+ Base. @propagate_inbounds function Base. getindex (A:: AbstractDiffEqArray{T, N} ,sym,args... ) where {T, N}
79+ if issymbollike (sym) && A. syms != = nothing
80+ i = findfirst (isequal (Symbol (sym)),A. syms)
81+ else
82+ i = sym
83+ end
84+
85+ if i === nothing
86+ if issymbollike (i) && A. indepsym != = nothing && Symbol (i) == A. indepsym
87+ A. t[args... ]
88+ else
89+ observed (A,sym,args... )
90+ end
91+ else
92+ Base. getindex .(A. u, args... )
93+ end
94+ end
95+ Base. @propagate_inbounds Base. getindex (A:: AbstractDiffEqArray{T, N} , I:: Int... ) where {T, N} = A. u[I[end ]][Base. front (I)... ]
96+ Base. @propagate_inbounds Base. getindex (A:: AbstractDiffEqArray{T, N} , i:: Int ) where {T, N} = A. u[i]
97+ function observed (A:: AbstractDiffEqArray{T, N} ,sym,i:: Int ) where {T, N}
98+ A. observed (sym,A. u[i],A. p,A. t[i])
99+ end
100+ function observed (A:: AbstractDiffEqArray{T, N} ,sym,i:: AbstractArray{Int} ) where {T, N}
101+ A. observed .((sym,),A. u[i],(A. p,),A. t[i])
102+ end
103+ function observed (A:: AbstractDiffEqArray{T, N} ,sym,:: Colon ) where {T, N}
104+ A. observed .((sym,),A. u,(A. p,),A. t)
105+ end
106+
41107Base. @propagate_inbounds Base. getindex (VA:: AbstractVectorOfArray{T, N} , i:: Int ,:: Colon ) where {T, N} = [VA. u[j][i] for j in 1 : length (VA)]
42108Base. @propagate_inbounds function Base. getindex (VA:: AbstractVectorOfArray{T,N} , ii:: CartesianIndex ) where {T, N}
43109 ti = Tuple (ii)
@@ -145,6 +211,8 @@ Base.show(io::IO, m::MIME"text/plain", x::AbstractDiffEqArray) = (print(io,"t: "
145211 convert (Array,VA)
146212end
147213@recipe function f (VA:: AbstractDiffEqArray )
214+ xguide --> ((VA. indepsym != = nothing ) ? string (VA. indepsym) : " " )
215+ label --> ((VA. syms != = nothing ) ? reshape (string .(VA. syms), 1 , :) : " " )
148216 VA. t,VA'
149217end
150218@recipe function f (VA:: DiffEqArray{T,1} ) where {T}
0 commit comments