Skip to content

Commit d194b55

Browse files
authored
[Utilities] style fixes for Utilities/mutable_arithmetics.jl (#2209)
1 parent fe27396 commit d194b55

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

src/Utilities/mutable_arithmetics.jl

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function MA.mutable_copy(func::MOI.ScalarAffineFunction)
1717
]
1818
return MOI.ScalarAffineFunction(terms, MA.copy_if_mutable(func.constant))
1919
end
20+
2021
function 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)
106107
end
108+
107109
function 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)
113115
end
116+
114117
function 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)
120123
end
124+
121125
function 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
136140
end
141+
137142
function 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
152157
end
158+
153159
function MA.operate!(::typeof(-), f::MOI.ScalarAffineFunction)
154160
operate_terms!(-, f.terms)
155161
f.constant = -f.constant
156162
return f
157163
end
164+
158165
function 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
165172
end
173+
166174
function 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
173181
end
182+
174183
function 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
185194
end
195+
186196
function 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
200210
end
211+
201212
function 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
209220
end
221+
210222
function 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
217229
end
230+
218231
function 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
225238
end
239+
226240
function 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
234248
end
249+
235250
function MA.operate!(
236251
op::Union{typeof(+),typeof(-)},
237252
f::MOI.ScalarQuadraticFunction{T},
@@ -244,10 +259,13 @@ function MA.operate!(
244259
end
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

253271
function _add_sub_affine_terms(
@@ -260,6 +278,7 @@ function _add_sub_affine_terms(
260278
push!(terms, MOI.ScalarAffineTerm(op* β), f))
261279
return
262280
end
281+
263282
function _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
271290
end
291+
272292
function _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
280300
end
301+
281302
function _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
300322
end
323+
301324
function _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
310334
end
335+
311336
function _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
320346
end
347+
321348
function _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
328355
end
356+
329357
function _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
335363
end
364+
336365
function _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
344374
end
345375

346376
function MA.operate!(
@@ -369,7 +399,9 @@ function _add_quadratic_terms(
369399
),
370400
)
371401
end
402+
return
372403
end
404+
373405
function _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
382415
end
416+
383417
function _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
392427
end
428+
393429
function _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
400436
end
437+
401438
function _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
409446
end
447+
410448
function _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...)
417455
end
456+
418457
function _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
424464
end
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+
428470
function _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...)
434476
end
477+
435478
function 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
448491
end
492+
449493
# `args` could be `(x', a)` where `a` is a vector of constants and `x` a vector
450494
# of affine functions for instance.
451495
function MA.operate!(

0 commit comments

Comments
 (0)