Skip to content

Commit 633bb7c

Browse files
authored
Merge pull request #981 from JuliaOpt/bl/MAv0.2
Update to MA v0.2
2 parents 9d3cf0a + c0434a0 commit 633bb7c

3 files changed

Lines changed: 52 additions & 39 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
1313

1414
[compat]
1515
BenchmarkTools = "0.4"
16-
MutableArithmetics = "0.1.1"
16+
MutableArithmetics = "0.2"
1717
OrderedCollections = "1"
1818
julia = "1"

src/Utilities/functions.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,11 @@ function promote_operation end
793793

794794
# Helpers
795795

796+
function operate_term(::typeof(+), term::Union{
797+
MOI.ScalarAffineTerm, MOI.ScalarQuadraticTerm,
798+
MOI.VectorAffineTerm, MOI.VectorQuadraticTerm})
799+
return term
800+
end
796801
function operate_term(::typeof(-), term::MOI.ScalarAffineTerm)
797802
return MOI.ScalarAffineTerm(-term.coefficient, term.variable_index)
798803
end

src/Utilities/mutable_arithmetics.jl

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -171,76 +171,83 @@ _constant(::Type{T}, func::TypedScalarLike{T}) where {T} = MOI.constant(func)
171171
_affine_terms(f::MOI.ScalarAffineFunction) = f.terms
172172
_affine_terms(f::MOI.ScalarQuadraticFunction) = f.affine_terms
173173

174-
function _add_affine_terms(terms::Vector{MOI.ScalarAffineTerm{T}}, α::T,
175-
f::TypedScalarLike{T}, β::T) where T
174+
function _add_sub_affine_terms(
175+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarAffineTerm{T}},
176+
α::T, f::TypedScalarLike{T}, β::T) where T
176177
for t in _affine_terms(f)
177-
push!(terms, operate_term(*, α, t, β))
178+
push!(terms, operate_term(op, operate_term(*, α, t, β)))
178179
end
179180
end
180-
function _add_affine_terms(terms::Vector{MOI.ScalarAffineTerm{T}},
181-
f::TypedScalarLike{T}, β::T) where T
181+
function _add_sub_affine_terms(
182+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarAffineTerm{T}},
183+
f::TypedScalarLike{T}, β::T) where T
182184
for t in _affine_terms(f)
183-
push!(terms, operate_term(*, t, β))
185+
push!(terms, operate_term(op, operate_term(*, t, β)))
184186
end
185187
end
186-
function _add_affine_terms(terms::Vector{MOI.ScalarAffineTerm{T}}, α::T,
187-
f::TypedScalarLike{T}) where T
188+
function _add_sub_affine_terms(
189+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarAffineTerm{T}},
190+
α::T, f::TypedScalarLike{T}) where T
188191
for t in _affine_terms(f)
189-
push!(terms, operate_term(*, α, t))
192+
push!(terms, operate_term(op, operate_term(*, α, t)))
190193
end
191194
end
192-
function _add_affine_terms(terms::Vector{MOI.ScalarAffineTerm{T}},
193-
f::TypedScalarLike{T}) where T
194-
append!(terms, _affine_terms(f))
195+
function _add_sub_affine_terms(
196+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarAffineTerm{T}},
197+
f::TypedScalarLike{T}) where T
198+
append!(terms, operate_terms(op, _affine_terms(f)))
195199
return
196200
end
197-
function _add_affine_terms(terms::Vector{MOI.ScalarAffineTerm{T}},
198-
args::Vararg{T, N}) where {T, N}
201+
function _add_sub_affine_terms(
202+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarAffineTerm{T}},
203+
args::Vararg{T, N}) where {T, N}
199204
return
200205
end
201-
function _add_affine_terms(terms::Vector{MOI.ScalarAffineTerm{T}}, α::T, β::T,
202-
args::Vararg{ScalarQuadraticLike, N}) where {T, N}
203-
_add_affine_terms(terms, α * β, args...)
206+
function _add_sub_affine_terms(
207+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarAffineTerm{T}},
208+
α::T, β::T, args::Vararg{ScalarQuadraticLike, N}) where {T, N}
209+
_add_sub_affine_terms(op, terms, α * β, args...)
204210
end
205211

206-
function MA.mutable_operate!(::typeof(MA.add_mul), f::MOI.ScalarAffineFunction{T},
212+
function MA.mutable_operate!(op::MA.AddSubMul, f::MOI.ScalarAffineFunction{T},
207213
args::Vararg{ScalarAffineLike{T}, N}) where {T, N}
208-
f.constant = MA.add_mul(f.constant, _constant.(T, args)...)
209-
_add_affine_terms(f.terms, args...)
214+
f.constant = op(f.constant, _constant.(T, args)...)
215+
_add_sub_affine_terms(MA.add_sub_op(op), f.terms, args...)
210216
return f
211217
end
212218

213219
function _add_quadratic_terms(
214-
terms::Vector{MOI.ScalarQuadraticTerm{T}}, α::ScalarAffineLike{T},
215-
f::MOI.ScalarQuadraticFunction{T}, β::ScalarAffineLike{T}) where T
220+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarQuadraticTerm{T}},
221+
α::ScalarAffineLike{T}, f::MOI.ScalarQuadraticFunction{T},
222+
β::ScalarAffineLike{T}) where T
216223

