Skip to content

Commit

Permalink
Add eachrow and eachcol (#1834)
Browse files Browse the repository at this point in the history
  • Loading branch information
joschmitt authored Oct 7, 2024
1 parent 96c82d5 commit 63d54c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,20 @@ axes(t::MatrixElem{T}) where T <: NCRingElement = Base.OneTo.(size(t))

axes(t::MatrixElem{T}, d::Integer) where T <: NCRingElement = Base.OneTo(size(t, d))

###############################################################################
#
# eachrow / eachcol
#
###############################################################################

@static if VERSION < v"1.9"
Base.eachrow(a::MatrixElem) = (view(a, i, :) for i in 1:nrows(a))
Base.eachcol(a::MatrixElem) = (view(a, :, i) for i in 1:ncols(a))
else
Base.eachrow(a::MatrixElem) = Slices(a, (1, :), (axes(a, 1),))
Base.eachcol(a::MatrixElem) = Slices(a, (:, 1), (axes(a, 2),))
end

###############################################################################
#
# Matrix spaces iteration
Expand Down
14 changes: 14 additions & 0 deletions test/generic/Matrix-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4058,6 +4058,20 @@ end
end
end

@testset "Generic.Mat.slices" begin
A = ZZ[1 2 3; 4 5 6]
@test length(eachrow(A)) == nrows(A)
@test length(eachcol(A)) == ncols(A)

@test first(eachrow(A)) == [ZZ(1), ZZ(2), ZZ(3)]
@test first(eachcol(A)) == [ZZ(1), ZZ(4)]

A = zero_matrix(ZZ, 2, 0)
@test length(eachrow(A)) == nrows(A)
@test length(eachcol(A)) == ncols(A)
@test first(eachrow(A)) == elem_type(ZZ)[]
end

@testset "Generic.Mat.change_base_ring" begin
for (P, Q, T) in ((matrix_space(ZZ, 2, 3), matrix_space(ZZ, 3, 2), MatElem),
(matrix_ring(ZZ, 3), matrix_ring(ZZ, 3), MatRingElem))
Expand Down

0 comments on commit 63d54c2

Please sign in to comment.