diff --git a/src/Matrix.jl b/src/Matrix.jl index b52390798..08f4eb49e 100644 --- a/src/Matrix.jl +++ b/src/Matrix.jl @@ -3700,8 +3700,9 @@ such that $A^k = 0$. If `A` is not square an exception is raised. """ function is_nilpotent(A::MatrixElem{T}) where {T <: RingElement} !is_square(A) && error("Dimensions don't match in is_nilpotent") - A = deepcopy(A) + is_zero(tr(A)) || return false n = nrows(A) + A = deepcopy(A) i = 1 is_zero(A) && return true while i < n diff --git a/test/Matrix-test.jl b/test/Matrix-test.jl index 5f5776699..dd206c611 100644 --- a/test/Matrix-test.jl +++ b/test/Matrix-test.jl @@ -66,8 +66,9 @@ end @testset "Matrix.is_nilpotent" begin @test is_nilpotent(zero_matrix(QQ, 3, 3)) - @test !is_nilpotent(identity_matrix(QQ, 3)) @test is_nilpotent(upper_triangular_matrix(QQ.([0,1,1,0,1,0]))) + @test !is_nilpotent(identity_matrix(QQ, 3)) + @test !is_nilpotent(diagonal_matrix(QQ, [1,-1,0])) end @testset "Matrix.concat" begin