|
| 1 | +""" |
| 2 | + SHermitianCompact{N, T, L} <: StaticMatrix{N, N, T} |
| 3 | +
|
| 4 | +A [StaticArray](@ref) subtype that represents a Hermitian matrix. Unlike |
| 5 | +`LinearAlgebra.Hermitian`, `SHermitianCompact` stores only the lower triangle |
| 6 | +of the matrix (as an `SVector`). The lower triangle is stored in column-major order. |
| 7 | +For example, for an `SHermitianCompact{3}`, the indices of the stored elements |
| 8 | +can be visualized as follows: |
| 9 | +
|
| 10 | +``` |
| 11 | +┌ 1 ⋅ ⋅ ┐ |
| 12 | +| 2 4 ⋅ | |
| 13 | +└ 3 5 6 ┘ |
| 14 | +``` |
| 15 | +
|
| 16 | +Type parameters: |
| 17 | +* `N`: matrix dimension; |
| 18 | +* `T`: element type for lower triangle; |
| 19 | +* `L`: length of the `SVector` storing the lower triangular elements. |
| 20 | +
|
| 21 | +Note that `L` is always the `N`th [triangular number](https://en.wikipedia.org/wiki/Triangular_number). |
| 22 | +
|
| 23 | +An `SHermitianCompact` may be constructed either: |
| 24 | +
|
| 25 | +* from an `AbstractVector` containing the lower triangular elements; or |
| 26 | +* from a `Tuple` containing both upper and lower triangular elements in column major order; or |
| 27 | +* from another `StaticMatrix`. |
| 28 | +
|
| 29 | +For the latter two cases, only the lower triangular elements are used; the upper triangular |
| 30 | +elements are ignored. |
| 31 | +""" |
| 32 | +struct SHermitianCompact{N, T, L} <: StaticMatrix{N, N, T} |
| 33 | + lowertriangle::SVector{L, T} |
| 34 | + |
| 35 | + @inline function SHermitianCompact{N, T, L}(lowertriangle::SVector{L}) where {N, T, L} |
| 36 | + _check_hermitian_parameters(Val(N), Val(L)) |
| 37 | + new{N, T, L}(lowertriangle) |
| 38 | + end |
| 39 | +end |
| 40 | + |
| 41 | +@inline function _check_hermitian_parameters(::Val{N}, ::Val{L}) where {N, L} |
| 42 | + if 2 * L !== N * (N + 1) |
| 43 | + throw(ArgumentError("Size mismatch in SHermitianCompact parameters. Got dimension $N and length $L.")) |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | +Base.@pure triangularnumber(N::Int) = div(N * (N + 1), 2) |
| 48 | +Base.@pure triangularroot(L::Int) = div(isqrt(8 * L + 1) - 1, 2) # from quadratic formula |
| 49 | + |
| 50 | +lowertriangletype(::Type{SHermitianCompact{N, T, L}}) where {N, T, L} = SVector{L, T} |
| 51 | +lowertriangletype(::Type{SHermitianCompact{N, T}}) where {N, T} = SVector{triangularnumber(N), T} |
| 52 | +lowertriangletype(::Type{SHermitianCompact{N}}) where {N} = SVector{triangularnumber(N)} |
| 53 | + |
| 54 | +@inline (::Type{SHermitianCompact{N, T}})(lowertriangle::SVector{L}) where {N, T, L} = SHermitianCompact{N, T, L}(lowertriangle) |
| 55 | +@inline (::Type{SHermitianCompact{N}})(lowertriangle::SVector{L, T}) where {N, T, L} = SHermitianCompact{N, T, L}(lowertriangle) |
| 56 | + |
| 57 | +@inline function SHermitianCompact(lowertriangle::SVector{L, T}) where {T, L} |
| 58 | + N = triangularroot(L) |
| 59 | + SHermitianCompact{N, T, L}(lowertriangle) |
| 60 | +end |
| 61 | + |
| 62 | +@generated function (::Type{SHermitianCompact{N, T, L}})(a::Tuple) where {N, T, L} |
| 63 | + expr = Vector{Expr}(undef, L) |
| 64 | + i = 0 |
| 65 | + for col = 1 : N, row = col : N |
| 66 | + index = N * (col - 1) + row |
| 67 | + expr[i += 1] = :(a[$index]) |
| 68 | + end |
| 69 | + quote |
| 70 | + @_inline_meta |
| 71 | + @inbounds return SHermitianCompact{N, T, L}(SVector{L, T}(tuple($(expr...)))) |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | +@inline function (::Type{SHermitianCompact{N, T}})(a::Tuple) where {N, T} |
| 76 | + L = triangularnumber(N) |
| 77 | + SHermitianCompact{N, T, L}(a) |
| 78 | +end |
| 79 | + |
| 80 | +@inline (::Type{SHermitianCompact{N}})(a::Tuple) where {N} = SHermitianCompact{N, promote_tuple_eltype(a)}(a) |
| 81 | +@inline (::Type{SHermitianCompact{N}})(a::NTuple{M, T}) where {N, T, M} = SHermitianCompact{N, T}(a) |
| 82 | +@inline SHermitianCompact(a::StaticMatrix{N, N, T}) where {N, T} = SHermitianCompact{N, T}(a) |
| 83 | + |
| 84 | +@inline (::Type{SSC})(a::SHermitianCompact) where {SSC <: SHermitianCompact} = SSC(a.lowertriangle) |
| 85 | +@inline (::Type{SSC})(a::SSC) where {SSC <: SHermitianCompact} = a |
| 86 | + |
| 87 | +@inline (::Type{SSC})(a::AbstractVector) where {SSC <: SHermitianCompact} = SSC(convert(lowertriangletype(SSC), a)) |
| 88 | + |
| 89 | +@generated function _hermitian_compact_indices(::Val{N}) where N |
| 90 | + # Returns a Tuple{Pair{Int, Bool}} I such that for linear index i, |
| 91 | + # * I[i][1] is the index into the lowertriangle field of an SHermitianCompact{N}; |
| 92 | + # * I[i][2] is true iff i is an index into the lower triangle of an N × N matrix. |
| 93 | + indexmat = Matrix{Pair{Int, Bool}}(undef, N, N) |
| 94 | + i = 0 |
| 95 | + for col = 1 : N, row = 1 : N |
| 96 | + indexmat[row, col] = if row >= col |
| 97 | + (i += 1) => true |
| 98 | + else |
| 99 | + indexmat[col, row][1] => false |
| 100 | + end |
| 101 | + end |
| 102 | + quote |
| 103 | + Base.@_inline_meta |
| 104 | + return $(tuple(indexmat...)) |
| 105 | + end |
| 106 | +end |
| 107 | + |
| 108 | +Base.@propagate_inbounds function Base.getindex(a::SHermitianCompact{N}, i::Int) where {N} |
| 109 | + I = _hermitian_compact_indices(Val(N)) |
| 110 | + j, lower = I[i] |
| 111 | + @inbounds value = a.lowertriangle[j] |
| 112 | + return lower ? value : adjoint(value) |
| 113 | +end |
| 114 | + |
| 115 | +Base.@propagate_inbounds function Base.setindex(a::SHermitianCompact{N, T, L}, x, i::Int) where {N, T, L} |
| 116 | + I = _hermitian_compact_indices(Val(N)) |
| 117 | + j, lower = I[i] |
| 118 | + value = lower ? x : adjoint(x) |
| 119 | + return SHermitianCompact{N}(setindex(a.lowertriangle, value, j)) |
| 120 | +end |
| 121 | + |
| 122 | +# needed because it is used in convert.jl and the generic fallback is slow |
| 123 | +@generated function Base.Tuple(a::SHermitianCompact{N}) where N |
| 124 | + exprs = [:(a[$i]) for i = 1 : N^2] |
| 125 | + quote |
| 126 | + @_inline_meta |
| 127 | + tuple($(exprs...)) |
| 128 | + end |
| 129 | +end |
| 130 | + |
| 131 | +LinearAlgebra.ishermitian(a::SHermitianCompact) = true |
| 132 | +LinearAlgebra.issymmetric(a::SHermitianCompact) = true |
| 133 | + |
| 134 | +# TODO: factorize? |
| 135 | + |
| 136 | +@inline Base.:(==)(a::SHermitianCompact, b::SHermitianCompact) = a.lowertriangle == b.lowertriangle |
| 137 | +@generated function _map(f, a::SHermitianCompact...) |
| 138 | + S = Size(a[1]) |
| 139 | + N = S[1] |
| 140 | + L = triangularnumber(N) |
| 141 | + exprs = Vector{Expr}(undef, L) |
| 142 | + for i ∈ 1:L |
| 143 | + tmp = [:(a[$j].lowertriangle[$i]) for j ∈ 1:length(a)] |
| 144 | + exprs[i] = :(f($(tmp...))) |
| 145 | + end |
| 146 | + return quote |
| 147 | + @_inline_meta |
| 148 | + same_size(a...) |
| 149 | + @inbounds return SHermitianCompact(SVector(tuple($(exprs...)))) |
| 150 | + end |
| 151 | +end |
| 152 | + |
| 153 | +# Scalar-array. TODO: overload broadcast instead, once API has stabilized a bit |
| 154 | +@inline Base.:+(a::Number, b::SHermitianCompact) = SHermitianCompact(a + b.lowertriangle) |
| 155 | +@inline Base.:+(a::SHermitianCompact, b::Number) = SHermitianCompact(a.lowertriangle + b) |
| 156 | + |
| 157 | +@inline Base.:-(a::Number, b::SHermitianCompact) = SHermitianCompact(a - b.lowertriangle) |
| 158 | +@inline Base.:-(a::SHermitianCompact, b::Number) = SHermitianCompact(a.lowertriangle - b) |
| 159 | + |
| 160 | +@inline Base.:*(a::Number, b::SHermitianCompact) = SHermitianCompact(a * b.lowertriangle) |
| 161 | +@inline Base.:*(a::SHermitianCompact, b::Number) = SHermitianCompact(a.lowertriangle * b) |
| 162 | + |
| 163 | +@inline Base.:/(a::SHermitianCompact, b::Number) = SHermitianCompact(a.lowertriangle / b) |
| 164 | +@inline Base.:\(a::Number, b::SHermitianCompact) = SHermitianCompact(a \ b.lowertriangle) |
| 165 | + |
| 166 | +@generated function _plus_uniform(::Size{S}, a::SHermitianCompact{N, T, L}, λ) where {S, N, T, L} |
| 167 | + @assert S[1] == N |
| 168 | + @assert S[2] == N |
| 169 | + exprs = Vector{Expr}(undef, L) |
| 170 | + i = 0 |
| 171 | + for col = 1 : N, row = col : N |
| 172 | + i += 1 |
| 173 | + exprs[i] = row == col ? :(a.lowertriangle[$i] + λ) : :(a.lowertriangle[$i]) |
| 174 | + end |
| 175 | + return quote |
| 176 | + @_inline_meta |
| 177 | + R = promote_type(eltype(a), typeof(λ)) |
| 178 | + SHermitianCompact{N, R, L}(SVector{L, R}(tuple($(exprs...)))) |
| 179 | + end |
| 180 | +end |
| 181 | + |
| 182 | +@generated function LinearAlgebra.transpose(a::SHermitianCompact{N, T, L}) where {N, T, L} |
| 183 | + # To conform with LinearAlgebra, the transpose should be recursive. |
| 184 | + # For this compact representation of a Hermitian matrix, that means that |
| 185 | + # we should recursively transpose of the diagonal elements, but only |
| 186 | + # conjugate the off-diagonal elements: |
| 187 | + # [A Bᴴ]ᵀ = [Aᵀ Bᵀ] = [Aᵀ Bᵀ] |
| 188 | + # [B C ] [Bᴴᵀ Cᵀ] [conj(B) Cᵀ] |
| 189 | + exprs = Vector{Expr}(undef, L) |
| 190 | + i = 0 |
| 191 | + for col = 1 : N, row = col : N |
| 192 | + i += 1 |
| 193 | + exprs[i] = row == col ? :(transpose(a.lowertriangle[$i])) : :(conj(a.lowertriangle[$i])) |
| 194 | + end |
| 195 | + return quote |
| 196 | + @_inline_meta |
| 197 | + SHermitianCompact{N}(SVector{L}(tuple($(exprs...)))) |
| 198 | + end |
| 199 | +end |
| 200 | + |
| 201 | +@generated function LinearAlgebra.adjoint(a::SHermitianCompact{N, T, L}) where {N, T, L} |
| 202 | + # To conform with LinearAlgebra, the adjoint should be recursive. |
| 203 | + # Taking the adjoint of a Hermitian matrix is the identity, but |
| 204 | + # with this compact representation of a Hermitian matrix, care |
| 205 | + # should be taken that only the diagonal elements should be |
| 206 | + # recursively conjugate-transposed; the off-diagonal elements should |
| 207 | + # not: |
| 208 | + # [A Bᴴ]ᴴ = [Aᴴ Bᴴ] = [Aᴴ Bᴴ] |
| 209 | + # [B C ] [Bᴴᴴ Cᴴ] [B Cᴴ] |
| 210 | + exprs = Vector{Expr}(undef, L) |
| 211 | + i = 0 |
| 212 | + for col = 1 : N, row = col : N |
| 213 | + i += 1 |
| 214 | + exprs[i] = row == col ? :(adjoint(a.lowertriangle[$i])) : :(a.lowertriangle[$i]) |
| 215 | + end |
| 216 | + return quote |
| 217 | + @_inline_meta |
| 218 | + SHermitianCompact{N}(SVector{L}(tuple($(exprs...)))) |
| 219 | + end |
| 220 | +end |
| 221 | + |
| 222 | +@generated function _one(::Size{S}, ::Type{SSC}) where {S, SSC <: SHermitianCompact} |
| 223 | + N = S[1] |
| 224 | + L = triangularnumber(N) |
| 225 | + T = eltype(SSC) |
| 226 | + if T == Any |
| 227 | + T = Float64 |
| 228 | + end |
| 229 | + exprs = Vector{Expr}(undef, L) |
| 230 | + i = 0 |
| 231 | + for col = 1 : N, row = col : N |
| 232 | + exprs[i += 1] = row == col ? :(one($T)) : :(zero($T)) |
| 233 | + end |
| 234 | + quote |
| 235 | + @_inline_meta |
| 236 | + return SHermitianCompact(SVector(tuple($(exprs...)))) |
| 237 | + end |
| 238 | +end |
| 239 | + |
| 240 | +@inline _eye(s::Size{S}, t::Type{SSC}) where {S, SSC <: SHermitianCompact} = _one(s, t) |
| 241 | + |
| 242 | +# _fill covers fill, zeros, and ones: |
| 243 | +@generated function _fill(val, ::Size{s}, ::Type{SSC}) where {s, SSC <: SHermitianCompact} |
| 244 | + N = s[1] |
| 245 | + L = triangularnumber(N) |
| 246 | + v = [:val for i = 1:L] |
| 247 | + return quote |
| 248 | + @_inline_meta |
| 249 | + $SSC(SVector(tuple($(v...)))) |
| 250 | + end |
| 251 | +end |
| 252 | + |
| 253 | +@generated function _rand(randfun, rng::AbstractRNG, ::Type{SSC}) where {N, SSC <: SHermitianCompact{N}} |
| 254 | + T = eltype(SSC) |
| 255 | + if T == Any |
| 256 | + T = Float64 |
| 257 | + end |
| 258 | + L = triangularnumber(N) |
| 259 | + v = [:(randfun(rng, $T)) for i = 1:L] |
| 260 | + return quote |
| 261 | + @_inline_meta |
| 262 | + $SSC(SVector(tuple($(v...)))) |
| 263 | + end |
| 264 | +end |
| 265 | + |
| 266 | +@inline Random.rand(rng::AbstractRNG, ::Type{SSC}) where {SSC <: SHermitianCompact} = _rand(rand, rng, SSC) |
| 267 | +@inline Random.randn(rng::AbstractRNG, ::Type{SSC}) where {SSC <: SHermitianCompact} = _rand(randn, rng, SSC) |
| 268 | +@inline Random.randexp(rng::AbstractRNG, ::Type{SSC}) where {SSC <: SHermitianCompact} = _rand(randexp, rng, SSC) |
0 commit comments