Skip to content

Commit

Permalink
Add conformance tests for is_unit & is_nilpotent
Browse files Browse the repository at this point in the history
Also improve is_nilpotent for matrices / matrix ring elements
to work if the base ring is trivial.
  • Loading branch information
fingolfin committed Dec 22, 2024
1 parent e5916d3 commit aef5263
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 40 additions & 3 deletions test/Rings-conformance-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,26 +315,63 @@ 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
A = deepcopy(a)
@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
Expand Down

0 comments on commit aef5263

Please sign in to comment.