@@ -17,6 +17,7 @@ function MA.mutable_copy(func::MOI.ScalarAffineFunction)
1717 ]
1818 return MOI. ScalarAffineFunction (terms, MA. copy_if_mutable (func. constant))
1919end
20+
2021function MA. mutable_copy (func:: MOI.ScalarQuadraticFunction )
2122 affine_terms = [
2223 MOI. ScalarAffineTerm (MA. copy_if_mutable (t. coefficient), t. variable)
@@ -104,20 +105,23 @@ function MA.promote_operation(
104105) where {T}
105106 return promote_operation (op, T, F, G)
106107end
108+
107109function MA. promote_operation (
108110 op:: PROMOTE_IMPLEMENTED_OP ,
109111 F:: Type{<:TypedLike{T}} ,
110112 G:: Type{T} ,
111113) where {T}
112114 return promote_operation (op, T, F, G)
113115end
116+
114117function MA. promote_operation (
115118 op:: PROMOTE_IMPLEMENTED_OP ,
116119 F:: Type{<:Number} ,
117120 G:: Type{<:Union{MOI.VariableIndex,MOI.VectorOfVariables}} ,
118121)
119122 return promote_operation (op, F, F, G)
120123end
124+
121125function MA. promote_operation (
122126 op:: PROMOTE_IMPLEMENTED_OP ,
123127 F:: Type{<:Union{MOI.VariableIndex,MOI.VectorOfVariables}} ,
@@ -134,6 +138,7 @@ function MA.operate!(
134138 f. constant = op (f. constant)
135139 return f
136140end
141+
137142function MA. operate! (
138143 op:: Union{typeof(zero),typeof(one)} ,
139144 f:: MOI.ScalarQuadraticFunction ,
@@ -150,11 +155,13 @@ function MA.operate!(::typeof(-), f::MOI.ScalarQuadraticFunction)
150155 f. constant = - f. constant
151156 return f
152157end
158+
153159function MA. operate! (:: typeof (- ), f:: MOI.ScalarAffineFunction )
154160 operate_terms! (- , f. terms)
155161 f. constant = - f. constant
156162 return f
157163end
164+
158165function MA. operate! (
159166 op:: Union{typeof(+),typeof(-)} ,
160167 f:: MOI.ScalarAffineFunction{T} ,
@@ -163,6 +170,7 @@ function MA.operate!(
163170 f. constant = op (f. constant, g)
164171 return f
165172end
173+
166174function MA. operate! (
167175 op:: Union{typeof(+),typeof(-)} ,
168176 f:: MOI.ScalarAffineFunction{T} ,
@@ -171,6 +179,7 @@ function MA.operate!(
171179 push! (f. terms, MOI. ScalarAffineTerm (op (one (T)), g))
172180 return f
173181end
182+
174183function MA. operate_to! (
175184 output:: MOI.ScalarAffineFunction{T} ,
176185 op:: Union{typeof(+),typeof(-)} ,
@@ -183,6 +192,7 @@ function MA.operate_to!(
183192 output. constant = op (f. constant, g. constant)
184193 return output
185194end
195+
186196function MA. operate_to! (
187197 output:: MOI.ScalarQuadraticFunction{T} ,
188198 op:: Union{typeof(+),typeof(-)} ,
@@ -198,6 +208,7 @@ function MA.operate_to!(
198208 output. constant = op (f. constant, g. constant)
199209 return output
200210end
211+
201212function MA. operate! (
202213 op:: Union{typeof(+),typeof(-)} ,
203214 f:: MOI.ScalarAffineFunction{T} ,
@@ -207,6 +218,7 @@ function MA.operate!(
207218 f. constant = op (f. constant, g. constant)
208219 return f
209220end
221+
210222function MA. operate! (
211223 op:: Union{typeof(+),typeof(-)} ,
212224 f:: MOI.ScalarQuadraticFunction{T} ,
@@ -215,6 +227,7 @@ function MA.operate!(
215227 f. constant = op (f. constant, g)
216228 return f
217229end
230+
218231function MA. operate! (
219232 op:: Union{typeof(+),typeof(-)} ,
220233 f:: MOI.ScalarQuadraticFunction{T} ,
@@ -223,6 +236,7 @@ function MA.operate!(
223236 push! (f. affine_terms, MOI. ScalarAffineTerm (op (one (T)), g))
224237 return f
225238end
239+
226240function MA. operate! (
227241 op:: Union{typeof(+),typeof(-)} ,
228242 f:: MOI.ScalarQuadraticFunction{T} ,
@@ -232,6 +246,7 @@ function MA.operate!(
232246 f. constant = op (f. constant, g. constant)
233247 return f
234248end
249+
235250function MA. operate! (
236251 op:: Union{typeof(+),typeof(-)} ,
237252 f:: MOI.ScalarQuadraticFunction{T} ,
@@ -244,10 +259,13 @@ function MA.operate!(
244259end
245260
246261_constant (:: Type{T} , α:: T ) where {T} = α
262+
247263_constant (:: Type{T} , :: MOI.VariableIndex ) where {T} = zero (T)
264+
248265_constant (:: Type{T} , func:: TypedScalarLike{T} ) where {T} = MOI. constant (func, T)
249266
250267_affine_terms (f:: MOI.ScalarAffineFunction ) = f. terms
268+
251269_affine_terms (f:: MOI.ScalarQuadraticFunction ) = f. affine_terms
252270
253271function _add_sub_affine_terms (
@@ -260,6 +278,7 @@ function _add_sub_affine_terms(
260278 push! (terms, MOI. ScalarAffineTerm (op (α * β), f))
261279 return
262280end
281+
263282function _add_sub_affine_terms (
264283 op:: Union{typeof(+),typeof(-)} ,
265284 terms:: Vector{MOI.ScalarAffineTerm{T}} ,
@@ -269,6 +288,7 @@ function _add_sub_affine_terms(
269288 push! (terms, MOI. ScalarAffineTerm (op (β), f))
270289 return
271290end
291+
272292function _add_sub_affine_terms (
273293 op:: Union{typeof(+),typeof(-)} ,
274294 terms:: Vector{MOI.ScalarAffineTerm{T}} ,
@@ -278,6 +298,7 @@ function _add_sub_affine_terms(
278298 push! (terms, MOI. ScalarAffineTerm (op (α), f))
279299 return
280300end
301+
281302function _add_sub_affine_terms (
282303 op:: Union{typeof(+),typeof(-)} ,
283304 terms:: Vector{MOI.ScalarAffineTerm{T}} ,
@@ -297,7 +318,9 @@ function _add_sub_affine_terms(
297318 for t in _affine_terms (f)
298319 push! (terms, operate_term (op, operate_term (* , α, t, β)))
299320 end
321+ return
300322end
323+
301324function _add_sub_affine_terms (
302325 op:: Union{typeof(+),typeof(-)} ,
303326 terms:: Vector{MOI.ScalarAffineTerm{T}} ,
@@ -307,7 +330,9 @@ function _add_sub_affine_terms(
307330 for t in _affine_terms (f)
308331 push! (terms, operate_term (op, operate_term (* , t, β)))
309332 end
333+ return
310334end
335+
311336function _add_sub_affine_terms (
312337 op:: Union{typeof(+),typeof(-)} ,
313338 terms:: Vector{MOI.ScalarAffineTerm{T}} ,
@@ -317,7 +342,9 @@ function _add_sub_affine_terms(
317342 for t in _affine_terms (f)
318343 push! (terms, operate_term (op, operate_term (* , α, t)))
319344 end
345+ return
320346end
347+
321348function _add_sub_affine_terms (
322349 op:: Union{typeof(+),typeof(-)} ,
323350 terms:: Vector{MOI.ScalarAffineTerm{T}} ,
@@ -326,21 +353,24 @@ function _add_sub_affine_terms(
326353 append! (terms, operate_terms (op, _affine_terms (f)))
327354 return
328355end
356+
329357function _add_sub_affine_terms (
330358 op:: Union{typeof(+),typeof(-)} ,
331359 terms:: Vector{MOI.ScalarAffineTerm{T}} ,
332360 args:: Vararg{T,N} ,
333361) where {T,N}
334362 return
335363end
364+
336365function _add_sub_affine_terms (
337366 op:: Union{typeof(+),typeof(-)} ,
338367 terms:: Vector{MOI.ScalarAffineTerm{T}} ,
339368 α:: T ,
340369 β:: T ,
341370 args:: Vararg{ScalarQuadraticLike,N} ,
342371) where {T,N}
343- return _add_sub_affine_terms (op, terms, α * β, args... )
372+ _add_sub_affine_terms (op, terms, α * β, args... )
373+ return
344374end
345375
346376function MA. operate! (
@@ -369,7 +399,9 @@ function _add_quadratic_terms(
369399 ),
370400 )
371401 end
402+ return
372403end
404+
373405function _add_quadratic_terms (
374406 op:: Union{typeof(+),typeof(-)} ,
375407 terms:: Vector{MOI.ScalarQuadraticTerm{T}} ,
@@ -379,7 +411,9 @@ function _add_quadratic_terms(
379411 for t in f. quadratic_terms
380412 push! (terms, operate_term (op, operate_term (* , t, _constant (T, β))))
381413 end
414+ return
382415end
416+
383417function _add_quadratic_terms (
384418 op:: Union{typeof(+),typeof(-)} ,
385419 terms:: Vector{MOI.ScalarQuadraticTerm{T}} ,
@@ -389,7 +423,9 @@ function _add_quadratic_terms(
389423 for t in f. quadratic_terms
390424 push! (terms, operate_term (op, operate_term (* , _constant (T, α), t)))
391425 end
426+ return
392427end
428+
393429function _add_quadratic_terms (
394430 op:: Union{typeof(+),typeof(-)} ,
395431 terms:: Vector{MOI.ScalarQuadraticTerm{T}} ,
@@ -398,6 +434,7 @@ function _add_quadratic_terms(
398434 append! (terms, operate_terms (op, f. quadratic_terms))
399435 return
400436end
437+
401438function _add_quadratic_terms (
402439 op:: Union{typeof(+),typeof(-)} ,
403440 terms:: Vector{MOI.ScalarQuadraticTerm{T}} ,
@@ -407,6 +444,7 @@ function _add_quadratic_terms(
407444) where {T}
408445 return
409446end
447+
410448function _merge_constants (
411449 :: Type{T} ,
412450 α:: ScalarAffineLike{T} ,
@@ -415,23 +453,28 @@ function _merge_constants(
415453) where {T,N}
416454 return (_constant (T, α) * _constant (T, β), args... )
417455end
456+
418457function _add_quadratic_terms (
419458 op:: Union{typeof(+),typeof(-)} ,
420459 terms:: Vector{MOI.ScalarQuadraticTerm{T}} ,
421460 args:: Vararg{Any,N} ,
422461) where {T,N}
423- return _add_quadratic_terms (op, terms, _merge_constants (T, args... )... )
462+ _add_quadratic_terms (op, terms, _merge_constants (T, args... )... )
463+ return
424464end
425465
426466_num_function_with_terms (:: Type{T} , :: T ) where {T} = 0
467+
427468_num_function_with_terms (:: Type{T} , :: ScalarLike{T} ) where {T} = 1
469+
428470function _num_function_with_terms (
429471 :: Type{T} ,
430472 f:: ScalarQuadraticLike{T} ,
431473 args:: Vararg{ScalarQuadraticLike{T},N} ,
432474) where {T,N}
433475 return _num_function_with_terms (T, f) + _num_function_with_terms (T, args... )
434476end
477+
435478function MA. operate! (
436479 op:: MA.AddSubMul ,
437480 f:: MOI.ScalarQuadraticFunction{T} ,
@@ -446,6 +489,7 @@ function MA.operate!(
446489 return MA. operate! (MA. add_sub_op (op), f, * (args... ))
447490 end
448491end
492+
449493# `args` could be `(x', a)` where `a` is a vector of constants and `x` a vector
450494# of affine functions for instance.
451495function MA. operate! (
0 commit comments