Skip to content

Commit 0dc7e17

Browse files
committed
Updates
1 parent 157ea9c commit 0dc7e17

File tree

5 files changed

+112
-135
lines changed

5 files changed

+112
-135
lines changed

src/Bridges/Constraint/bridges/soc_rsoc.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ end
7070
function MOI.Bridges.map_function(::Type{<:SOCtoRSOCBridge{T}}, func) where {T}
7171
scalars = MOI.Utilities.eachscalar(func)
7272
t, u, x = scalars[1], scalars[2], scalars[3:end]
73-
ts = MOI.Utilities.operate(/, T, t, sqrt(T(2)))
74-
us = MOI.Utilities.operate(/, T, u, sqrt(T(2)))
73+
ts = MOI.Utilities.operate!(/, T, t, sqrt(T(2)))
74+
us = MOI.Utilities.operate!(/, T, u, sqrt(T(2)))
7575
return MOI.Utilities.operate(
7676
vcat,
7777
T,
78-
MOI.Utilities.operate(+, T, ts, us),
79-
MOI.Utilities.operate(-, T, ts, us),
78+
MOI.Utilities.operate!(+, T, ts, us),
79+
MOI.Utilities.operate!(-, T, ts, us),
8080
x,
8181
)
8282
end

src/Utilities/functions.jl

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,10 @@ function vector_type(::Type{MOI.ScalarQuadraticFunction{T}}) where {T}
513513
return MOI.VectorQuadraticFunction{T}
514514
end
515515

516+
function vector_type(::Type{MOI.ScalarNonlinearFunction})
517+
return MOI.VectorNonlinearFunction
518+
end
519+
516520
"""
517521
ScalarFunctionIterator{F<:MOI.AbstractVectorFunction}
518522
@@ -1858,15 +1862,6 @@ function operate(
18581862
return MOI.ScalarNonlinearFunction(Symbol(op), Any[f, g])
18591863
end
18601864

1861-
function promote_operation(
1862-
::typeof(*),
1863-
::Type{T},
1864-
::Type{T},
1865-
::Type{MOI.ScalarNonlinearFunction},
1866-
) where {T}
1867-
return MOI.ScalarNonlinearFunction
1868-
end
1869-
18701865
function operate(
18711866
::typeof(*),
18721867
::Type{T},
@@ -1887,14 +1882,6 @@ end
18871882

18881883
### VectorNonlinearFunction
18891884

1890-
function promote_operation(
1891-
::Union{typeof(+),typeof(-)},
1892-
::Type{T},
1893-
::Type{MOI.VectorNonlinearFunction},
1894-
) where {T}
1895-
return MOI.VectorNonlinearFunction
1896-
end
1897-
18981885
function operate(
18991886
op::Union{typeof(+),typeof(-)},
19001887
::Type{T},
@@ -1905,24 +1892,6 @@ function operate(
19051892
)
19061893
end
19071894

1908-
function promote_operation(
1909-
::Union{typeof(+),typeof(-)},
1910-
::Type{T},
1911-
::Type{MOI.VectorNonlinearFunction},
1912-
::Type{S},
1913-
) where {
1914-
T<:Number,
1915-
S<:Union{
1916-
AbstractVector{T},
1917-
MOI.VectorOfVariables,
1918-
MOI.VectorAffineFunction{T},
1919-
MOI.VectorQuadraticFunction{T},
1920-
MOI.VectorNonlinearFunction,
1921-
},
1922-
}
1923-
return MOI.VectorNonlinearFunction
1924-
end
1925-
19261895
function operate(
19271896
op::Union{typeof(-)},
19281897
::Type{T},
@@ -1981,24 +1950,6 @@ function operate(
19811950
return MOI.VectorNonlinearFunction([operate(op, T, fi, g) for fi in f.args])
19821951
end
19831952

1984-
function promote_operation(
1985-
::typeof(vcat),
1986-
::Type{T},
1987-
::Type{
1988-
<:Union{
1989-
ScalarQuadraticLike{T},
1990-
MOI.VectorOfVariables,
1991-
MOI.VectorAffineFunction{T},
1992-
MOI.VectorQuadraticFunction{T},
1993-
MOI.ScalarNonlinearFunction,
1994-
MOI.VectorNonlinearFunction,
1995-
Vector{Any},
1996-
},
1997-
}...,
1998-
) where {T}
1999-
return MOI.VectorQuadraticFunction{T}
2000-
end
2001-
20021953
function operate(
20031954
::typeof(vcat),
20041955
::Type{T},
@@ -2023,15 +1974,6 @@ function operate(
20231974
return MOI.VectorNonlinearFunction(out)
20241975
end
20251976

2026-
function promote_operation(
2027-
::typeof(*),
2028-
::Type{T},
2029-
::Type{T},
2030-
::Type{MOI.VectorNonlinearFunction},
2031-
) where {T}
2032-
return MOI.VectorNonlinearFunction
2033-
end
2034-
20351977
function operate(
20361978
::typeof(*),
20371979
::Type{T},

src/Utilities/promote_operation.jl

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ of the arguments `args` are `ArgsTypes`.
1717
One assumption is that the element type `T` is invariant under each operation.
1818
That is, `op(::T, ::T)::T` where `op` is a `+`, `-`, `*`, and `/`.
1919
20-
There are five methods for which we implement `Utilities.promote_operation`:
20+
There are six methods for which we implement `Utilities.promote_operation`:
2121
2222
1. `+`
2323
a. `promote_operation(::typeof(+), ::Type{T}, ::Type{F1}, ::Type{F2})`
@@ -38,10 +38,10 @@ There are five methods for which we implement `Utilities.promote_operation`:
3838
a. `promote_operation(::typeof(imag), ::Type{T}, ::Type{F})`
3939
where `F` is `VariableIndex` or `VectorOfVariables`
4040
41-
In each case, `F` (or `F1` and `F2`) is one of the nine supported types, with
41+
In each case, `F` (or `F1` and `F2`) is one of the ten supported types, with
4242
a restriction that the mathematical operation makes sense, for example, we don't
4343
define `promote_operation(-, T, F1, F2)` where `F1` is a scalar-valued function
44-
and `F2` is a vector-valued function. The nine supported types are:
44+
and `F2` is a vector-valued function. The ten supported types are:
4545
4646
1. ::T
4747
2. ::VariableIndex
@@ -52,6 +52,7 @@ and `F2` is a vector-valued function. The nine supported types are:
5252
7. ::VectorOfVariables
5353
8. ::VectorAffineFunction{T}
5454
9. ::VectorQuadraticFunction{T}
55+
10. ::VectorNonlinearFunction
5556
"""
5657
function promote_operation end
5758

