Skip to content

Commit 393825a

Browse files
odowblegat
andauthored
[Bridges] fix bug in HermitianToSymmetricPSDBridge (#2171)
Co-authored-by: Benoît Legat <[email protected]>
1 parent 54f2c48 commit 393825a

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

src/Bridges/Constraint/bridges/hermitian.jl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ function MOI.Bridges.map_function(
118118
half_real_set = MOI.PositiveSemidefiniteConeTriangle(n)
119119
half_real_dim = MOI.dimension(half_real_set)
120120
real_index_1 = 0
121+
# `+ n` is for jumping over the indices corresponding to the imaginary part
121122
real_index_2 = half_real_dim + n
122123
for j in 1:n
123124
for i in 1:j
@@ -127,24 +128,25 @@ function MOI.Bridges.map_function(
127128
real_index_2 += 1
128129
real_scalars[real_index_2] = complex_scalars[complex_index]
129130
if i == j
131+
# We are at the end of a column so we do `+ n` for jumping over
132+
# the indices corresponding to the imaginary part
130133
real_index_2 += n
131134
end
132135
end
133136
end
137+
# Index corresponding to the upper triangular part
134138
real_index_1 = half_real_dim
135-
real_index_2 = real_dim - n + 1
136139
for j in 1:n
137140
for i in 1:(j-1)
138141
complex_index += 1
139142
real_index_1 += 1
140143
real_scalars[real_index_1] = complex_scalars[complex_index]
141-
real_index_2 -= 1
142-
real_scalars[real_index_2] =
144+
real_scalars[MOI.Utilities.trimap(j, n + i)] =
143145
MOI.Utilities.operate(-, T, complex_scalars[complex_index])
144146
end
145147
real_scalars[real_index_1+1] = zero(S)
148+
# jump over the lower triangular part and the (2, 2) block
146149
real_index_1 += n + 1
147-
real_index_2 -= 2 * (n - j) + 1
148150
end
149151
@assert length(complex_scalars) == complex_index
150152
return MOI.Utilities.vectorize(real_scalars)
@@ -218,17 +220,15 @@ function MOI.Bridges.adjoint_map_function(
218220
end
219221
end
220222
real_index_1 = half_real_dim
221-
real_index_2 = real_dim - n + 1
222223
for j in 1:n
223224
for i in 1:(j-1)
224225
complex_index += 1
225226
real_index_1 += 1
226-
real_index_2 -= 1
227227
complex_scalars[complex_index] =
228-
real_scalars[real_index_1] - real_scalars[real_index_2]
228+
real_scalars[real_index_1] -
229+
real_scalars[MOI.Utilities.trimap(j, i + n)]
229230
end
230231
real_index_1 += n + 1
231-
real_index_2 -= 2 * (n - j) + 1
232232
end
233233
@assert length(complex_scalars) == complex_index
234234
return MOI.Utilities.vectorize(complex_scalars)
@@ -271,18 +271,15 @@ function MOI.Bridges.inverse_adjoint_map_function(
271271
end
272272
end
273273
real_index_1 = half_real_dim
274-
real_index_2 = real_dim - n + 1
275274
for j in 1:n
276275
for i in 1:(j-1)
277276
complex_index += 1
278277
real_index_1 += 1
279278
real_scalars[real_index_1] = complex_scalars[complex_index]
280-
real_index_2 -= 1
281-
real_scalars[real_index_2] = zero(S)
279+
real_scalars[MOI.Utilities.trimap(j, i + n)] = zero(S)
282280
end
283281
real_scalars[real_index_1+1] = zero(S)
284282
real_index_1 += n + 1
285-
real_index_2 -= 2 * (n - j) + 1
286283
end
287284
@assert length(complex_scalars) == complex_index
288285
return MOI.Utilities.vectorize(real_scalars)

test/Bridges/Constraint/hermitian.jl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function runtests()
2121
return
2222
end
2323

24-
function test_runtests()
24+
function test_dimension_2()
2525
MOI.Bridges.runtests(
2626
MOI.Bridges.Constraint.HermitianToSymmetricPSDBridge,
2727
"""
@@ -36,6 +36,36 @@ function test_runtests()
3636
return
3737
end
3838

39+
function test_dimension_3()
40+
MOI.Bridges.runtests(
41+
MOI.Bridges.Constraint.HermitianToSymmetricPSDBridge,
42+
"""
43+
variables: x11, x12, x22, x13, x23, x33, y12, y13, y23
44+
[x11, x12, x22, x13, x23, x33, y12, y13, y23] in HermitianPositiveSemidefiniteConeTriangle(3)
45+
""",
46+
"""
47+
variables: x11, x12, x22, x13, x23, x33, y12, y13, y23
48+
[x11, x12, x22, x13, x23, x33, 0, -1 * y12, -1 * y13, x11, y12, 0, -1 * y23, x12, x22, y13, y23, 0, x13, x23, x33] in PositiveSemidefiniteConeTriangle(6)
49+
""",
50+
)
51+
return
52+
end
53+
54+
function test_dimension_4()
55+
MOI.Bridges.runtests(
56+
MOI.Bridges.Constraint.HermitianToSymmetricPSDBridge,
57+
"""
58+
variables: x11, x12, x22, x13, x23, x33, x14, x24, x34, x44, y12, y13, y23, y14, y24, y34
59+
[x11, x12, x22, x13, x23, x33, x14, x24, x34, x44, y12, y13, y23, y14, y24, y34] in HermitianPositiveSemidefiniteConeTriangle(4)
60+
""",
61+
"""
62+
variables: x11, x12, x22, x13, x23, x33, x14, x24, x34, x44, y12, y13, y23, y14, y24, y34
63+
[x11, x12, x22, x13, x23, x33, x14, x24, x34, x44, 0, -1 * y12, -1 * y13, -1 * y14, x11, y12, 0, -1 * y23, -1 * y24, x12, x22, y13, y23, 0, -1 * y34, x13, x23, x33, y14, y24, y34, 0, x14, x24, x34, x44] in PositiveSemidefiniteConeTriangle(8)
64+
""",
65+
)
66+
return
67+
end
68+
3969
end # module
4070

4171
TestConstraintHermitianToSymmetricPSD.runtests()

0 commit comments

Comments
 (0)