# Weird empty sum behavior in JuMP

**URL:** <https://discourse.julialang.org/t/weird-empty-sum-behavior-in-jump/113386>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, sum\
**Created:** [April 23, 2024, 10:56am UTC](https://discourse.julialang.org/t/weird-empty-sum-behavior-in-jump/113386 "2024-04-23T10:56:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KSepetanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksepetanc/32/10841_2.png) [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Post date:** [April 23, 2024, 10:56am UTC](https://discourse.julialang.org/t/weird-empty-sum-behavior-in-jump/113386/1 "2024-04-23T10:56:31Z")

</div>

I am coding basic AC OPF model.

The following sum results in an error:  
`-(Vd[n]^2+Vq[n]^2)*sum(gs[sh] for sh in bus_shunts[n])`

`LoadError: MethodError: no method matching zero(::Type{MutableArithmetics.Zero}) Closest candidates are: zero(!Matched::Type{Union{}}, Any...) `

However, the following example does not result in error:  
`-(Vd[n]^2)*sum(gs[sh] for sh in bus_shunts[n])`  
`-(Vq[n]^2)*sum(gs[sh] for sh in bus_shunts[n])`

Also for reactive power where there is one nonzero entry does not result in an error:  
`+(Vd[n]^2+Vq[n]^2)*sum(bs[sh] for sh in bus_shunts[n])`

I am aware that setting init value to zero will avoid the error (;init=0), however I do not understand in which cases do I need to define init value. All other sums in JuMP for OPF formulation work ok without init value and putting init to all of them would make the code long and ugly. Any suggestions on why such behavior that it gives error when multiplying with a sum of two variables but no error when multiplied with single variable.

---

<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 23, 2024, 8:58pm UTC](https://discourse.julialang.org/t/weird-empty-sum-behavior-in-jump/113386/2 "2024-04-23T20:58:29Z")

</div>

Thanks for reporting this. I can reproduce it:

```Julia
julia> model = Model();

julia> @variable(model, x);

julia> @expression(model, -x^2 * sum(x for i in 1:0))
ERROR: MethodError: no method matching zero(::Type{MutableArithmetics.Zero})

Closest candidates are:
  zero(::Type{Union{}}, Any...)
   @ Base number.jl:310
  zero(::Type{Pkg.Resolve.FieldValue})
   @ Pkg ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/Pkg/src/Resolve/fieldvalues.jl:38
  zero(::Type{Missing})
   @ Base missing.jl:104
  ...

Stacktrace:
  [1] _instantiate_zero(::Type{MutableArithmetics.Zero})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:28
  [2] promote_operation_fallback(op::typeof(*), ::Type{QuadExpr}, ::Type{MutableArithmetics.Zero})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:51
  [3] promote_operation(::typeof(*), ::Type, ::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:113
  [4] mutability(::Type, ::Function, ::Type, ::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:273
  [5] mutability(::QuadExpr, ::Function, ::QuadExpr, ::MutableArithmetics.Zero)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:281
  [6] operate!!(::typeof(*), ::QuadExpr, ::MutableArithmetics.Zero)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:616
  [7] macro expansion
    @ ~/.julia/dev/MutableArithmetics/src/rewrite.jl:321 [inlined]
  [8] macro expansion
    @ ~/.julia/dev/JuMP/src/macros.jl:257 [inlined]
  [9] macro expansion
    @ ~/.julia/dev/JuMP/src/macros/@expression.jl:86 [inlined]
 [10] macro expansion
    @ ~/.julia/dev/JuMP/src/macros.jl:393 [inlined]
 [11] top-level scope
    @ ./REPL[37]:1

```

I’ll open an issue to get this fixed: [MethodError with empty summations · Issue #3736 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/issues/3736)

> I am aware that setting init value to zero will avoid the error (;init=0)

Yes, this will have to be the work-around for now.

---

<div class="post-metadata">

**Author:** ![KSepetanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksepetanc/32/10841_2.png) [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Post date:** [April 24, 2024, 12:45pm UTC](https://discourse.julialang.org/t/weird-empty-sum-behavior-in-jump/113386/3 "2024-04-24T12:45:47Z")

</div>

Thanks for creating the issue. I’ve created other issues for MadNLP on GPU and ExaModels directly on Github this time. Not sure if these packages are related to you, but I think we will need to solve them. In particular, I am eager to see Fp32 GPU optimization working. Most of us do not have good Fp64 GPU as they are expensive.

---

<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 24, 2024, 9:36pm UTC](https://discourse.julialang.org/t/weird-empty-sum-behavior-in-jump/113386/4 "2024-04-24T21:36:57Z")

</div>

This should now work if you update your packages. (Although if the summation can be over zero elements, it’s probably always good practice to use `init`.)

Dropping links to the issues you opened at MadNLP [CUDSS internal error (invalid argument: inertia correction) · Issue #333 · MadNLP/MadNLP.jl · GitHub](https://github.com/MadNLP/MadNLP.jl/issues/333) and ExaModels [convert\_data Float32 support and missing documantation · Issue #86 · exanauts/ExaModels.jl · GitHub](https://github.com/exanauts/ExaModels.jl/issues/86). I’m sure @sshin23 will be along to help you out.

(Note that the only packages which are “official” JuMP packages are those in the `jump-dev` GitHub repository: [Repositories · GitHub](https://github.com/orgs/jump-dev/repositories). See also [Introduction · JuMP](https://jump.dev/JuMP.jl/stable/packages/solvers/) and [Introduction · JuMP](https://jump.dev/JuMP.jl/stable/extensions/introduction/.))
