Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/parse_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,12 @@ function destructive_add!(ex::AbstractArray{<:GenericAffExpr}, c::Number,
return result
end


destructive_add!(ex, c, x) = ex .+ c * x
# For some reason, the broadcast syntax `ex .+ c * x` fails if `x` is an
# `Adjoint`. But if we explicitly call `broadcast` it seems to work.
# See JuMP PR #1698 for more discussion.
function destructive_add!(ex, c, x)
return Broadcast.materialize(Broadcast.broadcast(+, ex, c * x))
end

destructive_add_with_reorder!(ex, arg) = destructive_add!(ex, 1.0, arg)
# Special case because "Val{false}()" is used as the default empty expression.
Expand Down
11 changes: 11 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,17 @@ end
"DenseAxisArray, or SparseAxisArray.")
@test_throws exception @variable(model, x[1:3], container=Oops)
end

@testset "Adjoints" begin
model = Model()
@variable(model, x[1:2])
obj = @objective(model, Min, x' * x)
@test obj == sum(x.^2)
cref = @constraint(model, x' * x <= 1)
c = JuMP.constraint_object(cref)
@test JuMP.isequal_canonical(c.func, sum(x.^2))
@test c.set == MOI.LessThan(1.0)
end
end

@testset "Macros for JuMPExtension.MyModel" begin
Expand Down