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
1611 lines (1463 loc) · 46.8 KB
/
vector_of_array.jl
File metadata and controls
1611 lines (1463 loc) · 46.8 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}, I,
} <:
AbstractDiffEqArray{T, N, A}
u::A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
t::B
p::F
sys::S
discretes::D
interp::I
dense::Bool
end
### Abstract Interface
struct AllObserved
end
function Base.Array(
VA::AbstractVectorOfArray{
T,
N,
A,
}
) where {
T, N,
A <: AbstractVector{
<:AbstractVector,
},
}
if allequal(length.(VA.u))
return reduce(hcat, VA.u)
else
# Ragged: zero-padded
s = size(VA)
result = zeros(T, s)
for j in 1:length(VA.u)
u_j = VA.u[j]
for i in 1:length(u_j)
result[i, j] = u_j[i]
end
end
return result
end
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{T, N}) where {T, N}
if allequal(size.(VA.u))
vecs = vec.(VA.u)
return Array(reshape(reduce(hcat, vecs), size(VA.u[1])..., length(VA.u)))
else
# Ragged: create zero-padded dense array
s = size(VA)
result = zeros(T, s)
for j in 1:length(VA.u)
u_j = VA.u[j]
for ci in CartesianIndices(size(u_j))
result[ci, j] = u_j[ci]
end
end
return result
end
end
function Base.Array{U}(VA::AbstractVectorOfArray{T, N}) where {U, T, N}
if allequal(size.(VA.u))
vecs = vec.(VA.u)
return Array{U}(reshape(reduce(hcat, vecs), size(VA.u[1])..., length(VA.u)))
else
s = size(VA)
result = zeros(U, s)
for j in 1:length(VA.u)
u_j = VA.u[j]
for ci in CartesianIndices(size(u_j))
result[ci, j] = U(u_j[ci])
end
end
return result
end
end
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,
interp = nothing, dense = false
)
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),
typeof(interp),
}(
vec,
ts,
nothing,
sys,
discretes,
interp,
dense
)
end
# T and N from type
function DiffEqArray(
vec::AbstractVector{VT}, ts::AbstractVector;
discretes = nothing, variables = nothing, parameters = nothing,
independent_variables = nothing, interp = nothing, dense = false
) 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),
typeof(interp),
}(
vec,
ts,
nothing,
sys,
discretes,
interp,
dense
)
end
#### 3-argument
# NTuple, T from type
function DiffEqArray(
vec::AbstractVector{T}, ts::AbstractVector,
::NTuple{N, Int}; discretes = nothing, interp = nothing, dense = false
) where {T, N}
return DiffEqArray{
eltype(T), N, typeof(vec), typeof(ts), Nothing, Nothing, typeof(discretes),
typeof(interp),
}(
vec,
ts,
nothing,
nothing,
discretes,
interp,
dense
)
end
# NTuple parameter
function DiffEqArray(
vec::AbstractVector{VT}, ts::AbstractVector, p::NTuple{N2, Int};
discretes = nothing, interp = nothing, dense = false
) where {T, N, VT <: AbstractArray{T, N}, N2}
return DiffEqArray{
eltype(T), N + 1, typeof(vec), typeof(ts), typeof(p), Nothing, typeof(discretes),
typeof(interp),
}(
vec,
ts,
p,
nothing,
discretes,
interp,
dense
)
end
# first element representative
function DiffEqArray(
vec::AbstractVector, ts::AbstractVector, p; discretes = nothing,
variables = nothing, parameters = nothing, independent_variables = nothing,
interp = nothing, dense = false
)
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),
typeof(interp),
}(
vec,
ts,
p,
sys,
discretes,
interp,
dense
)
end
# T and N from type
function DiffEqArray(
vec::AbstractVector{VT}, ts::AbstractVector, p;
discretes = nothing, variables = nothing, parameters = nothing,
independent_variables = nothing, interp = nothing, dense = false
) 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),
typeof(interp),
}(
vec,
ts,
p,
sys,
discretes,
interp,
dense
)
end
#### 4-argument
# NTuple, T from type
function DiffEqArray(
vec::AbstractVector{T}, ts::AbstractVector,
::NTuple{N, Int}, p; discretes = nothing, interp = nothing, dense = false
) where {T, N}
return DiffEqArray{
eltype(T), N, typeof(vec), typeof(ts), typeof(p), Nothing, typeof(discretes),
typeof(interp),
}(
vec,
ts,
p,
nothing,
discretes,
interp,
dense
)
end
# NTuple parameter
function DiffEqArray(
vec::AbstractVector{VT}, ts::AbstractVector, p::NTuple{N2, Int}, sys;
discretes = nothing, interp = nothing, dense = false
) where {T, N, VT <: AbstractArray{T, N}, N2}
return DiffEqArray{
eltype(T), N + 1, typeof(vec), typeof(ts),
typeof(p), typeof(sys), typeof(discretes),
typeof(interp),
}(
vec,
ts,
p,
sys,
discretes,
interp,
dense
)
end
# first element representative
function DiffEqArray(
vec::AbstractVector, ts::AbstractVector, p, sys;
discretes = nothing, interp = nothing, dense = false
)
_size = size(vec[1])
T = eltype(vec[1])
return DiffEqArray{
T,
length(_size) + 1,
typeof(vec),
typeof(ts),
typeof(p),
typeof(sys),
typeof(discretes),
typeof(interp),
}(
vec,
ts,
p,
sys,
discretes,
interp,
dense
)
end
# T and N from type
function DiffEqArray(
vec::AbstractVector{VT}, ts::AbstractVector, p, sys;
discretes = nothing, interp = nothing, dense = false
) where {T, N, VT <: AbstractArray{T, N}}
return DiffEqArray{
eltype(T), N + 1, typeof(vec), typeof(ts),
typeof(p), typeof(sys), typeof(discretes),
typeof(interp),
}(
vec,
ts,
p,
sys,
discretes,
interp,
dense
)
end
#### 5-argument
# NTuple, T from type
function DiffEqArray(
vec::AbstractVector{T}, ts::AbstractVector,
::NTuple{N, Int}, p, sys; discretes = nothing, interp = nothing, dense = false
) where {T, N}
return DiffEqArray{
eltype(T), N, typeof(vec), typeof(ts), typeof(p), typeof(sys), typeof(discretes),
typeof(interp),
}(
vec,
ts,
p,
sys,
discretes,
interp,
dense
)
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, I,
},
}
) where {T, N, A, B, F, S, D <: ParameterIndexingProxy, I}
return Timeseries()
end
SymbolicIndexingInterface.state_values(A::AbstractDiffEqArray) = A.u
SymbolicIndexingInterface.current_time(A::AbstractDiffEqArray) = A.t
SymbolicIndexingInterface.parameter_values(A::AbstractDiffEqArray) = A.p
# Need explicit 2-arg method since AbstractDiffEqArray <: AbstractArray
# and SymbolicIndexingInterface defines parameter_values(::AbstractArray, i) = arr[i]
function SymbolicIndexingInterface.parameter_values(A::AbstractDiffEqArray, i)
return parameter_values(A.p, i)
end
SymbolicIndexingInterface.symbolic_container(A::AbstractDiffEqArray) = A.sys
function SymbolicIndexingInterface.get_parameter_timeseries_collection(A::AbstractDiffEqArray)
return get_discretes(A)
end
## Callable interface for interpolation
#
# Any AbstractDiffEqArray with a non-nothing `interp` field supports `da(t)`.
# The interpolation object is called as `interp(t, idxs, deriv, p, continuity)`.
# SciMLBase's more-specific `(::AbstractODESolution)(t,...)` methods win dispatch
# for solution objects and handle symbolic idxs, discrete params, etc.
function (da::AbstractDiffEqArray)(
t, ::Type{deriv} = Val{0};
idxs = nothing, continuity = :left
) where {deriv}
da.interp === nothing &&
error("No interpolation data is available. Provide an interpolation object via the `interp` keyword.")
return da.interp(t, idxs, deriv, da.p, continuity)
end
Base.IndexStyle(::Type{<:AbstractVectorOfArray}) = IndexCartesian()
## Linear indexing: convert to Cartesian and dispatch to the N-ary getindex
Base.@propagate_inbounds function Base.getindex(A::AbstractVectorOfArray{T, N}, i::Int) where {T, N}
@boundscheck checkbounds(A, i)
if N == 1
return @inbounds A.u[i]
end
return @inbounds A[CartesianIndices(size(A))[i]]
end
Base.@propagate_inbounds function Base.setindex!(A::AbstractVectorOfArray{T, N}, v, i::Int) where {T, N}
@boundscheck checkbounds(A, i)
if N == 1
A.u[i] = v
return v
end
ci = CartesianIndices(size(A))[i]
return @inbounds A[ci] = v
end
Base.@propagate_inbounds function _getindex(
A::AbstractVectorOfArray{T, N}, ::NotSymbolic, ::Colon, I::Int
) where {T, N}
u_col = A.u[I]
s = size(A)
leading_size = Base.front(s)
# If inner array matches the rectangular size, return directly
if size(u_col) == leading_size
return u_col
end
# If inner array has different ndims, return as-is (can't meaningfully reshape)
if ndims(u_col) != N - 1
return u_col
end
# Zero-padded for ragged arrays with same ndims but different sizes
result = zeros(T, leading_size)
for ci in CartesianIndices(size(u_col))
result[ci] = u_col[ci]
end
return result
end
Base.@propagate_inbounds function _getindex(
A::AbstractDiffEqArray{T, N}, ::NotSymbolic, ::Colon, I::Int
) where {T, N}
u_col = A.u[I]
s = size(A)
leading_size = Base.front(s)
if size(u_col) == leading_size
return u_col
end
if ndims(u_col) != N - 1
return u_col
end
result = zeros(T, leading_size)
for ci in CartesianIndices(size(u_col))
result[ci] = u_col[ci]
end
return result
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{T}, ::NotSymbolic, ii::CartesianIndex
) where {T}
ti = Tuple(ii)
col = last(ti)
inner_I = Base.front(ti)
u_col = VA.u[col]
# Return zero for indices outside ragged storage
for d in 1:length(inner_I)
if inner_I[d] > size(u_col, d)
return zero(T)
end
end
jj = CartesianIndex(inner_I)
return u_col[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
# CartesianIndex with more dimensions than ndims(A) — for heterogeneous inner arrays
# where (inner_indices..., column_index) may have more entries than ndims(A)
Base.@propagate_inbounds function Base.getindex(
A::AbstractVectorOfArray{T, N}, ii::CartesianIndex
) where {T, N}
ti = Tuple(ii)
if length(ti) == N
# Standard case: let AbstractArray handle via the N-ary method
return A[ti...]
end
# Heterogeneous case: last element is column, rest are inner indices
col = last(ti)
inner_I = Base.front(ti)
u_col = A.u[col]
for d in 1:length(inner_I)
if inner_I[d] > size(u_col, d)
return zero(T)
end
end
return u_col[CartesianIndex(inner_I)]
end
Base.@propagate_inbounds function Base.setindex!(
A::AbstractVectorOfArray{T, N}, x, ii::CartesianIndex
) where {T, N}
ti = Tuple(ii)
if length(ti) == N
return A[ti...] = x
end
col = last(ti)
inner_I = Base.front(ti)
u_col = A.u[col]
for d in 1:length(inner_I)
if inner_I[d] > size(u_col, d)
iszero(x) && return x
throw(
ArgumentError(
"Cannot set non-zero value at index $ii: outside ragged storage bounds."
)
)
end
end
return u_col[CartesianIndex(inner_I)] = x
end
Base.@propagate_inbounds function Base.getindex(A::AbstractVectorOfArray, _arg, args...)
# Flatten CartesianIndex arguments (e.g. from sum(A; dims=d)) to plain Ints
# so they hit the N-ary getindex method instead of the symbolic dispatch.
if _arg isa Int && length(args) == 1 && args[1] isa CartesianIndex
return A[_arg, Tuple(args[1])...]
end
symtype = symbolic_type(_arg)
elsymtype = symbolic_type(eltype(_arg))
return if symtype == NotSymbolic() && elsymtype == NotSymbolic()
if _arg isa Union{Tuple, AbstractArray} &&
any(x -> symbolic_type(x) != NotSymbolic(), _arg)
_getindex(A, symtype, elsymtype, _arg, args...)
else
_getindex(A, symtype, _arg, args...)
end
else
_getindex(A, symtype, elsymtype, _arg, args...)
end
end
function _observed(A::AbstractDiffEqArray{T, N}, sym, i::Int) where {T, N}
return observed(A, sym)(A.u[i], A.p, A.t[i])
end
function _observed(A::AbstractDiffEqArray{T, N}, sym, i::AbstractArray{Int}) where {T, N}
return observed(A, sym).(A.u[i], (A.p,), A.t[i])
end
function _observed(A::AbstractDiffEqArray{T, N}, sym, ::Colon) where {T, N}
return observed(A, sym).(A.u, (A.p,), A.t)
end
Base.@propagate_inbounds function Base.setindex!(
VA::AbstractVectorOfArray{T, N}, v,
::Colon, I::Int
) where {T, N}
return VA.u[I] = v
end
Base.@propagate_inbounds function Base.setindex!(
VA::AbstractVectorOfArray{T, N}, v,
::Colon, I::Colon
) where {T, N}
return VA.u[I] = v
end
Base.@propagate_inbounds function Base.setindex!(
VA::AbstractVectorOfArray{T, N}, v,
::Colon, I::AbstractArray{Int}
) where {T, N}
return VA.u[I] = v
end
Base.@propagate_inbounds function Base.setindex!(
VA::AbstractVectorOfArray{T, N}, v, i::Int,
::Colon
) where {T, N}
for j in 1:length(VA.u)
VA.u[j][i] = v[j]
end
return v
end
Base.@propagate_inbounds function Base.setindex!(
VA::AbstractVectorOfArray{T, N},
x,
idxs::Union{Int, Colon, AbstractArray{Int}, AbstractArray{Bool}}...
) where {
T, N,
}
v = view(VA, idxs...)
# error message copied from Base by running `ones(3, 3, 3)[:, 2, :] = 2`
if length(v) != length(x)
throw(ArgumentError("indexed assignment with a single value to possibly many locations is not supported; perhaps use broadcasting `.=` instead?"))
end
for (i, j) in zip(eachindex(v), eachindex(x))
v[i] = x[j]
end
return x
end
# Interface for the AbstractArray interface
@inline function Base.size(VA::AbstractVectorOfArray{T, N}) where {T, N}
isempty(VA.u) && return ntuple(_ -> 0, Val(N))
leading = ntuple(Val(N - 1)) do d
maximum(size(u, d) for u in VA.u)
end
return (leading..., length(VA.u))
end
Base.@propagate_inbounds function Base.setindex!(
VA::AbstractVectorOfArray{T, N}, v,
I::Int...
) where {T, N}
col = I[end]
inner_I = Base.front(I)
u_col = VA.u[col]
# Check if within ragged storage bounds
for d in 1:length(inner_I)
if inner_I[d] > size(u_col, d)
iszero(v) && return v
throw(
ArgumentError(
"Cannot set non-zero value at index $I: outside ragged storage bounds. " *
"Inner array $col has size $(size(u_col)) but index requires $(inner_I)."
)
)
end
end
return u_col[inner_I...] = v
end
# Core N-dimensional getindex for AbstractArray interface
# Handles ragged arrays by returning zero for out-of-bounds inner indices
Base.@propagate_inbounds function Base.getindex(
A::AbstractVectorOfArray{T, N}, I::Vararg{Int, N}
) where {T, N}
@boundscheck checkbounds(A, I...)
col = I[N]
inner_I = Base.front(I)
u_col = A.u[col]
# Return zero for indices outside ragged storage
for d in 1:(N - 1)
if inner_I[d] > size(u_col, d)
return zero(T)
end
end
return @inbounds u_col[inner_I...]
end
function Base.:(==)(A::AbstractVectorOfArray, B::AbstractVectorOfArray)
return A.u == B.u
end
# Comparison with plain arrays uses AbstractArray element-wise comparison via default
tuples(VA::DiffEqArray) = tuple.(VA.t, VA.u)
# Growing the array simply adds to the container vector
function _copyfield(VA, fname)
return if fname == :u
copy(VA.u)
elseif fname == :t
copy(VA.t)
else
getfield(VA, fname)
end
end
function Base.copy(VA::AbstractVectorOfArray)
return typeof(VA)((_copyfield(VA, fname) for fname in fieldnames(typeof(VA)))...)
end
function Base.zero(VA::AbstractVectorOfArray)
T = typeof(VA)
u_zero = rewrap(VA.u, [zero(u) for u in VA.u])
fields = [fname == :u ? u_zero : _copyfield(VA, fname) for fname in fieldnames(T)]
return T(fields...)
end
Base.sizehint!(VA::AbstractVectorOfArray{T, N}, i) where {T, N} = sizehint!(VA.u, i)
Base.reverse!(VA::AbstractVectorOfArray) = reverse!(VA.u)
Base.reverse(VA::AbstractVectorOfArray) = VectorOfArray(reverse(VA.u))
function Base.reverse(VA::AbstractDiffEqArray)
return DiffEqArray(reverse(VA.u), VA.t, parameter_values(VA), symbolic_container(VA))
end
function Base.resize!(VA::AbstractVectorOfArray, i::Integer)
if Base.hasproperty(VA, :sys) && VA.sys !== nothing
error("resize! is not allowed on AbstractVectorOfArray with a sys")
end
Base.resize!(VA.u, i)
return if Base.hasproperty(VA, :t) && VA.t !== nothing
Base.resize!(VA.t, i)
end
end
function Base.pointer(VA::AbstractVectorOfArray)
return Base.pointer(VA.u)
end
function Base.push!(VA::AbstractVectorOfArray{T, N}, new_item::AbstractArray) where {T, N}
return push!(VA.u, new_item)
end
function Base.append!(
VA::AbstractVectorOfArray{T, N},
new_item::AbstractVectorOfArray{T, N}
) where {T, N}
for item in copy(new_item.u)
push!(VA, item)
end
return VA
end
function Base.stack(VA::AbstractVectorOfArray; dims = :)
return stack(stack.(VA.u); dims)
end
# AbstractArray methods
function Base.view(
A::AbstractVectorOfArray{T, N, <:AbstractVector{T}},
I::Vararg{Any, M}
) where {T, N, M}
@inline
if length(I) == 1
J = map(i -> Base.unalias(A, i), to_indices(A, I))