# Empty sums in expressions

**URL:** <https://discourse.julialang.org/t/empty-sums-in-expressions/32454>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, bug\
**Created:** [December 18, 2019, 8:32pm UTC](https://discourse.julialang.org/t/empty-sums-in-expressions/32454 "2019-12-18T20:32:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![waumpel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waumpel/32/11892_2.png) [@waumpel](https://discourse.julialang.org/u/waumpel)\
**Post date:** [December 18, 2019, 8:32pm UTC](https://discourse.julialang.org/t/empty-sums-in-expressions/32454/1 "2019-12-18T20:32:41Z")

</div>

When you create an expression that is an empty sum, it evaluates to `Val{false}()` instead of `0`:

```julia-repl
model = Model()
@expression(model, sum(1 for i in 1:0))
julia> Val{false}()

```

A similar problem was posted in the GitHub issue [Problem with Empty Summations](https://github.com/JuliaOpt/JuMP.jl/issues/1954) and closed [here](https://github.com/JuliaOpt/JuMP.jl/pull/1955), but it seems to work only for constraints, not for expressions.

Something even weirder happens when you try to create a collection of such expressions:

```julia-repl
model = Model()
@expression(model, [i in 1:2], sum(1 for j in 1:0))
julia> ERROR: Collection of expressions with @expression must be linear.
  For quadratic expressions, use your own array.

```

Is this a or am I doing something wrong?

---

<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:** [December 18, 2019, 8:43pm UTC](https://discourse.julialang.org/t/empty-sums-in-expressions/32454/2 "2019-12-18T20:43:19Z")

</div>

The first case is arguably a bug. I’ve opened an issue: [https://github.com/JuliaOpt/JuMP.jl/issues/2120](https://github.com/JuliaOpt/JuMP.jl/issues/2120)

One issue is that:

```nohighlight
julia> sum(1 for i = 1:0)
ERROR: ArgumentError: reducing over an empty collection is not allowed
Stacktrace:
 [1] _empty_reduce_error() at ./reduce.jl:216
 [2] mapreduce_empty_iter(::Function, ::Function, ::Base.Generator{UnitRange{Int64},getfield(Main, Symbol("##5#6"))}, ::Base.EltypeUnknown) at ./reduce.jl:261
 [3] mapfoldl_impl at ./reduce.jl:57 [inlined]
 [4] #mapfoldl#187 at ./reduce.jl:72 [inlined]
 [5] mapfoldl at ./reduce.jl:72 [inlined]
 [6] #mapreduce#191 at ./reduce.jl:205 [inlined]
 [7] mapreduce at ./reduce.jl:205 [inlined]
 [8] sum at ./reduce.jl:399 [inlined]
 [9] sum(::Base.Generator{UnitRange{Int64},getfield(Main, Symbol("##5#6"))}) at ./reduce.jl:416
 [10] top-level scope at none:0

```

But it [seems like we agreed](https://github.com/JuliaOpt/JuMP.jl/issues/1954#issuecomment-489875627) that empty sums should be `0.0`.

The collection case is fixed (in the sense that it now returns a vector of `Val{false}()`) in the latest release of JuMP.

---

<div class="post-metadata">

**Author:** ![waumpel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waumpel/32/11892_2.png) [@waumpel](https://discourse.julialang.org/u/waumpel)\
**Post date:** [December 20, 2019, 8:00pm UTC](https://discourse.julialang.org/t/empty-sums-in-expressions/32454/3 "2019-12-20T20:00:36Z")

</div>

Thanks!

In the latest _relase_ of JuMP, i.e. version 0.20.1, it actually throws the error I posted above. The result you posted is returned in version 0.20.0, the current _development_ version.
