diff --git a/docs/src/matrix.md b/docs/src/matrix.md index 643d8b7d50..31498dff48 100644 --- a/docs/src/matrix.md +++ b/docs/src/matrix.md @@ -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 diff --git a/src/AbstractAlgebra.jl b/src/AbstractAlgebra.jl index 085025f93c..c5de77faeb 100644 --- a/src/AbstractAlgebra.jl +++ b/src/AbstractAlgebra.jl @@ -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! @@ -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 ############################################################################### diff --git a/src/Matrix.jl b/src/Matrix.jl index 7541cafb58..05a0bf8961 100644 --- a/src/Matrix.jl +++ b/src/Matrix.jl @@ -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} @@ -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 diff --git a/src/NemoStuff.jl b/src/NemoStuff.jl index 3d6407384a..9cee15ac19 100644 --- a/src/NemoStuff.jl +++ b/src/NemoStuff.jl @@ -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) diff --git a/test/AbstractAlgebra-test.jl b/test/AbstractAlgebra-test.jl index 09e3d0d8d7..f6ad5ff957 100644 --- a/test/AbstractAlgebra-test.jl +++ b/test/AbstractAlgebra-test.jl @@ -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") diff --git a/test/Matrix-test.jl b/test/Matrix-test.jl new file mode 100644 index 0000000000..e5e6b131e9 --- /dev/null +++ b/test/Matrix-test.jl @@ -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