217224
for t in f.quadratic_terms
218-
push!(terms, operate_term(*, _constant(T, α), t, _constant(T, β)))
225+
push!(terms, operate_term(op, operate_term(*, _constant(T, α), t, _constant(T, β))))
219226
end
220227
end
221228
function _add_quadratic_terms(
222-
terms::Vector{MOI.ScalarQuadraticTerm{T}},
229+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarQuadraticTerm{T}},
223230
f::MOI.ScalarQuadraticFunction{T}, β::ScalarAffineLike{T}) where T
224231

225232
for t in f.quadratic_terms
226-
push!(terms, operate_term(*, t, _constant(T, β)))
233+
push!(terms, operate_term(op, operate_term(*, t, _constant(T, β))))
227234
end
228235
end
229236
function _add_quadratic_terms(
230-
terms::Vector{MOI.ScalarQuadraticTerm{T}}, α::ScalarAffineLike{T},
231-
f::MOI.ScalarQuadraticFunction{T}) where T
237+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarQuadraticTerm{T}},
238+
α::ScalarAffineLike{T}, f::MOI.ScalarQuadraticFunction{T}) where T
232239
for t in f.quadratic_terms
233-
push!(terms, operate_term(*, _constant(T, α), t))
240+
push!(terms, operate_term(op, operate_term(*, _constant(T, α), t)))
234241
end
235242
end
236243
function _add_quadratic_terms(
237-
terms::Vector{MOI.ScalarQuadraticTerm{T}},
244+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarQuadraticTerm{T}},
238245
f::MOI.ScalarQuadraticFunction{T}) where T
239-
append!(terms, f.quadratic_terms)
246+
append!(terms, operate_terms(op, f.quadratic_terms))
240247
return
241248
end
242249
function _add_quadratic_terms(
243-
terms::Vector{MOI.ScalarQuadraticTerm{T}},
250+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarQuadraticTerm{T}},
244251
# Compiler fails in StackOverflowError on Julia v1.1
245252
#args::Vararg{ScalarAffineLike{T}, N}) where {T, N}
246253
args::ScalarAffineLike{T}) where T
@@ -251,8 +258,9 @@ function _merge_constants(::Type{T}, α::ScalarAffineLike{T}, β::ScalarAffineLi
251258
return (_constant(T, α) * _constant(T, β), args...)
252259
end
253260
function _add_quadratic_terms(
254-
terms::Vector{MOI.ScalarQuadraticTerm{T}}, args::Vararg{Any, N}) where {T, N}
255-
_add_quadratic_terms(terms, _merge_constants(T, args...)...)
261+
op::Union{typeof(+), typeof(-)}, terms::Vector{MOI.ScalarQuadraticTerm{T}},
262+
args::Vararg{Any, N}) where {T, N}
263+
_add_quadratic_terms(op, terms, _merge_constants(T, args...)...)
256264
end
257265

258266
_num_function_with_terms(::Type{T}, ::T) where {T} = 0
@@ -261,14 +269,14 @@ function _num_function_with_terms(::Type{T}, f::ScalarQuadraticLike{T},
261269
args::Vararg{ScalarQuadraticLike{T}, N}) where {T, N}
262270
return _num_function_with_terms(T, f) + _num_function_with_terms(T, args...)
263271
end
264-
function MA.mutable_operate!(::typeof(MA.add_mul), f::MOI.ScalarQuadraticFunction{T},
272+
function MA.mutable_operate!(op::MA.AddSubMul, f::MOI.ScalarQuadraticFunction{T},
265273
args::Vararg{ScalarQuadraticLike{T}, N}) where {T, N}
266274
if isone(_num_function_with_terms(T, args...))
267-
f.constant = MA.add_mul(f.constant, _constant.(T, args)...)
268-
_add_affine_terms(f.affine_terms, args...)
269-
_add_quadratic_terms(f.quadratic_terms, args...)
275+
f.constant = op(f.constant, _constant.(T, args)...)
276+
_add_sub_affine_terms(MA.add_sub_op(op), f.affine_terms, args...)
277+
_add_quadratic_terms(MA.add_sub_op(op), f.quadratic_terms, args...)
270278
return f
271279
else
272-
return MA.mutable_operate!(+, f, *(args...))
280+
return MA.mutable_operate!(MA.add_sub_op(op), f, *(args...))
273281
end
274282
end

0 commit comments

Comments
 (0)