Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 6 additions & 0 deletions src/parse_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ function destructive_add!(ex::AbstractArray{<:GenericAffExpr}, c::Number,
return result
end

# For cases such as x' * x, broadcasting the ex .+ will cause the Adjoint to be
# collected into an Array{T, 2}. The easiest way to work around this is to undo
# the adjoint, perform the operation, and then re-apply the adjoint.
function destructive_add!(ex::Number, c::Number, x::Adjoint{T, Vector{T}}) where {T}
return (ex .+ c * x')'
end

destructive_add!(ex, c, x) = ex .+ c * x

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