Skip to content

Commit 16c8c5d

Browse files
authored
Remove ordering from cmp (#319)
* Remove ordering from cmp * Fix * Fix * Fix format * Fix * Fix * Fix * Fix
1 parent bf15d78 commit 16c8c5d

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

src/comparison.jl

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,15 @@ ordering(p::AbstractPolynomialLike) = ordering(typeof(p))
307307
# of x < y is equal to the result of Monomial(x) < Monomial(y)
308308
# Without `Base.@pure`, TypedPolynomials allocates on Julia v1.6
309309
# with `promote(x * y, x)`
310-
Base.@pure function Base.cmp(
311-
::AbstractMonomialOrdering,
312-
v1::AbstractVariable,
313-
v2::AbstractVariable,
314-
)
310+
Base.@pure function Base.cmp(v1::AbstractVariable, v2::AbstractVariable)
315311
return -cmp(name(v1), name(v2))
316312
end
317313

314+
function Base.cmp(m1::AbstractMonomial, m2::AbstractMonomial)
315+
s1, s2 = promote_variables(m1, m2)
316+
return cmp(ordering(m1)(), exponents(s1), exponents(s2))
317+
end
318+
318319
function compare(
319320
m1::AbstractMonomial,
320321
m2::AbstractMonomial,
@@ -335,22 +336,14 @@ end
335336
# less than `b`, they are considered sort of equal.
336337
_cmp_coefficient(a, b) = 0
337338

338-
function Base.cmp(
339-
ordering::O,
340-
t1::AbstractTermLike,
341-
t2::AbstractTermLike,
342-
) where {O<:AbstractMonomialOrdering}
343-
Δ = cmp(ordering, monomial(t1), monomial(t2))
339+
function Base.cmp(t1::AbstractTermLike, t2::AbstractTermLike)
340+
Δ = cmp(monomial(t1), monomial(t2))
344341
if iszero(Δ)
345342
return _cmp_coefficient(coefficient(t1), coefficient(t2))
346343
end
347344
return Δ
348345
end
349346

350-
function Base.cmp(t1::AbstractTermLike, t2::AbstractTermLike)
351-
return cmp(ordering(t1)(), t1, t2)
352-
end
353-
354347
Base.isless(t1::AbstractTermLike, t2::AbstractTermLike) = compare(t1, t2) < 0
355348

356349
_last_lex_index(n, ::Type{LexOrder}) = n

test/commutative/comparison.jl

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,22 @@
141141
@test p !== nothing
142142
@test p !== Dict{Int,Int}()
143143
end
144-
@testset "compare" begin
145-
lex = LexOrder
146-
grlex = Graded{lex}
147-
rinvlex = Reverse{InverseLexOrder}
148-
grevlex = Graded{rinvlex}
149-
Mod.@polyvar x y z
150-
# [CLO13, p. 58]
151-
@test compare(x * y^2 * z^3, x^3 * y^2, lex) < 0
152-
@test compare(x * y^2 * z^3, x^3 * y^2, grlex) > 0
153-
@test compare(x * y^2 * z^3, x^3 * y^2, rinvlex) < 0
154-
@test compare(x * y^2 * z^3, x^3 * y^2, grevlex) > 0
155-
@test compare(x * y^2 * z^4, x * y * z^5, lex) > 0
156-
@test compare(x * y^2 * z^4, x * y * z^5, grlex) > 0
157-
@test compare(x * y^2 * z^4, x * y * z^5, rinvlex) > 0
158-
@test compare(x * y^2 * z^4, x * y * z^5, grevlex) > 0
159-
# [CLO13, p. 59]
160-
@test compare(x^5 * y * z, x^4 * y * z^2, lex) > 0
161-
@test compare(x^5 * y * z, x^4 * y * z^2, grlex) > 0
162-
@test compare(x^5 * y * z, x^4 * y * z^2, rinvlex) > 0
163-
@test compare(x^5 * y * z, x^4 * y * z^2, grevlex) > 0
164-
# [CLO13] Cox, D., Little, J., & OShea, D.
165-
# *Ideals, varieties, and algorithms: an introduction to computational algebraic geometry and commutative algebra*.
166-
# Springer Science & Business Media, **2013**.
144+
lex = LexOrder
145+
grlex = Graded{lex}
146+
rinvlex = Reverse{InverseLexOrder}
147+
grevlex = Graded{rinvlex}
148+
@static if Symbol(Mod) == :DynamicPolynomials
149+
@testset "compare $M" for M in [lex, grlex, rinvlex, grevlex]
150+
Mod.@polyvar x y z monomial_order = M
151+
# [CLO13, p. 58]
152+
sgn = (M == lex || M == rinvlex) ? -1 : 1
153+
@test sgn * compare(x * y^2 * z^3, x^3 * y^2) > 0
154+
@test compare(x * y^2 * z^4, x * y * z^5) > 0
155+
# [CLO13, p. 59]
156+
@test compare(x^5 * y * z, x^4 * y * z^2) > 0
157+
# [CLO13] Cox, D., Little, J., & OShea, D.
158+
# *Ideals, varieties, and algorithms: an introduction to computational algebraic geometry and commutative algebra*.
159+
# Springer Science & Business Media, **2013**.
160+
end
167161
end
168162
end

0 commit comments

Comments
 (0)