Ticket #9541: trac_9541-part2.patch

File trac_9541-part2.patch, 21.1 KB (added by was, 7 years ago)
  • sage/rings/number_field/number_field.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1279638268 -7200
    # Node ID e2e7a6415ab660aff68af20125275dd25cb95e5a
    # Parent  e122468eb71e63393f1d8a5ac09f97ab05023f35
    [mq]: trac_9541-part2.patch
    
    diff -r e122468eb71e -r e2e7a6415ab6 sage/rings/number_field/number_field.py
    a b  
    418418        sage: K.<a> = NumberField(x^3 + 17); type(a)
    419419        <type 'sage.rings.number_field.number_field_element.NumberFieldElement_absolute'>
    420420        sage: K.<a> = NumberField(x^3 + 17, implementation='ntl'); type(a)
    421         <type 'sage.rings.number_field.number_field_element.NumberFieldElement_absolute'>
     421        <type 'sage.rings.number_field.number_field_element_ntl.NumberFieldElement_ntl_absolute'>           
    422422        sage: K.<a> = NumberField(x^3 + 17, implementation='flint'); type(a)
    423423        <type 'sage.rings.number_field.number_field_element_flint.NumberFieldElement_flint'>
    424424        sage: K.<a> = NumberField(x^3 + 17, implementation='generic'); type(a)
     
    55575557       
    55585558            sage: phi = K.embeddings(L)[0]
    55595559            sage: phi(K.0)
    5560             1/120*a2^5 + 19/60*a2
     5560            -1/120*a2^5 - 19/60*a2
    55615561            sage: phi(K.0).minpoly()
    55625562            x^4 - 2
    55635563
     
    56595659            Ring morphism:
    56605660              From: Number Field in a with defining polynomial x^3 - 2
    56615661              To:   Number Field in a1 with defining polynomial x^6 + 40*x^3 + 1372
    5662               Defn: a |--> 1/84*a1^4 + 13/42*a1
     5662              Defn: a |--> -1/126*a1^4 + 8/63*a1
    56635663            sage: K.embeddings(L)  is K.embeddings(L)
    56645664            True
    56655665       
     
    56725672            Ring morphism:
    56735673              From: Number Field in a with defining polynomial x^2 + 7
    56745674              To:   Cyclotomic Field of order 7 and degree 6
    5675               Defn: a |--> 2*zeta7^4 + 2*zeta7^2 + 2*zeta7 + 1,
     5675              Defn: a |--> -2*zeta7^4 - 2*zeta7^2 - 2*zeta7 - 1,
    56765676            Ring morphism:
    56775677              From: Number Field in a with defining polynomial x^2 + 7
    56785678              To:   Cyclotomic Field of order 7 and degree 6
    5679               Defn: a |--> -2*zeta7^4 - 2*zeta7^2 - 2*zeta7 - 1
     5679              Defn: a |--> 2*zeta7^4 + 2*zeta7^2 + 2*zeta7 + 1
    56805680            ]
    56815681       
    56825682        We embed a cubic field in the complex numbers::
     
    59775977            sage: K.<a> = NumberField(x^4 + 2*x^2 + 2)
    59785978            sage: K0 = K.subfields(2)[0][0]; K0
    59795979            Number Field in a0 with defining polynomial x^2 - 2*x + 2
    5980             sage: rho, tau = K0.embeddings(K)
     5980            sage: tau, rho = K0.embeddings(K)
    59815981            sage: L0 = K.relativize(rho(K0.gen()), 'b'); L0
    59825982            Number Field in b0 with defining polynomial x^2 - b1 + 2 over its base field
    59835983            sage: L1 = K.relativize(rho, 'b'); L1
     
    73657365            sage: K.<a> = NumberField(x^2 + 17, implementation='flint'); type(a)
    73667366            <type 'sage.rings.number_field.number_field_element_flint.NumberFieldElement_flint'>
    73677367            sage: K.<a> = NumberField(x^2 + 17, implementation='ntl'); type(a)
    7368             <type 'sage.rings.number_field.number_field_element.NumberFieldElement_absolute'>       
     7368            <type 'sage.rings.number_field.number_field_element_ntl.NumberFieldElement_ntl_absolute'>
    73697369        """
    73707370        NumberField_absolute.__init__(self, polynomial, name=name, check=check, embedding=embedding, latex_name=latex_name)
    73717371        if implementation == 'quadratic':
  • sage/rings/number_field/number_field_element.pxd

    diff -r e122468eb71e -r e2e7a6415ab6 sage/rings/number_field/number_field_element.pxd
    a b  
    1818    cdef object __pari
    1919    cdef object __matrix
    2020    cdef object __symbolic
     21    cdef bint cmp_eq(left, right)
    2122   
    2223   
    2324   
  • sage/rings/number_field/number_field_element.pyx

    diff -r e122468eb71e -r e2e7a6415ab6 sage/rings/number_field/number_field_element.pyx
    a b  
    249249       
    250250        EXAMPLES::
    251251       
    252             sage: k.<a> = NumberField(x^3 - 17*x^2 + 1)
     252            sage: k.<a> = NumberField(x^3 - 17*x^2 + 1, implementation='ntl')
    253253            sage: t = a.__reduce__(); t
    254             (<built-in function __create__NumberFieldElement_version1>, (Number Field in a with defining polynomial x^3 - 17*x^2 + 1, <type 'sage.rings.number_field.number_field_element.NumberFieldElement_absolute'>, x))
     254            (<built-in function __create__NumberFieldElement_version1>, (Number Field in a with defining polynomial x^3 - 17*x^2 + 1, <type 'sage.rings.number_field.number_field_element_ntl.NumberFieldElement_ntl_absolute'>, x))
    255255            sage: t[0](*t[1]) == a
    256256            True
    257257        """
     
    629629            if n < 0 or n >= self.parent().relative_degree():
    630630                raise IndexError, "index must be between 0 and the relative degree minus 1."
    631631            return self.vector()[n]
    632 
     632    ####################################################
     633    # Comparision
     634    ####################################################
     635    # First some boilerplate
     636    def _richcmp_(left, right, int op):
     637        if op == 2: return (<NumberFieldElement>left).cmp_eq(right)
     638        elif op == 3: return not (<NumberFieldElement>left).cmp_eq(right)
     639        return (<Element>left)._richcmp(right, op)
     640    # Derived classes should overload both of the following for optimal efficiency.
     641    cdef int _cmp_c_impl(left, sage.structure.element.Element right) except -2:
     642        return cmp(left.polynomial(), right.polynomial())
     643    cdef bint cmp_eq(left, right):
     644        return left._cmp_c_impl(right) == 0
     645    # Derived classes that implement __hash__ !!! MUST !!! copy the following two lines!!!
    633646    def __richcmp__(left, right, int op):
    634647        return (<Element>left)._richcmp(right, op)
     648    ####################################################
    635649
    636     cdef int _cmp_c_impl(left, sage.structure.element.Element right) except -2:
    637         raise NotImplementedError, "derived class must implement"
    638650   
    639651    def _random_element(self, num_bound=None, den_bound=None, distribution=None):
    640652        """
     
    18861898            sage: K.<a> = NumberField(x^4 + 1); L.<a2> = NumberField(x^2 + 1)
    18871899            sage: a.matrix(L)
    18881900            [ 0  1]
    1889             [a2  0]
     1901            [-a2  0]
    18901902       
    18911903        Notice that if we compute all embeddings and choose a different
    18921904        one, then the matrix is changed as it should be::
     
    18941906            sage: v = L.embeddings(K)
    18951907            sage: a.matrix(v[1])
    18961908            [  0   1]
    1897             [-a2   0]
     1909            [a2   0]
    18981910       
    18991911        The norm is also changed::
    19001912       
    19011913            sage: a.norm(v[1])
     1914            -a2
     1915            sage: a.norm(v[0])
    19021916            a2
    1903             sage: a.norm(v[0])
    1904             -a2
    19051917       
    19061918        TESTS::
    19071919       
     
    30053017        EXAMPLES::
    30063018
    30073019            sage: K.<a,b> = NumberField([x^3+5, x^2-7])
    3008             sage: (a+b).coordinates_in_terms_of_powers()
     3020            sage: C = (a+b).coordinates_in_terms_of_powers(); C
     3021            Coordinate function that writes elements in terms of the powers of a + b
    30093022            sage: C.alpha()
    30103023            a + b
    30113024        """
  • sage/rings/number_field/number_field_element_generic.pyx

    diff -r e122468eb71e -r e2e7a6415ab6 sage/rings/number_field/number_field_element_generic.pyx
    a b  
    2323
    2424cdef class NumberFieldElement_generic(NumberFieldElement):
    2525    cdef object _f
     26
    2627    """
    2728    EXAMPLES::
    2829
    29         sage: from sage.rings.number_field.number_field_element_generic import NumberFieldElement_generic
    30         sage: K.<a> = NumberField(x^3-2); b = a^2 - 3; b
     30        sage: K.<a> = NumberField(x^3 - 2, implementation='generic'); b = a^2 - 3; b
    3131        a^2 - 3
    32         sage: b.parent()
    33         Number Field in a with defining polynomial x^3 - 2
     32        sage: type(b)
     33        <type 'sage.rings.number_field.number_field_element_generic.NumberFieldElement_generic'>
    3434    """
    3535    cpdef bint is_absolute(self):
    3636        """
     37        Return true if this is an element of an absolute extension.
     38       
    3739        EXAMPLES::
    3840
    39         This must be defined in the derived class.
    40        
    41             sage: ?
     41            sage: K.<a> = NumberField(x^3-2, implementation='generic'); type(a)
     42            <type 'sage.rings.number_field.number_field_element_generic.NumberFieldElement_generic'>
     43            sage: a.is_absolute()
     44            True
    4245        """
    4346        return self._parent.is_absolute()
    4447   
    4548    def __init__(self, parent, f):
     49        """
     50        INPUT:
     51            - ``parent`` -- number field
     52            - ``f`` -- element
     53           
     54        EXAMPLES::
     55
     56            sage: K.<abc> = NumberField(x^7-2/3*x+1, implementation='generic'); abc
     57            abc
     58            sage: type(abc)
     59            <type 'sage.rings.number_field.number_field_element_generic.NumberFieldElement_generic'>
     60        """
    4661        self._parent = parent
    4762        self._f = parent.polynomial_ring()(f)
    4863
  • sage/rings/number_field/number_field_element_ntl.pyx

    diff -r e122468eb71e -r e2e7a6415ab6 sage/rings/number_field/number_field_element_ntl.pyx
    a b  
    22Number Field Elements
    33
    44AUTHORS:
    5 
    6 - William Stein: version before it got Cython'd
    7 
    8 - Joel B. Mohler (2007-03-09): First reimplementation in Cython
    9 
    10 - William Stein (2007-09-04): add doctests
    11 
    12 - Robert Bradshaw (2007-09-15): specialized classes for relative and
    13   absolute elements
    14 
    15 - John Cremona (2009-05-15): added support for local and global
    16   logarithmic heights.
    17 
     5    - William Stein: version before it got Cython'd
     6    - Joel B. Mohler (2007-03-09): First reimplementation in Cython
     7    - William Stein (2007-09-04): add doctests
     8    - Robert Bradshaw (2007-09-15): specialized classes for relative and
     9      absolute elements
     10    - John Cremona (2009-05-15): added support for local and global
     11      logarithmic heights.
     12    - William Stein (2010-07): major refactoring. 
    1813"""
    1914
    2015# TODO -- relative extensions need to be completely rewritten, so one
     
    2823# Relative elements need to be a derived class or something.  This is
    2924# terrible as it is now.
    3025
    31 #*****************************************************************************
     26###############################################################################
    3227#       Copyright (C) 2004, 2007 William Stein <wstein@gmail.com>
    3328#
    3429#  Distributed under the terms of the GNU General Public License (GPL)
     
    4136#  The full text of the GPL is available at:
    4237#
    4338#                  http://www.gnu.org/licenses/
    44 #*****************************************************************************
     39###############################################################################
    4540
    4641import operator
    4742
     
    389384        ZZX_rem(x.__numerator, result, x.__fld_numerator.x)
    390385        return x
    391386
     387
    392388    cdef int _cmp_c_impl(left, sage.structure.element.Element right) except -2:
    393389        """
    394390        EXAMPLES::
    395391
    396             sage: ?
     392            sage: K.<a> = NumberField(x^7-2/3*x+1, implementation='ntl')
     393            sage: cmp(a, a+5)            # indirect doctest
     394            -1
     395            sage: cmp(a+5, a)
     396            1
     397        """
     398        return cmp(left.polynomial(), right.polynomial())
     399
     400    cdef bint cmp_eq(left, right):
     401        """
     402        EXAMPLES::
     403
     404            sage: K.<a> = NumberField(x^7-2/3*x+1, implementation='ntl')
     405            sage: a == a               # indirect doctest
     406            True
     407            sage: a != a
     408            False
    397409        """
    398410        cdef NumberFieldElement_ntl _right = right
    399         return not (ZZX_equal(left.__numerator, _right.__numerator) and ZZ_equal(left.__denominator, _right.__denominator))
     411        return (ZZX_equal(left.__numerator, _right.__numerator) and
     412                ZZ_equal(left.__denominator, _right.__denominator))
    400413
    401414    cdef void _randomize(self, num_bound, den_bound, distribution):
    402415        cdef int i
     
    734747        to this number field element.
    735748       
    736749        OUTPUT:
    737 
    738         - a list whose length corresponding to the degree of this
    739           element written in terms of a generator.
     750            - a list whose length corresponding to the degree of this
     751              element written in terms of a generator.
    740752       
    741753        EXAMPLES::
    742754       
    743             sage: ??
    744            
     755            sage: K.<a> = NumberField(x^7-2/3*x+1, implementation='ntl'); (a^3 - 7/8*a + 5)._coefficients()
     756            [5, -7/8, 0, 1]
     757            sage: (a^5 - 7/8*a + 5)._coefficients()
     758            [5, -7/8, 0, 0, 0, 1]
    745759        """
    746760        coeffs = []
    747761        cdef Integer den = (<IntegerRing_class>ZZ)._coerce_ZZ(&self.__denominator)
     
    784798        """
    785799        EXAMPLES::
    786800
    787              sage: ?
     801            sage: K.<a> = NumberField(x^7-2*x+5, implementation='ntl')
     802            sage: (a+7/3).is_rational()
     803            False
     804            sage: K(7/3).is_rational()
     805            True
    788806        """
    789807        return ZZX_deg(self.__numerator) == 0
    790808
     
    794812        """
    795813        EXAMPLES::
    796814       
    797             sage: ?
     815            sage: K.<a> = NumberField(x^7-2*x+5, implementation='ntl'); (a+5).is_absolute()
     816            True
    798817        """
    799818        return True
    800819
     
    810829        """
    811830        EXAMPLES::
    812831       
    813             sage: ?
     832            sage: K.<a,b> = NumberField([x^7-2*x+5,x^2+5], implementation='ntl'); (a+b).is_absolute()
     833            False
    814834        """
    815835        return False
    816836   
     
    845865        """
    846866        EXAMPLES::
    847867       
    848             sage: ?
     868            sage: b = NumberField(x^7-2*x+5, names='alpha', implementation='ntl').maximal_order().1; b
     869            alpha
     870            sage: b.is_order_element()
     871            True
     872            sage: (1/b).is_order_element()
     873            False
    849874        """
    850875        return True
    851876
     
    880905        """
    881906        EXAMPLES::
    882907
    883             sage: ?
     908            sage: b = NumberField(x^7-2*x+5, names='alpha', implementation='ntl').maximal_order().1; b
     909            alpha
     910            sage: b.number_field()
     911            Number Field in alpha with defining polynomial x^7 - 2*x + 5
    884912        """
    885913        return self._number_field
    886914
     
    969997        sage: c = O.1; c
    970998        (-2*b^2 - 2)*a - 2*b^2 - b
    971999        sage: type(c)
    972         <type 'sage.rings.number_field.number_field_element.OrderElement_relative'>
     1000        <type 'sage.rings.number_field.number_field_element_ntl.OrderElement_ntl_relative'>
    9731001    """
    9741002    cpdef bint is_order_element(self):
    9751003        """
    9761004        EXAMPLES::
    9771005       
    978             sage: ?
     1006            sage: b = NumberField([x^2-2*x+5,x^3+17], names='alpha', implementation='ntl').maximal_order().1; b
     1007            (1/3*alpha1^2 - 50/3*alpha1 + 1451/6)*alpha0 - 211/3*alpha1^2 - 1/3*alpha1 - 3131/6
     1008            sage: b.is_order_element()
     1009            True
    9791010        """
    9801011        return True
    9811012
    9821013    def __init__(self, order, f):
     1014        """
     1015        EXAMPLES::
     1016
     1017            sage: b = NumberField([x^2+1, x^2+3], names='alpha', implementation='ntl').maximal_order().1; type(b)
     1018            <type 'sage.rings.number_field.number_field_element_ntl.OrderElement_ntl_relative'>
     1019        """
    9831020        K = order.number_field()
    9841021        NumberFieldElement_ntl_relative.__init__(self, K, f)
    9851022        (<Element>self)._parent = order
  • sage/rings/number_field/number_field_element_quadratic.pyx

    diff -r e122468eb71e -r e2e7a6415ab6 sage/rings/number_field/number_field_element_quadratic.pyx
    a b  
    4242
    4343import number_field
    4444
    45 # TODO: this doesn't belong here, but robert thinks it would be nice
    46 # to have globally available....
    47 #
    48 # cdef mpz_to_str(mpz_t z):
    49 #     cdef Integer zz = PY_NEW(Integer)
    50 #     mpz_set(zz.value, z)
    51 #     return str(zz)
    52 
    5345
    5446def __make_NumberFieldElement_quadratic0(parent, a, b, denom):
    5547    """
     
    870862        """
    871863        return hash(self.polynomial())
    872864
    873     def __richcmp__(left, right, int op):
     865    def __richcmp__(left, right, int op):  # must be here since we implement __hash__.
    874866        return (<Element>left)._richcmp(right, op)
    875 
    876     cdef int _cmp_c_impl(self, Element _right) except -2:
     867   
     868    cdef bint cmp_eq(left, right):
    877869        """
    878870        EXAMPLES:
    879871            sage: K.<a> = NumberField(x^2+163)
    880             sage: K(1/2)==1/2
     872            sage: K(1/2)==1/2      # indirect doctest
    881873            True
    882874            sage: a == 1/2
    883875            False
    884876            sage: 2+a == a+2
    885877            True
    886878        """
    887         cdef NumberFieldElement_quadratic right = _right
    888         return not mpz_cmp(self.a, right.a)==0  \
    889             or not mpz_cmp(self.b, right.b)==0  \
    890             or not mpz_cmp(self.denom, right.denom) == 0
     879        cdef NumberFieldElement_quadratic _right = right
     880        return (mpz_cmp(left.a, _right.a) == 0  and mpz_cmp(left.b, _right.b) == 0  and
     881                mpz_cmp(left.denom, _right.denom) == 0)
    891882
    892883
    893884    def __nonzero__(self):
     
    11441135       
    11451136    cpdef bint is_rational(self):
    11461137        """
     1138        Return True if self lies in the rational numbers.
     1139       
    11471140        EXAMPLES::
    11481141
    1149             sage: ?
     1142            sage: K.<a> = NumberField(x^2+x+41); a.is_rational()
     1143            False
     1144            sage: K(2/3).is_rational()
     1145            True
    11501146        """
    11511147        return mpz_cmp_ui(self.b, 0) == 0
    11521148
     
    13351331        """
    13361332        EXAMPLES::
    13371333       
    1338             sage: ?
     1334            sage: OK.<y> = EquationOrder(x^2 + 5); y.is_order_element()
     1335            True
     1336            sage: (1/y).is_order_element()
     1337            False
    13391338        """
    13401339        return True
    13411340   
     
    14321431        """
    14331432        EXAMPLES::
    14341433
    1435             sage: ?
     1434            sage: OK.<y> = EquationOrder(x^2 + 5); type(y)
     1435            <type 'sage.rings.number_field.number_field_element_quadratic.OrderElement_quadratic'>
     1436            sage: y.number_field()
     1437            Number Field in y with defining polynomial x^2 + 5
    14361438        """
    14371439        # So few functions actually use self.number_field() for quadratic elements, so
    14381440        # it is better *not* to return a cached value (since the call to _parent.number_field())
  • sage/rings/number_field/number_field_ideal_rel.py

    diff -r e122468eb71e -r e2e7a6415ab6 sage/rings/number_field/number_field_ideal_rel.py
    a b  
    392392
    393393        The choice of embedding of quadratic field into quartic field matters::
    394394
    395             sage: rho, tau = K0.embeddings(K)
     395            sage: tau, rho = K0.embeddings(K)
    396396            sage: L1 = K.relativize(rho, 'b')
    397397            sage: L2 = K.relativize(tau, 'b')
    398398            sage: L1_into_K, K_into_L1 = L1.structure()
  • sage/rings/number_field/todo.txt

    diff -r e122468eb71e -r e2e7a6415ab6 sage/rings/number_field/todo.txt
    a b  
    1818   [x] delete redundant order functions...
    1919   [x] move is_OrderElement
    2020   [x] test
     21   [x] fill in all the sage: doctests, so all tests pass again
    2122
    22    [ ] fill in all the sage: doctests, so all tests pass again
     23   [ ] doctest existing code -- make ntl file use implementation='ntl' everywhere.
     24
     25   [ ] generic elements
    2326   [ ] libsingular elements
    2427   [ ] flint elements
    2528
     29   [ ] make default implementation computation be centralized?
     30
    2631   [ ] fix order.py -- it explicitly references ntl but must not.
    2732   [ ] rewrite order elements to use implementation, e.g., this line in order.py:
    2833       #TODO: fix this
  • sage/rings/number_field/totallyreal_rel.py

    diff -r e122468eb71e -r e2e7a6415ab6 sage/rings/number_field/totallyreal_rel.py
    a b  
    6363
    6464        sage: v = sage.rings.number_field.totallyreal_rel.integral_elements_in_box(K, C)
    6565        sage: sorted(v)
    66         [-alpha + 3, -alpha + 2, 5, 4, 3, 2, 1, 0, alpha + 6, alpha + 5, alpha + 4, alpha + 3, alpha + 2, 2*alpha + 7, 2*alpha + 6, 2*alpha + 5, 2*alpha + 4, 2*alpha + 3, 3*alpha + 5]
     66        [0, 1, 2, 3, 4, 5, -alpha + 2, -alpha + 3, alpha + 2, alpha + 3, alpha + 4, alpha + 5, alpha + 6, 2*alpha + 3, 2*alpha + 4, 2*alpha + 5, 2*alpha + 6, 2*alpha + 7, 3*alpha + 5]
    6767
    6868
    6969    A cubic field::
     
    7777    Note that the output is platform dependent (sometimes a 5 is listed below, and
    7878    sometimes it isn't)::
    7979   
    80         sage: sorted(v) 
    81         [-1/2*a + 2, 1/4*a^2 + 1/2*a, 0, 1, 2, 3, 4,...-1/4*a^2 - 1/2*a + 5, 1/2*a + 3, -1/4*a^2 + 5]
     80        sage: sorted(v)
     81        [0, 1, 2, 3, 4, 5, -1/2*a + 2, 1/2*a + 3, -1/4*a^2 - 1/2*a + 5, -1/4*a^2 + 5, 1/4*a^2 + 1/2*a]
    8282       
    8383    """
    8484    d = K.degree()