Skip to content

Commit

Permalink
Add is_diagonal(A::MatrixElem) (#1472)
Browse files Browse the repository at this point in the history
... as alias for LinearAlgebra.isdiag -- and replacement
for the Nemo method.
  • Loading branch information
fingolfin authored Oct 27, 2023
1 parent dd13b88 commit 1885232
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/src/matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ is_lower_triangular(::MatrixElem)
is_upper_triangular(::MatrixElem)
```

```@docs
is_diagonal(::MatrixElem)
```

```@docs
change_base_ring(::Ring, ::MatElem{T}) where T <: RingElement
```
Expand Down
5 changes: 4 additions & 1 deletion src/AbstractAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import LinearAlgebra: det
import LinearAlgebra: hessenberg
import LinearAlgebra: ishermitian
import LinearAlgebra: issymmetric
import LinearAlgebra: isdiag
import LinearAlgebra: istril
import LinearAlgebra: istriu
import LinearAlgebra: lu
Expand Down Expand Up @@ -319,8 +320,9 @@ export is_trivial
@alias is_trivial istrivial

# alternative names for some functions from LinearAlgebra
export is_hermitian, is_symmetric, is_upper_triangular, is_lower_triangular
export is_diagonal, is_hermitian, is_symmetric, is_upper_triangular, is_lower_triangular

@alias is_diagonal isdiag
@alias is_hermitian ishermitian
@alias is_symmetric issymmetric
@alias is_lower_triangular istril
Expand Down Expand Up @@ -909,6 +911,7 @@ export invmod
export is_compatible
export is_constant
export is_degree
export is_diagonal
export is_divisible_by
export is_domain_type
export is_exact_type
Expand Down
18 changes: 18 additions & 0 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3434,6 +3434,24 @@ function is_lower_triangular(M::MatrixElem)
return true
end

@doc raw"""
is_diagonal(A::MatrixElem)
Return `true` if $A$ is a diagonal matrix.
Alias for `LinearAlgebra.isdiag`.
"""
function is_diagonal(A::MatrixElem)
for i = 1:ncols(A)
for j = 1:nrows(A)
if i != j && !is_zero_entry(A, j, i)
return false
end
end
end
return true
end

###############################################################################
#
# Can solve
Expand Down
30 changes: 30 additions & 0 deletions test/Matrix-test.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
@testset "Matrix.is_diagonal" begin
A = [1 0 0; 0 0 1; 1 0 1]
@test !is_diagonal(A)
@test !is_diagonal(matrix(ZZ, A))

A = [1 0 0; 0 0 1; 0 0 1]
@test !is_diagonal(A)
@test !is_diagonal(matrix(ZZ, A))

A = [1 0 0; 0 0 1]
@test !is_diagonal(A)
@test !is_diagonal(matrix(ZZ, A))

A = [1 0 0; 0 1 0]
@test is_diagonal(A)
@test is_diagonal(matrix(ZZ, A))

A = [1 0 0; 0 2 0; 0 0 3]
@test is_diagonal(A)
@test is_diagonal(matrix(ZZ, A))
end

@testset "Matrix.is_lower_triangular" begin
A = [1 0 0; 0 0 1; 1 0 1]
@test !is_lower_triangular(A)
Expand All @@ -14,6 +36,10 @@
A = [1 0 0; 0 1 0]
@test is_lower_triangular(A)
@test is_lower_triangular(matrix(ZZ, A))

A = [1 0 0; 0 2 0; 0 0 3]
@test is_lower_triangular(A)
@test is_lower_triangular(matrix(ZZ, A))
end

@testset "Matrix.is_upper_triangular" begin
Expand All @@ -32,6 +58,10 @@ end
A = [1 0 0; 0 1 0]
@test is_upper_triangular(A)
@test is_upper_triangular(matrix(ZZ, A))

A = [1 0 0; 0 2 0; 0 0 3]
@test is_upper_triangular(A)
@test is_upper_triangular(matrix(ZZ, A))
end

@testset "Matrix.concat" begin
Expand Down

0 comments on commit 1885232

Please sign in to comment.