Skip to content

Commit

Permalink
Merge 9e32bfb into b88eb99
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Apr 8, 2019
2 parents b88eb99 + 9e32bfb commit e00a4e8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/aff_expr.jl
Expand Up @@ -82,7 +82,9 @@ function GenericAffExpr{V,K}(constant, kv::Pair...) where {K,V}
return GenericAffExpr{V,K}(convert(V, constant), _new_ordered_dict(K, V, kv...))
end

Base.iszero(a::GenericAffExpr) = isempty(a.terms) && iszero(a.constant)
function Base.iszero(expr::GenericAffExpr)
return iszero(expr.constant) && all(iszero, values(expr.terms))
end
Base.zero(::Type{GenericAffExpr{C,V}}) where {C,V} = GenericAffExpr{C,V}(zero(C), OrderedDict{V,C}())
Base.one(::Type{GenericAffExpr{C,V}}) where {C,V} = GenericAffExpr{C,V}(one(C), OrderedDict{V,C}())
Base.zero(a::GenericAffExpr) = zero(typeof(a))
Expand Down
4 changes: 3 additions & 1 deletion src/quad_expr.jl
Expand Up @@ -49,7 +49,9 @@ function GenericQuadExpr{V,K}(aff::GenericAffExpr{V,K}, kv::Pair...) where {K,V}
return GenericQuadExpr{V,K}(aff, _new_ordered_dict(UnorderedPair{K}, V, kv...))
end

Base.iszero(q::GenericQuadExpr) = isempty(q.terms) && iszero(q.aff)
function Base.iszero(expr::GenericQuadExpr)
return iszero(expr.aff) && all(iszero, values(expr.terms))
end
function Base.zero(::Type{GenericQuadExpr{C,V}}) where {C,V}
return GenericQuadExpr(zero(GenericAffExpr{C,V}), OrderedDict{UnorderedPair{V}, C}())
end
Expand Down
19 changes: 19 additions & 0 deletions test/expr.jl
Expand Up @@ -22,6 +22,15 @@ function expressions_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType
@test hash(x + 1) == hash(x + 1)
end

@testset "iszero(::GenericAffExpr)" begin
m = ModelType()
@variable(m, x)
@test !iszero(x + 1)
@test !iszero(x + 0)
@test iszero(0 * x + 0)
@test iszero(x - x)
end

@testset "isequal(::GenericQuadExpr)" begin
m = ModelType()
@variable(m, x)
Expand All @@ -34,6 +43,16 @@ function expressions_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType
@test hash(x^2 + 1) == hash(x^2 + 1)
end

@testset "iszero(::GenericQuadExpr)" begin
m = ModelType()
@variable(m, x)
@test !iszero(x^2 + 1)
@test !iszero(x^2 + 0)
@test !iszero(x^2 + 0 * x + 0)
@test iszero(0 * x^2 + 0 * x + 0)
@test iszero(x^2 - x^2)
end

@testset "value for GenericAffExpr" begin
expr1 = JuMP.GenericAffExpr(3.0, 3 => -5.0, 2 => 4.0)
@test @inferred(JuMP.value(expr1, -)) == 10.0
Expand Down

0 comments on commit e00a4e8

Please sign in to comment.