# Arrays of vector variables in definitions of nonlinear programs in JuMP

**URL:** <https://discourse.julialang.org/t/arrays-of-vector-variables-in-definitions-of-nonlinear-programs-in-jump/97593>\
**Category:** Optimization (Mathematical)\
**Created:** [April 17, 2023, 10:01pm UTC](https://discourse.julialang.org/t/arrays-of-vector-variables-in-definitions-of-nonlinear-programs-in-jump/97593 "2023-04-17T22:01:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [April 17, 2023, 10:01pm UTC](https://discourse.julialang.org/t/arrays-of-vector-variables-in-definitions-of-nonlinear-programs-in-jump/97593/1 "2023-04-17T22:01:29Z")

</div>

Is it possible to define arrays of vector variables while building a nonlinear programming problem in JuMP?

I know we can define a scalar variable through

```plaintext
@variable(model,x)

```

I also know we can define a vector variable through

```julia
@variable(model,x[1:10])

```

which allows us later refer to individual scalar components using, say, `x[3]`.

This can also be extended to matrices and possibly higher-dimension arrays.

But can we also define arrays of vector variables? In the 1D case the notation `x[3]` would refer to a the 3rd vector variable of the array. The second element of the third vector would then be access through `x[3][2]`. Is this possible?

As an example, I would like to turn this (artificial) code

```julia
f(x) = cos(x)
N = 10
using JuMP, Ipopt
model = Model(Ipopt.Optimizer)
@variable(model, x[1:N])
for i in 1:N-1
    @NLconstraint(model, x[i+1] == f(x[i]))
end
@NLobjective(model, Min, abs(x[N])) 
optimize!(model)

```

into a vector/array version

```plaintext
f2(x::Vector) = cos.(x)
model2 = Model(Ipopt.Optimizer)
@variable(model2, x[1:2,1:N]) # Here I'd like to create an array of vectors variable, but this doesn't really work.
for i in 1:N-1
    @NLconstraint(model, x[:,i+1] == f2(x[:,i])) # Related to the above comment, not functional.
end
@NLobjective(model, Min, norm(x[N])) 
optimize!(model)

```

---

<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:** [April 18, 2023, 12:27am UTC](https://discourse.julialang.org/t/arrays-of-vector-variables-in-definitions-of-nonlinear-programs-in-jump/97593/2 "2023-04-18T00:27:15Z")

</div>

> Is it possible to define arrays of vector variables while building a nonlinear programming problem in JuMP?

JuMP variables are normal Julia objects, so you can build them into arbitrary data structures of your choosing.

```Julia
model = Model()
x = [@variable(model, [1:10]) for _ in 1:5]
x[3]
x[3][2]

```

> As an example, I would like to turn this (artificial) code  
> into a vector/array version

This is not supported.

See [Tips and tricks · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/nonlinear/tips_and_tricks/#User-defined-functions-with-vector-outputs)
