# Indexing Nonlinear Expressions with nested indices in JuMP

**URL:** <https://discourse.julialang.org/t/indexing-nonlinear-expressions-with-nested-indices-in-jump/36140>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, syntax\
**Created:** [March 18, 2020, 1:09pm UTC](https://discourse.julialang.org/t/indexing-nonlinear-expressions-with-nested-indices-in-jump/36140 "2020-03-18T13:09:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shoshievass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shoshievass/32/3260_2.png) [@shoshievass](https://discourse.julialang.org/u/shoshievass)\
**Post date:** [March 18, 2020, 1:09pm UTC](https://discourse.julialang.org/t/indexing-nonlinear-expressions-with-nested-indices-in-jump/36140/1 "2020-03-18T13:09:14Z")

</div>

I’m trying to define a JuMP @NLexpression with embedded indexing and I need to use numerical indices instead of collections. I would have thought that the below would work but I’m getting an error saying that `F` was already defined… Might anyone know how to circumvent this problem?

```julia
arr_1 = [A,B,C,...] ## A,B,C are structs that have arr_2 as fields
for a = 1:length(arr_1)
	arr_2_a = arr_1[a].arr_2 ## arr_1[a].arr_2 = [x_a, y_a, z_a, ...], each of which is another struct
	
	for b = 1:length(arr_2_a)
		@NLexpression(
			model,
			F[a,b] = some_function_of_arr_1[a]_and_arr_2_a[b]
			)
	end
end

```

---

<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:** [March 18, 2020, 1:32pm UTC](https://discourse.julialang.org/t/indexing-nonlinear-expressions-with-nested-indices-in-jump/36140/2 "2020-03-18T13:32:34Z")

</div>

`@NLexpressions` have the same collection syntax as normal `@expression`s.

Here I the documentation:  
[https://www.juliaopt.org/JuMP.jl/stable/nlp/#Syntax-notes-1](https://www.juliaopt.org/JuMP.jl/stable/nlp/#Syntax-notes-1)  
[https://www.juliaopt.org/JuMP.jl/stable/nlp/#JuMP.@NLexpression](https://www.juliaopt.org/JuMP.jl/stable/nlp/#JuMP.@NLexpression)

```nohighlight
# Instead of:
for a = 1:4
    @NLexpression(model, F[a], x^a)
end
# use
@NLexpression(model, F[a = 1:4], x^a)

```

---

<div class="post-metadata">

**Author:** ![shoshievass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shoshievass/32/3260_2.png) [@shoshievass](https://discourse.julialang.org/u/shoshievass)\
**Post date:** [March 18, 2020, 1:42pm UTC](https://discourse.julialang.org/t/indexing-nonlinear-expressions-with-nested-indices-in-jump/36140/3 "2020-03-18T13:42:52Z")

</div>

Right but my issue here is that the indexing is embedded – I need to index by a=1:4 and then by b=1:f(a) where f(a) is a number in a dict

---

<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:** [March 18, 2020, 1:51pm UTC](https://discourse.julialang.org/t/indexing-nonlinear-expressions-with-nested-indices-in-jump/36140/4 "2020-03-18T13:51:11Z")

</div>

```nohighlight
julia> using JuMP

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

julia> @variable(model, x)
x

julia> f = Dict(1 => 2, 2 => 4)
Dict{Int64,Int64} with 2 entries:
  2 => 4
  1 => 2

julia> @NLexpression(model, F[a=1:2, b=1:f[a]], b * x^a)
JuMP.Containers.SparseAxisArray{NonlinearExpression,2,Tuple{Int64,Int64}} with 6 entries:
  [1, 2] = "Reference to nonlinear expression #2"
  [2, 3] = "Reference to nonlinear expression #5"
  [2, 4] = "Reference to nonlinear expression #6"
  [2, 2] = "Reference to nonlinear expression #4"
  [1, 1] = "Reference to nonlinear expression #1"
  [2, 1] = "Reference to nonlinear expression #3"

```
