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_lower_triangular, tweak is_upper_triangular #1430

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -3350,31 +3350,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 @@ -3420,6 +3413,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 @@ -727,18 +727,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
Loading