Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 4 additions & 2 deletions src/parse_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,10 @@ 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.
destructive_add!(ex, c, x) = broadcast(+, ex, c * x)

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
15 changes: 15 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,21 @@ 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])
Q = [1.0 0.0; 0.0 1.0]
obj = @objective(model, Min, x' * Q * x)
@test JuMP.isequal_canonical(obj, sum(x.^2))
cref = @constraint(model, x' * Q * x <= 1)
c = JuMP.constraint_object(cref)
@test JuMP.isequal_canonical(c.func, sum(x.^2))
@test c.set == MOI.LessThan(1.0)
@test JuMP.isequal_canonical(
JuMP.destructive_add!(0.0, x', Q), (1.0 .* x)'
)
end
end

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