From 540ec6697d0c6a3bfec0f88d484c60c9595078c1 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sat, 20 Oct 2018 20:12:42 -0700 Subject: [PATCH 01/25] first pass, sans tests --- stdlib/LinearAlgebra/src/LinearAlgebra.jl | 2 ++ stdlib/LinearAlgebra/src/generic.jl | 28 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/stdlib/LinearAlgebra/src/LinearAlgebra.jl b/stdlib/LinearAlgebra/src/LinearAlgebra.jl index c58f98dd83f31..445dba7856e30 100644 --- a/stdlib/LinearAlgebra/src/LinearAlgebra.jl +++ b/stdlib/LinearAlgebra/src/LinearAlgebra.jl @@ -67,6 +67,7 @@ export copyto!, copy_transpose!, cross, + cols, adjoint, adjoint!, det, @@ -123,6 +124,7 @@ export opnorm, rank, rdiv!, + rows, schur, schur!, svd, diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index fa0238a739c2c..88317e4479420 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1415,3 +1415,31 @@ function normalize(v::AbstractVector, p::Real = 2) return T[] end end + +""" + rows(m::Matrix{T}) + +Get a collection containing the rows of m. +See also [`cols`](@ref). + +# Examples +```jldoctest + +```` +""" +rows(m::Matrix) = [m[i, :] for i in 1:size(m, 1)] + + +""" + cols(m::Matrix{T}) + +Get a collection containing the columns of m. +See also [`rows`](@ref). + +# Examples +```jldoctest + +```` +""" +cols(m::Matrix) = [m[:, i] for i in 1:size(m, 2)] + From e770e7282798cc82f98d3a529263e38fdcfd99a9 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sat, 20 Oct 2018 20:17:03 -0700 Subject: [PATCH 02/25] add test --- stdlib/LinearAlgebra/test/generic.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 47fbf290a79a7..5841e66e931df 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -324,6 +324,13 @@ end @test_throws ErrorException transpose(rand(2,2,2,2)) end + +@testset "rows and columns tests" begin + M = [1 2 3; 4 5 6; 7 8 9] + @test rows(M) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + @test cols(M) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] +end + @testset "generic functions for checking whether matrices have banded structure" begin using LinearAlgebra: isbanded pentadiag = [1 2 3; 4 5 6; 7 8 9] From 230b7fbd54cfb384687669ff2e3386169ceba32a Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sat, 20 Oct 2018 21:06:10 -0700 Subject: [PATCH 03/25] refactor a bit --- stdlib/LinearAlgebra/src/generic.jl | 39 ++++++++++++++++++---------- stdlib/LinearAlgebra/test/generic.jl | 4 +-- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 88317e4479420..7e0938bf476c3 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1416,30 +1416,41 @@ function normalize(v::AbstractVector, p::Real = 2) end end +# Iterator for the rows() and cols() +struct MatrixDimIterator{T} + row::Bool + length::Int + M::Matrix{T} +end + +function Base.iterate(it::MatrixDimIterator, state = 1) + if state > it.length + return nothing + elseif it.row == true + obj = @view it.M[state, :] + return (obj, state+1) + else + obj = @view it.M[:, state] + return (obj, state+1) + end +end + +Base.eltype(it::MatrixDimIterator) = Array{eltype(it.M), 1} +Base.length(it::MatrixDimIterator) = it.length """ rows(m::Matrix{T}) -Get a collection containing the rows of m. +Get an iterator for the rows of m. See also [`cols`](@ref). - -# Examples -```jldoctest - -```` """ -rows(m::Matrix) = [m[i, :] for i in 1:size(m, 1)] +rows(m::Matrix) = MatrixDimIterator(true, size(M, 1), M) """ cols(m::Matrix{T}) -Get a collection containing the columns of m. +Get a iterator for the columns of m. See also [`rows`](@ref). - -# Examples -```jldoctest - -```` """ -cols(m::Matrix) = [m[:, i] for i in 1:size(m, 2)] +cols(m::Matrix) = MatrixDimIterator(false, size(M, 2), M) diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 5841e66e931df..87242c6fede59 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -327,8 +327,8 @@ end @testset "rows and columns tests" begin M = [1 2 3; 4 5 6; 7 8 9] - @test rows(M) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] - @test cols(M) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] + @test collect(rows(M)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + @test collect(cols(M)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] end @testset "generic functions for checking whether matrices have banded structure" begin From bd7c66661333a65afeb8c845359fa009f33d7a85 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sat, 20 Oct 2018 21:15:09 -0700 Subject: [PATCH 04/25] fix eltype to show views --- stdlib/LinearAlgebra/src/generic.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 7e0938bf476c3..891c280f37bcb 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1435,7 +1435,7 @@ function Base.iterate(it::MatrixDimIterator, state = 1) end end -Base.eltype(it::MatrixDimIterator) = Array{eltype(it.M), 1} +Base.eltype(it::MatrixDimIterator) = it.row == true ? typeof(@view it.M[1, :]) : typeof(@view it.M[:, 1]) Base.length(it::MatrixDimIterator) = it.length """ rows(m::Matrix{T}) From 6efae6b4db29e186a385cfaa1aff4ce31e63f389 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sat, 20 Oct 2018 21:23:56 -0700 Subject: [PATCH 05/25] fix whitespace --- stdlib/LinearAlgebra/src/generic.jl | 19 +++++++++---------- stdlib/LinearAlgebra/test/generic.jl | 4 ++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 891c280f37bcb..8f404449c0866 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1421,22 +1421,22 @@ struct MatrixDimIterator{T} row::Bool length::Int M::Matrix{T} -end +end -function Base.iterate(it::MatrixDimIterator, state = 1) - if state > it.length - return nothing - elseif it.row == true +function Base.iterate(it::MatrixDimIterator, state = 1) + if state > it.length + return nothing + elseif it.row == true obj = @view it.M[state, :] return (obj, state+1) - else + else obj = @view it.M[:, state] return (obj, state+1) - end -end + end +end Base.eltype(it::MatrixDimIterator) = it.row == true ? typeof(@view it.M[1, :]) : typeof(@view it.M[:, 1]) -Base.length(it::MatrixDimIterator) = it.length +Base.length(it::MatrixDimIterator) = it.length """ rows(m::Matrix{T}) @@ -1453,4 +1453,3 @@ Get a iterator for the columns of m. See also [`rows`](@ref). """ cols(m::Matrix) = MatrixDimIterator(false, size(M, 2), M) - diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 87242c6fede59..17b33691878f5 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -325,11 +325,11 @@ end end -@testset "rows and columns tests" begin +@testset "rows and columns tests" begin M = [1 2 3; 4 5 6; 7 8 9] @test collect(rows(M)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] @test collect(cols(M)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] -end +end @testset "generic functions for checking whether matrices have banded structure" begin using LinearAlgebra: isbanded From 912fc364b2d3039ca4d05c56c2a8596225f277e5 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sat, 20 Oct 2018 23:08:41 -0700 Subject: [PATCH 06/25] fix typo --- stdlib/LinearAlgebra/src/generic.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 8f404449c0866..cdc407a7b3a2f 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1438,18 +1438,18 @@ end Base.eltype(it::MatrixDimIterator) = it.row == true ? typeof(@view it.M[1, :]) : typeof(@view it.M[:, 1]) Base.length(it::MatrixDimIterator) = it.length """ - rows(m::Matrix{T}) + rows(M::Matrix{T}) -Get an iterator for the rows of m. +Get an iterator for the rows of M. See also [`cols`](@ref). """ -rows(m::Matrix) = MatrixDimIterator(true, size(M, 1), M) +rows(M::Matrix) = MatrixDimIterator(true, size(M, 1), M) """ - cols(m::Matrix{T}) + cols(M::Matrix{T}) -Get a iterator for the columns of m. +Get a iterator for the columns of M. See also [`rows`](@ref). """ -cols(m::Matrix) = MatrixDimIterator(false, size(M, 2), M) +cols(M::Matrix) = MatrixDimIterator(false, size(M, 2), M) From 9be648335fdc098f2380612d9bc6fa27cd231a55 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sun, 21 Oct 2018 10:57:06 -0700 Subject: [PATCH 07/25] rename [ci skip] --- stdlib/LinearAlgebra/src/LinearAlgebra.jl | 4 ++-- stdlib/LinearAlgebra/src/generic.jl | 4 ++-- stdlib/LinearAlgebra/test/generic.jl | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/stdlib/LinearAlgebra/src/LinearAlgebra.jl b/stdlib/LinearAlgebra/src/LinearAlgebra.jl index 445dba7856e30..be41c146c8c1f 100644 --- a/stdlib/LinearAlgebra/src/LinearAlgebra.jl +++ b/stdlib/LinearAlgebra/src/LinearAlgebra.jl @@ -67,7 +67,6 @@ export copyto!, copy_transpose!, cross, - cols, adjoint, adjoint!, det, @@ -75,6 +74,8 @@ export diagind, diagm, dot, + eachcolumn, + eachrow eigen, eigen!, eigmax, @@ -124,7 +125,6 @@ export opnorm, rank, rdiv!, - rows, schur, schur!, svd, diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index cdc407a7b3a2f..3c808d31b63e8 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1443,7 +1443,7 @@ Base.length(it::MatrixDimIterator) = it.length Get an iterator for the rows of M. See also [`cols`](@ref). """ -rows(M::Matrix) = MatrixDimIterator(true, size(M, 1), M) +eachrow(M::Matrix) = MatrixDimIterator(true, size(M, 1), M) """ @@ -1452,4 +1452,4 @@ rows(M::Matrix) = MatrixDimIterator(true, size(M, 1), M) Get a iterator for the columns of M. See also [`rows`](@ref). """ -cols(M::Matrix) = MatrixDimIterator(false, size(M, 2), M) +eachcolumn(M::Matrix) = MatrixDimIterator(false, size(M, 2), M) diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 17b33691878f5..872e9eefb5f1f 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -327,8 +327,8 @@ end @testset "rows and columns tests" begin M = [1 2 3; 4 5 6; 7 8 9] - @test collect(rows(M)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] - @test collect(cols(M)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] + @test collect(eachrow(M)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + @test collect(eachcolumn(M)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] end @testset "generic functions for checking whether matrices have banded structure" begin From a17aec6d81e103efce261d974dd43ff671c6a181 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sun, 21 Oct 2018 10:57:41 -0700 Subject: [PATCH 08/25] typofix [ci skip] --- stdlib/LinearAlgebra/src/LinearAlgebra.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/src/LinearAlgebra.jl b/stdlib/LinearAlgebra/src/LinearAlgebra.jl index be41c146c8c1f..99b1a0387747c 100644 --- a/stdlib/LinearAlgebra/src/LinearAlgebra.jl +++ b/stdlib/LinearAlgebra/src/LinearAlgebra.jl @@ -75,7 +75,7 @@ export diagm, dot, eachcolumn, - eachrow + eachrow, eigen, eigen!, eigmax, From 4aa571efa5162b19aed51211244034994e095b3d Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sun, 21 Oct 2018 11:01:39 -0700 Subject: [PATCH 09/25] add eachslice() [ci skip] --- stdlib/LinearAlgebra/src/generic.jl | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 3c808d31b63e8..9765337fc66dc 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1438,18 +1438,27 @@ end Base.eltype(it::MatrixDimIterator) = it.row == true ? typeof(@view it.M[1, :]) : typeof(@view it.M[:, 1]) Base.length(it::MatrixDimIterator) = it.length """ - rows(M::Matrix{T}) + eachrow(M::Matrix) Get an iterator for the rows of M. -See also [`cols`](@ref). +See also [`eachcolumn`](@ref). """ eachrow(M::Matrix) = MatrixDimIterator(true, size(M, 1), M) """ - cols(M::Matrix{T}) + eachcolumn(M::Matrix) Get a iterator for the columns of M. -See also [`rows`](@ref). +See also [`eachrow`](@ref). """ eachcolumn(M::Matrix) = MatrixDimIterator(false, size(M, 2), M) + +""" + eachslice(M::Matrix, dim) + +Get an iterator either for the rows of M +(dim = 1), or the cols. +See also [`eachrow`](@ref) and [`eachcolumn`](@ref). +""" +eachslice(M::Matrix, dim) = MatrixDimIterator(dim == 1, size(M, dim), M) From f4675530bcfebf0b9de49eb5fe7bc819e17e71af Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sun, 21 Oct 2018 11:50:08 -0700 Subject: [PATCH 10/25] eachcolumn => eachcol and a bit of error handling [ci skip] --- stdlib/LinearAlgebra/src/LinearAlgebra.jl | 2 +- stdlib/LinearAlgebra/src/generic.jl | 12 ++++++------ stdlib/LinearAlgebra/test/generic.jl | 5 +++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/stdlib/LinearAlgebra/src/LinearAlgebra.jl b/stdlib/LinearAlgebra/src/LinearAlgebra.jl index 99b1a0387747c..64a23a6466eed 100644 --- a/stdlib/LinearAlgebra/src/LinearAlgebra.jl +++ b/stdlib/LinearAlgebra/src/LinearAlgebra.jl @@ -74,7 +74,7 @@ export diagind, diagm, dot, - eachcolumn, + eachcol, eachrow, eigen, eigen!, diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 9765337fc66dc..a6650fa3fc211 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1441,24 +1441,24 @@ Base.length(it::MatrixDimIterator) = it.length eachrow(M::Matrix) Get an iterator for the rows of M. -See also [`eachcolumn`](@ref). +See also [`eachcol`](@ref) and [`eachslice`](@ref). """ eachrow(M::Matrix) = MatrixDimIterator(true, size(M, 1), M) """ - eachcolumn(M::Matrix) + eachcol(M::Matrix) Get a iterator for the columns of M. -See also [`eachrow`](@ref). +See also [`eachrow`](@ref) and [`eachslice`](@ref). """ -eachcolumn(M::Matrix) = MatrixDimIterator(false, size(M, 2), M) +eachcol(M::Matrix) = MatrixDimIterator(false, size(M, 2), M) """ eachslice(M::Matrix, dim) Get an iterator either for the rows of M (dim = 1), or the cols. -See also [`eachrow`](@ref) and [`eachcolumn`](@ref). +See also [`eachrow`](@ref) and [`eachcol`](@ref). """ -eachslice(M::Matrix, dim) = MatrixDimIterator(dim == 1, size(M, dim), M) +eachslice(M::Matrix, dim) = dim ∈ [1, 2] ? MatrixDimIterator(dim == 1, size(M, dim), M) : throw(ArgumentError("dim must be 1 or 2")) diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 872e9eefb5f1f..9502f9d6b36c6 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -327,8 +327,9 @@ end @testset "rows and columns tests" begin M = [1 2 3; 4 5 6; 7 8 9] - @test collect(eachrow(M)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] - @test collect(eachcolumn(M)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] + @test collect(eachrow(M)) == collect(eachslice(M, 1)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + @test collect(eachcol(M)) == collect(eachslice(M, 2)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] + @test_throws ArgumentError eachslice(M, 4) end @testset "generic functions for checking whether matrices have banded structure" begin From d04fbcf6e7f259f0b40dcb28ff45bd45c88ce44d Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Mon, 22 Oct 2018 16:47:53 -0700 Subject: [PATCH 11/25] feedback --- stdlib/LinearAlgebra/src/LinearAlgebra.jl | 1 + stdlib/LinearAlgebra/src/generic.jl | 42 ++++++----------------- stdlib/LinearAlgebra/test/generic.jl | 9 ++++- 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/stdlib/LinearAlgebra/src/LinearAlgebra.jl b/stdlib/LinearAlgebra/src/LinearAlgebra.jl index 64a23a6466eed..f366866b45f92 100644 --- a/stdlib/LinearAlgebra/src/LinearAlgebra.jl +++ b/stdlib/LinearAlgebra/src/LinearAlgebra.jl @@ -76,6 +76,7 @@ export dot, eachcol, eachrow, + eachslice, eigen, eigen!, eigmax, diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index a6650fa3fc211..1ca425b19580b 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1416,49 +1416,29 @@ function normalize(v::AbstractVector, p::Real = 2) end end -# Iterator for the rows() and cols() -struct MatrixDimIterator{T} - row::Bool - length::Int - M::Matrix{T} -end - -function Base.iterate(it::MatrixDimIterator, state = 1) - if state > it.length - return nothing - elseif it.row == true - obj = @view it.M[state, :] - return (obj, state+1) - else - obj = @view it.M[:, state] - return (obj, state+1) - end -end - -Base.eltype(it::MatrixDimIterator) = it.row == true ? typeof(@view it.M[1, :]) : typeof(@view it.M[:, 1]) -Base.length(it::MatrixDimIterator) = it.length """ - eachrow(M::Matrix) + eachrow(A::AbstractVecOrMat) -Get an iterator for the rows of M. +Get a generator over views of A's first dimension. See also [`eachcol`](@ref) and [`eachslice`](@ref). """ -eachrow(M::Matrix) = MatrixDimIterator(true, size(M, 1), M) +eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) """ - eachcol(M::Matrix) + eachcol(A::AbstractVecOrMat) -Get a iterator for the columns of M. +Get a generator over views of A's second dimension. See also [`eachrow`](@ref) and [`eachslice`](@ref). """ -eachcol(M::Matrix) = MatrixDimIterator(false, size(M, 2), M) +eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) """ - eachslice(M::Matrix, dim) + eachslice(A::AbstractArray, d) -Get an iterator either for the rows of M -(dim = 1), or the cols. +Get an iterator over views of A's dth dimension. If +A has less than d dimensions, collect(eachslice(A, d)) +will just return a trivial collection with a view into A. See also [`eachrow`](@ref) and [`eachcol`](@ref). """ -eachslice(M::Matrix, dim) = dim ∈ [1, 2] ? MatrixDimIterator(dim == 1, size(M, dim), M) : throw(ArgumentError("dim must be 1 or 2")) +eachslice(A, d) = (view(A, ntuple(n->n==d ? i : (:), ndims(A))...) for i in axes(A, d)) \ No newline at end of file diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 9502f9d6b36c6..2585a6ca152b2 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -326,10 +326,17 @@ end @testset "rows and columns tests" begin + # Simple ones M = [1 2 3; 4 5 6; 7 8 9] @test collect(eachrow(M)) == collect(eachslice(M, 1)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] @test collect(eachcol(M)) == collect(eachslice(M, 2)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] - @test_throws ArgumentError eachslice(M, 4) + @test collect(eachslice(M, 4))[1] == M + + # 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, 1))[1][:, :, 1] == [1 5; 3 7] end @testset "generic functions for checking whether matrices have banded structure" begin From 6c7bb08388a4a1a219fc98276f3d462b14ff0277 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Mon, 22 Oct 2018 16:49:38 -0700 Subject: [PATCH 12/25] fix whitespace --- stdlib/LinearAlgebra/src/generic.jl | 2 +- stdlib/LinearAlgebra/test/generic.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 1ca425b19580b..3469ca4c82e6d 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1428,7 +1428,7 @@ eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) """ eachcol(A::AbstractVecOrMat) -Get a generator over views of A's second dimension. +Get a generator over views of A's second dimension. See also [`eachrow`](@ref) and [`eachslice`](@ref). """ eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 2585a6ca152b2..d045fb27e8649 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -326,13 +326,13 @@ end @testset "rows and columns tests" begin - # Simple ones + # Simple ones M = [1 2 3; 4 5 6; 7 8 9] @test collect(eachrow(M)) == collect(eachslice(M, 1)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] @test collect(eachcol(M)) == collect(eachslice(M, 2)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] @test collect(eachslice(M, 4))[1] == M - # Higher-dimensional case + # Higher-dimensional case M = reshape([(1:16)...], 2, 2, 2, 2) @test_throws MethodError collect(eachrow(M)) @test_throws MethodError collect(eachcol(M)) From 32da140a5eaa47cbb1a18bfc7eef87ab52125e5e Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Wed, 31 Oct 2018 12:35:00 -0700 Subject: [PATCH 13/25] refactor eachslice; type-stable now? --- stdlib/LinearAlgebra/src/generic.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 3469ca4c82e6d..3442ddbbd75ff 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1441,4 +1441,4 @@ A has less than d dimensions, collect(eachslice(A, d)) will just return a trivial collection with a view into A. See also [`eachrow`](@ref) and [`eachcol`](@ref). """ -eachslice(A, d) = (view(A, ntuple(n->n==d ? i : (:), ndims(A))...) for i in axes(A, d)) \ No newline at end of file +eachslice(A, dim) = (selectdim(A, dim, i) for i in axes(A, dim)) \ No newline at end of file From ff2e0d9b020848b396d216b84b8292f39304b3d3 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Wed, 28 Nov 2018 11:40:57 -0800 Subject: [PATCH 14/25] git feedback --- stdlib/LinearAlgebra/src/generic.jl | 16 +++++++++------- stdlib/LinearAlgebra/test/generic.jl | 8 ++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 3442ddbbd75ff..34708f4058662 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1419,7 +1419,7 @@ end """ eachrow(A::AbstractVecOrMat) -Get a generator over views of A's first dimension. +Create a generator iterating over views of A's first dimension. See also [`eachcol`](@ref) and [`eachslice`](@ref). """ eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) @@ -1428,17 +1428,19 @@ eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) """ eachcol(A::AbstractVecOrMat) -Get a generator over views of A's second dimension. +Create a generator iterating over views of A's second dimension. See also [`eachrow`](@ref) and [`eachslice`](@ref). """ eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) """ - eachslice(A::AbstractArray, d) + eachslice(A; dims = 1) -Get an iterator over views of A's dth dimension. If -A has less than d dimensions, collect(eachslice(A, d)) -will just return a trivial collection with a view into A. +Create a generator iterating over views of A's nth dimension. See also [`eachrow`](@ref) and [`eachcol`](@ref). """ -eachslice(A, dim) = (selectdim(A, dim, i) for i in axes(A, dim)) \ No newline at end of file +@inline function eachslice(A; dims = 1) + dims <= ndims(A) || throw(DimensionMismatch("A doesn't have that many dimensions")) + idx1, idx2 = ntuple(d->(:), dims-1), ntuple(d->(:), ndims(A)-dims) + return (view(A, idx1..., i, idx2...) for i in axes(A, dims)) +end \ No newline at end of file diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index d045fb27e8649..f1ab729d6b708 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -328,15 +328,15 @@ end @testset "rows and columns tests" begin # Simple ones M = [1 2 3; 4 5 6; 7 8 9] - @test collect(eachrow(M)) == collect(eachslice(M, 1)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] - @test collect(eachcol(M)) == collect(eachslice(M, 2)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] - @test collect(eachslice(M, 4))[1] == M + @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, 1))[1][:, :, 1] == [1 5; 3 7] + @test collect(eachslice(M, dims = 1))[1][:, :, 1] == [1 5; 3 7] end @testset "generic functions for checking whether matrices have banded structure" begin From 510031ae9470eb7a40abf7076da41f55cdb2c732 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Wed, 28 Nov 2018 14:19:23 -0800 Subject: [PATCH 15/25] more git feedback --- base/abstractarraymath.jl | 33 +++++++++++++++++++++++ base/exports.jl | 3 +++ stdlib/LinearAlgebra/src/LinearAlgebra.jl | 3 --- stdlib/LinearAlgebra/src/generic.jl | 29 -------------------- stdlib/LinearAlgebra/test/generic.jl | 15 ----------- 5 files changed, 36 insertions(+), 47 deletions(-) diff --git a/base/abstractarraymath.jl b/base/abstractarraymath.jl index 9e6cd7af7ca23..5bc6a4fd0356f 100644 --- a/base/abstractarraymath.jl +++ b/base/abstractarraymath.jl @@ -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. +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; dims = 1) + +Creates a generator that iterates over the given dims of A, returning views that select all the data from the other dimensions in A. + +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 = 1) + 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 \ No newline at end of file diff --git a/base/exports.jl b/base/exports.jl index 5da9a06207a62..aafd6d0496dd1 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -365,7 +365,10 @@ export cumsum!, accumulate, accumulate!, + eachcol, eachindex, + eachrow, + eachslice, extrema, fill!, fill, diff --git a/stdlib/LinearAlgebra/src/LinearAlgebra.jl b/stdlib/LinearAlgebra/src/LinearAlgebra.jl index f366866b45f92..c58f98dd83f31 100644 --- a/stdlib/LinearAlgebra/src/LinearAlgebra.jl +++ b/stdlib/LinearAlgebra/src/LinearAlgebra.jl @@ -74,9 +74,6 @@ export diagind, diagm, dot, - eachcol, - eachrow, - eachslice, eigen, eigen!, eigmax, diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 34708f4058662..5995bfdba1517 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1414,33 +1414,4 @@ function normalize(v::AbstractVector, p::Real = 2) T = typeof(zero(eltype(v))/nrm) return T[] end -end - -""" - eachrow(A::AbstractVecOrMat) - -Create a generator iterating over views of A's first dimension. -See also [`eachcol`](@ref) and [`eachslice`](@ref). -""" -eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) - - -""" - eachcol(A::AbstractVecOrMat) - -Create a generator iterating over views of A's second dimension. -See also [`eachrow`](@ref) and [`eachslice`](@ref). -""" -eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) - -""" - eachslice(A; dims = 1) - -Create a generator iterating over views of A's nth dimension. -See also [`eachrow`](@ref) and [`eachcol`](@ref). -""" -@inline function eachslice(A; dims = 1) - dims <= ndims(A) || throw(DimensionMismatch("A doesn't have that many dimensions")) - idx1, idx2 = ntuple(d->(:), dims-1), ntuple(d->(:), ndims(A)-dims) - return (view(A, idx1..., i, idx2...) for i in axes(A, dims)) end \ No newline at end of file diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index f1ab729d6b708..47fbf290a79a7 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -324,21 +324,6 @@ end @test_throws ErrorException transpose(rand(2,2,2,2)) end - -@testset "rows and columns tests" 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 - @testset "generic functions for checking whether matrices have banded structure" begin using LinearAlgebra: isbanded pentadiag = [1 2 3; 4 5 6; 7 8 9] From 9d5531fbbf44e6b157c8c0890fa0e666389aa992 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Wed, 28 Nov 2018 14:31:20 -0800 Subject: [PATCH 16/25] remove default for eachslice --- base/abstractarraymath.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/abstractarraymath.jl b/base/abstractarraymath.jl index 5bc6a4fd0356f..f5f0a91497585 100644 --- a/base/abstractarraymath.jl +++ b/base/abstractarraymath.jl @@ -425,14 +425,14 @@ See also [`eachrow`](@ref) and [`eachslice`](@ref). eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) """ - eachslice(A; dims = 1) + eachslice(A; 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. 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 = 1) +@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")) From ce54df0d420972f6c935e5b33a735bc4f92b94ab Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Wed, 28 Nov 2018 14:35:19 -0800 Subject: [PATCH 17/25] add tests --- test/arrayops.jl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/arrayops.jl b/test/arrayops.jl index b15bcc3779fde..88fcd546e4a4d 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1932,6 +1932,21 @@ end @test IndexStyle(selectdim(A, 3, 1)) == IndexStyle(view(A, :, :, 1)) == IndexLinear() end +# row/column/slice iterator tests +@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 ### From 85c6ca366a00dd98f5010bc659d5c513a612ca83 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Fri, 30 Nov 2018 14:46:42 -0800 Subject: [PATCH 18/25] prune eachrow eachcol exports and newline fix? --- base/exports.jl | 2 -- stdlib/LinearAlgebra/src/generic.jl | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/base/exports.jl b/base/exports.jl index aafd6d0496dd1..1a1456364cc09 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -366,8 +366,6 @@ export accumulate, accumulate!, eachcol, - eachindex, - eachrow, eachslice, extrema, fill!, diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 5995bfdba1517..fa0238a739c2c 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1414,4 +1414,4 @@ function normalize(v::AbstractVector, p::Real = 2) T = typeof(zero(eltype(v))/nrm) return T[] end -end \ No newline at end of file +end From 689fb9ad9328683e52e234582fc7376af1748c7d Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Fri, 30 Nov 2018 14:54:46 -0800 Subject: [PATCH 19/25] typo in exports --- base/exports.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/exports.jl b/base/exports.jl index 1a1456364cc09..9c5744aeb9143 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -365,7 +365,7 @@ export cumsum!, accumulate, accumulate!, - eachcol, + eachindex, eachslice, extrema, fill!, From efdf8d967a8dd0e47d3125a768a7491c782c3888 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Fri, 30 Nov 2018 14:57:31 -0800 Subject: [PATCH 20/25] update docs for AbstractArray [ci skip] --- base/abstractarraymath.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/abstractarraymath.jl b/base/abstractarraymath.jl index f5f0a91497585..ba07360188398 100644 --- a/base/abstractarraymath.jl +++ b/base/abstractarraymath.jl @@ -425,7 +425,7 @@ See also [`eachrow`](@ref) and [`eachslice`](@ref). eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) """ - eachslice(A; dims) + 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. @@ -438,4 +438,4 @@ See also [`eachrow`](@ref), [`eachcol`](@ref), and [`selectdim`](@ref). 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 \ No newline at end of file +end From 21877c8e2781ce65abdc20c6276280643b53d018 Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Fri, 30 Nov 2018 15:20:02 -0800 Subject: [PATCH 21/25] Update test/arrayops.jl Co-Authored-By: arnavs --- test/arrayops.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/arrayops.jl b/test/arrayops.jl index 88fcd546e4a4d..45b7125fb8051 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1933,6 +1933,7 @@ end end # row/column/slice iterator tests +using Base: eachrow, eachcol @testset "row/column/slice iterators" begin # Simple ones M = [1 2 3; 4 5 6; 7 8 9] From 61fe5fe384d1be4c5f168370fe4b433859804ae1 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sat, 1 Dec 2018 10:55:11 -0800 Subject: [PATCH 22/25] Update base/abstractarraymath.jl Co-Authored-By: arnavs --- base/abstractarraymath.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/abstractarraymath.jl b/base/abstractarraymath.jl index ba07360188398..d0aace923c048 100644 --- a/base/abstractarraymath.jl +++ b/base/abstractarraymath.jl @@ -410,7 +410,8 @@ end """ eachrow(A::AbstractVecOrMat) -Creates a generator that iterates over the first dimension of matrix A, returning the rows as views. +Creates a generator that iterates over the first dimension of vector or matrix `A`, +returning the rows as views. See also [`eachcol`](@ref) and [`eachslice`](@ref). """ eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) From a9473d9652cb121ef91fa5b0452983556942db2b Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sat, 1 Dec 2018 10:59:03 -0800 Subject: [PATCH 23/25] bikeshedding --- base/abstractarraymath.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/base/abstractarraymath.jl b/base/abstractarraymath.jl index d0aace923c048..c085b68375b31 100644 --- a/base/abstractarraymath.jl +++ b/base/abstractarraymath.jl @@ -410,7 +410,7 @@ end """ eachrow(A::AbstractVecOrMat) -Creates a generator that iterates over the first dimension of vector or matrix `A`, +Create a generator that iterates over the first dimension of vector or matrix `A`, returning the rows as views. See also [`eachcol`](@ref) and [`eachslice`](@ref). """ @@ -420,7 +420,8 @@ 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. +Create 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)) @@ -428,9 +429,11 @@ 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. +Create a generator that iterates over the given dims of A, returning views that select all +the data from the other dimensions in A. -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. +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) From 6959465cb8befc07780b722051d70d7d38f8dc81 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sun, 2 Dec 2018 14:33:07 +0100 Subject: [PATCH 24/25] Add missing backticks and improve message [ci skip] --- base/abstractarraymath.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/base/abstractarraymath.jl b/base/abstractarraymath.jl index c085b68375b31..a5609d1be9709 100644 --- a/base/abstractarraymath.jl +++ b/base/abstractarraymath.jl @@ -420,7 +420,7 @@ eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) """ eachcol(A::AbstractVecOrMat) -Create a generator that iterates over the second dimension of matrix A, returning the +Create a generator that iterates over the second dimension of matrix `A`, returning the columns as views. See also [`eachrow`](@ref) and [`eachslice`](@ref). """ @@ -429,17 +429,17 @@ eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) """ eachslice(A::AbstractArray; dims) -Create a generator that iterates over the given dims of A, returning views that select all -the data from the other dimensions in A. +Create a generator that iterates over dimensions `dims` of `A`, returning views that select all +the data from the other dimensions in `A`. -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. +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")) + dim <= ndims(A) || throw(DimensionMismatch("A doesn't have $dim 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 From b3ced1a65ada6c67ecc7580f85fbcae1769b9ec0 Mon Sep 17 00:00:00 2001 From: Arnav Sood Date: Sun, 2 Dec 2018 15:31:56 -0800 Subject: [PATCH 25/25] add exports for eachcol, eachrow [ci skip] --- base/exports.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/base/exports.jl b/base/exports.jl index 9c5744aeb9143..aafd6d0496dd1 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -365,7 +365,9 @@ export cumsum!, accumulate, accumulate!, + eachcol, eachindex, + eachrow, eachslice, extrema, fill!,