@@ -119,13 +120,13 @@ function promote_operation(
119120
T,
120121
F1<:Union{
121122
AbstractVector{T},
122-
MOI.VectorOfVariables,
123+
MOI.GenericVectorFunction,
123124
MOI.VectorAffineFunction{T},
124125
MOI.VectorQuadraticFunction{T},
125126
},
126127
F2<:Union{
127128
AbstractVector{T},
128-
MOI.VectorOfVariables,
129+
MOI.GenericVectorFunction,
129130
MOI.VectorAffineFunction{T},
130131
MOI.VectorQuadraticFunction{T},
131132
},
@@ -166,9 +167,9 @@ end
166167
function promote_operation(
167168
::typeof(-),
168169
::Type{T},
169-
::Type{MOI.VectorOfVariables},
170-
) where {T}
171-
return MOI.VectorAffineFunction{T}
170+
::Type{F},
171+
) where {T,F<:MOI.GenericVectorFunction}
172+
return vector_type(promote_operation(-, T, scalar_type(F)))
172173
end
173174

174175
### Method 3a
@@ -215,9 +216,9 @@ function promote_operation(
215216
::typeof(*),
216217
::Type{T},
217218
::Type{T},
218-
::Type{MOI.VectorOfVariables},
219-
) where {T}
220-
return MOI.VectorAffineFunction{T}
219+
::Type{F},
220+
) where {T,F<:MOI.GenericVectorFunction}
221+
return vector_type(promote_operation(*, T, T, scalar_type(F)))
221222
end
222223

223224
### Method 3b
@@ -254,10 +255,10 @@ end
254255
function promote_operation(
255256
::typeof(*),
256257
::Type{T},
257-
::Type{MOI.VectorOfVariables},
258+
::Type{F},
258259
::Type{T},
259-
) where {T}
260-
return MOI.VectorAffineFunction{T}
260+
) where {T,F<:MOI.GenericVectorFunction}
261+
return vector_type(promote_operation(*, T, scalar_type(F), T))
261262
end
262263

263264
### Method 3c
@@ -325,10 +326,10 @@ end
325326
function promote_operation(
326327
::typeof(/),
327328
::Type{T},
328-
::Type{MOI.VectorOfVariables},
329+
::Type{F},
329330
::Type{T},
330-
) where {T}
331-
return MOI.VectorAffineFunction{T}
331+
) where {T,F<:MOI.GenericVectorFunction}
332+
return vector_type(promote_operation(/, T, scalar_type(F), T))
332333
end
333334

