Ticket #7160: trac_7160.patch

File trac_7160.patch, 8.6 KB (added by mhansen, 9 years ago)
  • sage/rings/number_field/number_field.py

    # HG changeset patch
    # User Mike Hansen <mhansen@gmail.com>
    # Date 1324375761 0
    # Node ID 1786dd31c7146a8b815bd2ef9e53f9c008e3f7fb
    # Parent  9e29a3d84c48c399daaf3920bcb8b17273a0e876
    #7160: comparison with quadratic number field elements needs work
    
    diff --git a/sage/rings/number_field/number_field.py b/sage/rings/number_field/number_field.py
    a b  
    215215_nf_cache = {}
    216216def NumberField(polynomial, name=None, check=True, names=None, cache=True,
    217217                embedding=None, latex_name=None,
    218                 assume_disc_small=False,
     218                assume_disc_small=False, element_class=None,
    219219                maximize_at_primes=None):
    220220    r"""
    221221    Return *the* number field defined by the given irreducible
     
    458458
    459459    if polynomial.degree() == 2:
    460460        K = NumberField_quadratic(polynomial, name, latex_name, check, embedding,
    461              assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes)
     461             assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes,
     462             element_class=element_class)
    462463    else:
    463464        K = NumberField_absolute(polynomial, name, latex_name, check, embedding,
    464              assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes)
     465             assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes,
     466             element_class=element_class)
    465467
    466468    if cache:
    467469        _nf_cache[key] = weakref.ref(K)
     
    50355037class NumberField_absolute(NumberField_generic):
    50365038
    50375039    def __init__(self, polynomial, name, latex_name=None, check=True, embedding=None,
    5038                  assume_disc_small=False, maximize_at_primes=None):
     5040                 assume_disc_small=False, maximize_at_primes=None, element_class=None):
    50395041        """
    50405042        Function to initialize an absolute number field.
    50415043
     
    50495051        """
    50505052        NumberField_generic.__init__(self, polynomial, name, latex_name, check, embedding,
    50515053                                     assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes)
    5052         self._element_class = number_field_element.NumberFieldElement_absolute
     5054        if (element_class is None or
     5055            not issubclass(element_class, number_field_element.NumberFieldElement_absolute)):
     5056            self._element_class = number_field_element.NumberFieldElement_absolute
     5057        else:
     5058            self._element_class = element_class
    50535059        self._zero_element = self(0)
    50545060        self._one_element =  self(1)
    50555061
     
    77707776        Number Field in b with defining polynomial x^2 + 4
    77717777    """
    77727778    def __init__(self, polynomial, name=None, latex_name=None, check=True, embedding=None,
    7773                  assume_disc_small=False, maximize_at_primes=None):
     7779                 assume_disc_small=False, maximize_at_primes=None,
     7780                 element_class=None, **kwds):
    77747781        """
    77757782        Create a quadratic number field.
    77767783       
     
    77977804        NumberField_absolute.__init__(self, polynomial, name=name, check=check,
    77987805                                      embedding=embedding, latex_name=latex_name,
    77997806                                      assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes)
    7800         self._element_class = number_field_element_quadratic.NumberFieldElement_quadratic
     7807        if (element_class is None or
     7808            not issubclass(element_class, number_field_element_quadratic.NumberFieldElement_quadratic)):
     7809            self._element_class = number_field_element_quadratic.NumberFieldElement_quadratic
     7810        else:
     7811            self._element_class = element_class
     7812       
    78017813        c, b, a = [rational.Rational(t) for t in self.defining_polynomial().list()]
    78027814        # set the generator
    78037815        Dpoly = b*b - 4*a*c
  • sage/rings/number_field/number_field_element_quadratic.pxd

    diff --git a/sage/rings/number_field/number_field_element_quadratic.pxd b/sage/rings/number_field/number_field_element_quadratic.pxd
    a b  
    2121
    2222cdef class OrderElement_quadratic(NumberFieldElement_quadratic):
    2323    pass
     24
     25cdef class NumberFieldElement_quadratic_complex(NumberFieldElement_quadratic):
     26    pass
  • sage/rings/number_field/number_field_element_quadratic.pyx

    diff --git a/sage/rings/number_field/number_field_element_quadratic.pyx b/sage/rings/number_field/number_field_element_quadratic.pyx
    a b  
    3939from sage.rings.integer_ring import ZZ
    4040from sage.categories.morphism cimport Morphism
    4141from sage.rings.number_field.number_field_element import _inverse_mod_generic
     42from sage.structure.element cimport Element, generic_power_c
    4243
    4344import number_field
    4445
     
    16481649            'Natural morphism:\n  From: Rational Field\n  To:   Number Field in a with defining polynomial x^2 - 3'
    16491650        """
    16501651        return "Natural"
     1652
     1653
     1654cdef class NumberFieldElement_quadratic_complex(NumberFieldElement_quadratic):
     1655    """
     1656    This class is designed primarily to be used by the symbolics
     1657    system.  Upon comparison, the instances of this class will try to
     1658    see if they can be embedded as a real number and perform the
     1659    comparision as real numbers.  This fixes bug #6132 where we would
     1660    have things like ``bool(I^2) > 0`` returning ``True``.  See
     1661    :func:`sage.symbolic.pynac.init_pynac_I`.
     1662    """
     1663    cdef _new(self):
     1664        """
     1665        Quickly creates a new initialized
     1666        :class:``NumberFieldElement_quadratic_complex` with the same
     1667        parent as self.
     1668
     1669        EXAMPLES::
     1670
     1671            sage: from sage.rings.number_field.number_field import QuadraticField
     1672            sage: from sage.rings.number_field.number_field_element_quadratic import NumberFieldElement_quadratic_complex
     1673            sage: K = QuadraticField(-1, 'I', embedding=CC.gen(), latex_name='i', cache=False, element_class=NumberFieldElement_quadratic_complex)
     1674            sage: I = K.gen()
     1675
     1676            sage: type(I + I) == type(I)  # indirect test
     1677            True
     1678        """
     1679        cdef NumberFieldElement_quadratic_complex x
     1680        x = <NumberFieldElement_quadratic_complex>PY_NEW_SAME_TYPE(self)
     1681        x._parent = self._parent
     1682        x.D = self.D
     1683        return x
     1684
     1685    cdef int _cmp_c_impl(left, Element right) except -2:
     1686        """
     1687        EXAMPLES::
     1688       
     1689            sage: from sage.rings.number_field.number_field import QuadraticField
     1690            sage: from sage.rings.number_field.number_field_element_quadratic import NumberFieldElement_quadratic_complex
     1691            sage: K = QuadraticField(-1, 'I', embedding=CC.gen(), latex_name='i', cache=False, element_class=NumberFieldElement_quadratic_complex)
     1692            sage: I = K.gen()
     1693
     1694            sage: I^2
     1695            -1
     1696            sage: I^2 < 0
     1697            True
     1698            sage: I^2 > 0
     1699            False
     1700            sage: bool(2/3*I^2 >  -0.666666666666666666666666666666666)
     1701            False
     1702            sage: bool(2/3*I^2 == -0.666666666666666666666666666666667)
     1703            True
     1704            sage: bool(2/3*I^2 >  -0.666666666666666666666666666666668)
     1705            True
     1706        """
     1707        embedding = left.parent().coerce_embedding()
     1708        if embedding is not None:
     1709            try:
     1710                from sage.rings.all import QQbar
     1711                l = embedding(left)
     1712                return cmp(l, right)
     1713            except (TypeError, ValueError), exc:
     1714                pass
     1715        cdef NumberFieldElement_quadratic _right = right
     1716        return NumberFieldElement_quadratic._cmp_c_impl(left, _right)
  • sage/symbolic/pynac.pyx

    diff --git a/sage/symbolic/pynac.pyx b/sage/symbolic/pynac.pyx
    a b  
    19931993        sage: type(sage.symbolic.pynac.I)
    19941994        <type 'sage.symbolic.expression.Expression'>
    19951995        sage: type(sage.symbolic.pynac.I.pyobject())
    1996         <type 'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic'>
     1996        <type 'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic_complex'>
    19971997    """
    19981998    global pynac_I, I
    19991999    from sage.rings.number_field.number_field import QuadraticField
    2000     K = QuadraticField(-1, 'I', embedding=CC.gen(), latex_name='i')
     2000    from sage.rings.number_field.number_field_element_quadratic import NumberFieldElement_quadratic_complex
     2001    K = QuadraticField(-1, 'I', embedding=CC.gen(), latex_name='i', cache=False,
     2002                        element_class=NumberFieldElement_quadratic_complex)
    20012003    pynac_I = K.gen()
    20022004    ginac_pyinit_I(pynac_I)
    20032005    I = new_Expression_from_GEx(ring.SR, g_I)