Skip to content

Commit

Permalink
Wrap reshaped vector in LinearAlgebra.Hermitian (#3245)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat authored Feb 27, 2023
1 parent 64cbb63 commit 45f64f0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
10 changes: 5 additions & 5 deletions docs/src/manual/complex.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ JuMP supports creating matrices where are Hermitian.
julia> model = Model();
julia> @variable(model, H[1:3, 1:3] in HermitianPSDCone())
3×3 Matrix{GenericAffExpr{ComplexF64, VariableRef}}:
real(H[1,1]) … real(H[1,3]) + (0.0 + 1.0im) imag(H[1,3])
real(H[1,2]) + (-0.0 - 1.0im) imag(H[1,2]) real(H[2,3]) + (0.0 + 1.0im) imag(H[2,3])
real(H[1,3]) + (-0.0 - 1.0im) imag(H[1,3]) real(H[3,3])
3×3 LinearAlgebra.Hermitian{GenericAffExpr{ComplexF64, VariableRef}, Matrix{GenericAffExpr{ComplexF64, VariableRef}}}:
real(H[1,1]) … real(H[1,3]) + (0.0 + 1.0im) imag(H[1,3])
real(H[1,2]) + (0.0 - 1.0im) imag(H[1,2]) real(H[2,3]) + (0.0 + 1.0im) imag(H[2,3])
real(H[1,3]) + (0.0 - 1.0im) imag(H[1,3]) real(H[3,3])
```

Behind the scenes, JuMP has created nine real-valued decision variables:
Expand Down Expand Up @@ -273,7 +273,7 @@ julia> H = LinearAlgebra.Hermitian([x[1] 1im; -1im -x[2]])
julia> @constraint(model, H in HermitianPSDCone())
[x[1] (0.0 + 1.0im);
(0.0 - 1.0im) (-1.0 + 0.0im) x[2]] ∈ HermitianPSDCone()
(0.0 - 1.0im) (-1.0 - 0.0im) x[2]] ∈ HermitianPSDCone()
```

!!! note
Expand Down
2 changes: 1 addition & 1 deletion src/sd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ function reshape_vector(v::Vector{T}, shape::HermitianMatrixShape) where {T}
real_k += 1
matrix[j, j] = v[real_k]
end
return matrix
return LinearAlgebra.Hermitian(matrix)
end

function _vectorize_complex_variables(_error::Function, matrix::Matrix)
Expand Down
24 changes: 17 additions & 7 deletions test/test_variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,8 @@ function test_extension_variables_constrained_on_creation(
@variable(model, set = MOI.Semiinteger(1.0, 2.0))
@test num_constraints(model, typeof(z), MOI.Semiinteger{Float64}) == 2

@variable(model, [1:3, 1:3] in PSDCone())
X = @variable(model, [1:3, 1:3] in PSDCone())
@test X isa LinearAlgebra.Symmetric
@test num_constraints(
model,
typeof(x),
Expand Down Expand Up @@ -1233,17 +1234,20 @@ end

function test_Hermitian_PSD()
model = Model()
@variable(model, Q[1:2, 1:2] in HermitianPSDCone())
@variable(model, H[1:2, 1:2] in HermitianPSDCone())
@test H isa LinearAlgebra.Hermitian
Q = parent(H)
@test num_variables(model) == 4
v = all_variables(model)
_test_variable_name_util(v[1], "real(Q[1,1])")
_test_variable_name_util(v[1], "real(H[1,1])")
@test Q[1, 1] == 1v[1]
_test_variable_name_util(v[2], "real(Q[1,2])")
_test_variable_name_util(v[4], "imag(Q[1,2])")
_test_variable_name_util(v[2], "real(H[1,2])")
_test_variable_name_util(v[4], "imag(H[1,2])")
@test Q[1, 2] == v[2] + v[4] * im
_test_variable_name_util(v[3], "real(Q[2,2])")
_test_variable_name_util(v[3], "real(H[2,2])")
@test Q[2, 2] == 1v[3]
@test Q[2, 1] == conj(Q[1, 2])
@test H[2, 1] == conj(H[1, 2])
return
end

Expand Down Expand Up @@ -1323,11 +1327,17 @@ end

function test_Hermitian_PSD_anon()
model = Model()
x = @variable(model, [1:2, 1:2] in HermitianPSDCone())
y = @variable(model, [1:2, 1:2] in HermitianPSDCone())
@test y isa LinearAlgebra.Hermitian
x = parent(y)
@test sprint(show, x[1, 1]) == "_[1]"
@test sprint(show, y[1, 1]) == "_[1]"
@test sprint(show, x[1, 2]) == "_[2] + (0.0 + 1.0im) _[4]"
@test sprint(show, y[1, 2]) == "_[2] + (0.0 + 1.0im) _[4]"
@test sprint(show, x[2, 1]) == "_[2] + (-0.0 - 1.0im) _[4]"
@test sprint(show, y[2, 1]) == "_[2] + (0.0 - 1.0im) _[4]"
@test sprint(show, x[2, 2]) == "_[3]"
@test sprint(show, y[2, 2]) == "_[3]"
return
end

Expand Down

0 comments on commit 45f64f0

Please sign in to comment.