diff --git a/docs/src/index.md b/docs/src/index.md index 362f4711..c18b2024 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -604,6 +604,7 @@ LinearAlgebra.hermitianpart LinearAlgebra.hermitianpart! LinearAlgebra.copy_adjoint! LinearAlgebra.copy_transpose! +LinearAlgebra.uplo ``` ## Low-level matrix operations diff --git a/src/LinearAlgebra.jl b/src/LinearAlgebra.jl index d97c308b..661672cd 100644 --- a/src/LinearAlgebra.jl +++ b/src/LinearAlgebra.jl @@ -181,7 +181,8 @@ public AbstractTriangular, zeroslike, matprod_dest, fillstored!, - fillband! + fillband!, + uplo const BlasFloat = Union{Float64,Float32,ComplexF64,ComplexF32} const BlasReal = Union{Float64,Float32} diff --git a/src/bidiag.jl b/src/bidiag.jl index cc5e6de7..0d6e1c75 100644 --- a/src/bidiag.jl +++ b/src/bidiag.jl @@ -115,6 +115,13 @@ Bidiagonal(A::Bidiagonal) = A Bidiagonal{T}(A::Bidiagonal{T}) where {T} = A Bidiagonal{T}(A::Bidiagonal) where {T} = Bidiagonal{T}(A.dv, A.ev, A.uplo) +""" + LinearAlgebra.uplo(S::Bidiagonal)::Symbol + +Return a `Symbol` corresponding to whether the upper (`:U`) or lower (`:L`) off-diagonal band is stored. +""" +uplo(B::Bidiagonal) = sym_uplo(B.uplo) + _offdiagind(uplo) = uplo == 'U' ? 1 : -1 @inline function Base.isassigned(A::Bidiagonal, i::Int, j::Int) diff --git a/src/symmetric.jl b/src/symmetric.jl index 2cc74763..72adf41b 100644 --- a/src/symmetric.jl +++ b/src/symmetric.jl @@ -27,6 +27,10 @@ if any specialized algorithms.) To compute the symmetric part of a real matrix, or more generally the Hermitian part `(A + A') / 2` of a real or complex matrix `A`, use [`hermitianpart`](@ref). +The `uplo` symbol corresponding to the triangular half of `A` that is shared by the symmetric view may be +fetched by using the function [`LinearAlgebra.uplo`](@ref). The underlying matrix `A` may be fetched from the symmetric +view by using `parent`. + # Examples ```jldoctest julia> A = [1 2 3; 4 5 6; 7 8 9] @@ -112,6 +116,10 @@ triangle of the matrix `A`. To compute the Hermitian part of `A`, use [`hermitianpart`](@ref). +The `uplo` symbol corresponding to the triangular half of `A` that is shared by the hermitian view may be +fetched by using the function [`LinearAlgebra.uplo`](@ref). The underlying matrix `A` may be fetched from the hermitian +view by using `parent`. + # Examples ```jldoctest julia> A = [1 2+2im 3-3im; 4 5 6-6im; 7 8+8im 9] @@ -237,6 +245,33 @@ nonhermitianwrappertype(::SymSymTri{<:Real}) = Symmetric nonhermitianwrappertype(::Hermitian{<:Real}) = Symmetric nonhermitianwrappertype(::Hermitian) = identity +""" + LinearAlgebra.uplo(S::Union{Symmetric, Hermitian})::Symbol + +Return a `Symbol` corresponding to the stored triangular half (`:U` or `:L`) in the matrix `S`, +that is, the elements are common between `S` and `parent(S)` for that triangular half. + +# Example +```jldoctest +julia> S = Symmetric([1 2; 3 4], :U) +2×2 Symmetric{Int64, Matrix{Int64}}: + 1 2 + 2 4 + +julia> LinearAlgebra.uplo(S) +:U + +julia> H = Hermitian([1 2; 3 4], :L) +2×2 Hermitian{Int64, Matrix{Int64}}: + 1 3 + 3 4 + +julia> LinearAlgebra.uplo(H) +:L +``` +""" +uplo(S::HermOrSym) = sym_uplo(S.uplo) + size(A::HermOrSym) = size(A.data) axes(A::HermOrSym) = axes(A.data) @inline function Base.isassigned(A::HermOrSym, i::Int, j::Int) diff --git a/test/bidiag.jl b/test/bidiag.jl index 94a377f6..08581d39 100644 --- a/test/bidiag.jl +++ b/test/bidiag.jl @@ -47,6 +47,8 @@ Random.seed!(1) # from vectors ubd = Bidiagonal(x, y, :U) lbd = Bidiagonal(x, y, :L) + @test LinearAlgebra.uplo(ubd) == :U + @test LinearAlgebra.uplo(lbd) == :L @test ubd != lbd || x === dv0 @test ubd.dv === x @test lbd.ev === y diff --git a/test/symmetric.jl b/test/symmetric.jl index 707b392d..45125591 100644 --- a/test/symmetric.jl +++ b/test/symmetric.jl @@ -1343,4 +1343,11 @@ end @test_throws msg LinearAlgebra.fillband!(Symmetric(A), 2, 0, 1) end +@testset "uplo" begin + S = Symmetric([1 2; 3 4], :U) + @test LinearAlgebra.uplo(S) == :U + H = Hermitian([1 2; 3 4], :L) + @test LinearAlgebra.uplo(H) == :L +end + end # module TestSymmetric