Skip to content

Commit 7238aef

Browse files
odowblegat
andauthored
[Utilities] refactor promote_operation (#2206)
Co-authored-by: Benoît Legat <[email protected]>
1 parent c3708aa commit 7238aef

File tree

5 files changed

+652
-251
lines changed

5 files changed

+652
-251
lines changed

src/Utilities/Utilities.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ include("copy.jl")
6060
include("results.jl")
6161
include("variables.jl")
6262

63+
include("promote_operation.jl")
64+
6365
include("objective_container.jl")
6466
include("variables_container.jl")
6567
include("free_variables.jl")

src/Utilities/functions.jl

Lines changed: 20 additions & 251 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,26 @@ function scalar_type(::Type{MOI.VectorQuadraticFunction{T}}) where {T}
482482
return MOI.ScalarQuadraticFunction{T}
483483
end
484484

485+
"""
486+
vector_type(::Type{<:MOI.AbstractScalarFunction})
487+
488+
Return the [`MOI.AbstractVectorFunction`](@ref) associated with the scalar type
489+
`F`.
490+
"""
491+
function vector_type end
492+
493+
vector_type(::Type{T}) where {T} = Vector{T}
494+
495+
vector_type(::Type{MOI.VariableIndex}) = MOI.VectorOfVariables
496+
497+
function vector_type(::Type{MOI.ScalarAffineFunction{T}}) where {T}
498+
return MOI.VectorAffineFunction{T}
499+
end
500+
501+
function vector_type(::Type{MOI.ScalarQuadraticFunction{T}}) where {T}
502+
return MOI.VectorQuadraticFunction{T}
503+
end
504+
485505
"""
486506
ScalarFunctionIterator{F<:MOI.AbstractVectorFunction}
487507
@@ -1330,19 +1350,6 @@ function operate_output_index!(
13301350
return x[i] = operate!(op, T, x[i], args...)
13311351
end
13321352

1333-
"""
1334-
promote_operation(
1335-
op::Function,
1336-
::Type{T},
1337-
ArgsTypes::Type{<:Union{T, MOI.AbstractFunction}}...,
1338-
) where {T}
1339-
1340-
Returns the type of the `MOI.AbstractFunction` returned to the call
1341-
`operate(op, T, args...)` where the types of the arguments `args` are
1342-
`ArgsTypes`.
1343-
"""
1344-
function promote_operation end
1345-
13461353
# Helpers
13471354

13481355
function operate_term(
@@ -1597,44 +1604,6 @@ end
15971604
###################################### +/- #####################################
15981605
## promote_operation
15991606

1600-
function promote_operation(::typeof(-), ::Type{T}, ::Type{T}) where {T}
1601-
return T
1602-
end
1603-
1604-
function promote_operation(
1605-
::typeof(-),
1606-
::Type{T},
1607-
::Type{<:Union{MOI.VariableIndex,MOI.ScalarAffineFunction{T}}},
1608-
) where {T}
1609-
return MOI.ScalarAffineFunction{T}
1610-
end
1611-
1612-
function promote_operation(
1613-
::Union{typeof(+),typeof(-)},
1614-
::Type{T},
1615-
::Type{<:ScalarAffineLike{T}},
1616-
::Type{<:ScalarAffineLike{T}},
1617-
) where {T}
1618-
return MOI.ScalarAffineFunction{T}
1619-
end
1620-
1621-
function promote_operation(
1622-
::typeof(-),
1623-
::Type{T},
1624-
::Type{<:ScalarQuadraticLike{T}},
1625-
) where {T}
1626-
return MOI.ScalarQuadraticFunction{T}
1627-
end
1628-
1629-
function promote_operation(
1630-
::Union{typeof(+),typeof(-)},
1631-
::Type{T},
1632-
::Type{<:ScalarQuadraticLike{T}},
1633-
::Type{<:ScalarQuadraticLike{T}},
1634-
) where {T}
1635-
return MOI.ScalarQuadraticFunction{T}
1636-
end
1637-
16381607
## operate!
16391608
# + with at least 3 arguments
16401609
function operate!(op::typeof(+), ::Type{T}, f, g, h, args...) where {T}
@@ -1816,14 +1785,6 @@ end
18161785

18171786
### ScalarNonlinearFunction
18181787

1819-
function promote_operation(
1820-
::Union{typeof(+),typeof(-)},
1821-
::Type{<:Number},
1822-
::Type{MOI.ScalarNonlinearFunction},
1823-
)
1824-
return MOI.ScalarNonlinearFunction
1825-
end
1826-
18271788
function operate(
18281789
::typeof(-),
18291790
::Type{T},
@@ -1838,24 +1799,6 @@ function operate(
18381799
return MOI.ScalarNonlinearFunction(:-, Any[f])
18391800
end
18401801

1841-
function promote_operation(
1842-
::Union{typeof(+),typeof(-),typeof(*),typeof(/)},
1843-
::Type{T},
1844-
::Type{MOI.ScalarNonlinearFunction},
1845-
::Type{S},
1846-
) where {
1847-
T<:Number,
1848-
S<:Union{
1849-
T,
1850-
MOI.VariableIndex,
1851-
MOI.ScalarAffineFunction{T},
1852-
MOI.ScalarQuadraticFunction{T},
1853-
MOI.ScalarNonlinearFunction,
1854-
},
1855-
}
1856-
return MOI.ScalarNonlinearFunction
1857-
end
1858-
18591802
function operate(
18601803
op::Union{typeof(+),typeof(-),typeof(*),typeof(/)},
18611804
::Type{T},
@@ -1945,40 +1888,6 @@ end
19451888
# Vector +/-
19461889
###############################################################################
19471890

1948-
function promote_operation(
1949-
::typeof(-),
1950-
::Type{T},
1951-
::Type{<:VectorAffineLike{T}},
1952-
) where {T}
1953-
return MOI.VectorAffineFunction{T}
1954-
end
1955-
1956-
function promote_operation(
1957-
::typeof(-),
1958-
::Type{T},
1959-
::Type{<:VectorQuadraticLike{T}},
1960-
) where {T}
1961-
return MOI.VectorQuadraticFunction{T}
1962-
end
1963-
1964-
function promote_operation(
1965-
::Union{typeof(+),typeof(-)},
1966-
::Type{T},
1967-
::Type{<:VectorAffineLike{T}},
1968-
::Type{<:VectorAffineLike{T}},
1969-
) where {T}
1970-
return MOI.VectorAffineFunction{T}
1971-
end
1972-
1973-
function promote_operation(
1974-
::Union{typeof(+),typeof(-)},
1975-
::Type{T},
1976-
::Type{<:VectorQuadraticLike{T}},
1977-
::Type{<:VectorQuadraticLike{T}},
1978-
) where {T}
1979-
return MOI.VectorQuadraticFunction{T}
1980-
end
1981-
19821891
# Vector Variable +/- ...
19831892
function operate!(
19841893
op::Union{typeof(+),typeof(-)},
@@ -2376,51 +2285,6 @@ end
23762285

23772286
####################################### * ######################################
23782287

2379-
function promote_operation(
2380-
::typeof(*),
2381-
::Type{T},
2382-
::Type{T},
2383-
::Type{<:Union{MOI.VariableIndex,MOI.ScalarAffineFunction{T}}},
2384-
) where {T}
2385-
return MOI.ScalarAffineFunction{T}
2386-
end
2387-
2388-
function promote_operation(
2389-
::typeof(*),
2390-
::Type{T},
2391-
::Type{<:Union{MOI.VariableIndex,MOI.ScalarAffineFunction{T}}},
2392-
::Type{T},
2393-
) where {T}
2394-
return MOI.ScalarAffineFunction{T}
2395-
end
2396-
2397-
function promote_operation(
2398-
::typeof(*),
2399-
::Type{T},
2400-
::Type{T},
2401-
::Type{MOI.ScalarQuadraticFunction{T}},
2402-
) where {T}
2403-
return MOI.ScalarQuadraticFunction{T}
2404-
end
2405-
2406-
function promote_operation(
2407-
::typeof(*),
2408-
::Type{T},
2409-
::Type{MOI.ScalarQuadraticFunction{T}},
2410-
::Type{T},
2411-
) where {T}
2412-
return MOI.ScalarQuadraticFunction{T}
2413-
end
2414-
2415-
function promote_operation(
2416-
::typeof(*),
2417-
::Type{T},
2418-
::Type{<:Union{MOI.VariableIndex,MOI.ScalarAffineFunction{T}}},
2419-
::Type{<:Union{MOI.VariableIndex,MOI.ScalarAffineFunction{T}}},
2420-
) where {T}
2421-
return MOI.ScalarQuadraticFunction{T}
2422-
end
2423-
24242288
function operate!(::typeof(*), ::Type{T}, f::MOI.VariableIndex, α::T) where {T}
24252289
return operate(*, T, α, f)
24262290
end
@@ -2639,24 +2503,6 @@ LinearAlgebra.symmetric(f::ScalarLike, ::Symbol) = f
26392503

26402504
####################################### / ######################################
26412505

2642-
function promote_operation(
2643-
::typeof(/),
2644-
::Type{T},
2645-
::Type{<:Union{MOI.VariableIndex,MOI.ScalarAffineFunction{T}}},
2646-
::Type{T},
2647-
) where {T}
2648-
return MOI.ScalarAffineFunction{T}
2649-
end
2650-
2651-
function promote_operation(
2652-
::typeof(/),
2653-
::Type{T},
2654-
::Type{MOI.ScalarQuadraticFunction{T}},
2655-
::Type{T},
2656-
) where {T}
2657-
return MOI.ScalarQuadraticFunction{T}
2658-
end
2659-
26602506
function operate!(::typeof(/), ::Type{T}, f::MOI.VariableIndex, α::T) where {T}
26612507
return operate(/, T, f, α)
26622508
end
@@ -2826,14 +2672,6 @@ function fill_variables(
28262672
return
28272673
end
28282674

2829-
function promote_operation(
2830-
::typeof(vcat),
2831-
::Type{T},
2832-
::Type{<:Union{MOI.VariableIndex,MOI.VectorOfVariables}}...,
2833-
) where {T}
2834-
return MOI.VectorOfVariables
2835-
end
2836-
28372675
function operate(
28382676
::typeof(vcat),
28392677
::Type{T},
@@ -3094,19 +2932,6 @@ function vectorize(
30942932
return MOI.VectorQuadraticFunction(quadratic_terms, affine_terms, constant)
30952933
end
30962934

3097-
function promote_operation(::typeof(vcat), ::Type{T}, ::Type{T}...) where {T}
3098-
return Vector{T}
3099-
end
3100-
3101-
# TODO Remove `<:Number` when we drop Julia v1.1.1, otherwise it gives a `StackOverflowError`
3102-
function promote_operation(
3103-
::typeof(vcat),
3104-
::Type{T},
3105-
::Union{Type{T},Type{<:AbstractVector{T}}}...,
3106-
) where {T<:Number}
3107-
return Vector{T}
3108-
end
3109-
31102935
# TODO Remove `<:Number` when we drop Julia v1.1.1, it's needed for disambiguation
31112936
function operate(
31122937
::typeof(vcat),
@@ -3116,35 +2941,6 @@ function operate(
31162941
return vcat(funcs...)
31172942
end
31182943

3119-
function promote_operation(
3120-
::typeof(vcat),
3121-
::Type{T},
3122-
::Type{
3123-
<:Union{
3124-
ScalarAffineLike{T},
3125-
MOI.VectorOfVariables,
3126-
MOI.VectorAffineFunction{T},
3127-
},
3128-
}...,
3129-
) where {T}
3130-
return MOI.VectorAffineFunction{T}
3131-
end
3132-
3133-
function promote_operation(
3134-
::typeof(vcat),
3135-
::Type{T},
3136-
::Type{
3137-
<:Union{
3138-
ScalarQuadraticLike{T},
3139-
MOI.VectorOfVariables,
3140-
MOI.VectorAffineFunction{T},
3141-
MOI.VectorQuadraticFunction{T},
3142-
},
3143-
}...,
3144-
) where {T}
3145-
return MOI.VectorQuadraticFunction{T}
3146-
end
3147-
31482944
function operate(
31492945
::typeof(vcat),
31502946
::Type{T},
@@ -3495,22 +3291,6 @@ end
34953291

34963292
Base.real(f::Union{MOI.VariableIndex,MOI.VectorOfVariables}) = f
34973293

3498-
function promote_operation(
3499-
::typeof(imag),
3500-
::Type{T},
3501-
::Type{MOI.VariableIndex},
3502-
) where {T}
3503-
return MOI.ScalarAffineFunction{T}
3504-
end
3505-
3506-
function promote_operation(
3507-
::typeof(imag),
3508-
::Type{T},
3509-
::Type{MOI.VectorOfVariables},
3510-
) where {T}
3511-
return MOI.VectorAffineFunction{T}
3512-
end
3513-
35143294
function operate(::typeof(imag), ::Type{T}, f::MOI.VectorOfVariables) where {T}
35153295
return zero_with_output_dimension(
35163296
MOI.VectorAffineFunction{T},
@@ -3539,17 +3319,6 @@ Base.conj(f::Union{MOI.VariableIndex,MOI.VectorOfVariables}) = f
35393319

35403320
## Matrix operations
35413321

3542-
function promote_operation(
3543-
::typeof(*),
3544-
::Type{T},
3545-
::Type{<:Diagonal{T}},
3546-
::Type{F},
3547-
) where {T,F}
3548-
S = scalar_type(F)
3549-
U = promote_operation(*, T, T, S)
3550-
return promote_operation(vcat, T, U)
3551-
end
3552-
35533322
function operate_term(::typeof(*), D::Diagonal, term::MOI.VectorAffineTerm)
35543323
return MOI.VectorAffineTerm(
35553324
term.output_index,

0 commit comments

Comments
 (0)