Jump.jl: more tools for determining if expressions are zero

Created on 15 Mar 2019  Â·  2Comments  Â·  Source: jump-dev/JuMP.jl

It would be nice to have a function that determines if, for example, a GenericAffExpr is truly zero. For example

function expr_iszero(expr::GenericAffExpr)  
    iszero(expr.constant) || (return false)  
    for c ∈ values(expr.terms)
        iszero(c) || (return false) 
    end 
    true
end

The current behavior is to check whether the terms are empty. Presumably this is for performance reasons. Since the current iszero doesn't give the correct answer, I wonder if it makes sense to change Base.iszero to the above, but to use a different internal function with the current definition in cases where the efficiency is needed. If so, at the same time we should expose functions for pruning zeros from expressions (perhaps iszero!). Obviously it's still possible to get unexpected answers due to floating point error, but there's nothing we can do about that.

Thoughts?

Most helpful comment

Sure, you could implement _is_zero(expr) as the fast check and then overload Base.iszero for the comprehensive.

I think we would probably consider a PR with tests and docs. I've needed this myself at some points.

we should expose functions for pruning zeros from expressions (perhaps iszero!)

The JuMP style guide would suggest drop_zeros(expr).

All 2 comments

Sure, you could implement _is_zero(expr) as the fast check and then overload Base.iszero for the comprehensive.

I think we would probably consider a PR with tests and docs. I've needed this myself at some points.

we should expose functions for pruning zeros from expressions (perhaps iszero!)

The JuMP style guide would suggest drop_zeros(expr).

@odow, is this resolved now?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chriscoey picture chriscoey  Â·  3Comments

angeris picture angeris  Â·  6Comments

mlubin picture mlubin  Â·  8Comments

Thuener picture Thuener  Â·  7Comments

igormcoelho picture igormcoelho  Â·  3Comments