Skip to content

Commit aef5263

Browse files
committed
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.
1 parent e5916d3 commit aef5263

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/Matrix.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3876,13 +3876,13 @@ Return if `A` is nilpotent, i.e. if there exists a natural number $k$
38763876
such that $A^k = 0$. If `A` is not square an exception is raised.
38773877
"""
38783878
function is_nilpotent(A::MatrixElem{T}) where {T <: RingElement}
3879-
is_domain_type(T) || error("Only supported over integral domains")
38803879
!is_square(A) && error("Dimensions don't match in is_nilpotent")
3880+
is_zero(A) && return true
3881+
is_domain_type(T) || error("Only supported over integral domains")
38813882
is_zero(tr(A)) || return false
38823883
n = nrows(A)
38833884
A = deepcopy(A)
38843885
i = 1
3885-
is_zero(A) && return true
38863886
while i < n
38873887
i *= 2
38883888
A = mul!(A, A, A)

test/Rings-conformance-tests.jl

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,26 +315,63 @@ function test_NCRing_interface(R::AbstractAlgebra.NCRing; reps = 50)
315315
end
316316
end
317317

318-
@testset "Basic functions" begin
318+
@testset "Basic properties" begin
319319
@test iszero(R()) # R() is supposed to construct 0 ?
320320
@test iszero(zero(R))
321321
@test isone(one(R))
322322
@test iszero(R(0))
323323
@test isone(R(1))
324-
@test isone(R(0)) || !is_unit(R(0))
324+
325325
@test is_unit(R(1))
326+
if is_trivial(R)
327+
@test isone(R(0))
328+
@test iszero(R(1))
329+
@test R(0) == R(1)
330+
else
331+
@test !is_unit(R(0))
332+
for i in 1:reps
333+
a = test_elem(R)::T
334+
@test is_unit(a) == is_unit(a^2)
335+
end
336+
end
337+
338+
# test is_nilpotent if it is supported
339+
try
340+
f = is_nilpotent(R(1))
341+
@test is_nilpotent(R(0))
342+
if is_trivial(R)
343+
@test is_nilpotent(R(1))
344+
else
345+
@test !is_unit(R(0))
346+
@test !is_nilpotent(R(1))
347+
for i in 1:reps
348+
a = test_elem(R)::T
349+
@test !(is_unit(a) && is_nilpotent(a))
350+
@test is_nilpotent(a) == is_nilpotent(a^2)
351+
if is_domain_type(typeof(a))
352+
@test is_nilpotent(a) == is_zero(a)
353+
end
354+
end
355+
end
356+
catch
357+
end
358+
end
359+
360+
@testset "hash, deepcopy, equality, printing, parent" begin
326361
for i in 1:reps
327362
a = test_elem(R)::T
328363
@test hash(a) isa UInt
329364
A = deepcopy(a)
330365
@test !ismutable(a) || a !== A
331366
@test equality(a, A)
332367
@test hash(a) == hash(A)
333-
@test parent(a) === parent(A)
368+
@test parent(a) === R
334369
@test sprint(show, "text/plain", a) isa String
335370
end
336371
@test sprint(show, "text/plain", R) isa String
372+
end
337373

374+
@testset "Basic arithmetic" begin
338375
for i in 1:reps
339376
a = test_elem(R)::T
340377
b = test_elem(R)::T

0 commit comments

Comments
 (0)