# Creating an array of symmetric matrix variables in \`JuMP\`

**URL:** <https://discourse.julialang.org/t/creating-an-array-of-symmetric-matrix-variables-in-jump/66229>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [August 11, 2021, 8:45pm UTC](https://discourse.julialang.org/t/creating-an-array-of-symmetric-matrix-variables-in-jump/66229 "2021-08-11T20:45:48Z")\
**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 11, 2021, 8:45pm UTC](https://discourse.julialang.org/t/creating-an-array-of-symmetric-matrix-variables-in-jump/66229/1 "2021-08-11T20:45:48Z")

</div>

Dear All,

I am trying to create an array of symmetric matrix variables in `JuMP`, _i.e.,_ an array of matrix variables X\_i\in\mathbf{S}^n for i\in\{1,2,\ldots,N\}. If I had only one symmetric matrix variable X\in\mathbf{S}^n, I could create it in `JuMP` using

method 1: `@variable(model, X[1:n, 1:n], Symmetric)`

and if I had regular matrix variables X\_i\in\mathbf{R}^{n \times n} for i\in\{1,2,\ldots,N\}, then I could do:

method 2: `@variable(model, X[1:n, 1:n, 1:N])`.

Is there any command where I can enforce symmetry on each `X[:,:,i]` in method 2, or any way to extend method 1 to an array of matrices? I could individually impose the symmetry constraint X\_i[j,k]=X\_i[k,j] for matrix indices j,k for every X\_i, but I am wondering if there is any other way.

I will very much appreciate any tips/suggestion!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [August 11, 2021, 9:05pm UTC](https://discourse.julialang.org/t/creating-an-array-of-symmetric-matrix-variables-in-jump/66229/2 "2021-08-11T21:05:59Z")

</div>

> Is there any command

Not directly. See [Broadcasting variables constrained on creation · Issue #2148 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/issues/2148)

A good thing to remember is that you aren’t limited by the available JuMP syntax. You can create new data structures using Julia:

```Julia
julia> using JuMP

julia> n, N = 2, 3
(2, 3)

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> X = model[:X] = reshape(
           hcat([
               @variable(model, [1:n, 1:n], Symmetric, base_name = "X$(i)")
               for i in 1:N
           ]...),
           n, n, N,
       )
2×2×3 Array{VariableRef, 3}:
[:, :, 1] =
 X1[1,1] X1[1,2]
 X1[1,2] X1[2,2]

[:, :, 2] =
 X2[1,1] X2[1,2]
 X2[1,2] X2[2,2]

[:, :, 3] =
 X3[1,1] X3[1,2]
 X3[1,2] X3[2,2]

julia> model
A JuMP Model
Feasibility problem with:
Variables: 9
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: X

julia> model[:X]
2×2×3 Array{VariableRef, 3}:
[:, :, 1] =
 X1[1,1] X1[1,2]
 X1[1,2] X1[2,2]

[:, :, 2] =
 X2[1,1] X2[1,2]
 X2[1,2] X2[2,2]

[:, :, 3] =
 X3[1,1] X3[1,2]
 X3[1,2] X3[2,2]

```

---

<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 11, 2021, 9:13pm UTC](https://discourse.julialang.org/t/creating-an-array-of-symmetric-matrix-variables-in-jump/66229/3 "2021-08-11T21:13:23Z")

</div>

This is a great solution, thanks so much @odow !
