forked from SciML/RecursiveArrayTools.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_of_array.jl
More file actions
1653 lines (1511 loc) · 53.2 KB
/
vector_of_array.jl
File metadata and controls
1653 lines (1511 loc) · 53.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Based on code from M. Bauman Stackexchange answer + Gitter discussion
"""
```julia
VectorOfArray(u::AbstractVector)
```
A `VectorOfArray` is an array which has the underlying data structure `Vector{AbstractArray{T}}`
(but, hopefully, concretely typed!). This wrapper over such data structures allows one to lazily
act like it's a higher-dimensional vector, and easily convert it to different forms. The indexing
structure is:
```julia
A.u[i] # Returns the ith array in the vector of arrays
A[j, i] # Returns the jth component in the ith array
A[j1, ..., jN, i] # Returns the (j1,...,jN) component of the ith array
```
which presents itself as a column-major matrix with the columns being the arrays from the vector.
The `AbstractArray` interface is implemented, giving access to `copy`, `push`, `append!`, etc. functions,
which act appropriately. Points to note are:
- The length is the number of vectors, or `length(A.u)` where `u` is the vector of arrays.
- Iteration follows the linear index and goes over the vectors
Additionally, the `convert(Array,VA::AbstractVectorOfArray)` function is provided, which transforms
the `VectorOfArray` into a matrix/tensor. Also, `vecarr_to_vectors(VA::AbstractVectorOfArray)`
returns a vector of the series for each component, that is, `A[i,:]` for each `i`.
A plot recipe is provided, which plots the `A[i,:]` series.
There is also support for `VectorOfArray` constructed from multi-dimensional arrays
```julia
VectorOfArray(u::AbstractArray{AT}) where {T, N, AT <: AbstractArray{T, N}}
```
where `IndexStyle(typeof(u)) isa IndexLinear`.
"""
mutable struct VectorOfArray{T, N, A} <: AbstractVectorOfArray{T, N, A}
u::A # A <: AbstractArray{<: AbstractArray{T, N - 1}}
end
# VectorOfArray with an added series for time
"""
```julia
DiffEqArray(u::AbstractVector, t::AbstractVector)
```
This is a `VectorOfArray`, which stores `A.t` that matches `A.u`. This will plot
`(A.t[i],A[i,:])`. The function `tuples(diffeq_arr)` returns tuples of `(t,u)`.
To construct a DiffEqArray
```julia
t = 0.0:0.1:10.0
f(t) = t - 1
f2(t) = t^2
vals = [[f(tval) f2(tval)] for tval in t]
A = DiffEqArray(vals, t)
A[1, :] # all time periods for f(t)
A.t
```
"""
mutable struct DiffEqArray{
T, N, A, B, F, S, D <: Union{Nothing, ParameterTimeseriesCollection},
} <:
AbstractDiffEqArray{T, N, A}
u::A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
t::B
p::F
sys::S
discretes::D
end
### Abstract Interface
struct AllObserved
end
function Base.Array(
VA::AbstractVectorOfArray{
T,
N,
A,
}
) where {
T, N,
A <: AbstractVector{
<:AbstractVector,
},
}
return reduce(hcat, VA.u)
end
function Base.Array(
VA::AbstractVectorOfArray{
T,
N,
A,
}
) where {
T, N,
A <:
AbstractVector{<:Number},
}
return VA.u
end
function Base.Matrix(
VA::AbstractVectorOfArray{
T,
N,
A,
}
) where {
T, N,
A <: AbstractVector{
<:AbstractVector,
},
}
return reduce(hcat, VA.u)
end
function Base.Matrix(
VA::AbstractVectorOfArray{
T,
N,
A,
}
) where {
T, N,
A <:
AbstractVector{<:Number},
}
return Matrix(VA.u)
end
function Base.Vector(
VA::AbstractVectorOfArray{
T,
N,
A,
}
) where {
T, N,
A <: AbstractVector{
<:AbstractVector,
},
}
return vec(reduce(hcat, VA.u))
end
function Base.Vector(
VA::AbstractVectorOfArray{
T,
N,
A,
}
) where {
T, N,
A <:
AbstractVector{<:Number},
}
return VA.u
end
function Base.Array(VA::AbstractVectorOfArray)
vecs = vec.(VA.u)
return Array(reshape(reduce(hcat, vecs), size(VA.u[1])..., length(VA.u)))
end
function Base.Array{U}(VA::AbstractVectorOfArray) where {U}
vecs = vec.(VA.u)
return Array(reshape(reduce(hcat, vecs), size(VA.u[1])..., length(VA.u)))
end
Base.convert(::Type{AbstractArray}, VA::AbstractVectorOfArray) = stack(VA.u)
function Adapt.adapt_structure(to, VA::AbstractVectorOfArray)
return VectorOfArray(Adapt.adapt.((to,), VA.u))
end
function Adapt.adapt_structure(to, VA::AbstractDiffEqArray)
return DiffEqArray(Adapt.adapt.((to,), VA.u), Adapt.adapt(to, VA.t))
end
function VectorOfArray(vec::AbstractVector{T}, ::NTuple{N}) where {T, N}
return VectorOfArray{eltype(T), N, typeof(vec)}(vec)
end
# Assume that the first element is representative of all other elements
function VectorOfArray(vec::AbstractVector)
T = eltype(vec[1])
N = ndims(vec[1])
if all(x isa Union{<:AbstractArray, <:AbstractVectorOfArray} for x in vec)
A = Vector{Union{typeof.(vec)...}}
else
A = typeof(vec)
end
return VectorOfArray{T, N + 1, A}(vec)
end
function VectorOfArray(vec::AbstractVector{VT}) where {T, N, VT <: AbstractArray{T, N}}
return VectorOfArray{T, N + 1, typeof(vec)}(vec)
end
# allow multi-dimensional arrays as long as they're linearly indexed.
# currently restricted to arrays whose elements are all the same type
function VectorOfArray(array::AbstractArray{AT}) where {T, N, AT <: AbstractArray{T, N}}
@assert IndexStyle(typeof(array)) isa IndexLinear
return VectorOfArray{T, N + 1, typeof(array)}(array)
end
Base.parent(vec::VectorOfArray) = vec.u
#### 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,
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
#### 3-argument
# NTuple, T from type
function DiffEqArray(
vec::AbstractVector{T}, ts::AbstractVector,
::NTuple{N, Int}; discretes = nothing
) where {T, N}
return DiffEqArray{
eltype(T), N, typeof(vec), typeof(ts), Nothing, Nothing, typeof(discretes),
}(
vec,
ts,
nothing,
nothing,
discretes
)
end
# NTuple parameter
function DiffEqArray(
vec::AbstractVector{VT}, ts::AbstractVector, p::NTuple{N2, Int};
discretes = nothing
) where {T, N, VT <: AbstractArray{T, N}, N2}
return DiffEqArray{
eltype(T), N + 1, typeof(vec), typeof(ts), typeof(p), Nothing, typeof(discretes),
}(
vec,
ts,
p,
nothing,
discretes
)
end
# 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, [])
)
_size = size(vec[1])
T = eltype(vec[1])
return DiffEqArray{
T,
length(_size) + 1,
typeof(vec),
typeof(ts),
typeof(p),
typeof(sys),
typeof(discretes),
}(
vec,
ts,
p,
sys,
discretes
)
end
# 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, [])
)
return 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}
return 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}
return 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)
_size = size(vec[1])
T = eltype(vec[1])
return DiffEqArray{
T,
length(_size) + 1,
typeof(vec),
typeof(ts),
typeof(p),
typeof(sys),
typeof(discretes),
}(
vec,
ts,
p,
sys,
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}}
return 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}
return 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)
SymbolicIndexingInterface.is_timeseries(::Type{<:AbstractVectorOfArray}) = Timeseries()
function SymbolicIndexingInterface.is_parameter_timeseries(
::Type{
DiffEqArray{
T, N, A, B,
F, S, D,
},
}
) where {T, N, A, B, F, S, D <: ParameterIndexingProxy}
return Timeseries()
end
SymbolicIndexingInterface.state_values(A::AbstractDiffEqArray) = A.u
SymbolicIndexingInterface.current_time(A::AbstractDiffEqArray) = A.t
SymbolicIndexingInterface.parameter_values(A::AbstractDiffEqArray) = A.p
SymbolicIndexingInterface.symbolic_container(A::AbstractDiffEqArray) = A.sys
function SymbolicIndexingInterface.get_parameter_timeseries_collection(A::AbstractDiffEqArray)
return get_discretes(A)
end
Base.IndexStyle(A::AbstractVectorOfArray) = Base.IndexStyle(typeof(A))
Base.IndexStyle(::Type{<:AbstractVectorOfArray}) = IndexCartesian()
@inline Base.length(VA::AbstractVectorOfArray) = length(VA.u)
@inline function Base.eachindex(VA::AbstractVectorOfArray)
return eachindex(VA.u)
end
@inline function Base.eachindex(
::IndexLinear, VA::AbstractVectorOfArray{T, N, <:AbstractVector{T}}
) where {T, N}
return eachindex(IndexLinear(), VA.u)
end
@inline Base.IteratorSize(::Type{<:AbstractVectorOfArray}) = Base.HasLength()
@inline Base.first(VA::AbstractVectorOfArray) = first(VA.u)
@inline Base.last(VA::AbstractVectorOfArray) = last(VA.u)
function Base.firstindex(VA::AbstractVectorOfArray{T, N, A}) where {T, N, A}
N > 1 && Base.depwarn(
"Linear indexing of `AbstractVectorOfArray` is deprecated. Change `A[i]` to `A.u[i]` ",
:firstindex
)
return firstindex(VA.u)
end
function Base.lastindex(VA::AbstractVectorOfArray{T, N, A}) where {T, N, A}
N > 1 && Base.depwarn(
"Linear indexing of `AbstractVectorOfArray` is deprecated. Change `A[i]` to `A.u[i]` ",
:lastindex
)
return lastindex(VA.u)
end
# Always return RaggedEnd for type stability. Use dim=0 to indicate a plain index stored in offset.
# _resolve_ragged_index and _column_indices handle the dim=0 case to extract the actual index value.
@inline function Base.lastindex(VA::AbstractVectorOfArray, d::Integer)
if d == ndims(VA)
return RaggedEnd(0, Int(lastindex(VA.u)))
elseif d < ndims(VA)
isempty(VA.u) && return RaggedEnd(0, 0)
return RaggedEnd(Int(d), 0)
else
return RaggedEnd(0, 1)
end
end
Base.getindex(A::AbstractVectorOfArray, I::Int) = A.u[I]
Base.getindex(A::AbstractVectorOfArray, I::AbstractArray{Int}) = A.u[I]
Base.getindex(A::AbstractDiffEqArray, I::Int) = A.u[I]
Base.getindex(A::AbstractDiffEqArray, I::AbstractArray{Int}) = A.u[I]
@deprecate Base.getindex(
VA::AbstractVectorOfArray{T, N, A},
I::Int
) where {T, N, A <: Union{AbstractArray, AbstractVectorOfArray}} VA.u[I] false
@deprecate Base.getindex(
VA::AbstractVectorOfArray{T, N, A},
I::AbstractArray{Int}
) where {T, N, A <: Union{AbstractArray, AbstractVectorOfArray}} VA.u[I] false
@deprecate Base.getindex(
VA::AbstractDiffEqArray{T, N, A},
I::AbstractArray{Int}
) where {T, N, A <: Union{AbstractArray, AbstractVectorOfArray}} VA.u[I] false
@deprecate Base.getindex(
VA::AbstractDiffEqArray{T, N, A},
i::Int
) where {T, N, A <: Union{AbstractArray, AbstractVectorOfArray}} VA.u[i] false
__parameterless_type(T) = Base.typename(T).wrapper
# `end` support for ragged inner arrays
# Use runtime fields instead of type parameters for type stability
struct RaggedEnd
dim::Int
offset::Int
end
RaggedEnd(dim::Int) = RaggedEnd(dim, 0)
Base.:+(re::RaggedEnd, n::Integer) = RaggedEnd(re.dim, re.offset + Int(n))
Base.:-(re::RaggedEnd, n::Integer) = RaggedEnd(re.dim, re.offset - Int(n))
Base.:+(n::Integer, re::RaggedEnd) = re + n
# Make RaggedEnd and RaggedRange broadcast as scalars to avoid
# issues with collect/length in broadcasting contexts (e.g., SymbolicIndexingInterface)
Base.broadcastable(x::RaggedEnd) = Ref(x)
struct RaggedRange
dim::Int
start::Int
step::Int
offset::Int
end
Base.:(:)(stop::RaggedEnd) = RaggedRange(stop.dim, 1, 1, stop.offset)
function Base.:(:)(start::Integer, stop::RaggedEnd)
return RaggedRange(stop.dim, Int(start), 1, stop.offset)
end
function Base.:(:)(start::Integer, step::Integer, stop::RaggedEnd)
return RaggedRange(stop.dim, Int(start), Int(step), stop.offset)
end
function Base.:(:)(start::RaggedEnd, stop::RaggedEnd)
return RaggedRange(stop.dim, start.offset, 1, stop.offset)
end
function Base.:(:)(start::RaggedEnd, step::Integer, stop::RaggedEnd)
return RaggedRange(stop.dim, start.offset, Int(step), stop.offset)
end
function Base.:(:)(start::RaggedEnd, stop::Integer)
return RaggedRange(start.dim, start.offset, 1, Int(stop))
end
function Base.:(:)(start::RaggedEnd, step::Integer, stop::Integer)
return RaggedRange(start.dim, start.offset, Int(step), Int(stop))
end
Base.broadcastable(x::RaggedRange) = Ref(x)
@inline function _is_ragged_dim(VA::AbstractVectorOfArray, d::Integer)
length(VA.u) <= 1 && return false
first_size = size(VA.u[1], d)
@inbounds for idx in 2:length(VA.u)
size(VA.u[idx], d) == first_size || return true
end
return false
end
Base.@propagate_inbounds function _getindex(
A::AbstractVectorOfArray, ::NotSymbolic, ::Colon, I::Int
)
return A.u[I]
end
Base.@propagate_inbounds function _getindex(
A::AbstractDiffEqArray, ::NotSymbolic, ::Colon, I::Int
)
return A.u[I]
end
Base.@propagate_inbounds function _getindex(
A::AbstractVectorOfArray, ::NotSymbolic,
I::Union{Int, AbstractArray{Int}, AbstractArray{Bool}, Colon}...
)
return if last(I) isa Int
A.u[last(I)][Base.front(I)...]
else
stack(getindex.(A.u[last(I)], tuple.(Base.front(I))...))
end
end
Base.@propagate_inbounds function _getindex(
A::AbstractDiffEqArray, ::NotSymbolic,
I::Union{Int, AbstractArray{Int}, AbstractArray{Bool}, Colon}...
)
return if last(I) isa Int
A.u[last(I)][Base.front(I)...]
else
col_idxs = last(I)
# Only preserve DiffEqArray type if all prefix indices are Colons (selecting whole inner arrays)
if all(idx -> idx isa Colon, Base.front(I))
# For Colon, select all columns
if col_idxs isa Colon
col_idxs = eachindex(A.u)
end
# For DiffEqArray, we need to preserve the time values and type
# Create a vector of sliced arrays instead of stacking into higher-dim array
u_slice = [A.u[col][Base.front(I)...] for col in col_idxs]
# Return as DiffEqArray with sliced time values
return DiffEqArray(u_slice, A.t[col_idxs], parameter_values(A), symbolic_container(A))
else
# Prefix indices are not all Colons - do the same as VectorOfArray
# (stack the results into a higher-dimensional array)
return stack(getindex.(A.u[col_idxs], tuple.(Base.front(I))...))
end
end
end
Base.@propagate_inbounds function _getindex(
VA::AbstractVectorOfArray, ::NotSymbolic, ii::CartesianIndex
)
ti = Tuple(ii)
i = last(ti)
jj = CartesianIndex(Base.front(ti))
return VA.u[i][jj]
end
Base.@propagate_inbounds function _getindex(
A::AbstractVectorOfArray, ::NotSymbolic, ::Colon,
I::Union{AbstractArray{Int}, AbstractArray{Bool}}
)
return VectorOfArray(A.u[I])
end
Base.@propagate_inbounds function _getindex(
A::AbstractDiffEqArray, ::NotSymbolic, ::Colon,
I::Union{AbstractArray{Int}, AbstractArray{Bool}}
)
return DiffEqArray(A.u[I], A.t[I], parameter_values(A), symbolic_container(A))
end
struct ParameterIndexingError <: Exception
sym::Any
end
function Base.showerror(io::IO, pie::ParameterIndexingError)
return print(
io,
"Indexing with parameters is deprecated. Use `getp(A, $(pie.sym))` for parameter indexing."
)
end
# Symbolic Indexing Methods
for (symtype, elsymtype, valtype, errcheck) in [
(
ScalarSymbolic, SymbolicIndexingInterface.SymbolicTypeTrait, Any,
:(is_parameter(A, sym) && !is_timeseries_parameter(A, sym)),
),
(
ArraySymbolic, SymbolicIndexingInterface.SymbolicTypeTrait, Any,
:(is_parameter(A, sym) && !is_timeseries_parameter(A, sym)),
),
(
NotSymbolic, SymbolicIndexingInterface.SymbolicTypeTrait,
Union{<:Tuple, <:AbstractArray},
:(all(x -> is_parameter(A, x) && !is_timeseries_parameter(A, x), sym)),
),
]
@eval Base.@propagate_inbounds function _getindex(
A::AbstractDiffEqArray, ::$symtype,
::$elsymtype, sym::$valtype, arg...
)
if $errcheck
throw(ParameterIndexingError(sym))
end
return getu(A, sym)(A, arg...)
end
end
Base.@propagate_inbounds function _getindex(
A::AbstractDiffEqArray, ::ScalarSymbolic,
::NotSymbolic, ::SymbolicIndexingInterface.SolvedVariables, args...
)
return getindex(A, variable_symbols(A), args...)
end
Base.@propagate_inbounds function _getindex(
A::AbstractDiffEqArray, ::ScalarSymbolic,
::NotSymbolic, ::SymbolicIndexingInterface.AllVariables, args...
)
return getindex(A, all_variable_symbols(A), args...)
end
@inline _column_indices(VA::AbstractVectorOfArray, idx) = idx === Colon() ?
eachindex(VA.u) : idx
@inline function _column_indices(VA::AbstractVectorOfArray, idx::AbstractArray{Bool})
return findall(idx)
end
@inline function _column_indices(VA::AbstractVectorOfArray, idx::RaggedEnd)
# RaggedEnd with dim=0 means it's just a plain index stored in offset
return idx.dim == 0 ? idx.offset : idx
end
@inline function _column_indices(VA::AbstractVectorOfArray, idx::RaggedRange)
# RaggedRange with dim=0 means it's a column range with pre-resolved indices
if idx.dim == 0
# Create a range with the offset as the stop value
return Base.range(idx.start; step = idx.step, stop = idx.offset)
else
# dim != 0 means it's an inner-dimension range that needs column expansion
return idx
end
end
@inline _resolve_ragged_index(idx, ::AbstractVectorOfArray, ::Any) = idx
@inline function _resolve_ragged_index(idx::RaggedEnd, VA::AbstractVectorOfArray, col)
if idx.dim == 0
# Special case: dim=0 means the offset contains the actual index value
return idx.offset
else
return lastindex(VA.u[col], idx.dim) + idx.offset
end
end
@inline function _resolve_ragged_index(idx::RaggedRange, VA::AbstractVectorOfArray, col)
stop_val = if idx.dim == 0
# dim == 0 is the sentinel for an already-resolved plain index stored in offset
idx.offset
else
lastindex(VA.u[col], idx.dim) + idx.offset
end
return Base.range(idx.start; step = idx.step, stop = stop_val)
end
@inline function _resolve_ragged_index(
idx::AbstractRange{<:RaggedEnd}, VA::AbstractVectorOfArray, col
)
return Base.range(
_resolve_ragged_index(first(idx), VA, col); step = step(idx),
stop = _resolve_ragged_index(last(idx), VA, col)
)
end
@inline function _resolve_ragged_index(idx::Base.Slice, VA::AbstractVectorOfArray, col)
return Base.Slice(_resolve_ragged_index(idx.indices, VA, col))
end
@inline function _resolve_ragged_index(idx::CartesianIndex, VA::AbstractVectorOfArray, col)
return CartesianIndex(_resolve_ragged_indices(Tuple(idx), VA, col)...)
end
@inline function _resolve_ragged_index(
idx::AbstractArray{<:RaggedEnd}, VA::AbstractVectorOfArray, col
)
return map(i -> _resolve_ragged_index(i, VA, col), idx)
end
@inline function _resolve_ragged_index(
idx::AbstractArray{<:RaggedRange}, VA::AbstractVectorOfArray, col
)
return map(i -> _resolve_ragged_index(i, VA, col), idx)
end
@inline function _resolve_ragged_index(idx::AbstractArray, VA::AbstractVectorOfArray, col)
return _has_ragged_end(idx) ? map(i -> _resolve_ragged_index(i, VA, col), idx) : idx
end
@inline function _resolve_ragged_indices(idxs::Tuple, VA::AbstractVectorOfArray, col)
return map(i -> _resolve_ragged_index(i, VA, col), idxs)
end
@inline function _has_ragged_end(x)
x isa RaggedEnd && return true
x isa RaggedRange && return true
x isa Base.Slice && return _has_ragged_end(x.indices)
x isa CartesianIndex && return _has_ragged_end(Tuple(x))
x isa AbstractRange && return eltype(x) <: Union{RaggedEnd, RaggedRange}
if x isa AbstractArray
el = eltype(x)
return el <: Union{RaggedEnd, RaggedRange} ||
(el === Any && any(_has_ragged_end, x))
end
x isa Tuple && return any(_has_ragged_end, x)
return false
end
@inline _has_ragged_end(x, xs...) = _has_ragged_end(x) || _has_ragged_end(xs)
# Helper function to resolve RaggedEnd objects in a tuple of arguments
@inline function _resolve_ragged_end_args(A::AbstractVectorOfArray, args::Tuple)
# Handle empty tuple case
length(args) == 0 && return args
if !_has_ragged_end(args...)
return args
end
# For now, we need to resolve only the last argument if it's RaggedEnd (column selector)
# This handles the common case sol[:x, end] where end gets converted to RaggedEnd(0, lastindex)
if args[end] isa RaggedEnd
resolved_last = _column_indices(A, args[end])
if length(args) == 1
return (resolved_last,)
else
return (Base.front(args)..., resolved_last)
end
elseif args[end] isa RaggedRange
# Only pre-resolve if it's an inner-dimension range (dim != 0)
# Column ranges (dim == 0) are handled later by _column_indices
if args[end].dim == 0
# Column range - let _column_indices handle it
return args
else
resolved_last = _resolve_ragged_index(args[end], A, 1)
if length(args) == 1
return (resolved_last,)
else
return (Base.front(args)..., resolved_last)
end
end
end
return args
end
# Helper function to preserve DiffEqArray type when slicing
@inline function _preserve_array_type(A::AbstractVectorOfArray, u_slice, col_idxs)
return VectorOfArray(u_slice)
end
@inline function _preserve_array_type(A::AbstractDiffEqArray, u_slice, col_idxs)
return DiffEqArray(u_slice, A.t[col_idxs], parameter_values(A), symbolic_container(A))
end
@inline function _ragged_getindex(A::AbstractVectorOfArray, I...)
n = ndims(A)
# Special-case when user provided one fewer index than ndims(A): last index is column selector.
if length(I) == n - 1
raw_cols = last(I)
# Determine if we're doing column selection (preserve type) or inner-dimension selection (don't preserve)
is_column_selection = if raw_cols isa RaggedEnd && raw_cols.dim != 0
false # Inner dimension - don't preserve type
elseif raw_cols isa RaggedRange && raw_cols.dim != 0
true # Inner dimension range converted to column range - DO preserve type
else
true # Column selection (dim == 0 or not ragged)
end
# If the raw selector is a RaggedEnd/RaggedRange referring to inner dims, reinterpret as column selector.
cols = if raw_cols isa RaggedEnd && raw_cols.dim != 0
lastindex(A.u) + raw_cols.offset
elseif raw_cols isa RaggedRange && raw_cols.dim != 0
# Convert inner-dimension range to column range by resolving bounds
start_val = raw_cols.start < 0 ? lastindex(A.u) + raw_cols.start : raw_cols.start
stop_val = lastindex(A.u) + raw_cols.offset
Base.range(start_val; step = raw_cols.step, stop = stop_val)
else
_column_indices(A, raw_cols)
end
prefix = Base.front(I)
if cols isa Int
resolved_prefix = _resolve_ragged_indices(prefix, A, cols)
inner_nd = ndims(A.u[cols])
n_missing = inner_nd - length(resolved_prefix)
padded = if n_missing > 0
if all(idx -> idx === Colon(), resolved_prefix)
(resolved_prefix..., ntuple(_ -> Colon(), n_missing)...)
else
(
resolved_prefix...,
(lastindex(A.u[cols], length(resolved_prefix) + i) for i in 1:n_missing)...,
)
end
else
resolved_prefix
end
return A.u[cols][padded...]
else
u_slice = [
begin
resolved_prefix = _resolve_ragged_indices(prefix, A, col)
inner_nd = ndims(A.u[col])
n_missing = inner_nd - length(resolved_prefix)
padded = if n_missing > 0
if all(idx -> idx === Colon(), resolved_prefix)
(
resolved_prefix...,
ntuple(_ -> Colon(), n_missing)...,
)
else
(
resolved_prefix...,
(
lastindex(
A.u[col],
length(resolved_prefix) + i
) for i in 1:n_missing
)...,
)
end
else
resolved_prefix
end
A.u[col][padded...]
end
for col in cols
]
# Only preserve DiffEqArray type if we're selecting actual columns, not inner dimensions
if is_column_selection
return _preserve_array_type(A, u_slice, cols)
else
return VectorOfArray(u_slice)
end
end
end
# Otherwise, use the full-length interpretation (last index is column selector; missing columns default to Colon()).
if length(I) == n
cols = last(I)
prefix = Base.front(I)
else
cols = Colon()
prefix = I
end
if cols isa Int
if all(idx -> idx === Colon(), prefix)
return A.u[cols]
end
resolved = _resolve_ragged_indices(prefix, A, cols)
inner_nd = ndims(A.u[cols])
padded = (resolved..., ntuple(_ -> Colon(), max(inner_nd - length(resolved), 0))...)
return A.u[cols][padded...]
else
col_idxs = _column_indices(A, cols)
# Resolve sentinel RaggedEnd/RaggedRange (dim==0) for column selection
if col_idxs isa RaggedEnd
col_idxs = _resolve_ragged_index(col_idxs, A, 1)
elseif col_idxs isa RaggedRange
col_idxs = _resolve_ragged_index(col_idxs, A, 1)
end
# If we're selecting whole inner arrays (all leading indices are Colons),
# keep the result as a VectorOfArray to match non-ragged behavior.
if all(idx -> idx === Colon(), prefix)
if col_idxs isa Int
return A.u[col_idxs]
else
return _preserve_array_type(A, A.u[col_idxs], col_idxs)
end
end
# If col_idxs resolved to a single Int, handle it directly
if col_idxs isa Int
resolved = _resolve_ragged_indices(prefix, A, col_idxs)
inner_nd = ndims(A.u[col_idxs])
padded = (
resolved..., ntuple(_ -> Colon(), max(inner_nd - length(resolved), 0))...,
)
return A.u[col_idxs][padded...]
end
vals = map(col_idxs) do col
resolved = _resolve_ragged_indices(prefix, A, col)
inner_nd = ndims(A.u[col])
padded = (
resolved..., ntuple(_ -> Colon(), max(inner_nd - length(resolved), 0))...,
)
A.u[col][padded...]
end
return stack(vals)
end
end
@inline function _checkbounds_ragged(::Type{Bool}, VA::AbstractVectorOfArray, idxs...)
cols = _column_indices(VA, last(idxs))
prefix = Base.front(idxs)
if cols isa Int
resolved = _resolve_ragged_indices(prefix, VA, cols)
return checkbounds(Bool, VA.u, cols) && checkbounds(Bool, VA.u[cols], resolved...)
else
for col in cols
resolved = _resolve_ragged_indices(prefix, VA, col)
checkbounds(Bool, VA.u, col) || return false
checkbounds(Bool, VA.u[col], resolved...) || return false
end
return true
end
end
Base.@propagate_inbounds function Base.getindex(A::AbstractVectorOfArray, _arg, args...)
symtype = symbolic_type(_arg)
elsymtype = symbolic_type(eltype(_arg))
return if symtype == NotSymbolic() && elsymtype == NotSymbolic()
if _has_ragged_end(_arg, args...)
return _ragged_getindex(A, _arg, args...)