Skip to content

Commit 31c09e9

Browse files
authored
improved definitions of ishermitian/issymmetric (#1266)
* improved definitions of ishermitian/issymmetric * better explanation of implementation in documentation
1 parent cc1ed9c commit 31c09e9

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/SHermitianCompact.jl

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""
22
SHermitianCompact{N, T, L} <: StaticMatrix{N, N, T}
33
4-
A [`StaticArray`](@ref) subtype that represents a Hermitian matrix. Unlike
4+
A [`StaticArray`](@ref) subtype that can represent a Hermitian matrix. Unlike
55
`LinearAlgebra.Hermitian`, `SHermitianCompact` stores only the lower triangle
6-
of the matrix (as an `SVector`). The lower triangle is stored in column-major order.
6+
of the matrix (as an `SVector`), and the diagonal may not be real. The lower
7+
triangle is stored in column-major order and the superdiagonal entries are
8+
`adjoint` to the transposed subdiagonal entries. The diagonal is returned as-is.
79
For example, for an `SHermitianCompact{3}`, the indices of the stored elements
810
can be visualized as follows:
911
@@ -28,6 +30,13 @@ An `SHermitianCompact` may be constructed either:
2830
2931
For the latter two cases, only the lower triangular elements are used; the upper triangular
3032
elements are ignored.
33+
34+
When its element type is real, then a `SHermitianCompact` is both Hermitian and
35+
symmetric. Otherwise, to ensure that a `SHermitianCompact` matrix, `a`, is
36+
Hermitian according to `LinearAlgebra.ishermitian`, take an average with its
37+
adjoint, i.e. `(a+a')/2`, or take a Hermitian view of the data with
38+
`LinearAlgebra.Hermitian(a)`. However, the latter case is not specialized to use
39+
the compact storage.
3140
"""
3241
struct SHermitianCompact{N, T, L} <: StaticMatrix{N, N, T}
3342
lowertriangle::SVector{L, T}
@@ -129,8 +138,10 @@ end
129138
end
130139
end
131140

132-
LinearAlgebra.ishermitian(a::SHermitianCompact) = true
133-
LinearAlgebra.issymmetric(a::SHermitianCompact) = true
141+
LinearAlgebra.ishermitian(a::SHermitianCompact{<:Any,<:Real}) = true
142+
LinearAlgebra.ishermitian(a::SHermitianCompact) = a == a'
143+
LinearAlgebra.issymmetric(a::SHermitianCompact{<:Any,<:Real}) = true
144+
LinearAlgebra.issymmetric(a::SHermitianCompact) = a == transpose(a)
134145

135146
# TODO: factorize?
136147

test/SHermitianCompact.jl

+10-2
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,21 @@ fill3(x) = fill(3, x)
139139
@test issymmetric(a)
140140

141141
b = rand(SHermitianCompact{5, ComplexF64})
142-
@test ishermitian(b)
143-
@test issymmetric(b)
142+
@test !ishermitian(b)
143+
@test !issymmetric(b)
144144

145145
c = b + conj(b)
146146
@test ishermitian(c)
147147
@test issymmetric(c)
148148
@test_noalloc ishermitian(c)
149+
150+
d = b + b'
151+
@test ishermitian(d)
152+
@test !issymmetric(d)
153+
154+
e = rand(SHermitianCompact{5, Float64}) + im*I
155+
@test !ishermitian(e)
156+
@test issymmetric(e)
149157
end
150158

151159
@testset "==" begin

0 commit comments

Comments
 (0)