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?
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?
Most helpful comment
Sure, you could implement
_is_zero(expr)as the fast check and then overloadBase.iszerofor the comprehensive.I think we would probably consider a PR with tests and docs. I've needed this myself at some points.
The JuMP style guide would suggest
drop_zeros(expr).