@@ -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
179180end
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
185187end
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
191194end
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
196200end
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
200205end
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... )
204210end
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
211217end
212218
213219function _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
220227end
221228function _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
228235end
229236function _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
235242end
236243function _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
241248end
242249function _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... )
252259end
253260function _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... )... )
256264end
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... )
263271end
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
274282end
0 commit comments