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

Row and Column Iterator for Matrices #29749

Merged
merged 25 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions base/abstractarraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,36 @@ _reperr(s, n, N) = throw(ArgumentError("number of " * s * " repetitions " *

return R
end

"""
eachrow(A::AbstractVecOrMat)

Creates a generator that iterates over the first dimension of matrix A, returning the rows as views.
arnavs marked this conversation as resolved.
Show resolved Hide resolved
See also [`eachcol`](@ref) and [`eachslice`](@ref).
"""
eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1))


"""
eachcol(A::AbstractVecOrMat)

Creates a generator that iterates over the second dimension of matrix A, returning the columns as views.
See also [`eachrow`](@ref) and [`eachslice`](@ref).
"""
eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2))

"""
eachslice(A::AbstractArray; dims)

Creates a generator that iterates over the given dims of A, returning views that select all the data from the other dimensions in A.
arnavs marked this conversation as resolved.
Show resolved Hide resolved

Only a single dimension in dims is currently supported. Equivalent to (view(A,:,:,...,i,:,:,...)) for i in axes(A, dims)), where i is in position dims.
See also [`eachrow`](@ref), [`eachcol`](@ref), and [`selectdim`](@ref).
"""
@inline function eachslice(A::AbstractArray; dims)
length(dims) == 1 || throw(ArgumentError("only single dimensions are supported"))
dim = first(dims)
dim <= ndims(A) || throw(DimensionMismatch("A doesn't have that many dimensions"))
idx1, idx2 = ntuple(d->(:), dim-1), ntuple(d->(:), ndims(A)-dim)
return (view(A, idx1..., i, idx2...) for i in axes(A, dim))
end
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ export
accumulate,
accumulate!,
eachindex,
eachslice,
extrema,
fill!,
fill,
Expand Down
16 changes: 16 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1932,6 +1932,22 @@ end
@test IndexStyle(selectdim(A, 3, 1)) == IndexStyle(view(A, :, :, 1)) == IndexLinear()
end

# row/column/slice iterator tests
arnavs marked this conversation as resolved.
Show resolved Hide resolved
using Base: eachrow, eachcol
@testset "row/column/slice iterators" begin
# Simple ones
M = [1 2 3; 4 5 6; 7 8 9]
@test collect(eachrow(M)) == collect(eachslice(M, dims = 1)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
@test collect(eachcol(M)) == collect(eachslice(M, dims = 2)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
@test_throws DimensionMismatch eachslice(M, dims = 4)

# Higher-dimensional case
M = reshape([(1:16)...], 2, 2, 2, 2)
@test_throws MethodError collect(eachrow(M))
@test_throws MethodError collect(eachcol(M))
@test collect(eachslice(M, dims = 1))[1][:, :, 1] == [1 5; 3 7]
end

###
### IndexCartesian workout
###
Expand Down