Skip to content

Commit ad33d55

Browse files
Merge pull request #357 from jlchan/jc/VectorOfArray_multidim
Add support for `VectorOfArray(AbstractArray{<:AbstractArray})`
2 parents 758ac84 + a475a85 commit ad33d55

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/vector_of_array.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ Additionally, the `convert(Array,VA::AbstractVectorOfArray)` function is provide
2727
the `VectorOfArray` into a matrix/tensor. Also, `vecarr_to_vectors(VA::AbstractVectorOfArray)`
2828
returns a vector of the series for each component, that is, `A[i,:]` for each `i`.
2929
A plot recipe is provided, which plots the `A[i,:]` series.
30+
31+
There is also support for `VectorOfArray` with constructed from multi-dimensional arrays
32+
```julia
33+
VectorOfArray(u::AbstractArray{AT}) where {T, N, AT <: AbstractArray{T, N}}
34+
```
35+
where `IndexStyle(typeof(u)) isa IndexLinear`.
3036
"""
3137
mutable struct VectorOfArray{T, N, A} <: AbstractVectorOfArray{T, N, A}
3238
u::A # A <: AbstractVector{<: AbstractArray{T, N - 1}}
@@ -150,6 +156,13 @@ function VectorOfArray(vec::AbstractVector{VT}) where {T, N, VT <: AbstractArray
150156
VectorOfArray{T, N + 1, typeof(vec)}(vec)
151157
end
152158

159+
# allow multi-dimensional arrays as long as they're linearly indexed
160+
function VectorOfArray(array::AbstractArray{AT}) where {T, N, AT <: AbstractArray{T, N}}
161+
@assert IndexStyle(typeof(array)) isa IndexLinear
162+
163+
return VectorOfArray{T, N + 1, typeof(array)}(array)
164+
end
165+
153166
function DiffEqArray(vec::AbstractVector{T},
154167
ts::AbstractVector,
155168
::NTuple{N, Int},

test/basic_indexing.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,24 @@ mulX .= sqrt.(abs.(testva .* testvb))
232232
a = ArrayPartition(1:5, 1:6)
233233
a[1:8]
234234
a[[1, 3, 8]]
235+
236+
####################################################################
237+
# test when VectorOfArray is constructed from a linearly indexed
238+
# multidimensional array of arrays
239+
####################################################################
240+
241+
u_matrix = VectorOfArray(fill([1, 2], 2, 3))
242+
u_vector = VectorOfArray(vec(u_matrix.u))
243+
244+
# test broadcasting
245+
function foo!(u)
246+
@. u += 2 * u * abs(u)
247+
return u
248+
end
249+
foo!(u_matrix)
250+
foo!(u_vector)
251+
@test u_matrix u_vector
252+
253+
# test efficiency
254+
num_allocs = @allocations foo!(u_matrix)
255+
@test num_allocs == 0

0 commit comments

Comments
 (0)