# Problem summing multiple dims in NLobjective

**URL:** <https://discourse.julialang.org/t/problem-summing-multiple-dims-in-nlobjective/89442>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [October 28, 2022, 10:43am UTC](https://discourse.julialang.org/t/problem-summing-multiple-dims-in-nlobjective/89442 "2022-10-28T10:43:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [October 28, 2022, 10:43am UTC](https://discourse.julialang.org/t/problem-summing-multiple-dims-in-nlobjective/89442/1 "2022-10-28T10:43:05Z")

</div>

I’m having trouble using matrices in an `NLobjective`

```nohighlight
model = Model()

@variable(model, 0 .<= x[1:100, 1:100] .<= 1)
@NLexpression(model,x_con[ii=1:100, jj=1:100], x[ii,jj] * abs(x[ii,jj]))

```

This works

```nohighlight
@NLobjective(model, Min, sum(x_con[ii] for ii in 1:100*100))

```

while this gives

```nohighlight
julia> @NLobjective(model, Min, sum(x_con[ii,jj] for ii in 1:100 for jj in 1:100))
ERROR: UndefVarError: jj not defined
Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/JuMP/gVq7V/src/macros.jl:1834 [inlined]
 [2] top-level scope
   @ REPL[6]:1

```

I appreciate these are doing the same thing, but it could be helpful for matrix sums to work.

---

<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:** [October 28, 2022, 7:40pm UTC](https://discourse.julialang.org/t/problem-summing-multiple-dims-in-nlobjective/89442/2 "2022-10-28T19:40:27Z")

</div>

> [@this\_josh](#):
>
> `@NLobjective(model, Min, sum(x_con[ii,jj] for ii in 1:100 for jj in 1:100))`

Oops. This is a pretty bad bug that I likely introduced in the recent nonlinear changes.

For a work-around, use `sum(x_con[ii,jj] for ii in 1:100, jj in 1:100)`.

I’ll get it fixed ASAP: [Fix parsing of nonlinear expressions with :flatten nodes by odow · Pull Request #3116 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/pull/3116)

---

<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:** [October 29, 2022, 1:39am UTC](https://discourse.julialang.org/t/problem-summing-multiple-dims-in-nlobjective/89442/3 "2022-10-29T01:39:27Z")

</div>

Fixed in JuMP v1.4.0.

~~which will be available once this PR is merged: [New version: JuMP v1.4.0 by JuliaRegistrator · Pull Request #71272 · JuliaRegistries/General · GitHub](https://github.com/JuliaRegistries/General/pull/71272)\</s.~~

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [October 29, 2022, 8:30am UTC](https://discourse.julialang.org/t/problem-summing-multiple-dims-in-nlobjective/89442/4 "2022-10-29T08:30:34Z")

</div>

That was super quick, thank you. I’ll update now.
