From cbabac91ba8f176128534b343ba6bbccc29beff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 27 Feb 2023 10:19:41 +0100 Subject: [PATCH 1/3] Wrap reshaped vector in LinearAlgebra.Hermitian --- src/sd.jl | 2 +- test/test_variable.jl | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/sd.jl b/src/sd.jl index 93788d9e38e..20e36dda1f8 100644 --- a/src/sd.jl +++ b/src/sd.jl @@ -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) diff --git a/test/test_variable.jl b/test/test_variable.jl index f7d0d732e1e..6b2661c20e1 100644 --- a/test/test_variable.jl +++ b/test/test_variable.jl @@ -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), @@ -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 @@ -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 From 3e2179d74f82676bebde82543992b3bed0d36c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 27 Feb 2023 11:32:12 +0100 Subject: [PATCH 2/3] Fix doctests --- docs/src/manual/complex.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/src/manual/complex.md b/docs/src/manual/complex.md index 1f5ca7e2891..34700c9b22e 100644 --- a/docs/src/manual/complex.md +++ b/docs/src/manual/complex.md @@ -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: @@ -269,7 +269,7 @@ julia> import LinearAlgebra julia> H = LinearAlgebra.Hermitian([x[1] 1im; -1im -x[2]]) 2×2 LinearAlgebra.Hermitian{GenericAffExpr{ComplexF64, VariableRef}, Matrix{GenericAffExpr{ComplexF64, VariableRef}}}: x[1] (0.0 + 1.0im) - (0.0 - 1.0im) (-1.0 - 0.0im) x[2] + (0.0 - 1.0im) (-1.0 + 0.0im) x[2] julia> @constraint(model, H in HermitianPSDCone()) [x[1] (0.0 + 1.0im); From 1d5ba9e55225cc64cb6b8549641f21a83d06f64c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 27 Feb 2023 12:24:07 +0100 Subject: [PATCH 3/3] Fix doc --- docs/src/manual/complex.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/manual/complex.md b/docs/src/manual/complex.md index 34700c9b22e..4bc04e409be 100644 --- a/docs/src/manual/complex.md +++ b/docs/src/manual/complex.md @@ -269,9 +269,9 @@ julia> import LinearAlgebra julia> H = LinearAlgebra.Hermitian([x[1] 1im; -1im -x[2]]) 2×2 LinearAlgebra.Hermitian{GenericAffExpr{ComplexF64, VariableRef}, Matrix{GenericAffExpr{ComplexF64, VariableRef}}}: x[1] (0.0 + 1.0im) - (0.0 - 1.0im) (-1.0 + 0.0im) x[2] + (0.0 - 1.0im) (-1.0 - 0.0im) 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() ```