# JuMP reports an error when attempting to sum the empty DenseAxisArray as the objective function

**URL:** <https://discourse.julialang.org/t/jump-reports-an-error-when-attempting-to-sum-the-empty-denseaxisarray-as-the-objective-function/133332>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [October 21, 2025, 1:23pm UTC](https://discourse.julialang.org/t/jump-reports-an-error-when-attempting-to-sum-the-empty-denseaxisarray-as-the-objective-function/133332 "2025-10-21T13:23:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [October 21, 2025, 1:23pm UTC](https://discourse.julialang.org/t/jump-reports-an-error-when-attempting-to-sum-the-empty-denseaxisarray-as-the-objective-function/133332/1 "2025-10-21T13:23:47Z")

</div>

**Minimal Reproducible Example**

```julia
using JuMP
set = Symbol[]
m = Model()
@variable(m, x[i in set])
@objective(m, Min, sum(x; init=0))

```

In this case, optimization is not impossible; rather, an unexpected error occurs when the program executes at `@objective`.

```julia
ERROR: LoadError: MethodError: no method matching _rewrite_generic_generator(::Expr, ::Symbol, ::Symbol, ::Symbol)
The function `_rewrite_generic_generator` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  _rewrite_generic_generator(::Expr, ::Symbol, ::Expr, ::Any)      
   @ MutableArithmetics .julia\packages\MutableArithmetics\VMS0x\src\rewrite_generic.jl:295
  _rewrite_generic_generator(::Expr, ::Symbol, ::Expr)
   @ MutableArithmetics .julia\packages\MutableArithmetics\VMS0x\src\rewrite_generic.jl:295

```

Does anyone know how to resolve this?

```julia
> versioninfo()
Julia Version 1.12.1
Commit ba1e628ee4 (2025-10-17 13:02 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)

```

---

<div class="post-metadata">

**Author:** ![langestefan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/langestefan/32/207923_2.png) [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Post date:** [October 21, 2025, 3:37pm UTC](https://discourse.julialang.org/t/jump-reports-an-error-when-attempting-to-sum-the-empty-denseaxisarray-as-the-objective-function/133332/2 "2025-10-21T15:37:26Z")

</div>

you can use the generator syntax:

```julia
using JuMP

set = Symbol[]
m = Model()

@variable(m, x[i in set])
@objective(m, Min, sum(x[i] for i in set; init=0.0))

```

---

<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 21, 2025, 8:52pm UTC](https://discourse.julialang.org/t/jump-reports-an-error-when-attempting-to-sum-the-empty-denseaxisarray-as-the-objective-function/133332/3 "2025-10-21T20:52:57Z")

</div>

Hi @karei, thanks for this bug report. I’m fixing this now 😄

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [October 21, 2025, 11:57pm UTC](https://discourse.julialang.org/t/jump-reports-an-error-when-attempting-to-sum-the-empty-denseaxisarray-as-the-objective-function/133332/4 "2025-10-21T23:57:16Z")

</div>

> [@langestefan](#):
>
> you can use the generator syntax:

Wow this works, thank you!

> [@odow](#):
>
> Hi @karei, thanks for this bug report. I’m fixing this now 😄

Thank you!

---

<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 22, 2025, 12:30am UTC](https://discourse.julialang.org/t/jump-reports-an-error-when-attempting-to-sum-the-empty-denseaxisarray-as-the-objective-function/133332/5 "2025-10-22T00:30:28Z")

</div>

I know that you’ve simplified your example, but did you consider just dropping the `init`?

This works:

```julia
@objective(m, Min, sum(x))

```

Your example actually exposed two bugs:

- We failed to rewrite `sum(expr; init)` correctly when `expr` was in fact not an expression and just a simple Symbol: [Fix rewrite generic of sum(::Symbol; init) by odow · Pull Request #339 · jump-dev/MutableArithmetics.jl · GitHub](https://github.com/jump-dev/MutableArithmetics.jl/pull/339)
- We didn’t support the `init` keyword for DenseAxisArray: [[Containers] fix support for sum(::DenseAxisArray; init) by odow · Pull Request #4085 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/pull/4085)

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [October 22, 2025, 12:39am UTC](https://discourse.julialang.org/t/jump-reports-an-error-when-attempting-to-sum-the-empty-denseaxisarray-as-the-objective-function/133332/6 "2025-10-22T00:39:59Z")

</div>

I added it precisely because the `sum` function threw an error when `init` wasn’t included.

When running `@objective(m, Min, sum(x))` on my computer, I got:

```julia-auto
ERROR: ArgumentError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer

```

🤔

---

<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 22, 2025, 1:03am UTC](https://discourse.julialang.org/t/jump-reports-an-error-when-attempting-to-sum-the-empty-denseaxisarray-as-the-objective-function/133332/7 "2025-10-22T01:03:22Z")

</div>

What version of Julia are you using?

As yet another work-around, you can do:

```julia
@objective(m, Min, sum(x.data))

```

The underlying issue is that most mathematicians consider sum of the empty set to be `0`, but Julia is a bit more picky. In JuMP we have tried to override the behaviour so that you get `0`, but we miss some edge cases 😢

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [October 22, 2025, 1:15am UTC](https://discourse.julialang.org/t/jump-reports-an-error-when-attempting-to-sum-the-empty-denseaxisarray-as-the-objective-function/133332/8 "2025-10-22T01:15:57Z")

</div>

My version is

```julia-auto
versioninfo()
Julia Version 1.12.1
Commit ba1e628ee4 (2025-10-17 13:02 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)

```

> [@odow](#):
>
> `@objective(m, Min, sum(x.data))`

This works!

Thank you for your kind help. Now I can bypass this bug using langestefan and your method and continue executing the code. Don’t worry. 😉
