From 06c567e5209585911584988f9482b99106b38850 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 22 Sep 2023 13:18:18 +1200 Subject: [PATCH 1/2] Fix unary min and max operators --- src/nlp_expr.jl | 5 +++++ test/test_nlp_expr.jl | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/nlp_expr.jl b/src/nlp_expr.jl index 7d3f87c141f..e77e9cd09da 100644 --- a/src/nlp_expr.jl +++ b/src/nlp_expr.jl @@ -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, diff --git a/test/test_nlp_expr.jl b/test/test_nlp_expr.jl index 1c28c9ed6d0..9c9862e9e98 100644 --- a/test/test_nlp_expr.jl +++ b/test/test_nlp_expr.jl @@ -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 @@ -962,6 +965,10 @@ 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 From d870b99f2524de5f5ccd3cc285006dec952a90d8 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 22 Sep 2023 13:23:28 +1200 Subject: [PATCH 2/2] Update test/test_nlp_expr.jl --- test/test_nlp_expr.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_nlp_expr.jl b/test/test_nlp_expr.jl index 9c9862e9e98..e07dc1ed036 100644 --- a/test/test_nlp_expr.jl +++ b/test/test_nlp_expr.jl @@ -968,7 +968,6 @@ function test_operator_max() for f in (x, 1.0 * x + 2.0, x^2, sin(x)) @test max(f) === f end - return end