Skip to content

Commit

Permalink
Fix bound calculation for zero functions + Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Oct 25, 2024
1 parent 77b0305 commit 4d65e63
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/library/function/abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ function mingap(f::F; tol::T = 1E-6) where {V,T,F<:AbstractPBF{V,T}}
end

function lowerbound(f::F) where {V,T,F<:AbstractPBF{V,T}}
return sum((c < zero(T) || isempty(ω)) ? c : zero(T) for (ω, c) in f)
return sum((c < zero(T) || isempty(ω)) ? c : zero(T) for (ω, c) in f; init = zero(T))
end

function upperbound(f::F) where {V,T,F<:AbstractPBF{V,T}}
return sum((c > zero(T) || isempty(ω)) ? c : zero(T) for (ω, c) in f)
return sum((c > zero(T) || isempty(ω)) ? c : zero(T) for (ω, c) in f; init = zero(T))
end

function Base.convert(::Type{U}, f::AbstractPBF{V,T}) where {V,T,U<:Number}
Expand Down
17 changes: 17 additions & 0 deletions test/unit/bounds.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function test_bounds()
@testset "Bounds" begin
let f = PBO.PBF{Symbol,Float64}(:x => 1.0, :y => -1.0)
@test PBO.lowerbound(f) -1.0
@test PBO.upperbound(f) 1.0
@test all(PBO.bounds(f) .≈ (-1.0, 1.0))
end

let f = zero(PBO.PBF{Symbol,Float64})
@test PBO.lowerbound(f) 0.0
@test PBO.upperbound(f) 0.0
@test all(PBO.bounds(f) .≈ (0.0, 0.0))
end
end

return nothing
end
2 changes: 2 additions & 0 deletions test/unit/unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include("term_parser.jl")
include("constructors.jl")
include("operators.jl")
include("calculus.jl")
include("bounds.jl")
# include("discretization.jl")
include("quadratization.jl")
include("print.jl")
Expand All @@ -14,6 +15,7 @@ function unit_tests()
test_constructors()
test_operators()
test_calculus()
test_bounds()
# test_discretization()
test_quadratization()
test_print()
Expand Down

0 comments on commit 4d65e63

Please sign in to comment.