Ticket #6132: 6132-nf-elt-cmp.patch

File 6132-nf-elt-cmp.patch, 3.7 KB (added by robertwb, 12 years ago)
  • sage/rings/number_field/number_field_element.pyx

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1243367232 25200
    # Node ID 91713ac254bdc6804ca756daf394f5d46842da68
    # Parent  923341d86e95bf17084a533a3cfe4c49c5ccb042
    Consistent ordering of number field elements.
    
    diff -r 923341d86e95 -r 91713ac254bd sage/rings/number_field/number_field_element.pyx
    a b  
    578578            raise IndexError, "index must be between 0 and degree minus 1."
    579579        return self.polynomial()[n]
    580580
    581     cdef int _cmp_c_impl(left, sage.structure.element.Element right) except -2:
    582         cdef NumberFieldElement _right = right
    583         return not (ZZX_equal(left.__numerator, _right.__numerator) and ZZ_equal(left.__denominator, _right.__denominator))
     581    cdef int _cmp_c_impl(left, sage.structure.element.Element _right) except -2:
     582        """
     583        Compares two elements of the same number field.
     584       
     585        EXAMPLES::
     586       
     587            sage: K.<a> = NumberField(x^3-2)
     588            sage: a == a
     589            True
     590            sage: a == a^2
     591            False
     592            sage: a+1/2 == a+1/2
     593            True
     594            sage: (a+1)/3 == (a+1)/5
     595            False
     596
     597        The ordering is arbitrary but consistent::
     598       
     599            sage: K.<a> = NumberField(x^5-x+1)
     600            sage: cmp(a^17, 1/a) == -cmp(1/a, a^17) in [1, -1]
     601            True
     602            sage: cmp(a+1/3, a+2/3) == -cmp(a+2/3, a+1/3) in [1, -1]
     603            True
     604            sage: len(set([a^k for k in range(50)] + [a^k for k in range(50)]))
     605            50
     606        """
     607        cdef NumberFieldElement right = _right
     608        cdef int c
     609        c = ZZ_compare(left.__denominator, right.__denominator)
     610        if c:
     611            return c
     612        c = ZZX_deg(left.__numerator) - ZZX_deg(right.__numerator)
     613        if c:
     614            return -1 if c < 0 else 1
     615        cdef int i
     616        cdef ZZ_c *left_elts, *right_elts
     617        left_elts = left.__numerator.rep.elts()
     618        right_elts = right.__numerator.rep.elts()
     619        for i from 0 <= i < ZZX_deg(left.__numerator):
     620            c = ZZ_compare(left_elts[i], right_elts[i])
     621            if c:
     622                return c
     623        return 0
    584624
    585625    def __abs__(self):
    586626        r"""
  • sage/rings/number_field/number_field_element_quadratic.pyx

    diff -r 923341d86e95 -r 91713ac254bd sage/rings/number_field/number_field_element_quadratic.pyx
    a b  
    731731           
    732732    cdef int _cmp_c_impl(self, Element _right) except -2:
    733733        """
    734         EXAMPLES:
     734        EXAMPLES::
     735       
    735736            sage: K.<a> = NumberField(x^2+163)
    736737            sage: K(1/2)==1/2
    737738            True
     
    739740            False
    740741            sage: 2+a == a+2
    741742            True
     743       
     744        The ordering is arbitrary, but consistent::
     745       
     746            sage: cmp(a, 1/2) == -cmp(1/2, a) in [1, -1]
     747            True
     748            sage: cmp(a, 1+a) == -cmp(1+a, a) in [1, -1]
     749            True
     750            sage: cmp(a, 2*a) == -cmp(2*a, a) in [1, -1]
     751            True
    742752        """
    743753        cdef NumberFieldElement_quadratic right = _right
    744         return not mpz_cmp(self.a, right.a)==0  \
    745             or not mpz_cmp(self.b, right.b)==0  \
    746             or not mpz_cmp(self.denom, right.denom) == 0
    747 
     754        cdef int c = mpz_cmp(self.denom, right.denom)
     755        if c != 0: return c
     756        c = mpz_cmp(self.b, right.b)
     757        if c != 0: return c
     758        return mpz_cmp(self.a, right.a)
    748759
    749760    def __nonzero__(self):
    750761        """