Equating two symbolically symmetric matrices in JuMP without redundant constraints

Dear All,

I am trying to define a constraint to equate two symbolically symmetric matrices and I am wondering if there is a compact way so that JuMP does not store redundant constraints. A simple example is as follows

using JuMP, Ipopt
test_model = Model(Ipopt.Optimizer)
n = 2
@variable(test_model, X[1:n,1:n], Symmetric)
@variable(test_model, Y[1:n,1:n], Symmetric)
@constraint(test_model, X .== Y)

where the last equality creates redundant constraint X[1,2] - Y[1,2] == 0.0 as shown below:

2×2 Matrix{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}, ScalarShape}}:
 X[1,1] - Y[1,1] == 0.0  X[1,2] - Y[1,2] == 0.0
 X[1,2] - Y[1,2] == 0.0  X[2,2] - Y[2,2] == 0.0

Of course, I can avoid the redundant constraint by:

@constraint(test_model, sym_con[i=1:n, j=1:i], X[i,j] == Y[i,j])


for i in 1:n
  for j in 1:i
    @constraint(test_model, X[i,j] == Y[i,j])

and both work just fine, but is this the best way to do it?

(Note that for the given example one may wonder why I am equating two variables that are the same rather than defining only one of X or Y. But for the problem I am working on, I need to enforce equality between two symbolically symmetric matrices, where the matrix Y corresponds to Y = z z^\top that is an outer product matrix, and the variable X is introduced to turn a cubic constraint into a quadratic constraint.)

Any tips/suggestions will be much appreciated!

You can do

@constraint(test_model, vectorize(X - Y, SymmetricMatrixShape(n)) .== 0)
1 Like

This is such a great solution, thanks so much @blegat !