From 21aac12040f8f5877af9e7161982b13a1f7a6faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Fri, 15 Nov 2024 22:57:26 +0100 Subject: [PATCH] Add `Base.keys(::MatElem)` (#1906) * Add `Base.keys(::MatElem)` * Bump version to 0.43.11 * Add test --- Project.toml | 2 +- src/Matrix.jl | 1 + test/Matrix-test.jl | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1a4ce21bf8..024a514e2f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "AbstractAlgebra" uuid = "c3fe647b-3220-5bb0-a1ea-a7954cac585d" -version = "0.43.10" +version = "0.43.11" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/Matrix.jl b/src/Matrix.jl index aa37148b2e..132f61d04f 100644 --- a/src/Matrix.jl +++ b/src/Matrix.jl @@ -567,6 +567,7 @@ end Base.IteratorSize(::Type{<:MatrixElem}) = Base.HasShape{2}() +Base.keys(M::MatElem) = CartesianIndices(axes(M)) Base.pairs(M::MatElem) = Base.pairs(IndexCartesian(), M) Base.pairs(::IndexCartesian, M::MatElem) = Base.Iterators.Pairs(M, CartesianIndices(axes(M))) diff --git a/test/Matrix-test.jl b/test/Matrix-test.jl index e9d24f536b..03077145c4 100644 --- a/test/Matrix-test.jl +++ b/test/Matrix-test.jl @@ -132,6 +132,21 @@ end @test Matrix(Sa) == a end +@testset "Matrix.keys and pairs" begin + a = matrix(ZZ, 2, 3, [6, 3, 0, 10, 12, 14]) + @test keys(a) == CartesianIndices((2, 3)) + @test issetequal( + keys(a), + [CartesianIndex(1, 1), CartesianIndex(1, 2), CartesianIndex(1, 3), + CartesianIndex(2, 1), CartesianIndex(2, 2), CartesianIndex(2, 3)], + ) + @test issetequal( + pairs(a), + [CartesianIndex(1, 1) => 6, CartesianIndex(1, 2) => 3, CartesianIndex(1, 3) => 0, + CartesianIndex(2, 1) => 10, CartesianIndex(2, 2) => 12, CartesianIndex(2, 3) => 14], + ) +end + @testset "Strassen" begin S = matrix(QQ, rand(-10:10, 100, 100)) T = S*S