# Equating two symbolically symmetric matrices in JuMP without redundant constraints

**URL:** <https://discourse.julialang.org/t/equating-two-symbolically-symmetric-matrices-in-jump-without-redundant-constraints/66274>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [August 12, 2021, 1:55pm UTC](https://discourse.julialang.org/t/equating-two-symbolically-symmetric-matrices-in-jump-without-redundant-constraints/66274 "2021-08-12T13:55:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)\
**Post date:** [August 12, 2021, 1:55pm UTC](https://discourse.julialang.org/t/equating-two-symbolically-symmetric-matrices-in-jump-without-redundant-constraints/66274/1 "2021-08-12T13:55:39Z")

</div>

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

```julia
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:

```julia
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:

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

```

or

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

```

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!

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [August 12, 2021, 2:33pm UTC](https://discourse.julialang.org/t/equating-two-symbolically-symmetric-matrices-in-jump-without-redundant-constraints/66274/2 "2021-08-12T14:33:32Z")

</div>

You can do

```julia
@constraint(test_model, vectorize(X - Y, SymmetricMatrixShape(n)) .== 0)

```

---

<div class="post-metadata">

**Author:** ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)\
**Post date:** [August 12, 2021, 3:14pm UTC](https://discourse.julialang.org/t/equating-two-symbolically-symmetric-matrices-in-jump-without-redundant-constraints/66274/3 "2021-08-12T15:14:06Z")

</div>

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