Skip to content

Commit

Permalink
Remplace const by import as (#89)
Browse files Browse the repository at this point in the history
* Remplace const by import as

* Fixes

* Fix

* Fix format
  • Loading branch information
blegat authored Jun 30, 2023
1 parent 4fa7efb commit 3f7e8de
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 95 deletions.
15 changes: 5 additions & 10 deletions src/Bridges/Constraint/Constraint.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
module Constraint

using MultivariatePolynomials
const MP = MultivariatePolynomials
import MultivariateBases
const MB = MultivariateBases
import SemialgebraicSets
const SS = SemialgebraicSets
import MultivariateMoments
const MM = MultivariateMoments
import MathOptInterface
const MOI = MathOptInterface
import MultivariatePolynomials as MP
import MultivariateBases as MB
import SemialgebraicSets as SS
import MultivariateMoments as MM
import MathOptInterface as MOI

using PolyJuMP

Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/to_polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import DynamicPolynomials

const VariableOrder =
DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}
const MonomialOrder = Graded{LexOrder}
const MonomialOrder = MP.Graded{MP.LexOrder}
const VarType = DynamicPolynomials.Variable{VariableOrder,MonomialOrder}
const PolyType{T} = DynamicPolynomials.Polynomial{VariableOrder,MonomialOrder,T}
const FuncType{T} = PolyJuMP.ScalarPolynomialFunction{T,PolyType{T}}
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/zero_polynomial.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct ZeroPolynomialBridge{
T,
F<:MOI.AbstractVectorFunction,
MT<:AbstractMonomial,
MT<:MP.AbstractMonomial,
MVT<:AbstractVector{MT},
} <: MOI.Bridges.Constraint.AbstractBridge
zero_constraint::MOI.ConstraintIndex{F,MOI.Zeros}
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/zero_polynomial_in_algebraic_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function MOI.Bridges.Constraint.bridge_constraint(
f::MOI.AbstractVectorFunction,
s::PolyJuMP.ZeroPolynomialSet{<:SS.AbstractAlgebraicSet},
) where {T,F,BT,DT,MT,MVT}
p = polynomial(MOI.Utilities.scalarize(f), s.monomials)
p = MP.polynomial(MOI.Utilities.scalarize(f), s.monomials)
# As `*(::MOI.ScalarAffineFunction{T}, ::S)` is only defined if `S == T`, we
# need to call `similar`. This is critical since `T` is
# `Float64` when used with JuMP and the coefficient type is often `Int` with
Expand Down
9 changes: 3 additions & 6 deletions src/KKT/KKT.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
module KKT

import MutableArithmetics
const MA = MutableArithmetics
import MutableArithmetics as MA

import MathOptInterface
const MOI = MathOptInterface
import MathOptInterface as MOI

import MultivariatePolynomials
const MP = MultivariatePolynomials
import MultivariatePolynomials as MP

import SemialgebraicSets

Expand Down
13 changes: 5 additions & 8 deletions src/PolyJuMP.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
module PolyJuMP

import MutableArithmetics
const MA = MutableArithmetics
import MutableArithmetics as MA

# MultivariatePolynomials extension

using MultivariatePolynomials
const MP = MultivariatePolynomials
import MultivariateBases
const MB = MultivariateBases
using MultivariateMoments
using SemialgebraicSets
import MultivariatePolynomials as MP
import MultivariateBases as MB
import MultivariateMoments as MM
import SemialgebraicSets as SS

# MOI extension

Expand Down
12 changes: 5 additions & 7 deletions src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ end

# This is type piracy but we tolerate it.
const ObjectWithoutIndex = Union{
AbstractMonomial,
AbstractTerm{<:MOI.Utilities.ObjectWithoutIndex},
AbstractPolynomial{<:MOI.Utilities.ObjectWithoutIndex},
MP.AbstractMonomial,
MP.AbstractTerm{<:MOI.Utilities.ObjectWithoutIndex},
MP.AbstractPolynomial{<:MOI.Utilities.ObjectWithoutIndex},
MB.AbstractPolynomialBasis,
MultivariateMoments.AbstractMomentMatrix{
<:MOI.Utilities.ObjectWithoutIndex,
},
MultivariateMoments.AbstractMeasure{<:MOI.Utilities.ObjectWithoutIndex},
MM.AbstractMomentMatrix{<:MOI.Utilities.ObjectWithoutIndex},
MM.AbstractMeasure{<:MOI.Utilities.ObjectWithoutIndex},
}
const ObjectOrTupleWithoutIndex =
Union{ObjectWithoutIndex,Tuple{Vararg{ObjectWithoutIndex}}}
Expand Down
44 changes: 22 additions & 22 deletions src/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,34 @@ function JuMP.function_string(::MIME"text/plain", p::MP.AbstractPolynomialLike)
end
function JuMP.function_string(::MIME"text/latex", p::MP.AbstractPolynomialLike)
# `show` prints `$$` around what `_show` prints.
return sprint(MultivariatePolynomials._show, MIME"text/latex"(), p)
return sprint(MP._show, MIME"text/latex"(), p)
end

### Shapes for polynomial/moments primal-dual pair ###

# Inspired from `JuMP.dual_shape` docstring example
struct PolynomialShape{MT<:AbstractMonomial,MVT<:AbstractVector{MT}} <:
struct PolynomialShape{MT<:MP.AbstractMonomial,MVT<:AbstractVector{MT}} <:
JuMP.AbstractShape
monomials::MVT
end
function JuMP.reshape_vector(x::Vector, shape::PolynomialShape)
return polynomial(x, shape.monomials)
return MP.polynomial(x, shape.monomials)
end
struct MomentsShape{MT<:AbstractMonomial,MVT<:AbstractVector{MT}} <:
struct MomentsShape{MT<:MP.AbstractMonomial,MVT<:AbstractVector{MT}} <:
JuMP.AbstractShape
monomials::MVT
end
function JuMP.reshape_vector(x::Vector, shape::MomentsShape)
return measure(x, shape.monomials)
return MM.measure(x, shape.monomials)
end
JuMP.dual_shape(shape::PolynomialShape) = MomentsShape(shape.monomials)
JuMP.dual_shape(shape::MomentsShape) = PolynomialShape(shape.monomials)

JuMP.reshape_set(::ZeroPolynomialSet, ::PolynomialShape) = ZeroPoly()
function JuMP.moi_set(
::ZeroPoly,
monos::AbstractVector{<:AbstractMonomial};
domain::AbstractSemialgebraicSet = FullSpace(),
monos::AbstractVector{<:MP.AbstractMonomial};
domain::SS.AbstractSemialgebraicSet = SS.FullSpace(),
basis = MB.MonomialBasis,
)
return ZeroPolynomialSet(domain, basis, monos)
Expand All @@ -56,7 +56,7 @@ end
Return the [`MomentsAttribute`](@ref) of `cref`.
"""
function MultivariateMoments.moments(cref::JuMP.ConstraintRef)
function MM.moments(cref::JuMP.ConstraintRef)
return MOI.get(cref.model, MomentsAttribute(), cref)
end

Expand All @@ -79,14 +79,14 @@ bridges(S::Type{<:MOI.AbstractSet}) = bridges(MOI.VectorOfVariables, S)

function bridges(
F::Type{<:MOI.AbstractVectorFunction},
::Type{<:ZeroPolynomialSet{FullSpace}},
::Type{<:ZeroPolynomialSet{SS.FullSpace}},
)
return [(Bridges.Constraint.ZeroPolynomialBridge, _coef_type(F))]
end

function bridges(
F::Type{<:MOI.AbstractVectorFunction},
::Type{<:ZeroPolynomialSet{<:AbstractAlgebraicSet}},
::Type{<:ZeroPolynomialSet{<:SS.AbstractAlgebraicSet}},
)
return [(
Bridges.Constraint.ZeroPolynomialInAlgebraicSetBridge,
Expand Down Expand Up @@ -183,19 +183,19 @@ function non_constant(a::AbstractVector{T}) where {T}
# also take care of `collect`ing into a `Vector`
return convert(Vector{non_constant_type(T)}, a)
end
non_constant_coefficients(p) = non_constant(coefficients(p))
non_constant_coefficients(p) = non_constant(MP.coefficients(p))

## ZeroPoly
function JuMP.build_constraint(
_error::Function,
p::AbstractPolynomialLike,
p::MP.AbstractPolynomialLike,
s::ZeroPoly;
domain::AbstractSemialgebraicSet = FullSpace(),
domain::SS.AbstractSemialgebraicSet = SS.FullSpace(),
kws...,
)
coefs = non_constant_coefficients(p)
monos = monomials(p)
if domain isa BasicSemialgebraicSet
monos = MP.monomials(p)
if domain isa SS.BasicSemialgebraicSet
# p(x) = 0 for all x in a basic semialgebraic set. We replace it by
# p(x) ≤ 0 and p(x) ≥ 0 for all x in the basic semialgebraic set.
# We need to determine the cone two use for `NonNegPoly` which is stored
Expand All @@ -222,7 +222,7 @@ function JuMP.build_constraint(
end
function JuMP.build_constraint(
_error::Function,
p::AbstractPolynomialLike,
p::MP.AbstractPolynomialLike,
s::MOI.EqualTo;
kws...,
)
Expand Down Expand Up @@ -258,7 +258,7 @@ function JuMP.add_constraint(
)
cone = getdefault(model, NonNegPoly())
coefs = non_constant_coefficients(constraint.polynomial_or_matrix)
monos = monomials(constraint.polynomial_or_matrix)
monos = MP.monomials(constraint.polynomial_or_matrix)
set = PlusMinusSet(JuMP.moi_set(cone, monos; constraint.kws...))
new_constraint = JuMP.VectorConstraint(coefs, set, PolynomialShape(monos))
bridgeable_con = bridgeable(
Expand Down Expand Up @@ -287,15 +287,15 @@ end
# `NonNegPoly`
function JuMP.build_constraint(
_error::Function,
p::AbstractPolynomialLike,
p::MP.AbstractPolynomialLike,
s::MOI.GreaterThan;
kws...,
)
return JuMP.build_constraint(_error, p - s.lower, NonNegPoly(); kws...)
end
function JuMP.build_constraint(
_error::Function,
p::AbstractPolynomialLike,
p::MP.AbstractPolynomialLike,
s::MOI.LessThan;
kws...,
)
Expand All @@ -307,7 +307,7 @@ end
# need a more specific here to avoid ambiguity
function JuMP.build_constraint(
_error::Function,
p::AbstractMatrix{<:AbstractPolynomialLike},
p::AbstractMatrix{<:MP.AbstractPolynomialLike},
s::PSDCone;
kws...,
)
Expand All @@ -317,7 +317,7 @@ end
# Needed for the syntax `@constraint(model, A >= B, PSDCone())`
function JuMP.build_constraint(
_error::Function,
f::AbstractMatrix{<:AbstractPolynomialLike},
f::AbstractMatrix{<:MP.AbstractPolynomialLike},
s::MOI.GreaterThan,
extra::PSDCone,
)
Expand All @@ -328,7 +328,7 @@ end
# Needed for the syntax `@constraint(model, A <= B, PSDCone())`
function JuMP.build_constraint(
_error::Function,
f::AbstractMatrix{<:AbstractPolynomialLike},
f::AbstractMatrix{<:MP.AbstractPolynomialLike},
s::MOI.LessThan,
extra::PSDCone,
)
Expand Down
12 changes: 6 additions & 6 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Defines the polynomial function of the variables `variables` where the variable
`variables(p)[i]` corresponds to `variables[i]`.
"""
struct ScalarPolynomialFunction{T,P<:AbstractPolynomial{T}} <:
struct ScalarPolynomialFunction{T,P<:MP.AbstractPolynomial{T}} <:
MOI.AbstractScalarFunction
polynomial::P
variables::Vector{MOI.VariableIndex}
Expand All @@ -29,7 +29,7 @@ function Base.convert(
vi::MOI.VariableIndex,
) where {T,P}
x = _polynomial_variable(P, vi)
return ScalarPolynomialFunction{T,P}(polynomial(x, T), [vi])
return ScalarPolynomialFunction{T,P}(MP.polynomial(x, T), [vi])
end

function _polynomial_variables!(::Type{P}, variables) where {P}
Expand Down Expand Up @@ -59,7 +59,7 @@ function Base.convert(
func::MOI.ScalarAffineFunction{T},
) where {T,P}
variables = [t.variable for t in func.terms]
x, d = _polynomial_variables!(P, variables)
_, d = _polynomial_variables!(P, variables)
poly = _polynomial_with_variables(P, func, d)
return ScalarPolynomialFunction{T,P}(poly, variables)
end
Expand All @@ -72,7 +72,7 @@ function Base.convert(
quad_variables_1 = [t.variable_1 for t in func.quadratic_terms]
quad_variables_2 = [t.variable_2 for t in func.quadratic_terms]
variables = [linear_variables; quad_variables_1; quad_variables_2]
x, d = _polynomial_variables!(P, variables)
_, d = _polynomial_variables!(P, variables)
terms = MP.term_type(P)[MOI.constant(func)]
for t in func.affine_terms
push!(terms, MP.term(t.coefficient, d[t.variable]))
Expand All @@ -81,7 +81,7 @@ function Base.convert(
coef = t.variable_1 == t.variable_2 ? t.coefficient / 2 : t.coefficient
push!(terms, MP.term(coef, d[t.variable_1] * d[t.variable_2]))
end
return ScalarPolynomialFunction{T,P}(polynomial(terms), variables)
return ScalarPolynomialFunction{T,P}(MP.polynomial(terms), variables)
end

function Base.convert(
Expand Down Expand Up @@ -111,7 +111,7 @@ function MOI.Utilities.substitute_variables(
new_aff =
MOI.ScalarAffineFunction{T}[variable_map(var) for var in func.variables]
variables = collect(Iterators.flatten(_variables(aff) for aff in new_aff))
x, d = _polynomial_variables!(P, variables)
_, d = _polynomial_variables!(P, variables)
new_polys = [_polynomial_with_variables(P, aff, d) for aff in new_aff]
new_poly = func.polynomial(MP.variables(func.polynomial) => new_polys)
return ScalarPolynomialFunction{T,typeof(new_poly)}(new_poly, variables)
Expand Down
2 changes: 1 addition & 1 deletion src/nl_to_polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import DynamicPolynomials
const VariableOrder =
DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}
const MonomialOrder = Graded{LexOrder}
const MonomialOrder = MP.Graded{MP.LexOrder}
const VarType = DynamicPolynomials.Variable{VariableOrder,MonomialOrder}
const PolyType{T} = DynamicPolynomials.Polynomial{VariableOrder,MonomialOrder,T}
const FuncType{T} = ScalarPolynomialFunction{T,PolyType{T}}
Expand Down
21 changes: 8 additions & 13 deletions src/variable.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
export Poly

function JuMP.value(f::Function, t::AbstractTerm{<:JuMP.AbstractJuMPScalar})
return JuMP.value(f, MultivariatePolynomials.coefficient(t)) * monomial(t)
function JuMP.value(f::Function, t::MP.AbstractTerm{<:JuMP.AbstractJuMPScalar})
return JuMP.value(f, MP.coefficient(t)) * MP.monomial(t)
end

function JuMP.value(t::AbstractTerm{<:JuMP.AbstractJuMPScalar})
function JuMP.value(t::MP.AbstractTerm{<:JuMP.AbstractJuMPScalar})
return JuMP.value(JuMP.value, t)
end

function JuMP.value(
f::Function,
p::AbstractPolynomialLike{<:JuMP.AbstractJuMPScalar},
p::MP.AbstractPolynomialLike{<:JuMP.AbstractJuMPScalar},
)
return polynomial(
JuMP.value.(f, terms(p)),
MultivariatePolynomials.SortedUniqState(),
)
return MP.polynomial(JuMP.value.(f, MP.terms(p)), MP.SortedUniqState())
end

function JuMP.value(p::AbstractPolynomialLike{<:JuMP.AbstractJuMPScalar})
function JuMP.value(p::MP.AbstractPolynomialLike{<:JuMP.AbstractJuMPScalar})
return JuMP.value(JuMP.value, p)
end

Expand All @@ -34,9 +31,7 @@ Polynomial variable ``v^\\top p`` where ``v`` is a vector of new decision variab
struct Poly{PB<:MB.AbstractPolynomialBasis} <: AbstractPoly
polynomial_basis::PB
end
function Poly(
x::AbstractVector{<:MultivariatePolynomials.AbstractPolynomialLike},
)
function Poly(x::AbstractVector{<:MP.AbstractPolynomialLike})
return Poly(MB.MonomialBasis(x))
end

Expand Down Expand Up @@ -112,5 +107,5 @@ function JuMP.add_variable(
end
return vref
end
return polynomial(_newvar, v.p.polynomial_basis)
return MP.polynomial(_newvar, v.p.polynomial_basis)
end
6 changes: 3 additions & 3 deletions src/zero_polynomial.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
struct ZeroPolynomialSet{
DT<:AbstractSemialgebraicSet,
DT<:SS.AbstractSemialgebraicSet,
BT<:MB.AbstractPolynomialBasis,
MT<:AbstractMonomial,
MT<:MP.AbstractMonomial,
MVT<:AbstractVector{MT},
} <: MOI.AbstractVectorSet
domain::DT
basis::Type{BT}
monomials::MVT
function ZeroPolynomialSet(
domain::AbstractSemialgebraicSet,
domain::SS.AbstractSemialgebraicSet,
basis::Type{BT},
monomials::AbstractVector{MT},
) where {BT<:MB.AbstractPolynomialBasis,MT<:MP.AbstractMonomial}
Expand Down
1 change: 0 additions & 1 deletion test/Mock/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using JuMP
const MOIT = MOI.Test

MOI.Utilities.@model(
NoFreeVariable,
Expand Down
Loading

2 comments on commit 3f7e8de

@blegat
Copy link
Member Author

@blegat blegat commented on 3f7e8de Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/86560

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.0 -m "<description of version>" 3f7e8de3dd9aa0161e78a01c364a73d719617d70
git push origin v0.7.0

Please sign in to comment.