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

RFC: add default dims to eachslice #48108

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mcabbott
Copy link
Contributor

@mcabbott mcabbott commented Jan 3, 2023

I wonder if eachslice should have a default of the last dimension.

In python you can do for mat in batch to iterate a 3D array by getting its slices... the ones which make sense for row-major arrays. With this, something like for mat in eachslice(batch) would iterate the slices which make sense for column-major arrays.

julia> sizes(A) = (inner = size(first(A)), outer = size(A));

julia> eachslice(rand(2,3,5,7)) |> sizes  # previously no default
(inner = (2, 3, 5), outer = (7,))

julia> stack(eachslice(rand(2,3,5,7))) |> size  # matches this
(2, 3, 5, 7)

Edit: closes #54626

Extend to eachcol? (Edit: removed for now)

Less obviously, we could also extend eachcol to always give a slice along the first dimension. Then for col in eachcol(batch) would return dense 1D views for any higher-dim Array.

julia> eachcol(rand(2,3,5)) |> sizes  # always 1st dim inner
(inner = (2,), outer = (3, 5))

julia> eachcol(rand(2,3)) |> sizes  # as before
(inner = (2,), outer = (3,))

julia> eachcol(rand(2,)) |> sizes  # as before, involves a reshape
(inner = (2,), outer = (1,))

For symmetry we should then extend eachrow; note that on vectors it currently picks the 2nd (trivial) dimension, so I think that's what it would have to do always:

julia> eachrow(rand(2,3,5)) |> sizes  # always 2st dim inner
(inner = (3,), outer = (2, 5))

julia> eachrow(rand(2,3)) |> sizes  # as before
(inner = (3,), outer = (2,))

julia> eachrow(rand(2)) |> sizes  # as before,  involves a reshape
(inner = (1,), outer = (2,))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays [a, r, r, a, y, s]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

default dims = ndims(x) in eachslice(x; dims = ndims(x)) to make eachslice the inverse stack
2 participants