Skip to content

Commit 5292380

Browse files
committed
Add another method
1 parent 8fc8aad commit 5292380

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/Utilities/promote_operation.jl

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ There are five methods for which we implement `Utilities.promote_operation`:
2424
c. `promote_operation(::typeof(-), ::Type{T}, ::Type{F1}, ::Type{Vector{T}})`
2525
3. `*`
2626
a. `promote_operation(::typeof(*), ::Type{T}, ::Type{T}, ::Type{F})`
27-
b. `promote_operation(::typeof(*), ::Type{T}, ::Type{F1}, ::Type{F2})`
27+
b. `promote_operation(::typeof(*), ::Type{T}, ::Type{T}, ::Type{F})`
28+
c. `promote_operation(::typeof(*), ::Type{T}, ::Type{F1}, ::Type{F2})`
2829
where `F1` and `F2` are `VariableIndex` or `ScalarAffineFunction`
29-
c. `promote_operation(::typeof(*), ::Type{T}, ::Type{<:Diagonal{T}}, ::Type{F}`
30+
d. `promote_operation(::typeof(*), ::Type{T}, ::Type{<:Diagonal{T}}, ::Type{F}`
3031
4. `/`
3132
a. `promote_operation(::typeof(/), ::Type{T}, ::Type{F}, ::Type{T})`
3233
5. `vcat`
@@ -243,6 +244,46 @@ end
243244

244245
### Method 3b
245246

247+
function promote_operation(
248+
::typeof(*),
249+
::Type{T},
250+
::Type{F},
251+
::Type{T},
252+
) where {
253+
T,
254+
F<:Union{
255+
# T, # Stackoverflow if included
256+
MOI.ScalarAffineFunction{T},
257+
MOI.ScalarQuadraticFunction{T},
258+
MOI.ScalarNonlinearFunction,
259+
AbstractVector{T},
260+
MOI.VectorAffineFunction{T},
261+
MOI.VectorQuadraticFunction{T},
262+
},
263+
}
264+
return F
265+
end
266+
267+
function promote_operation(
268+
::typeof(*),
269+
::Type{T},
270+
::Type{MOI.VariableIndex},
271+
::Type{T},
272+
) where {T}
273+
return MOI.ScalarAffineFunction{T}
274+
end
275+
276+
function promote_operation(
277+
::typeof(*),
278+
::Type{T},
279+
::Type{MOI.VectorOfVariables},
280+
::Type{T},
281+
) where {T}
282+
return MOI.VectorAffineFunction{T}
283+
end
284+
285+
### Method 3c
286+
246287
function promote_operation(
247288
::typeof(*),
248289
::Type{T},
@@ -252,7 +293,7 @@ function promote_operation(
252293
return MOI.ScalarQuadraticFunction{T}
253294
end
254295

255-
### Method 3c
296+
### Method 3d
256297

257298
function promote_operation(
258299
::typeof(*),

test/Utilities/test_promote_operation.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,27 @@ function test_promote_operation_3a()
143143
return
144144
end
145145

146+
function test_promote_operation_3b()
147+
T = Int
148+
F = (
149+
T,
150+
MOI.VariableIndex,
151+
MOI.ScalarAffineFunction{T},
152+
MOI.ScalarQuadraticFunction{T},
153+
MOI.ScalarNonlinearFunction,
154+
Vector{T},
155+
MOI.VectorOfVariables,
156+
MOI.VectorAffineFunction{T},
157+
MOI.VectorQuadraticFunction{T},
158+
)
159+
special_cases = Dict(2 => 3, 7 => 8)
160+
for i in 1:9
161+
j = get(special_cases, i, i)
162+
@test MOI.Utilities.promote_operation(*, T, F[i], T) == F[j]
163+
end
164+
return
165+
end
166+
146167
function test_promote_operation_4a()
147168
T = Int
148169
F = (

0 commit comments

Comments
 (0)