Skip to content

Commit 8d6ca14

Browse files
jishnubtpapp
andauthored
Public function to access the uplo for Symmetric/Hermitian (#1440)
This lets one access the `uplo` field of a `Symmetric`/`Hermitian` matrix without accessing the internal fields of the struct. --------- Co-authored-by: Tamas K. Papp <[email protected]>
1 parent 7a4b27e commit 8d6ca14

File tree

6 files changed

+54
-1
lines changed

6 files changed

+54
-1
lines changed

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ LinearAlgebra.hermitianpart
604604
LinearAlgebra.hermitianpart!
605605
LinearAlgebra.copy_adjoint!
606606
LinearAlgebra.copy_transpose!
607+
LinearAlgebra.uplo
607608
```
608609

609610
## Low-level matrix operations

src/LinearAlgebra.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ public AbstractTriangular,
181181
zeroslike,
182182
matprod_dest,
183183
fillstored!,
184-
fillband!
184+
fillband!,
185+
uplo
185186

186187
const BlasFloat = Union{Float64,Float32,ComplexF64,ComplexF32}
187188
const BlasReal = Union{Float64,Float32}

src/bidiag.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ Bidiagonal(A::Bidiagonal) = A
115115
Bidiagonal{T}(A::Bidiagonal{T}) where {T} = A
116116
Bidiagonal{T}(A::Bidiagonal) where {T} = Bidiagonal{T}(A.dv, A.ev, A.uplo)
117117

118+
"""
119+
LinearAlgebra.uplo(S::Bidiagonal)::Symbol
120+
121+
Return a `Symbol` corresponding to whether the upper (`:U`) or lower (`:L`) off-diagonal band is stored.
122+
"""
123+
uplo(B::Bidiagonal) = sym_uplo(B.uplo)
124+
118125
_offdiagind(uplo) = uplo == 'U' ? 1 : -1
119126

120127
@inline function Base.isassigned(A::Bidiagonal, i::Int, j::Int)

src/symmetric.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ if any specialized algorithms.)
2727
To compute the symmetric part of a real matrix, or more generally the Hermitian part `(A + A') / 2` of
2828
a real or complex matrix `A`, use [`hermitianpart`](@ref).
2929
30+
The `uplo` symbol corresponding to the triangular half of `A` that is shared by the symmetric view may be
31+
fetched by using the function [`LinearAlgebra.uplo`](@ref). The underlying matrix `A` may be fetched from the symmetric
32+
view by using `parent`.
33+
3034
# Examples
3135
```jldoctest
3236
julia> A = [1 2 3; 4 5 6; 7 8 9]
@@ -112,6 +116,10 @@ triangle of the matrix `A`.
112116
113117
To compute the Hermitian part of `A`, use [`hermitianpart`](@ref).
114118
119+
The `uplo` symbol corresponding to the triangular half of `A` that is shared by the hermitian view may be
120+
fetched by using the function [`LinearAlgebra.uplo`](@ref). The underlying matrix `A` may be fetched from the hermitian
121+
view by using `parent`.
122+
115123
# Examples
116124
```jldoctest
117125
julia> A = [1 2+2im 3-3im; 4 5 6-6im; 7 8+8im 9]
@@ -237,6 +245,33 @@ nonhermitianwrappertype(::SymSymTri{<:Real}) = Symmetric
237245
nonhermitianwrappertype(::Hermitian{<:Real}) = Symmetric
238246
nonhermitianwrappertype(::Hermitian) = identity
239247

248+
"""
249+
LinearAlgebra.uplo(S::Union{Symmetric, Hermitian})::Symbol
250+
251+
Return a `Symbol` corresponding to the stored triangular half (`:U` or `:L`) in the matrix `S`,
252+
that is, the elements are common between `S` and `parent(S)` for that triangular half.
253+
254+
# Example
255+
```jldoctest
256+
julia> S = Symmetric([1 2; 3 4], :U)
257+
2×2 Symmetric{Int64, Matrix{Int64}}:
258+
1 2
259+
2 4
260+
261+
julia> LinearAlgebra.uplo(S)
262+
:U
263+
264+
julia> H = Hermitian([1 2; 3 4], :L)
265+
2×2 Hermitian{Int64, Matrix{Int64}}:
266+
1 3
267+
3 4
268+
269+
julia> LinearAlgebra.uplo(H)
270+
:L
271+
```
272+
"""
273+
uplo(S::HermOrSym) = sym_uplo(S.uplo)
274+
240275
size(A::HermOrSym) = size(A.data)
241276
axes(A::HermOrSym) = axes(A.data)
242277
@inline function Base.isassigned(A::HermOrSym, i::Int, j::Int)

test/bidiag.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Random.seed!(1)
4747
# from vectors
4848
ubd = Bidiagonal(x, y, :U)
4949
lbd = Bidiagonal(x, y, :L)
50+
@test LinearAlgebra.uplo(ubd) == :U
51+
@test LinearAlgebra.uplo(lbd) == :L
5052
@test ubd != lbd || x === dv0
5153
@test ubd.dv === x
5254
@test lbd.ev === y

test/symmetric.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,4 +1343,11 @@ end
13431343
@test_throws msg LinearAlgebra.fillband!(Symmetric(A), 2, 0, 1)
13441344
end
13451345

1346+
@testset "uplo" begin
1347+
S = Symmetric([1 2; 3 4], :U)
1348+
@test LinearAlgebra.uplo(S) == :U
1349+
H = Hermitian([1 2; 3 4], :L)
1350+
@test LinearAlgebra.uplo(H) == :L
1351+
end
1352+
13461353
end # module TestSymmetric

0 commit comments

Comments
 (0)