Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_nilpotent(::MatrixElem) #1783

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "AbstractAlgebra"
uuid = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"
version = "0.42.4"
version = "0.42.5"

[deps]
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Expand Down
6 changes: 6 additions & 0 deletions docs/src/matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,12 @@ det{T <: RingElem}(::MatElem{T})
rank{T <: RingElem}(::MatElem{T})
```

### Nilpotency

```@docs
is_nilpotent(::MatrixElem{T}) where {T <: RingElement}
```

### Minors

```@docs
Expand Down
27 changes: 27 additions & 0 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3686,6 +3686,33 @@ function nullspace(M::MatElem{T}) where {T <: FieldElement}
return nullity, X
end

###############################################################################
#
# Nilpotency
#
###############################################################################

@doc raw"""
is_nilpotent(A::MatrixElem{T}) where {T <: RingElement}

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_square(A) && error("Dimensions don't match in is_nilpotent")
lgoettgens marked this conversation as resolved.
Show resolved Hide resolved
is_zero(tr(A)) || return false
n = nrows(A)
A = deepcopy(A)
i = 1
is_zero(A) && return true
while i < n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, is this algorithm even correct when A contains zero divisors? I normally think about matrices over integral domains, and then of course A is nilpotent iff A^n=0. But over a ring with zero-divisors, we could have a triangularizable matrix with nilpotent eigenvalues where $A^n\neq0$ but still $A^k=0$ for some $k&gt;n$.

Admittedly this is somewhat fringe, but we should still handle it right? For starters we could add is_domain_type(T) || error("Only supported over integral domains") ? (AFAIK there is no is_domain / is_integral_domain which for e.g. $\mathbb{Z}/n\mathbb{Z}$ takes the modulus $n$ into account)

Then if someone needs this over non-domains, they at least get a helpful error and can provide an implementation for their use case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks for mentioning this. I added the suggested error

i *= 2
A = mul!(A, A, A)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might or might not be faster to compute the minimal or characteristic polynomial (if there are dedicated optimized methods for this and the given matrix type

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that's true. but for that one would need to know which specialized methods exist in which cases, which I honestly don't

is_zero(A) && return true
end
return false
end

###############################################################################
#
# Hessenberg form
Expand Down
1 change: 1 addition & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ export is_monic
export is_monomial
export is_monomial_recursive
export is_negative
export is_nilpotent
export is_odd
export is_one
export is_perfect
Expand Down
7 changes: 7 additions & 0 deletions test/Matrix-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ end
@test is_upper_triangular(matrix(ZZ, A))
end

@testset "Matrix.is_nilpotent" begin
@test is_nilpotent(zero_matrix(QQ, 3, 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
for R in [ZZ, QQ, polynomial_ring(QQ, [:x, :y])[1]]
S = matrix_space(R, 3, 3)
Expand Down
Loading