Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

block method to assemble multi-dimensional arrays #46003

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
similar function, lolstack
  • Loading branch information
Nicolau Leal Werneck committed Jul 12, 2022
commit f29d999517787026a6962dede2ee30b5a2e3867e
38 changes: 38 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
@@ -2840,6 +2840,44 @@ function awfulstack_(aoa; indices=(), mydim=ndims(aoa))
end
catdim_(dims) = (a,b) -> cat(a,b,dims=dims)

"""
lolstack(list_of_lists; ndim=n)

Assembles a tensor of order `ndim` from a nested array-of-arrays. Vector sizes must match.

# Examples
```jldoctest
julia> lolstack([[[1,2],[3,4]], [[5,6],[7,8]]], ndim=3)
2×2×2 Array{Int64, 3}:
[:, :, 1] =
1 3
2 4

[:, :, 2] =
5 7
6 8

julia> a = eachcol(reshape(1:6,2,:)) |> collect
3-element Vector{SubArray{Int64, 1, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}}:
[1, 2]
[3, 4]
[5, 6]

julia> lolstack(a, ndim=2)
2×3 Matrix{Int64}:
1 3 5
2 4 6
"""
lolstack(array_of_arrays; ndim) = lolstack_(ndim, array_of_arrays)
lolstack(f, c...) = lolstack_(2, map(f, c...))
function lolstack_(ndim, aoa)
if ndim == 1
aoa
else
hvncat(ndim, lolstack_.(ndim-1, aoa)...)
end
end

## Reductions and accumulates ##

function isequal(A::AbstractArray, B::AbstractArray)