From aef52636f3a3f5b93568ef1afaeb13a1de80ef36 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sun, 22 Dec 2024 18:01:03 +0100 Subject: [PATCH] Add conformance tests for is_unit & is_nilpotent Also improve is_nilpotent for matrices / matrix ring elements to work if the base ring is trivial. --- src/Matrix.jl | 4 +-- test/Rings-conformance-tests.jl | 43 ++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/Matrix.jl b/src/Matrix.jl index 51f1d885b..cc224a5df 100644 --- a/src/Matrix.jl +++ b/src/Matrix.jl @@ -3876,13 +3876,13 @@ Return if `A` is nilpotent, i.e. if there exists a natural number $k$ such that $A^k = 0$. If `A` is not square an exception is raised. """ function is_nilpotent(A::MatrixElem{T}) where {T <: RingElement} - is_domain_type(T) || error("Only supported over integral domains") !is_square(A) && error("Dimensions don't match in is_nilpotent") + is_zero(A) && return true + is_domain_type(T) || error("Only supported over integral domains") is_zero(tr(A)) || return false n = nrows(A) A = deepcopy(A) i = 1 - is_zero(A) && return true while i < n i *= 2 A = mul!(A, A, A) diff --git a/test/Rings-conformance-tests.jl b/test/Rings-conformance-tests.jl index 3e45769ee..0088b0449 100644 --- a/test/Rings-conformance-tests.jl +++ b/test/Rings-conformance-tests.jl @@ -315,14 +315,49 @@ function test_NCRing_interface(R::AbstractAlgebra.NCRing; reps = 50) end end - @testset "Basic functions" begin + @testset "Basic properties" begin @test iszero(R()) # R() is supposed to construct 0 ? @test iszero(zero(R)) @test isone(one(R)) @test iszero(R(0)) @test isone(R(1)) - @test isone(R(0)) || !is_unit(R(0)) + @test is_unit(R(1)) + if is_trivial(R) + @test isone(R(0)) + @test iszero(R(1)) + @test R(0) == R(1) + else + @test !is_unit(R(0)) + for i in 1:reps + a = test_elem(R)::T + @test is_unit(a) == is_unit(a^2) + end + end + + # test is_nilpotent if it is supported + try + f = is_nilpotent(R(1)) + @test is_nilpotent(R(0)) + if is_trivial(R) + @test is_nilpotent(R(1)) + else + @test !is_unit(R(0)) + @test !is_nilpotent(R(1)) + for i in 1:reps + a = test_elem(R)::T + @test !(is_unit(a) && is_nilpotent(a)) + @test is_nilpotent(a) == is_nilpotent(a^2) + if is_domain_type(typeof(a)) + @test is_nilpotent(a) == is_zero(a) + end + end + end + catch + end + end + + @testset "hash, deepcopy, equality, printing, parent" begin for i in 1:reps a = test_elem(R)::T @test hash(a) isa UInt @@ -330,11 +365,13 @@ function test_NCRing_interface(R::AbstractAlgebra.NCRing; reps = 50) @test !ismutable(a) || a !== A @test equality(a, A) @test hash(a) == hash(A) - @test parent(a) === parent(A) + @test parent(a) === R @test sprint(show, "text/plain", a) isa String end @test sprint(show, "text/plain", R) isa String + end + @testset "Basic arithmetic" begin for i in 1:reps a = test_elem(R)::T b = test_elem(R)::T