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

stack should accept multiple dims #47794

Open
LilithHafner opened this issue Dec 4, 2022 · 2 comments
Open

stack should accept multiple dims #47794

LilithHafner opened this issue Dec 4, 2022 · 2 comments
Labels
collections Data structures holding multiple items, e.g. sets

Comments

@LilithHafner
Copy link
Member

From the docstring, "With keyword dims::Integer [...] stack reverses the action of eachslice with the same dims." Those semantics should be extended to dims::Integer....

julia> x = rand(3,3,3);

julia> all(stack(eachslice(x, dims=i), dims=i) == x for i in 1:3)
true

julia> stack(eachslice(x, dims=(1,2)), dims=(1,2)) == x
ERROR: MethodError: no method matching _stack(::Tuple{Int64, Int64}, ::Slices{Array{Float64, 3}, Tuple{Int64, Int64, Colon}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}, SubArray{Float64, 1, Array{Float64, 3}, Tuple{Int64, Int64, Base.Slice{Base.OneTo{Int64}}}, true}, 2})

Closest candidates are:
  _stack(::Union{Colon, Integer}, ::Any)
   @ Base abstractarray.jl:2769
  _stack(::Any, ::Union{Base.HasLength, Base.HasShape}, ::Any)
   @ Base abstractarray.jl:2773
  _stack(::Any, ::Base.IteratorSize, ::Any)
   @ Base abstractarray.jl:2771

Stacktrace:
 [1] stack(iter::Slices{Array{Float64, 3}, Tuple{Int64, Int64, Colon}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}, SubArray{Float64, 1, Array{Float64, 3}, Tuple{Int64, Int64, Base.Slice{Base.OneTo{Int64}}}, true}, 2}; dims::Tuple{Int64, Int64})
   @ Base ./abstractarray.jl:2737
 [2] kwcall(::NamedTuple{(:dims,), Tuple{Tuple{Int64, Int64}}}, ::typeof(stack), iter::Slices{Array{Float64, 3}, Tuple{Int64, Int64, Colon}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}, SubArray{Float64, 1, Array{Float64, 3}, Tuple{Int64, Int64, Base.Slice{Base.OneTo{Int64}}}, true}, 2})
   @ Base ./abstractarray.jl:2737
 [3] top-level scope
   @ REPL[68]:1

cc @mcabbott, author of stack. (#43334)

@LilithHafner LilithHafner added the collections Data structures holding multiple items, e.g. sets label Dec 4, 2022
@mcabbott
Copy link
Contributor

mcabbott commented Dec 4, 2022

Yes I think this has an obvious meaning, although with some edge case where ndims(outer) != length(dims). Going by eachslice's move on from dims::Integer, the ETA is Julia 1.17... What does work at the moment is all the trailing dimensions:

julia> y = rand(3,3,3,3);

julia> y == stack(eachslice(y, dims=(3,4))) == stack(eachslice(y, dims=(2,3,4)))
true

Perhaps related questions, but easier to implement:

  • Should there be a stack!?
  • Python has hstack and vstack, is that too many verbs & too close to hcat etc?
  • [Edit] At present stack((i,2i) for i in 3:5; dims=4) is forbidden in the name of type-stability. It could allow dims=Val(4) like cat does.

@jariji
Copy link
Contributor

jariji commented Apr 6, 2023

Python has hstack and vstack, is that too many verbs & too close to hcat etc?

It's not too many, imo. It could be a eachrow => stackrows, eachcol => stackcols?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collections Data structures holding multiple items, e.g. sets
Projects
None yet
Development

No branches or pull requests

3 participants