From 7111683501f6792d2cac2e1bee863df9b96ed2ab Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 14 Nov 2024 16:28:21 +1300 Subject: [PATCH] Fix broadcasting over NonlinearExpr objects (#3881) --- src/nlp_expr.jl | 2 ++ test/test_nlp_expr.jl | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/nlp_expr.jl b/src/nlp_expr.jl index 1436c417f11..6e99783c501 100644 --- a/src/nlp_expr.jl +++ b/src/nlp_expr.jl @@ -104,6 +104,8 @@ struct GenericNonlinearExpr{V<:AbstractVariableRef} <: AbstractJuMPScalar end end +Base.broadcastable(f::GenericNonlinearExpr) = Ref(f) + variable_ref_type(::Type{GenericNonlinearExpr}, ::Any) = nothing function variable_ref_type(::Type{GenericNonlinearExpr}, x::AbstractJuMPScalar) diff --git a/test/test_nlp_expr.jl b/test/test_nlp_expr.jl index a9401175868..8bb131dfa4f 100644 --- a/test/test_nlp_expr.jl +++ b/test/test_nlp_expr.jl @@ -1134,4 +1134,14 @@ function test_promote_type() return end +function test_broadcastable() + model = Model() + @variable(model, x, start = 2) + f = sin(x)^2 + @test Base.broadcastable(f) isa Base.RefValue{NonlinearExpr} + @test value(start_value, f) ≈ sin(2)^2 + @test value.(start_value, f) ≈ sin(2)^2 + return +end + end # module