# 3D variable in JuMP

**URL:** https://discourse.julialang.org/t/3d-variable-in-jump/30416
**Category:** Optimization (Mathematical)
**Created:** [October 28, 2019, 7:52pm UTC](https://discourse.julialang.org/t/3d-variable-in-jump/30416 "2019-10-28T19:52:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![g001234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g001234/32/2956_2.png) [@g001234](https://discourse.julialang.org/u/g001234)
#### Post date: [October 28, 2019, 7:52pm UTC](https://discourse.julialang.org/t/3d-variable-in-jump/30416/1 "2019-10-28T19:52:30Z")

</div>

I am working on a convex optimization problem that needs to define several (e.g. 5) symmetric matrices as variables. I was wondering if it is possible to define those variables with a single line like `@variable(m, x(N,N,M), Symmetric)`, where N is the dimension of the matrix, and M is the number of copies of symmetric matrices. Then I can access a symmetric matrix by, e.g., `x[:,:,1]`.

From this post ([Semidefinite relaxations of quadratic constraint - CVX Forum: a community-driven support forum](http://ask.cvxr.com/t/semidefinite-relaxations-of-quadratic-constraint/179)), it seems to be possible in CVX. So I was thinking it would be nice if I could do similar things in JuMP.

---

<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: [October 28, 2019, 9:24pm UTC](https://discourse.julialang.org/t/3d-variable-in-jump/30416/2 "2019-10-28T21:24:15Z")

</div>

This is not yet possible in JuMP, you need to do

```julia
x = Array{JuMP.VariableRef}(undef, N, N, M)
for i in 1:M
    x[:, :, i] = @variable(m, [1:N, 1:N], Symmetric)
end

```

or do

```julia
x = [@variable(m, [1:N, 1:N], Symmetric) for i in 1:M]

```

---

<div class="post-metadata">

### Author: ![g001234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g001234/32/2956_2.png) [@g001234](https://discourse.julialang.org/u/g001234)
#### Post date: [October 29, 2019, 12:50am UTC](https://discourse.julialang.org/t/3d-variable-in-jump/30416/3 "2019-10-29T00:50:42Z")

</div>

Thank you for the promp reply!

The second code works for me. I also have two tips for implementing this as a future reference:

1. In order to make the model more readable, I set the names of the matrices as follows: `x = [@variable(m, [1:N, 1:N], base_name = "x$i", Symmetric) for i in 1:M]`. Then in the printed model an item in the first matrix looks like this: `x1[i,j]`.

2. To access the ith matrix, use `x[i][:,:]`

For the first option though, I get a bug from `x = Matrix{JuMP.VariableRef}(undef, 2, 2, 3)`, which says `MethodError: no method matching Array{VariableRef,2}(::UndefInitializer, ::Int64, ::Int64, ::Int64)`. I assume this is because JuMP does not support 3-dimensional variables.

---

<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: [October 29, 2019, 11:53am UTC](https://discourse.julialang.org/t/3d-variable-in-jump/30416/4 "2019-10-29T11:53:52Z")

</div>

Oops, `Matrix` should be replaced by `Array` in the first code.

---

<div class="post-metadata">

### Author: ![g001234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g001234/32/2956_2.png) [@g001234](https://discourse.julialang.org/u/g001234)
#### Post date: [October 29, 2019, 11:45pm UTC](https://discourse.julialang.org/t/3d-variable-in-jump/30416/5 "2019-10-29T23:45:04Z")

</div>

Now it works. In this case the ith matrix is accessed by `x[:,:,i]`.
