Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions src/comparison.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,15 @@
# of x < y is equal to the result of Monomial(x) < Monomial(y)
# Without `Base.@pure`, TypedPolynomials allocates on Julia v1.6
# with `promote(x * y, x)`
Base.@pure function Base.cmp(
::AbstractMonomialOrdering,
v1::AbstractVariable,
v2::AbstractVariable,
)
Base.@pure function Base.cmp(v1::AbstractVariable, v2::AbstractVariable)
return -cmp(name(v1), name(v2))
end

function Base.cmp(m1::AbstractMonomial, m2::AbstractMonomial)
s1, s2 = promote_variables(m1, m2)
return cmp(ordering(m1)(), exponents(s1), exponents(s2))

Check warning on line 316 in src/comparison.jl

View check run for this annotation

Codecov / codecov/patch

src/comparison.jl#L314-L316

Added lines #L314 - L316 were not covered by tests
end

function compare(
m1::AbstractMonomial,
m2::AbstractMonomial,
Expand All @@ -335,22 +336,14 @@
# less than `b`, they are considered sort of equal.
_cmp_coefficient(a, b) = 0

function Base.cmp(
ordering::O,
t1::AbstractTermLike,
t2::AbstractTermLike,
) where {O<:AbstractMonomialOrdering}
Δ = cmp(ordering, monomial(t1), monomial(t2))
function Base.cmp(t1::AbstractTermLike, t2::AbstractTermLike)
Δ = cmp(monomial(t1), monomial(t2))

Check warning on line 340 in src/comparison.jl

View check run for this annotation

Codecov / codecov/patch

src/comparison.jl#L339-L340

Added lines #L339 - L340 were not covered by tests
if iszero(Δ)
return _cmp_coefficient(coefficient(t1), coefficient(t2))
end
return Δ
end

function Base.cmp(t1::AbstractTermLike, t2::AbstractTermLike)
return cmp(ordering(t1)(), t1, t2)
end

Base.isless(t1::AbstractTermLike, t2::AbstractTermLike) = compare(t1, t2) < 0

_last_lex_index(n, ::Type{LexOrder}) = n
Expand Down
40 changes: 17 additions & 23 deletions test/commutative/comparison.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,22 @@
@test p !== nothing
@test p !== Dict{Int,Int}()
end
@testset "compare" begin
lex = LexOrder
grlex = Graded{lex}
rinvlex = Reverse{InverseLexOrder}
grevlex = Graded{rinvlex}
Mod.@polyvar x y z
# [CLO13, p. 58]
@test compare(x * y^2 * z^3, x^3 * y^2, lex) < 0
@test compare(x * y^2 * z^3, x^3 * y^2, grlex) > 0
@test compare(x * y^2 * z^3, x^3 * y^2, rinvlex) < 0
@test compare(x * y^2 * z^3, x^3 * y^2, grevlex) > 0
@test compare(x * y^2 * z^4, x * y * z^5, lex) > 0
@test compare(x * y^2 * z^4, x * y * z^5, grlex) > 0
@test compare(x * y^2 * z^4, x * y * z^5, rinvlex) > 0
@test compare(x * y^2 * z^4, x * y * z^5, grevlex) > 0
# [CLO13, p. 59]
@test compare(x^5 * y * z, x^4 * y * z^2, lex) > 0
@test compare(x^5 * y * z, x^4 * y * z^2, grlex) > 0
@test compare(x^5 * y * z, x^4 * y * z^2, rinvlex) > 0
@test compare(x^5 * y * z, x^4 * y * z^2, grevlex) > 0
# [CLO13] Cox, D., Little, J., & OShea, D.
# *Ideals, varieties, and algorithms: an introduction to computational algebraic geometry and commutative algebra*.
# Springer Science & Business Media, **2013**.
lex = LexOrder
grlex = Graded{lex}
rinvlex = Reverse{InverseLexOrder}
grevlex = Graded{rinvlex}
@static if Symbol(Mod) == :DynamicPolynomials
@testset "compare $M" for M in [lex, grlex, rinvlex, grevlex]
Mod.@polyvar x y z monomial_order = M
# [CLO13, p. 58]
sgn = (M == lex || M == rinvlex) ? -1 : 1
@test sgn * compare(x * y^2 * z^3, x^3 * y^2) > 0
@test compare(x * y^2 * z^4, x * y * z^5) > 0
# [CLO13, p. 59]
@test compare(x^5 * y * z, x^4 * y * z^2) > 0
# [CLO13] Cox, D., Little, J., & OShea, D.
# *Ideals, varieties, and algorithms: an introduction to computational algebraic geometry and commutative algebra*.
# Springer Science & Business Media, **2013**.
end
end
end
Loading