RecursiveArrayTools.jl icon indicating copy to clipboard operation
RecursiveArrayTools.jl copied to clipboard

Vector of differently sized arrays

Open antoine-levitt opened this issue 3 years ago • 6 comments

julia> size(VectorOfArray([randn(2), randn(4)]))
(2, 2)

This is surprising at least, and runs the risk of subtle bugs if one tries to iterate on the VectorOfArray as if it was a multi-dimensional array.

antoine-levitt avatar Oct 19 '20 15:10 antoine-levitt

This is something we need and use in DiffEq for models which dynamically change over time.

ChrisRackauckas avatar Oct 19 '20 16:10 ChrisRackauckas

Oh I need it too and was pleasantly surprised to see that it worked. The thing is the size method: should it be undefined, or perhaps check that the sizes are all equal at runtime?

antoine-levitt avatar Oct 19 '20 16:10 antoine-levitt

Yeah... I don't know. It's something I've never figured out. size is useful in a lot of cases, but when you have a ragged array it's wrong. Maybe it's worth doing a runtime check.

ChrisRackauckas avatar Oct 19 '20 16:10 ChrisRackauckas

size is useful in a lot of cases

Thought so too but probably it's a code smell for generic code that doesn't assume much about array types. It's only really good for plain old arrays

antoine-levitt avatar Oct 19 '20 16:10 antoine-levitt

Yes, is used when doing conversions like Array(x), but there are issues with it we haven't solved.

ChrisRackauckas avatar Oct 19 '20 16:10 ChrisRackauckas

julia> vec(VectorOfArray([rand(3), rand(5)]))
ERROR: ArgumentError: number of rows of each array must match (got [3, 5])
Stacktrace:
 [1] _typed_hcat(#unused#::Type{Float64}, A::Vector{Vector{Float64}})
   @ Base ./abstractarray.jl:1518
 [2] reduce(#unused#::typeof(hcat), A::Vector{Vector{Float64}})
   @ Base ./abstractarray.jl:1572
 [3] Array
   @ ~/.julia/dev/RecursiveArrayTools/src/vector_of_array.jl:11 [inlined]
 [4] convert
   @ ./array.jl:532 [inlined]
 [5] vec(VA::VectorOfArray{Float64, 2, Vector{Vector{Float64}}})
   @ RecursiveArrayTools ~/.julia/dev/RecursiveArrayTools/src/vector_of_array.jl:120
 [6] top-level scope
   @ REPL[11]:1

This can be triggered e.g. when solving a SteadyStateProblem

garibarba avatar Mar 29 '21 22:03 garibarba