Skip to content

Remove unnecessary compare implementations #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 10 additions & 13 deletions src/comparison.jl
Original file line number Diff line number Diff line change
Expand Up @@ -308,20 +308,22 @@ ordering(p::AbstractPolynomialLike) = ordering(typeof(p))
# Without `Base.@pure`, TypedPolynomials allocates on Julia v1.6
# with `promote(x * y, x)`
Base.@pure function Base.cmp(
::AbstractMonomialOrdering,
v1::AbstractVariable,
v2::AbstractVariable,
)
return -cmp(name(v1), name(v2))
end

function compare(
function Base.:(==)(m1::AbstractMonomialLike, m2::AbstractMonomialLike)
return iszero(cmp(m1, m2))
end

function Base.cmp(
m1::AbstractMonomial,
m2::AbstractMonomial,
::Type{O},
) where {O<:AbstractMonomialOrdering}
)
s1, s2 = promote_variables(m1, m2)
return cmp(O(), exponents(s1), exponents(s2))
return cmp(ordering(m1)(), exponents(s1), exponents(s2))
end

# Implement this to make coefficients be compared with terms.
Expand All @@ -336,22 +338,17 @@ end
_cmp_coefficient(a, b) = 0

function Base.cmp(
ordering::O,
t1::AbstractTermLike,
t2::AbstractTermLike,
) where {O<:AbstractMonomialOrdering}
Δ = cmp(ordering, monomial(t1), monomial(t2))
)
Δ = cmp(monomial(t1), monomial(t2))
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
Base.isless(t1::AbstractTermLike, t2::AbstractTermLike) = cmp(t1, t2) < 0

_last_lex_index(n, ::Type{LexOrder}) = n
_prev_lex_index(i, ::Type{LexOrder}) = i - 1
Expand Down
8 changes: 4 additions & 4 deletions src/default_polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Base.one(p::Polynomial) = one(typeof(p))
Base.zero(::Type{Polynomial{C,T,A}}) where {C,T,A} = Polynomial{C,T,A}(A())
Base.zero(t::Polynomial) = zero(typeof(t))

compare_monomials(a, b) = compare(monomial(a), monomial(b))
compare_monomials(a, b) = cmp(monomial(a), monomial(b))

function join_terms(
terms1::AbstractArray{<:Term},
Expand Down Expand Up @@ -294,7 +294,7 @@ function __polynomial_merge!(
if tp isa Int && j isa Int
tp = get1(tp)
end
compare(monomial(*(monomials(q)[j], monomial.(t)...)), monomial(tp))
cmp(monomial(*(monomials(q)[j], monomial.(t)...)), monomial(tp))
end
end
get2 = let t = t, q = q
Expand Down Expand Up @@ -385,7 +385,7 @@ function __polynomial_merge!(
if tp isa Int && j isa Int
tp = get1(tp)
end
compare(monomial(monomial(t) * monomials(q)[j]), monomial(tp))
cmp(monomial(monomial(t) * monomials(q)[j]), monomial(tp))
end
end
get2 = let t = t, q = q
Expand Down Expand Up @@ -460,7 +460,7 @@ function __polynomial_merge!(
if t isa Int && j isa Int
t = get1(t)
end
compare(monomials(q)[j], monomial(t))
cmp(monomials(q)[j], monomial(t))
end
end
get2 = let q = q
Expand Down
2 changes: 1 addition & 1 deletion src/sequences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function merge_sorted!(
while i1 <= lastindex(v1) && i2 <= lastindex(v2)
x1 = v1[i1]
x2 = v2[i2]
Δ = compare(x1, x2)
Δ = cmp(x1, x2)
if Δ < 0
if filter(x1)
result[i] = x1
Expand Down
45 changes: 22 additions & 23 deletions test/commutative/comparison.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,27 @@
@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}
if 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 * cmp(x * y^2 * z^3, x^3 * y^2) > 0
@test cmp(x * y^2 * z^4, x * y * z^5) > 0
# [CLO13, p. 59]
@test cmp(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