Skip to content

Commit

Permalink
improved definitions of ishermitian/issymmetric (#1266)
Browse files Browse the repository at this point in the history
* improved definitions of ishermitian/issymmetric

* better explanation of implementation in documentation
  • Loading branch information
lxvm authored Jun 27, 2024
1 parent cc1ed9c commit 31c09e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/SHermitianCompact.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""
SHermitianCompact{N, T, L} <: StaticMatrix{N, N, T}
A [`StaticArray`](@ref) subtype that represents a Hermitian matrix. Unlike
A [`StaticArray`](@ref) subtype that can represent a Hermitian matrix. Unlike
`LinearAlgebra.Hermitian`, `SHermitianCompact` stores only the lower triangle
of the matrix (as an `SVector`). The lower triangle is stored in column-major order.
of the matrix (as an `SVector`), and the diagonal may not be real. The lower
triangle is stored in column-major order and the superdiagonal entries are
`adjoint` to the transposed subdiagonal entries. The diagonal is returned as-is.
For example, for an `SHermitianCompact{3}`, the indices of the stored elements
can be visualized as follows:
Expand All @@ -28,6 +30,13 @@ An `SHermitianCompact` may be constructed either:
For the latter two cases, only the lower triangular elements are used; the upper triangular
elements are ignored.
When its element type is real, then a `SHermitianCompact` is both Hermitian and
symmetric. Otherwise, to ensure that a `SHermitianCompact` matrix, `a`, is
Hermitian according to `LinearAlgebra.ishermitian`, take an average with its
adjoint, i.e. `(a+a')/2`, or take a Hermitian view of the data with
`LinearAlgebra.Hermitian(a)`. However, the latter case is not specialized to use
the compact storage.
"""
struct SHermitianCompact{N, T, L} <: StaticMatrix{N, N, T}
lowertriangle::SVector{L, T}
Expand Down Expand Up @@ -129,8 +138,10 @@ end
end
end

LinearAlgebra.ishermitian(a::SHermitianCompact) = true
LinearAlgebra.issymmetric(a::SHermitianCompact) = true
LinearAlgebra.ishermitian(a::SHermitianCompact{<:Any,<:Real}) = true
LinearAlgebra.ishermitian(a::SHermitianCompact) = a == a'
LinearAlgebra.issymmetric(a::SHermitianCompact{<:Any,<:Real}) = true
LinearAlgebra.issymmetric(a::SHermitianCompact) = a == transpose(a)

# TODO: factorize?

Expand Down
12 changes: 10 additions & 2 deletions test/SHermitianCompact.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,21 @@ fill3(x) = fill(3, x)
@test issymmetric(a)

b = rand(SHermitianCompact{5, ComplexF64})
@test ishermitian(b)
@test issymmetric(b)
@test !ishermitian(b)
@test !issymmetric(b)

c = b + conj(b)
@test ishermitian(c)
@test issymmetric(c)
@test_noalloc ishermitian(c)

d = b + b'
@test ishermitian(d)
@test !issymmetric(d)

e = rand(SHermitianCompact{5, Float64}) + im*I
@test !ishermitian(e)
@test issymmetric(e)
end

@testset "==" begin
Expand Down

2 comments on commit 31c09e9

@mateuszbaran
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/109942

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.9.6 -m "<description of version>" 31c09e92012f680a005c84c80cf17fe15b427722
git push origin v1.9.6

Please sign in to comment.