Skip to content

Commit

Permalink
Add is_lower_triangular, tweak is_upper_triangular (#1430)
Browse files Browse the repository at this point in the history
The old is_upper_triangular did something strange; it reported
matrices as upper triangular that I would not consider to be, e.g.
[ 1 0 0 ; 0 0 1 ; 1 0 1 ]
  • Loading branch information
fingolfin authored Sep 13, 2023
1 parent 6769474 commit 63f812c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 31 deletions.
6 changes: 5 additions & 1 deletion docs/src/matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,11 @@ strictly_upper_triangular_matrix(L::AbstractVector{T}) where {T <: RingElement}
```

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

```@docs
is_upper_triangular(::MatrixElem)
```

```@docs
Expand Down
4 changes: 3 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: istril
import LinearAlgebra: istriu
import LinearAlgebra: lu
import LinearAlgebra: lu!
Expand Down Expand Up @@ -318,10 +319,11 @@ export is_trivial
@alias is_trivial istrivial

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

@alias is_hermitian ishermitian
@alias is_symmetric issymmetric
@alias is_lower_triangular istril
@alias is_upper_triangular istriu

###############################################################################
Expand Down
51 changes: 34 additions & 17 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3347,31 +3347,24 @@ end
###############################################################################

@doc raw"""
is_upper_triangular(A::MatrixElem{T}) where T <: RingElement
is_upper_triangular(A::MatrixElem)
Return `true` if $A$ is an upper triangular matrix.
Alias for `LinearAlgebra.istriu`.
"""
function is_upper_triangular(A::MatrixElem{T}) where T <: RingElement
m = nrows(A)
n = ncols(A)
d = 0
for c = 1:n
for r = m:-1:1
if !iszero(A[r, c])
if r < d
return false
function is_upper_triangular(M::MatrixElem)
m = ncols(M)
for i = 2:nrows(M)
for j = 1:min(i - 1, m)
if !iszero(M[i, j])
return false
end
d = r
break
end
end
end
return true
end
end
return true
end


@doc raw"""
solve_triu(U::MatElem{T}, b::MatElem{T}, unit::Bool = false) where {T <: FieldElement}
Expand Down Expand Up @@ -3417,6 +3410,30 @@ function solve_triu(U::MatElem{T}, b::MatElem{T}, unit::Bool = false) where {T <
return X
end

###############################################################################
#
#
#
###############################################################################

@doc raw"""
is_lower_triangular(A::MatrixElem)
Return `true` if $A$ is an lower triangular matrix.
Alias for `LinearAlgebra.istril`.
"""
function is_lower_triangular(M::MatrixElem)
for i = 1:nrows(M)
for j = i+1:ncols(M)
if !iszero(M[i, j])
return false
end
end
end
return true
end

###############################################################################
#
# Can solve
Expand Down
12 changes: 0 additions & 12 deletions src/NemoStuff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -651,18 +651,6 @@ Random.gentype(::Type{T}) where {T<:FinField} = elem_type(T)
import LinearAlgebra
LinearAlgebra.dot(a::NCRingElem, b::NCRingElem) = a * b

function is_upper_triangular(M::MatElem)
n = nrows(M)
for i = 2:n
for j = 1:min(i - 1, ncols(M))
if !iszero(M[i, j])
return false
end
end
end
return true
end

transpose!(A::MatrixElem) = transpose(A)

function Base.div(f::PolyRingElem, g::PolyRingElem)
Expand Down
1 change: 1 addition & 0 deletions test/AbstractAlgebra-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ include("NCRings-test.jl")
include("Rings-test.jl")
include("Fields-test.jl")
include("Ideals-test.jl")
include("Matrix-test.jl")
include("Modules-test.jl")
include("Maps-test.jl")
include("Benchmark-test.jl")
Expand Down
35 changes: 35 additions & 0 deletions test/Matrix-test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@testset "Matrix.is_lower_triangular" begin
A = [ 1 0 0 ; 0 0 1 ; 1 0 1 ]
@test !is_lower_triangular(A)
@test !is_lower_triangular(matrix(ZZ, A))

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

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

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

@testset "Matrix.is_upper_triangular" begin
A = [ 1 0 0 ; 0 0 1 ; 1 0 1 ]
@test !is_upper_triangular(A)
@test !is_upper_triangular(matrix(ZZ, A))

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

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

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

0 comments on commit 63f812c

Please sign in to comment.