334335
### Method 5a
@@ -385,6 +386,27 @@ function promote_operation(
385386
return MOI.VectorQuadraticFunction{T}
386387
end
387388

389+
function promote_operation(
390+
::typeof(vcat),
391+
::Type{T},
392+
::Type{
393+
<:Union{
394+
T,
395+
MOI.VariableIndex,
396+
MOI.ScalarAffineFunction{T},
397+
MOI.ScalarQuadraticFunction{T},
398+
MOI.ScalarNonlinearFunction,
399+
AbstractVector{T},
400+
MOI.VectorOfVariables,
401+
MOI.VectorAffineFunction{T},
402+
MOI.VectorQuadraticFunction{T},
403+
MOI.VectorNonlinearFunction,
404+
},
405+
}...,
406+
) where {T}
407+
return MOI.VectorNonlinearFunction
408+
end
409+
388410
### Method 6a
389411

390412
function promote_operation(

src/functions.jl

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,40 @@ All subtypes of `AbstractVectorFunction` must implement:
407407
"""
408408
abstract type AbstractVectorFunction <: AbstractFunction end
409409

410+
struct GenericVectorFunction{T} <: AbstractVectorFunction
411+
rows::Vector{T}
412+
end
413+
414+
output_dimension(f::GenericVectorFunction) = length(f.rows)
415+
416+
function constant(f::GenericVectorFunction, ::Type{T}) where {T}
417+
return zeros(T, output_dimension(f))
418+
end
419+
420+
Base.copy(f::GenericVectorFunction) = GenericVectorFunction(copy(f.rows))
421+
422+
function Base.:(==)(
423+
f::GenericVectorFunction{T},
424+
g::GenericVectorFunction{T},
425+
) where {T}
426+
return f.rows == g.rows
427+
end
428+
429+
function Base.isapprox(
430+
x::GenericVectorFunction{T},
431+
y::GenericVectorFunction{T};
432+
kwargs...,
433+
) where {T}
434+
return all(isapprox(xi, yi; kwargs...) for (xi, yi) in zip(x.rows, y.rows))
435+
end
436+
437+
function Base.convert(
438+
::Type{GenericVectorFunction{T}},
439+
rows::Vector{T},
440+
) where {T}
441+
return GenericVectorFunction(rows)
442+
end
443+
410444
"""
411445
VectorOfVariables(variables::Vector{VariableIndex}) <: AbstractVectorFunction
412446
@@ -436,24 +470,15 @@ julia> MOI.output_dimension(f)
436470
3
437471
```
438472
"""
439-
struct VectorOfVariables <: AbstractVectorFunction
440-
variables::Vector{VariableIndex}
441-
end
473+
const VectorOfVariables = GenericVectorFunction{VariableIndex}
442474

443-
output_dimension(f::VectorOfVariables) = length(f.variables)
444-
445-
function constant(f::VectorOfVariables, ::Type{T}) where {T}
446-
return zeros(T, output_dimension(f))
447-
end
448-
449-
Base.copy(f::VectorOfVariables) = VectorOfVariables(copy(f.variables))
450-
451-
function Base.:(==)(f::VectorOfVariables, g::VectorOfVariables)
452-
return f.variables == g.variables
475+
function Base.getproperty(x::VectorOfVariables, key::Symbol)
476+
if key == :variables
477+
return x.rows
478+
end
479+
return getfield(x, key)
453480
end
454481

455-
Base.isapprox(x::VectorOfVariables, y::VectorOfVariables; kwargs...) = x == y
456-
457482
"""
458483
VectorAffineTerm{T}(
459484
output_index::Int64,
@@ -705,27 +730,7 @@ julia> MOI.VectorNonlinearFunction(Any[g, x])
705730
└ ┘
706731
```
707732
"""
708-
struct VectorNonlinearFunction <: AbstractVectorFunction
709-
args::Vector{Any}
710-
end
711-
712-
function Base.copy(f::VectorNonlinearFunction)
713-
return VectorNonlinearFunction(copy(f.args))
714-
end
715-
716-
output_dimension(f::VectorNonlinearFunction) = length(f.args)
717-
718-
function constant(f::VectorNonlinearFunction, ::Type{T}) where {T}
719-
return zeros(T, output_dimension(f))
720-
end
721-
722-
function Base.:(==)(f::VectorNonlinearFunction, g::VectorNonlinearFunction)
723-
return f.args == g.args
724-
end
725-
726-
function Base.convert(::Type{VectorNonlinearFunction}, args::Vector{Any})
727-
return VectorNonlinearFunction(args)
728-
end
733+
const VectorNonlinearFunction = GenericVectorFunction{Any}
729734

730735
# Function modifications
731736

0 commit comments

Comments
 (0)