Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Aug 1, 2024
1 parent f1faddd commit f0276e7
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/macros/@constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -599,19 +599,18 @@ julia> @constraint(model, A * x >= b)
"""
struct Nonnegatives end

operator_to_set(::Function, ::Union{Val{:(>=)},Val{:(≥)}}) = Nonnegatives()

"""
_OpGreaterThan()
GreaterThanMacroSet()
A struct used to intercept `>=` when used in macros.
"""
struct _OpGreaterThan end
operator_to_set(f::Function, sym::Val, ::Val{true}) = operator_to_set(f, sym)
This is not the same as [`Nonnegatives`](@ref) so that we can disambiguate
`x >= y` and `x - y in Nonnegatives()`.
"""
struct GreaterThanMacroSet end

function operator_to_set(::Function, ::Union{Val{:(>=)},Val{:(≥)}}, ::Val{true})
return _OpGreaterThan()
function operator_to_set(::Function, ::Union{Val{:(>=)},Val{:(≥)}})
return GreaterThanMacroSet()
end

"""
Expand Down Expand Up @@ -643,12 +642,18 @@ julia> @constraint(model, A * x <= b)
"""
struct Nonpositives end

operator_to_set(::Function, ::Union{Val{:(<=)},Val{:(≤)}}) = Nonpositives()
"""
LessThanMacroSet()
A struct used to intercept `<=` when used in macros.
struct _OpLessThan end
This is not the same as [`Nonpositives`](@ref) so that we can disambiguate
`x <= y` and `x - y in Nonpositives()`.
"""
struct LessThanMacroSet end

function operator_to_set(::Function, ::Union{Val{:(<=)},Val{:(≤)}}, ::Val{true})
return _OpLessThan()
function operator_to_set(::Function, ::Union{Val{:(<=)},Val{:(≤)}})
return LessThanMacroSet()
end

"""
Expand Down Expand Up @@ -799,7 +804,7 @@ function parse_constraint_call(
)
func = vectorized ? :($lhs .- $rhs) : :($lhs - $rhs)
f, parse_code = _rewrite_expression(func)
set = operator_to_set(error_fn, operator, Val{true}())
set = operator_to_set(error_fn, operator)
# `_functionize` deals with the pathological case where the `lhs` is a
# `VariableRef` and the `rhs` is a summation with no terms.
f = :(_functionize($f))
Expand All @@ -814,7 +819,7 @@ end
function build_constraint(
error_fn::Function,
f,
::_OpGreaterThan,
::GreaterThanMacroSet,
args...;
kwargs...,
)
Expand All @@ -824,7 +829,7 @@ end
function build_constraint(
error_fn::Function,
::Union{Matrix,LinearAlgebra.Symmetric,LinearAlgebra.Hermitian},
::_OpGreaterThan,
::GreaterThanMacroSet,
)
return error_fn(
"""
Expand Down Expand Up @@ -854,7 +859,7 @@ end
function build_constraint(
error_fn::Function,
f,
::_OpLessThan,
::LessThanMacroSet
args...;
kwargs...,
)
Expand All @@ -864,7 +869,7 @@ end
function build_constraint(
error_fn::Function,
::Union{Matrix,LinearAlgebra.Symmetric,LinearAlgebra.Hermitian},
::_OpLessThan,
::LessThanMacroSet
)
return error_fn(
"""
Expand Down

0 comments on commit f0276e7

Please sign in to comment.