-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from JuliaAlgebra/bl/hermitian
Add support for hermitian matrix
- Loading branch information
Showing
8 changed files
with
153 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
export VectorizedHermitianMatrix | ||
|
||
""" | ||
struct VectorizedHermitianMatrix{T} <: AbstractMatrix{T} | ||
Q::Vector{T} | ||
n::Int | ||
end | ||
Hermitian ``n \\times n`` matrix storing the vectorized upper triangular | ||
real part of the matrix followed by the vectorized upper triangular | ||
imaginary part in the `Q` vector (this corresponds to the vectorized | ||
form of `ComplexOptInterface.HermitianPositiveSemidefiniteConeTriangle`). | ||
It implement the `AbstractMatrix` interface except for `setindex!` as it might | ||
break its symmetry. The [`symmetric_setindex!`](@ref) function should be used | ||
instead. | ||
""" | ||
struct VectorizedHermitianMatrix{T} <: AbstractMatrix{T} | ||
Q::Vector{T} | ||
n::Int | ||
end | ||
|
||
Base.copy(Q::VectorizedHermitianMatrix) = VectorizedHermitianMatrix(copy(Q.Q), Q.n) | ||
function Base.map(f::Function, Q::VectorizedHermitianMatrix) | ||
if Q.n <= 1 | ||
return VectorizedHermitianMatrix(map(f, Q.Q), Q.n) | ||
else | ||
el = f(Q[1, 2]) | ||
ret = VectorizedHermitianMatrix(similar(Q.Q, typeof(real(el))), Q.n) | ||
for j in 1:Q.n | ||
for i in 1:j | ||
if i == 1 && j == 2 | ||
x = el | ||
else | ||
x = f(Q[i, j]) | ||
end | ||
symmetric_setindex!(ret, x, i, j) | ||
end | ||
end | ||
return ret | ||
end | ||
end | ||
|
||
imag_map(n, i, j) = trimap(n, n) + trimap(i, j - 1) | ||
imag_map(Q::VectorizedHermitianMatrix, i, j) = imag_map(Q.n, i, j) | ||
|
||
function trimat(::Type{T}, f, n, σ) where {T} | ||
Q = Vector{T}(undef, N + trimap(n - 1, n - 1)) | ||
for i in 1:n | ||
for j in 1:i | ||
x = f(σ[i], σ[j]) | ||
Q[trimap(i, j)] = real(x) | ||
if i != j | ||
Q[imag_map(n, i, j)] = imag(x) | ||
end | ||
end | ||
end | ||
return VectorizedHermitianMatrix{T}(Q, n) | ||
end | ||
|
||
Base.size(Q::VectorizedHermitianMatrix) = (Q.n, Q.n) | ||
|
||
""" | ||
symmetric_setindex!(Q::VectorizedHermitianMatrix, value, i::Integer, j::Integer) | ||
Set `Q[i, j]` to the value `value` and `Q[j, i]` to the value `-value`. | ||
""" | ||
function symmetric_setindex!(Q::VectorizedHermitianMatrix, value, i::Integer, j::Integer) | ||
if i > j | ||
symmetric_setindex!(Q, conj(value), j, i) | ||
else | ||
Q.Q[trimap(i, j)] = real(value) | ||
if i == j | ||
if !iszero(imag(value)) | ||
error("Cannot set diagonal entry ($i, $j) of hermitian matrix with a non-zero imaginary part $(imag(value))") | ||
end | ||
else | ||
Q.Q[imag_map(Q, i, j)] = imag(value) | ||
end | ||
end | ||
end | ||
|
||
function Base.getindex(Q::VectorizedHermitianMatrix, i::Integer, j::Integer) | ||
I, J = max(i, j), min(i, j) | ||
r = Q.Q[trimap(I, J)] | ||
if i == j | ||
return Complex(r) | ||
else | ||
c = Q.Q[imag_map(Q, I, J)] | ||
return Complex(r, i < j ? c : -c) | ||
end | ||
end | ||
Base.getindex(Q::VectorizedHermitianMatrix, I::Tuple) = Q[I...] | ||
Base.getindex(Q::VectorizedHermitianMatrix, I::CartesianIndex) = Q[I.I] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Test | ||
using MultivariateMoments | ||
|
||
@testset "VectorizedHermitianMatrix" begin | ||
Q = VectorizedHermitianMatrix([1, 2, 3, -1], 2) | ||
@test 1 == @inferred Q[1, 1] | ||
@test 2 - im == @inferred Q[1, 2] | ||
@test 2 + im == @inferred Q[2, 1] | ||
@test 3 == @inferred Q[2, 2] | ||
symmetric_setindex!(Q, 4, 1, 1) | ||
@test Q.Q == [4, 2, 3, -1] | ||
symmetric_setindex!(Q, 5 - 2im, 1, 2) | ||
@test Q.Q == [4, 5, 3, -2] | ||
P = copy(Q) | ||
@test P.n == 2 | ||
symmetric_setindex!(P, 6, 2, 2) | ||
@test Q.Q == [4, 5, 3, -2] | ||
@test P.n == 2 | ||
@test P.Q == [4, 5, 6, -2] | ||
R = map(i -> i - 1, P) | ||
@test R.n == 2 | ||
@test R.Q == [3, 4, 5, -2] | ||
@test P.Q == [4, 5, 6, -2] | ||
S = map(conj, R) | ||
@test S.n == 2 | ||
@test S.Q == [3, 4, 5, 2] | ||
@test_throws ErrorException symmetric_setindex!(S, im, 1, 1) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Test | ||
using MultivariateMoments | ||
|
||
@testset "SymMatrix" begin | ||
Q = SymMatrix([1, 2, 3], 2) | ||
symmetric_setindex!(Q, 4, 1, 1) | ||
@test Q.Q == [4, 2, 3] | ||
symmetric_setindex!(Q, 5, 1, 2) | ||
@test Q.Q == [4, 5, 3] | ||
P = copy(Q) | ||
@test P.n == 2 | ||
symmetric_setindex!(P, 6, 2, 2) | ||
@test Q.Q == [4, 5, 3] | ||
@test P.n == 2 | ||
@test P.Q == [4, 5, 6] | ||
R = map(i -> i - 1, P) | ||
@test R.n == 2 | ||
@test R.Q == [3, 4, 5] | ||
end |
871e868
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
871e868
There was a problem hiding this comment.
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/23444
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: