Skip to content

Commit

Permalink
Implement monomial_type (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat authored May 23, 2024
1 parent 3174bdd commit ff8a74f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/SemialgebraicSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function Base.show(io::IO, ::FullSpace)
end
nequalities(::FullSpace) = 0
equalities(::FullSpace) = []
MP.similar_type(S::Type{FullSpace}, T::Type) = S
MP.similar_type(S::Type{FullSpace}, ::Type) = S
MP.monomial_type(::Type{FullSpace}) = nothing

function Base.similar(set::AbstractSemialgebraicSet, T::Type)
return convert(MP.similar_type(typeof(set), T), set)
Expand Down
11 changes: 11 additions & 0 deletions src/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ function basic_semialgebraic_set(V, p)
return BasicSemialgebraicSet(V, p)
end

algebraic_set(set::BasicSemialgebraicSet) = set.V

function MP.similar_type(
::Type{BasicSemialgebraicSet{S,PS,AT}},
T::Type,
Expand All @@ -56,6 +58,15 @@ end
function MP.variables(S::BasicSemialgebraicSet)
return sort(union(MP.variables(S.V), MP.variables(S.p)); rev = true)
end
function MP.monomial_type(::Type{BasicSemialgebraicSet{T,P,A}}) where {T,P,A}
M1 = MP.monomial_type(A)
M2 = MP.monomial_type(P)
if isnothing(M1)
return M2
else
return promote_type(M1, M2)
end
end
nequalities(S::BasicSemialgebraicSet) = nequalities(S.V)
equalities(S::BasicSemialgebraicSet) = equalities(S.V)
add_equality!(S::BasicSemialgebraicSet, p) = add_equality!(S.V, p)
Expand Down
3 changes: 3 additions & 0 deletions src/fix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ end

ideal(set::FixedVariablesSet, args...) = set.ideal
MP.variables(set::FixedVariablesSet) = MP.variables(set.ideal)
function MP.monomial_type(::Type{FixedVariablesSet{V,T,M}}) where {V,T,M}
return M
end
function nequalities(set::FixedVariablesSet)
if set.ideal.substitutions === nothing
return 1
Expand Down
1 change: 1 addition & 0 deletions src/variety.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ end
ideal(V::AlgebraicSet) = V.I

MP.variables(V::AlgebraicSet) = MP.variables(V.I)
MP.monomial_type(::Type{<:AlgebraicSet{T,P}}) where {T,P} = MP.monomial_type(P)
nequalities(V::AlgebraicSet) = length(V.I.p)
equalities(V::AlgebraicSet) = V.I.p
add_equality!(V::AlgebraicSet, p) = push!(V.I.p, p)
Expand Down
20 changes: 20 additions & 0 deletions test/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ function SemialgebraicSets.default_gröbner_basis_algorithm(p, ::DummySolver)
end
SemialgebraicSets.promote_for(::Type{T}, ::Type{DummySolver}) where {T} = T

function _test_polynomial_API(set, vars)
mono = prod(vars)
@test @inferred(variables(set)) == variables(mono)
@test @inferred(monomial_type(typeof(set))) == monomial_type(typeof(mono))
end

function runtests()
Main.Mod.@polyvar x y
@test isa(FullSpace(), FullSpace)
V = @set x * y == 1
_test_polynomial_API(V, (x, y))
@test V isa AlgebraicSet{Rational{BigInt}}
@test_throws ArgumentError add_inequality!(V, x * y)
@testset "Basic" begin
Expand All @@ -22,6 +29,7 @@ function runtests()
add_inequality!(S, x + y - 1)
# Algebraic set forces `Rational{BigInt}`
@test S isa BasicSemialgebraicSet{Rational{BigInt}}
_test_polynomial_API(S, (x, y))
@test S == basic_semialgebraic_set(S.V, S.p)
@test sprint(show, S) ==
"{ (x, y) | -y + x = 0, -y + x^2 = 0, -1//1 + x^2*y ≥ 0, -1//1 + y + x ≥ 0 }"
Expand Down Expand Up @@ -49,29 +57,35 @@ function runtests()
1.0x^2 * y >= 0 &&
(6 // 3) * x^2 * y == -y &&
1.5x + y >= 0)
_test_polynomial_API(S, (x, y))
S2 = S V
S3 = V S
@test inequalities(S2) == inequalities(S3) == S.p
@test equalities(S2) == S3.V.I.p
end

T = (@set x * y^2 == -1 && x^2 + y^2 <= 1)
_test_polynomial_API(T, (x, y))
V2 = @set T.V && V && x + y == 2.0
_test_polynomial_API(V2, (x, y))
@test V2 isa AlgebraicSet
@test V2.I.p == [equalities(T); equalities(V); x + y - 2.0]
S4 = @set S && T
_test_polynomial_API(S4, (x, y))
@test S4.p == [S.p; inequalities(T)]
@test equalities(S4) == [S.V.I.p; T.V.I.p]

@testset "Different variables" begin
T = (@set x == x^2 && y <= y^2)
_test_polynomial_API(T, (x, y))
@test sprint(show, T) == "{ (x, y) | x - x^2 = 0, -y + y^2 ≥ 0 }"
@test sprint(show, MIME"text/plain"(), T) ==
"Basic semialgebraic Set defined by 1 equalitty\n x - x^2 = 0\n1 inequalitty\n -y + y^2 ≥ 0\n"
end
end
@testset "Basic with no equality" begin
S = @set x + y 1 && x y
_test_polynomial_API(S, (x, y))
@test sprint(show, S) == "{ (x, y) | -1 + y + x ≥ 0, y - x ≥ 0 }"
@test sprint(show, MIME"text/plain"(), S) == """
Basic semialgebraic Set defined by no equality
Expand All @@ -98,6 +112,7 @@ Algebraic Set defined by no equality
end
@testset "Basic with fixed variables" begin
S = @set x == 1 && x x^2
_test_polynomial_API(S, (x,))
@test sprint(show, S) == "{ (x) | -1 + x = 0, -x + x^2 ≥ 0 }"
@test sprint(show, MIME"text/plain"(), S) ==
"Basic semialgebraic Set defined by 1 equalitty\n -1 + x = 0\n1 inequalitty\n -x + x^2 ≥ 0\n"
Expand All @@ -106,6 +121,7 @@ Algebraic Set defined by no equality
"Algebraic Set defined by 1 equalitty\n -1 + x = 0\n"

S = @set x == 1 && x y && 2 == y
_test_polynomial_API(S, (x, y))
@test S isa BasicSemialgebraicSet{Int}
@test S.V isa FixedVariablesSet{<:AbstractVariable,Int}
@test rem(x + y, ideal(S.V)) == 3
Expand All @@ -118,6 +134,7 @@ Algebraic Set defined by no equality
@test rem(x + y, ideal(Sf.V)) == 3

S = @set x == 1 && x y && 2 == y && 1 == x
_test_polynomial_API(S, (x, y))
@test S isa BasicSemialgebraicSet{Int}
@test S.V isa FixedVariablesSet{<:AbstractVariable,Int}
@test rem(x + y, ideal(S.V)) == 3
Expand All @@ -136,6 +153,7 @@ Algebraic Set defined by no equality
)
@test S isa BasicSemialgebraicSet{Int}
@test S.V isa FixedVariablesSet{<:AbstractVariable,Int}
_test_polynomial_API(S, (x, y))
@test isempty(S.V)
@test iszero(length(S.V))
@test isempty(collect(S.V))
Expand All @@ -154,6 +172,7 @@ Algebraic Set defined by no equality
]
@test S isa BasicSemialgebraicSet{Rational{BigInt}}
@test S.V isa AlgebraicSet{Rational{BigInt}}
_test_polynomial_API(S, (x, y))
end

solver = DummySolver()
Expand All @@ -167,6 +186,7 @@ Algebraic Set defined by no equality
]
@test S isa BasicSemialgebraicSet{Rational{BigInt}}
@test S.V isa AlgebraicSet{Rational{BigInt}}
_test_polynomial_API(S, (x, y))
@test S.V.solver === solver
end
end
Expand Down

0 comments on commit ff8a74f

Please sign in to comment.