# Managing unnecessary 0's in JuMP

**URL:** <https://discourse.julialang.org/t/managing-unnecessary-0s-in-jump/10414>\
**Category:** Optimization (Mathematical)\
**Created:** [April 18, 2018, 2:32pm UTC](https://discourse.julialang.org/t/managing-unnecessary-0s-in-jump/10414 "2018-04-18T14:32:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 18, 2018, 2:32pm UTC](https://discourse.julialang.org/t/managing-unnecessary-0s-in-jump/10414/1 "2018-04-18T14:32:01Z")

</div>

I have a rather ugly quadratic objective function that requires a fair amount of manipulation of variables, and I suspect that one of the things that’s making this process incredibly slow is that I have lots of `GenericAffExpr` that are carrying around lots of unnecessary variables that have `0.0` coefficients. Is there an elegant way of dropping these?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [April 18, 2018, 2:45pm UTC](https://discourse.julialang.org/t/managing-unnecessary-0s-in-jump/10414/2 "2018-04-18T14:45:17Z")

</div>

May you post a MWE?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 18, 2018, 2:48pm UTC](https://discourse.julialang.org/t/managing-unnecessary-0s-in-jump/10414/3 "2018-04-18T14:48:55Z")

</div>

Here’s the (incredibly hackish) code I wrote for doing this, I guess my question was whether there was an equivalent function already available in JuMP, which perhaps would have been better to use depending on the context

```julia
function _dropzeros(ex::T) where T<:JuMP.GenericAffExpr
    mask = Bool[x > eps(Float32) for x ∈ ex.ceoffs]
    T(ex.vars[mask], ex.coeffs[mask], ex.constant)
end     

```

This seems to work quite well, by the way, and is much faster than I was expecting it to be.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [April 18, 2018, 4:18pm UTC](https://discourse.julialang.org/t/managing-unnecessary-0s-in-jump/10414/4 "2018-04-18T16:18:01Z")

</div>

I’m planning on changing `AffExpr` and `QuadExpr` to use a dictionary instead of a list of coefficients for JuMP 0.19 ([discussion](https://github.com/JuliaOpt/MathOptInterface.jl/issues/234#issuecomment-366868878)). That would resolve this issue.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 18, 2018, 4:27pm UTC](https://discourse.julialang.org/t/managing-unnecessary-0s-in-jump/10414/5 "2018-04-18T16:27:37Z")

</div>

That sounds wonderful. I’ve been noticing that things start getting _really_ ugly with quadratic expressions, it gets to the point that everything requires custom code to do efficiently.
