Skip to content

Commit ff8a74f

Browse files
authored
Implement monomial_type (#46)
1 parent 3174bdd commit ff8a74f

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

src/SemialgebraicSets.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function Base.show(io::IO, ::FullSpace)
3232
end
3333
nequalities(::FullSpace) = 0
3434
equalities(::FullSpace) = []
35-
MP.similar_type(S::Type{FullSpace}, T::Type) = S
35+
MP.similar_type(S::Type{FullSpace}, ::Type) = S
36+
MP.monomial_type(::Type{FullSpace}) = nothing
3637

3738
function Base.similar(set::AbstractSemialgebraicSet, T::Type)
3839
return convert(MP.similar_type(typeof(set), T), set)

src/basic.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ function basic_semialgebraic_set(V, p)
3030
return BasicSemialgebraicSet(V, p)
3131
end
3232

33+
algebraic_set(set::BasicSemialgebraicSet) = set.V
34+
3335
function MP.similar_type(
3436
::Type{BasicSemialgebraicSet{S,PS,AT}},
3537
T::Type,
@@ -56,6 +58,15 @@ end
5658
function MP.variables(S::BasicSemialgebraicSet)
5759
return sort(union(MP.variables(S.V), MP.variables(S.p)); rev = true)
5860
end
61+
function MP.monomial_type(::Type{BasicSemialgebraicSet{T,P,A}}) where {T,P,A}
62+
M1 = MP.monomial_type(A)
63+
M2 = MP.monomial_type(P)
64+
if isnothing(M1)
65+
return M2
66+
else
67+
return promote_type(M1, M2)
68+
end
69+
end
5970
nequalities(S::BasicSemialgebraicSet) = nequalities(S.V)
6071
equalities(S::BasicSemialgebraicSet) = equalities(S.V)
6172
add_equality!(S::BasicSemialgebraicSet, p) = add_equality!(S.V, p)

src/fix.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ end
6868

6969
ideal(set::FixedVariablesSet, args...) = set.ideal
7070
MP.variables(set::FixedVariablesSet) = MP.variables(set.ideal)
71+
function MP.monomial_type(::Type{FixedVariablesSet{V,T,M}}) where {V,T,M}
72+
return M
73+
end
7174
function nequalities(set::FixedVariablesSet)
7275
if set.ideal.substitutions === nothing
7376
return 1

src/variety.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ end
118118
ideal(V::AlgebraicSet) = V.I
119119

120120
MP.variables(V::AlgebraicSet) = MP.variables(V.I)
121+
MP.monomial_type(::Type{<:AlgebraicSet{T,P}}) where {T,P} = MP.monomial_type(P)
121122
nequalities(V::AlgebraicSet) = length(V.I.p)
122123
equalities(V::AlgebraicSet) = V.I.p
123124
add_equality!(V::AlgebraicSet, p) = push!(V.I.p, p)

test/macro.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ function SemialgebraicSets.default_gröbner_basis_algorithm(p, ::DummySolver)
1010
end
1111
SemialgebraicSets.promote_for(::Type{T}, ::Type{DummySolver}) where {T} = T
1212

13+
function _test_polynomial_API(set, vars)
14+
mono = prod(vars)
15+
@test @inferred(variables(set)) == variables(mono)
16+
@test @inferred(monomial_type(typeof(set))) == monomial_type(typeof(mono))
17+
end
18+
1319
function runtests()
1420
Main.Mod.@polyvar x y
1521
@test isa(FullSpace(), FullSpace)
1622
V = @set x * y == 1
23+
_test_polynomial_API(V, (x, y))
1724
@test V isa AlgebraicSet{Rational{BigInt}}
1825
@test_throws ArgumentError add_inequality!(V, x * y)
1926
@testset "Basic" begin
@@ -22,6 +29,7 @@ function runtests()
2229
add_inequality!(S, x + y - 1)
2330
# Algebraic set forces `Rational{BigInt}`
2431
@test S isa BasicSemialgebraicSet{Rational{BigInt}}
32+
_test_polynomial_API(S, (x, y))
2533
@test S == basic_semialgebraic_set(S.V, S.p)
2634
@test sprint(show, S) ==
2735
"{ (x, y) | -y + x = 0, -y + x^2 = 0, -1//1 + x^2*y ≥ 0, -1//1 + y + x ≥ 0 }"
@@ -49,29 +57,35 @@ function runtests()
4957
1.0x^2 * y >= 0 &&
5058
(6 // 3) * x^2 * y == -y &&
5159
1.5x + y >= 0)
60+
_test_polynomial_API(S, (x, y))
5261
S2 = S V
5362
S3 = V S
5463
@test inequalities(S2) == inequalities(S3) == S.p
5564
@test equalities(S2) == S3.V.I.p
5665
end
5766

5867
T = (@set x * y^2 == -1 && x^2 + y^2 <= 1)
68+
_test_polynomial_API(T, (x, y))
5969
V2 = @set T.V && V && x + y == 2.0
70+
_test_polynomial_API(V2, (x, y))
6071
@test V2 isa AlgebraicSet
6172
@test V2.I.p == [equalities(T); equalities(V); x + y - 2.0]
6273
S4 = @set S && T
74+
_test_polynomial_API(S4, (x, y))
6375
@test S4.p == [S.p; inequalities(T)]
6476
@test equalities(S4) == [S.V.I.p; T.V.I.p]
6577

6678
@testset "Different variables" begin
6779
T = (@set x == x^2 && y <= y^2)
80+
_test_polynomial_API(T, (x, y))
6881
@test sprint(show, T) == "{ (x, y) | x - x^2 = 0, -y + y^2 ≥ 0 }"
6982
@test sprint(show, MIME"text/plain"(), T) ==
7083
"Basic semialgebraic Set defined by 1 equalitty\n x - x^2 = 0\n1 inequalitty\n -y + y^2 ≥ 0\n"
7184
end
7285
end
7386
@testset "Basic with no equality" begin
7487
S = @set x + y 1 && x y
88+
_test_polynomial_API(S, (x, y))
7589
@test sprint(show, S) == "{ (x, y) | -1 + y + x ≥ 0, y - x ≥ 0 }"
7690
@test sprint(show, MIME"text/plain"(), S) == """
7791
Basic semialgebraic Set defined by no equality
@@ -98,6 +112,7 @@ Algebraic Set defined by no equality
98112
end
99113
@testset "Basic with fixed variables" begin
100114
S = @set x == 1 && x x^2
115+
_test_polynomial_API(S, (x,))
101116
@test sprint(show, S) == "{ (x) | -1 + x = 0, -x + x^2 ≥ 0 }"
102117
@test sprint(show, MIME"text/plain"(), S) ==
103118
"Basic semialgebraic Set defined by 1 equalitty\n -1 + x = 0\n1 inequalitty\n -x + x^2 ≥ 0\n"
@@ -106,6 +121,7 @@ Algebraic Set defined by no equality
106121
"Algebraic Set defined by 1 equalitty\n -1 + x = 0\n"
107122

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

120136
S = @set x == 1 && x y && 2 == y && 1 == x
137+
_test_polynomial_API(S, (x, y))
121138
@test S isa BasicSemialgebraicSet{Int}
122139
@test S.V isa FixedVariablesSet{<:AbstractVariable,Int}
123140
@test rem(x + y, ideal(S.V)) == 3
@@ -136,6 +153,7 @@ Algebraic Set defined by no equality
136153
)
137154
@test S isa BasicSemialgebraicSet{Int}
138155
@test S.V isa FixedVariablesSet{<:AbstractVariable,Int}
156+
_test_polynomial_API(S, (x, y))
139157
@test isempty(S.V)
140158
@test iszero(length(S.V))
141159
@test isempty(collect(S.V))
@@ -154,6 +172,7 @@ Algebraic Set defined by no equality
154172
]
155173
@test S isa BasicSemialgebraicSet{Rational{BigInt}}
156174
@test S.V isa AlgebraicSet{Rational{BigInt}}
175+
_test_polynomial_API(S, (x, y))
157176
end
158177

159178
solver = DummySolver()
@@ -167,6 +186,7 @@ Algebraic Set defined by no equality
167186
]
168187
@test S isa BasicSemialgebraicSet{Rational{BigInt}}
169188
@test S.V isa AlgebraicSet{Rational{BigInt}}
189+
_test_polynomial_API(S, (x, y))
170190
@test S.V.solver === solver
171191
end
172192
end

0 commit comments

Comments
 (0)