Skip to content

Commit

Permalink
Fix unary min and max operators (#3522)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Sep 22, 2023
1 parent ef4c115 commit 281602d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ for f in (:+, :-, :*, :^, :/, :atan, :min, :max)
end
end

# Base has unary methods `min(x::Real) = x` and `max(x::Real) = x`, so I guess
# we need to replicate them.
Base.min(x::AbstractJuMPScalar) = x
Base.max(x::AbstractJuMPScalar) = x

function _MA.operate!!(
::typeof(_MA.add_mul),
x::GenericNonlinearExpr,
Expand Down
6 changes: 6 additions & 0 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,9 @@ function test_operator_min()
@variable(model, x)
@test isequal_canonical(min(x, 1), NonlinearExpr(:min, Any[x, 1.0]))
@test isequal_canonical(min(1, x, x^2), min(min(1.0, x), x^2))
for f in (x, 1.0 * x + 2.0, x^2, sin(x))
@test min(f) === f
end
return
end

Expand All @@ -962,6 +965,9 @@ function test_operator_max()
@variable(model, x)
@test isequal_canonical(max(x, 1), NonlinearExpr(:max, Any[x, 1.0]))
@test isequal_canonical(max(1, x, x^2), max(max(1.0, x), x^2))
for f in (x, 1.0 * x + 2.0, x^2, sin(x))
@test max(f) === f
end
return
end

Expand Down

0 comments on commit 281602d

Please sign in to comment.