@@ -249,12 +249,19 @@ struct Graded{O<:AbstractMonomialOrdering} <: AbstractMonomialOrdering
249249 same_degree_ordering:: O
250250end
251251
252- _deg (exponents) = sum (exponents)
253- _deg (mono:: AbstractMonomial ) = degree (mono)
252+ function compare (a:: _TupleOrVector , b:: _TupleOrVector , :: Type{Graded{O}} ) where {O}
253+ deg_a = sum (a)
254+ deg_b = sum (b)
255+ if deg_a == deg_b
256+ return compare (a, b, O)
257+ else
258+ return deg_a - deg_b
259+ end
260+ end
254261
255- function compare (a, b, :: Type{Graded{O}} ) where {O}
256- deg_a = _deg (a)
257- deg_b = _deg (b)
262+ function compare (a:: AbstractMonomial , b:: AbstractMonomial , :: Type{Graded{O}} ) where {O}
263+ deg_a = degree (a)
264+ deg_b = degree (b)
258265 if deg_a == deg_b
259266 return compare (a, b, O)
260267 else
@@ -293,6 +300,43 @@ Returns the [`AbstractMonomialOrdering`](@ref) used for the monomials of `p`.
293300"""
294301function ordering end
295302
303+ ordering (:: Type{P} ) where {P} = ordering (monomial_type (P))
304+ ordering (p:: AbstractPolynomialLike ) = ordering (typeof (p))
305+
306+ # We reverse the order of comparisons here so that the result
307+ # of x < y is equal to the result of Monomial(x) < Monomial(y)
308+ function compare (v1:: AbstractVariable , v2:: AbstractVariable , :: Type{<:AbstractMonomialOrdering} )
309+ return - cmp (name (v1), name (v2))
310+ end
311+
312+ function compare (m1:: AbstractMonomial , m2:: AbstractMonomial , :: Type{O} ) where {O<: AbstractMonomialOrdering }
313+ s1, s2 = promote_variables (m1, m2)
314+ return compare (exponents (s1), exponents (s2), O)
315+ end
316+
317+ # Implement this to make coefficients be compared with terms.
318+ function _cmp_coefficient (a:: Real , b:: Real )
319+ return cmp (a, b)
320+ end
321+ function _cmp_coefficient (a:: Number , b:: Number )
322+ return cmp (abs (a), abs (b))
323+ end
324+ # By default, coefficients are not comparable so `a` is not strictly
325+ # less than `b`, they are considered sort of equal.
326+ _cmp_coefficient (a, b) = 0
327+
328+ function compare (t1:: AbstractTerm , t2:: AbstractTerm , :: Type{O} ) where {O<: AbstractMonomialOrdering }
329+ Δ = compare (monomial (t1), monomial (t2), O)
330+ if iszero (Δ)
331+ return _cmp_coefficient (coefficient (t1), coefficient (t2))
332+ end
333+ return Δ
334+ end
335+
336+ Base. cmp (t1:: AbstractTermLike , t2:: AbstractTermLike ) = compare (t1, t2, ordering (t1))
337+
338+ Base. isless (t1:: AbstractTermLike , t2:: AbstractTermLike ) = cmp (t1, t2) < 0
339+
296340_last_lex_index (n, :: Type{LexOrder} ) = n
297341_prev_lex_index (i, :: Type{LexOrder} ) = i - 1
298342_not_first_indices (n, :: Type{LexOrder} ) = n: - 1 : 2
0 commit comments