Ticket #9541: trac_9541-part3.patch

File trac_9541-part3.patch, 62.6 KB (added by was, 7 years ago)
  • module_list.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1279665508 -7200
    # Node ID d582c0c29177f956734dc132ce6f746e7825a8e1
    # Parent  e2e7a6415ab660aff68af20125275dd25cb95e5a
    cython arithmetic implementation object; doctesting generatic elts
    
    diff -r e2e7a6415ab6 -r d582c0c29177 module_list.py
    a b  
    12011201    Extension('sage.rings.number_field.number_field_base',
    12021202              sources = ['sage/rings/number_field/number_field_base.pyx']),
    12031203
     1204    Extension('sage.rings.number_field.implementation',
     1205              sources = ['sage/rings/number_field/implementation.pyx']),
     1206
     1207    Extension('sage.rings.number_field.order_base',
     1208              sources = ['sage/rings/number_field/order_base.pyx']),
     1209
    12041210    Extension('sage.rings.number_field.number_field_element',
    12051211              sources = ['sage/rings/number_field/number_field_element.pyx']),
    12061212
  • new file sage/rings/number_field/implementation.pxd

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/implementation.pxd
    - +  
     1cdef class ArithmeticImplementation:
     2    cdef object _name
     3
     4cdef class ArithmeticImplementation_generic(ArithmeticImplementation):
     5    cdef object modulus
     6   
     7cdef class ArithmeticImplementation_ntl(ArithmeticImplementation):
     8    pass
     9
     10cdef class ArithmeticImplementation_flint(ArithmeticImplementation):
     11    pass
     12
     13cdef class ArithmeticImplementation_singular(ArithmeticImplementation):
     14    pass
     15
     16cdef class ArithmeticImplementation_quadratic(ArithmeticImplementation):
     17    pass
  • new file sage/rings/number_field/implementation.pyx

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/implementation.pyx
    - +  
     1"""
     2Arithmetic Implementations
     3
     4A cython class that stores extra C-level data necessary for doing
     5arithmetic with a given implementation ('ntl', 'singular', etc.).
     6This is here mainly because number fields are a Python class, not a
     7Cython class.
     8"""
     9
     10##############################################################################
     11#       Copyright (C) 2004, 2005, 2006, 2007 William Stein <wstein@gmail.com>
     12#
     13#  Distributed under the terms of the GNU General Public License (GPL)
     14#
     15#    This code is distributed in the hope that it will be useful,
     16#    but WITHOUT ANY WARRANTY; without even the implied warranty of
     17#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     18#    General Public License for more details.
     19#
     20#  The full text of the GPL is available at:
     21#
     22#                  http://www.gnu.org/licenses/
     23##############################################################################
     24
     25
     26cdef class ArithmeticImplementation:
     27    def __init__(self, name):
     28        self._name = name
     29
     30    def name(self):
     31        return self._name
     32
     33    def __repr__(self):
     34        return "Arithmetic implementation '%s'"%self._name
     35
     36    def __cmp__(self, right):
     37        return cmp(self.name(), right.name())
     38
     39cdef class ArithmeticImplementation_generic(ArithmeticImplementation):
     40    def __init__(self, modulus):
     41        self.modulus = modulus
     42        ArithmeticImplementation.__init__(self, 'generic')
     43
     44cdef class ArithmeticImplementation_ntl(ArithmeticImplementation):
     45    def __init__(self):
     46        ArithmeticImplementation.__init__(self, 'ntl')
     47
     48cdef class ArithmeticImplementation_flint(ArithmeticImplementation):
     49    def __init__(self):
     50        ArithmeticImplementation.__init__(self, 'flint')
     51
     52cdef class ArithmeticImplementation_singular(ArithmeticImplementation):
     53    def __init__(self):
     54        ArithmeticImplementation.__init__(self, 'singular')
     55
     56cdef class ArithmeticImplementation_quadratic(ArithmeticImplementation):
     57    def __init__(self):
     58        ArithmeticImplementation.__init__(self, 'quadratic')
  • sage/rings/number_field/number_field.py

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field.py
    a b  
    7272    27*beta0
    7373"""
    7474
    75 #*****************************************************************************
     75##############################################################################
    7676#       Copyright (C) 2004, 2005, 2006, 2007 William Stein <wstein@gmail.com>
    7777#
    7878#  Distributed under the terms of the GNU General Public License (GPL)
     
    8585#  The full text of the GPL is available at:
    8686#
    8787#                  http://www.gnu.org/licenses/
    88 #*****************************************************************************
     88##############################################################################
    8989
    9090from __future__ import with_statement
    9191from sage.structure.parent_gens import localvars
     
    123123
    124124import sage.structure.parent_gens
    125125
     126import implementation as arithmetic_implementation
     127
    126128from sage.structure.proof.proof import get_flag
    127129import maps
    128130import number_field_morphisms
     
    258260    irreducible and over `\QQ`.
    259261   
    260262    INPUT:
    261 
    262263        - ``polynomial`` - a polynomial over `\QQ` or a number field,
    263264          or a list of polynomials.
    264265        - ``name`` - a string (default: 'a'), the name of the generator
     
    416417    Using the implementation option::
    417418
    418419        sage: K.<a> = NumberField(x^3 + 17); type(a)
    419         <type 'sage.rings.number_field.number_field_element.NumberFieldElement_absolute'>
     420        <type 'sage.rings.number_field.number_field_element_ntl.NumberFieldElement_ntl_absolute'>
    420421        sage: K.<a> = NumberField(x^3 + 17, implementation='ntl'); type(a)
    421422        <type 'sage.rings.number_field.number_field_element_ntl.NumberFieldElement_ntl_absolute'>           
    422423        sage: K.<a> = NumberField(x^3 + 17, implementation='flint'); type(a)
     
    459460        name = names
    460461
    461462    if isinstance(polynomial, (list, tuple)):
    462         return NumberFieldTower(polynomial, name)
     463        return NumberFieldTower(polynomial, name, implementation=implementation)
    463464       
    464465    name = sage.structure.parent_gens.normalize_names(1, name)
    465466
     
    469470        except (AttributeError, TypeError):
    470471            raise TypeError, "polynomial (=%s) must be a polynomial."%repr(polynomial)
    471472
    472     # convert ZZ to QQ
    473473    R = polynomial.base_ring()
    474     Q = polynomial.parent().base_extend(R.fraction_field())
    475     polynomial = Q(polynomial)
     474    polynomial = polynomial.change_ring(R.fraction_field())
    476475
    477476    if implementation == 'default':
    478         if polynomial.degree() == 2:
     477        if polynomial.degree() == 2 and polynomial.base_ring() == QQ:
    479478            implementation = 'quadratic'
    480479        else:
    481480            implementation = 'ntl'
     
    489488            if not K is None: return K
    490489
    491490    if isinstance(R, NumberField_generic):
    492         S = R.extension(polynomial, name, check=check)
     491        S = R.extension(polynomial, name, check=check, implementation=implementation)
    493492        if cache:
    494493            _nf_cache[key] = weakref.ref(S)
    495494        return S
     
    33143313            sage: L.<b> = NumberField(z^3 + 3 + a); L
    33153314            Number Field in b with defining polynomial z^3 + a0 + 3 over its base field
    33163315        """
     3316        if implementation == 'default':
     3317            implementation = self.implementation().name()
     3318            if implementation == 'quadratic':
     3319                # extension of quadratic must be represented by ntl
     3320                implementation = 'ntl'
    33173321        if not isinstance(poly, polynomial_element.Polynomial):
    33183322            try:
    33193323                poly = poly.polynomial(self)
     
    36833687        self.integral_basis().
    36843688       
    36853689        INPUT:
    3686        
    3687        
    3688         -  ``v`` - None, a prime, or a list of primes. See the
    3689            documentation for self.maximal_order.
    3690        
     3690            - ``v`` - None, a prime, or a list of primes. See the
     3691              documentation for self.maximal_order.
    36913692       
    36923693        EXAMPLES::
    36933694       
     
    37063707            [1, a, 1/2*a^2 + 1/2*a]
    37073708            sage: K.integral_basis()
    37083709            [1, 1/2*a^2 + 1/2*a, a^2]
     3710
     3711        There are currently limitations::
     3712       
     3713            sage: K.<a> = NumberField(x^3 -x + 2/3)
     3714            sage: K.maximal_order()
     3715            Traceback (most recent call last):
     3716            ...
     3717            NotImplementedError: computation of maximal order for nonmonic nonintegral polynomials not implemented
    37093718        """
    37103719        v = self._normalize_prime_list(v)
    37113720        try:
    37123721            return self._integral_basis_dict[v]
    37133722        except:
     3723            h = self.absolute_polynomial()
     3724            if not h.is_monic() or h.denominator() != 1:
     3725                raise NotImplementedError, "computation of maximal order for nonmonic nonintegral polynomials not implemented"
     3726           
    37143727            f = self.pari_polynomial()
    37153728
    37163729            if len(v) == 0:
     
    47504763        NumberField_generic.__init__(self, polynomial, name, latex_name, check, embedding)
    47514764        if implementation == 'default':
    47524765            implementation = 'ntl'
    4753         if implementation == 'flint':
     4766
     4767        if implementation == 'quadratic':
     4768            self._element_class = number_field_element_quadratic.NumberFieldElement_quadratic
     4769            self._order_element_class = number_field_element_quadratic.OrderElement_quadratic
     4770            self._set_implementation(arithmetic_implementation.ArithmeticImplementation_quadratic())
     4771        elif implementation == 'flint':
    47544772            import number_field_element_flint
    47554773            self._element_class = number_field_element_flint.NumberFieldElement_flint
     4774            self._order_element_class = number_field_element_flint.OrderElement_flint
     4775            self._set_implementation(arithmetic_implementation.ArithmeticImplementation_flint())
     4776
    47564777        elif implementation == 'ntl':
    47574778            import number_field_element_ntl
    47584779            self._element_class = number_field_element_ntl.NumberFieldElement_ntl_absolute
     4780            self._order_element_class = number_field_element_ntl.OrderElement_ntl_absolute
     4781            self._set_implementation(arithmetic_implementation.ArithmeticImplementation_ntl())
     4782           
    47594783        elif implementation == 'generic':
    47604784            import number_field_element_generic
    47614785            self._element_class = number_field_element_generic.NumberFieldElement_generic
     4786            self._order_element_class = number_field_element_generic.OrderElement_generic
     4787            self._set_implementation(arithmetic_implementation.ArithmeticImplementation_generic(polynomial))
    47624788        else:
    47634789            raise ValueError, "unknown implementation '%s'"%implementation
    4764         self._zero_element = self(0)
    4765         self._one_element =  self(1)
     4790       
     4791        assert self.implementation() is not None
     4792       
     4793        if not isinstance(self, NumberField_quadratic):
     4794            self._zero_element = self(0)
     4795            self._one_element =  self(1)
    47664796
    47674797    def _coerce_from_other_number_field(self, x):
    47684798        """
     
    48784908        Currently integers, rationals, and this field itself coerce
    48794909        canonically into this field.
    48804910
    4881         EXAMPLES:
    4882             sage: S.<y> = NumberField(x^3 + x + 1)
     4911        EXAMPLES::
     4912       
     4913            sage: S.<y> = NumberField(x^3 + x + 1, implementation='ntl')
    48834914            sage: S.coerce(int(4))
    48844915            4
    48854916            sage: S.coerce(long(7))
     
    48874918            sage: S.coerce(-Integer(2))
    48884919            -2
    48894920            sage: z = S.coerce(-7/8); z, type(z)
    4890             (-7/8, <type 'sage.rings.number_field.number_field_element.NumberFieldElement_absolute'>)
     4921            (-7/8, <type 'sage.rings.number_field.number_field_element_ntl.NumberFieldElement_ntl_absolute'>)
    48914922            sage: S.coerce(y) is y
    48924923            True
    48934924
     
    63646395            sage: type(CyclotomicField(6).one_element())
    63656396            <type 'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic'>
    63666397            sage: type(CyclotomicField(15).zero_element())
    6367             <type 'sage.rings.number_field.number_field_element.NumberFieldElement_absolute'>
     6398            <type 'sage.rings.number_field.number_field_element_ntl.NumberFieldElement_ntl_absolute'>
    63686399        """
    63696400        f = QQ['x'].cyclotomic_polynomial(n)
    63706401        if names[0].startswith('zeta'):
     
    73677398            sage: K.<a> = NumberField(x^2 + 17, implementation='ntl'); type(a)
    73687399            <type 'sage.rings.number_field.number_field_element_ntl.NumberFieldElement_ntl_absolute'>
    73697400        """
    7370         NumberField_absolute.__init__(self, polynomial, name=name, check=check, embedding=embedding, latex_name=latex_name)
    7371         if implementation == 'quadratic':
    7372             self._element_class = number_field_element_quadratic.NumberFieldElement_quadratic
    7373         elif implementation == 'flint':
    7374             import number_field_element_flint
    7375             self._element_class = number_field_element_flint.NumberFieldElement_flint
    7376         elif implementation == 'ntl':
    7377             import number_field_element_ntl           
    7378             self._element_class = number_field_element_ntl.NumberFieldElement_ntl_absolute
    7379         elif implementation == 'generic':
    7380             import number_field_element_generic
    7381             self._element_class = number_field_element_generic.NumberFieldElement_generic
    7382         else:
    7383             raise ValueError, "unknown implementation '%s'"%implementation
    7384         c, b, a = [rational.Rational(t) for t in self.defining_polynomial().list()]
     7401        NumberField_absolute.__init__(self, polynomial, name=name, check=check, embedding=embedding,
     7402                                      latex_name=latex_name, implementation=implementation)
     7403       
     7404        c, b, a = [rational.Rational(t) for t in polynomial.list()]
    73857405        # set the generator
    73867406        Dpoly = b*b - 4*a*c
    73877407        D = (Dpoly.numer() * Dpoly.denom()).squarefree_part(bound=10000)
    73887408        self._D = D
    7389         parts = -b/(2*a), (Dpoly/D).sqrt()/(2*a)
    7390         self._NumberField_generic__gen = self._element_class(self, parts)
    7391 
    7392         # NumberField_absolute.__init__(...) set _zero_element and
     7409        if implementation == 'quadratic':
     7410            parts = -b/(2*a), (Dpoly/D).sqrt()/(2*a)
     7411            self._NumberField_generic__gen = self._element_class(self, parts)           
     7412
     7413        # NumberField_absolute.__init__(...) could set _zero_element and
    73937414        # _one_element to NumberFieldElement_absolute values, which is
    73947415        # wrong (and dangerous; such elements can actually be used to
    73957416        # crash Sage: see #5316).  Overwrite them with correct values.
  • sage/rings/number_field/number_field_base.pxd

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field_base.pxd
    a b  
    11from sage.rings.ring cimport Field
     2from implementation cimport ArithmeticImplementation
    23
    34cdef class NumberField(Field):
    4     pass
     5    cdef ArithmeticImplementation _implementation
     6    cpdef ArithmeticImplementation implementation(self)
     7    cpdef _set_implementation(self, ArithmeticImplementation implementation)
  • sage/rings/number_field/number_field_base.pyx

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field_base.pyx
    a b  
    3131from sage.rings.ring cimport Field
    3232
    3333cdef class NumberField(Field):
     34    cpdef ArithmeticImplementation implementation(self):
     35        """
     36        EXAMPLES::
     37       
     38            sage: NumberField(x^2+1,'c').implementation()
     39            Arithmetic implementation 'quadratic'
     40            sage: NumberField(x^3+x+1,'c', implementation='ntl').implementation()
     41            Arithmetic implementation 'ntl'
     42            sage: NumberField(x^3+x+1,'c', implementation='generic').implementation()
     43            Arithmetic implementation 'generic'
     44            sage: NumberField(x^3+x+1,'c', implementation='flint').implementation()
     45            Arithmetic implementation 'flint'
     46        """
     47        return self._implementation
     48
     49    cpdef _set_implementation(self, ArithmeticImplementation implementation):
     50        """
     51        Do not call this.  It is for internal use only, during object construction.
     52
     53        EXAMPLES::
     54       
     55            sage: K = NumberField(x^3+x+1,'c', implementation='ntl')
     56            sage: K._set_implementation(sage.rings.number_field.implementation.ArithmeticImplementation_singular())
     57            sage: K.implementation()
     58            Arithmetic implementation 'singular'
     59        """
     60        self._implementation = implementation
    3461
    3562    def ring_of_integers(self, *args, **kwds):
    3663        r"""
  • sage/rings/number_field/number_field_element.pxd

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field_element.pxd
    a b  
    88from sage.structure.parent_base cimport ParentWithBase
    99
    1010cdef class NumberFieldElement(FieldElement):
    11     cdef _new(self)
    1211    cpdef number_field(self)
    1312    cpdef bint is_rational(self)
    1413    cpdef bint is_absolute(self)
     
    1918    cdef object __matrix
    2019    cdef object __symbolic
    2120    cdef bint cmp_eq(left, right)
    22    
    23    
    24    
  • sage/rings/number_field/number_field_element.pyx

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field_element.pyx
    a b  
    175175        sage: a^3
    176176        -a - 1
    177177    """
    178     cdef _new(self):
    179         raise NotImplementedError
    180    
    181178    cpdef bint is_absolute(self):
    182179        raise NotImplementedError  # exception will be ignored due to return type, so this can't be doctested.
    183180
     
    192189        return False
    193190   
    194191    cpdef number_field(self):
     192        """
     193        EXAMPLES::
     194
     195            sage: K.<a> = NumberField(x^3 -x + 2, implementation='generic'); b = K.maximal_order().1
     196            sage: a.number_field()
     197            Number Field in a with defining polynomial x^3 - x + 2
     198            sage: b.number_field()
     199            Number Field in a with defining polynomial x^3 - x + 2
     200            sage: b.number_field() is K
     201            True
     202        """
    195203        if self.is_order_element():
    196204            return self._parent.number_field()
    197205        else:
     
    234242            -zeta33^17
    235243       
    236244        AUTHORS:
    237 
    238         - Joel B. Mohler
    239 
    240         - Craig Citro (fixed behavior for different representation of
    241           quadratic field elements)
     245           - Joel B. Mohler
     246           - Craig Citro (fixed behavior for different representation of
     247             quadratic field elements)
    242248        """
    243249        # TODO -- rewrite; see function in number_field_element_ntl.pyx...
    244250        raise NotImplementedError
     
    285291            R = K.base_field()[K.variable_name()]
    286292            return repr(R(self.list()))
    287293
    288            
    289    
    290 
    291294    def _im_gens_(self, codomain, im_gens):
    292295        """
    293296        This is used in computing homomorphisms between number fields.
     
    666669            sage: a._random_element()
    667670            -2*a - 1
    668671        """
    669         cdef NumberFieldElement elt = self._new()
     672        cdef NumberFieldElement elt = self.parent()(0)
    670673        elt._randomize(num_bound, den_bound, distribution)
    671674        return elt
    672675
     
    12321235            False
    12331236        """
    12341237        if self.is_order_element():
    1235             return self.number_field()(self).__invert__()
     1238             return self.number_field()(self).__invert__()
    12361239        raise NotImplementedError
    12371240
    12381241    def _integer_(self, Z=None):
     
    14841487        else:
    14851488            raise NotImplementedError, "complex conjugation is not implemented (or doesn't make sense)."
    14861489
    1487     def polynomial(self, var='x'):
     1490    def polynomial(self, var=None):
    14881491        """
    14891492        Return the underlying polynomial corresponding to this number field
    14901493        element.
     
    15071510            sage: g is f.polynomial()
    15081511            False
    15091512        """
     1513        if var is None: var = 'x'
    15101514        return QQ[var](self._coefficients())
    15111515
    15121516    def __hash__(self):
     
    15181522
    15191523    def _coefficients(self):
    15201524        """
    1521         Return the coefficients of the underlying polynomial corresponding
    1522         to this number field element.
     1525        Return the list of all coefficients of the underlying
     1526        polynomial corresponding to this number field element.
    15231527       
    15241528        OUTPUT:
    15251529
     
    15281532       
    15291533        EXAMPLES:
    15301534        """
    1531         return self.polynomial().coefficients()
     1535        return self.polynomial().list()
    15321536       
    15331537    def denominator(self):
    15341538        """
  • sage/rings/number_field/number_field_element_flint.pxd

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field_element_flint.pxd
    a b  
    3030    cdef fmpq_poly_t _f
    3131    cdef fmpq_poly_t _m
    3232
     33    cdef _new(self)
     34   
     35cdef class OrderElement_flint(NumberFieldElement_flint):
     36    pass
  • sage/rings/number_field/number_field_element_flint.pyx

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field_element_flint.pyx
    a b  
     1"""
     2FLINT-based implementation of number field elements
     3"""
    14##############################################################################
    2 #       Copyright (C) 2004, 2007, 2010 William Stein <wstein@gmail.com>
     5#       Copyright (C) 2010 William Stein <wstein@gmail.com>
     6#       Copyright (C) 2010 Sebastian Pancratz
    37#
    48#  Distributed under the terms of the GNU General Public License (GPL)
    59#
     
    1721    fmpq_poly_power(f, g, n)
    1822
    1923cdef class NumberFieldElement_flint(NumberFieldElement):
     24    """
     25    EXAMPLES::
     26    """
     27    cpdef bint is_absolute(self):
     28        return True
    2029   
    2130    def __init__(self, parent, f): # FIXME
     31        """
     32        EXAMPLES::
     33        """
    2234        self._parent = parent
    2335        g = parent.defining_polynomial()
    2436        f = g.parent()(f)
     
    4456        fmpq_poly_clear(self._m) # FIXME
    4557
    4658    cdef void _randomize(self, num_bound, den_bound, distribution):
     59        """
     60        EXAMPLES::
     61        """
    4762        # TODO
    4863        pass
    4964
    5065    def __getitem__(self, long n):
     66        """
     67        EXAMPLES::
     68        """
    5169        cdef Rational res = PY_NEW(Rational)
    5270        if 0 <= n and n < fmpq_poly_length(self._f):
    5371            fmpq_poly_get_coeff_mpq(res.value, self._f, n)
    5472        return res
    5573
    5674    def __getslice__(self, long i, long j):
     75        """
     76        EXAMPLES::
     77        """
    5778        cdef NumberFieldElement_flint res = self._new()
    5879        fmpq_poly_getslice(res._f, self._f, i, j)
    5980        return res
    6081
    61     def polynomial(self): # FIXME
    62         from sage.rings.rational_field import QQ
    63         from sage.rings.polynomial.polynomial_ring import polygen
    64         t = polygen(QQ)
    65         cdef unsigned long i
    66         cdef unsigned long n = fmpq_poly_length(self._f)
    67         res = self[0]
    68         for i in xrange(1, n):
    69             res = res + self[i] * t**i
    70         return res
     82    def polynomial(self, var=None): # FIXME
     83        """
     84        EXAMPLES::
     85        """
     86        cdef unsigned long i, n
     87        if var is None:
     88            from sage.rings.rational_field import QQ
     89            from sage.rings.polynomial.polynomial_ring import polygen
     90            t = polygen(QQ)
     91            n = fmpq_poly_length(self._f)
     92            res = self[0]
     93            for i in xrange(1, n):
     94                res = res + self[i] * t**i
     95            return res
     96        else:
     97            return self.polynomial().change_variable(var)
    7198
    7299    cpdef RingElement _mul_(left, RingElement right):
    73         pass
     100        """
     101        EXAMPLES::
     102        """
    74103        cdef NumberFieldElement_flint res = left._new()
    75104        fmpq_poly_mul(res._f, left._f, (<NumberFieldElement_flint> right)._f)
    76105        fmpq_poly_mod(res._f, res._f, left._m)
    77106        return res
    78107
    79108    cpdef ModuleElement _add_(left, ModuleElement right):
     109        """
     110        EXAMPLES::
     111        """
    80112        cdef NumberFieldElement_flint res = left._new()
    81113        fmpq_poly_add(res._f, left._f, (<NumberFieldElement_flint> right)._f)
    82114        return res
    83115
    84116    cpdef ModuleElement _sub_(left, ModuleElement right):
     117        """
     118        EXAMPLES::
     119        """
    85120        cdef NumberFieldElement_flint res = left._new()
    86121        fmpq_poly_sub(res._f, left._f, (<NumberFieldElement_flint> right)._f)
    87122        return res
    88123
    89124    cpdef RingElement _div_(left, RingElement right):
     125        """
     126        EXAMPLES::
     127        """
    90128        return left * (~right) # FIXME
    91129
    92130    def __invert__(self):
     131        """
     132        EXAMPLES::
     133        """
    93134        cdef NumberFieldElement_flint u
    94135        cdef fmpq_poly_t d, v
    95136        if fmpq_poly_is_zero(self._f):
     
    104145        return u
    105146
    106147    def __richcmp__(left, right, int op):
     148        """
     149        EXAMPLES::
     150        """
    107151        return (<Element>left)._richcmp(right, op)
    108152
    109153    cdef int _cmp_c_impl(left, Element right) except -2:
     154        """
     155        EXAMPLES::
     156        """
    110157        return fmpq_poly_cmp(left._f, (<NumberFieldElement_flint> right)._f)
    111158
    112159    def _integer_(self, Z=None):
     160        """
     161        EXAMPLES::
     162        """
    113163        return Z(self)
    114164
    115165    def _rational_(self):
     166        """
     167        EXAMPLES::
     168        """
    116169        cdef Rational res
    117170        cdef unsigned long len = fmpq_poly_length(self._f)
    118171        if len == 0:
     
    125178        else:
    126179            raise TypeError, "cannot coerce nonconstant polynomial"
    127180
     181cdef class OrderElement_flint(NumberFieldElement_flint):
     182    #cdef _new(self):
     183    #    raise NotImplementedError
     184    #cdef _new_nfelt(self):
     185    #    raise NotImplementedError
     186    def __init__(self, parent, f):
     187        raise NotImplementedError
     188    cpdef bint is_order_element(self):
     189        return True
     190    cpdef RingElement _mul_(left, RingElement right):
     191        raise NotImplementedError
     192    cpdef ModuleElement _add_(left, ModuleElement right):
     193        raise NotImplementedError       
     194    cpdef ModuleElement _sub_(left, ModuleElement right):
     195        raise NotImplementedError
     196    cpdef RingElement _div_(left, RingElement right):
     197        raise NotImplementedError       
     198    def __invert__(self):
     199        raise NotImplementedError
     200    def __richcmp__(left, right, int op):
     201        return (<Element>left)._richcmp(right, op)
     202    cdef int _cmp_c_impl(left, Element right) except -2:
     203        raise NotImplementedError
  • sage/rings/number_field/number_field_element_generic.pyx

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field_element_generic.pyx
    a b  
     1"""
     2Generic number field elements
     3
     4We implement generic number field elements and order elements
     5represented using a polynomial over the base field.
     6
     7If you're going to implement a new type of number field element, this
     8is a good file to copy.
     9"""
    110
    211
    312###############################################################################
    4 #       Copyright (C) 2004, 2007, 2010 William Stein <wstein@gmail.com>
     13#       Copyright (C) 2010 William Stein <wstein@gmail.com>
    514#
    615#  Distributed under the terms of the GNU General Public License (GPL)
    716#
     
    1928
    2029from sage.structure.element cimport RingElement, ModuleElement, Element
    2130
     31from number_field_base cimport NumberField
     32from order_base cimport Order_base
     33
    2234from number_field_element cimport NumberFieldElement
    2335
     36from implementation cimport ArithmeticImplementation_generic
     37
    2438cdef class NumberFieldElement_generic(NumberFieldElement):
    2539    cdef object _f
    26 
    2740    """
    2841    EXAMPLES::
    2942
     
    3144        a^2 - 3
    3245        sage: type(b)
    3346        <type 'sage.rings.number_field.number_field_element_generic.NumberFieldElement_generic'>
     47
     48    Arithmetic in a relative extension::
     49
     50        sage: K.<a> = NumberField(x^3+7, implementation='generic'); R.<t> = K[]; L.<b> = K.extension(t^3 + 3); L
     51        Number Field in b with defining polynomial t^3 + 3 over its base field       
    3452    """
    3553    cpdef bint is_absolute(self):
    3654        """
     
    5977            <type 'sage.rings.number_field.number_field_element_generic.NumberFieldElement_generic'>
    6078        """
    6179        self._parent = parent
    62         self._f = parent.polynomial_ring()(f)
     80        if isinstance(f, NumberFieldElement_generic):
     81            self._f = (<NumberFieldElement_generic>f)._f
     82        else:
     83            self._f = parent.polynomial_ring()(f)
    6384
    64     cdef _new(self):
     85    cdef _new(self, f):
    6586        cdef NumberFieldElement_generic x = PY_NEW(NumberFieldElement_generic)
    6687        x._parent = self._parent
     88        x._f = f
    6789        return x
    6890
    6991    cdef void _randomize(self, num_bound, den_bound, distribution):
    70         # TODO
    71         pass
     92        raise NotImplementedError
    7293
    73     def polynomial(self):
    74         return self._f
     94    def polynomial(self, var=None):
     95        """
     96        Return polynomial representative for this number field element, in the variable x.
     97       
     98        EXAMPLES::
     99
     100            sage: R.<Y> = QQ[]
     101            sage: K.<a> = NumberField(Y^3 -7*Y + 2/3, implementation='generic'); (a^2 - a + 2/3).polynomial()
     102            Y^2 - Y + 2/3
     103        """
     104        if var is None: return self._f
     105        return self._f.change_variable_name(var)
     106
     107    cdef modulus(self):
     108        """
     109        Return the modulus of the parent number field.  This is used
     110        in basic arithmetic operations.
     111        """
     112        # We get the modulus with no Python calls -- it's all a Cython/C struct lookup.
     113        cdef NumberField P = self._parent
     114        cdef ArithmeticImplementation_generic I = P.implementation()
     115        return I.modulus
    75116
    76117    cpdef RingElement _mul_(left, RingElement right):
    77         g = (left._f *
    78                 (<NumberFieldElement_generic>right)._f) % left._parent.defining_polynomial()
    79         return NumberFieldElement_generic(left._parent, g)
     118        """
     119        EXAMPLES::
     120
     121            sage: K.<a> = NumberField(x^3 -x + 2/3, implementation='generic');  (a+17)*(a^2+5)
     122            17*a^2 + 6*a + 253/3
     123        """
     124        return left._new((left._f * (<NumberFieldElement_generic>right)._f) % left.modulus())
    80125
    81126    cpdef ModuleElement _add_(left, ModuleElement right):
    82         return NumberFieldElement_generic(left._parent,
    83               left._f + (<NumberFieldElement_generic>right)._f)
     127        """
     128        EXAMPLES::
     129       
     130            sage: K.<a> = NumberField(x^3 -x + 2/3, implementation='generic');  (a+17)+(a^2+5)
     131            a^2 + a + 22
     132        """
     133        return left._new(left._f + (<NumberFieldElement_generic>right)._f)
    84134
    85135    cpdef ModuleElement _sub_(left, ModuleElement right):
    86         return NumberFieldElement_generic(left._parent,
    87               left._f - (<NumberFieldElement_generic>right)._f)
     136        """
     137        EXAMPLES::
     138
     139            sage: K.<a> = NumberField(x^3 -x + 2/3, implementation='generic');  (a+17)-(a^2+5)
     140            -a^2 + a + 12
     141        """
     142        return left._new(left._f - (<NumberFieldElement_generic>right)._f)
    88143
    89144    cpdef RingElement _div_(left, RingElement right):
     145        """
     146        EXAMPLES::
     147
     148            sage: K.<a> = NumberField(x^3 -x + 2/3, implementation='generic')
     149            sage: (a+17)/(a^2+5)
     150            -33/58*a^2 + 3/29*a + 99/29
     151            sage: a/a
     152            1
     153            sage: c = 1/(a^2+1); c
     154            -9/20*a^2 - 3/20*a + 9/10
     155            sage: c*(a^2+1)
     156            1
     157        """
    90158        if not (<NumberFieldElement_generic>right)._f: raise ZeroDivisionError
    91         h = left._parent.defining_polynomial()
     159        if left is right: return left._parent.one_element()
     160        h = left.modulus()
    92161        one, a, b = (<NumberFieldElement_generic>right)._f.xgcd(h)
    93         return NumberFieldElement_generic(left._parent, (left._f * a) % h)
     162        return left._new((left._f * a) % h)
    94163
    95164    def __invert__(self):
    96         h = self._parent.defining_polynomial()
     165        """
     166        EXAMPLES::
     167       
     168            sage: K.<a> = NumberField(x^3 -x + 2/3, implementation='generic')
     169            sage: ~a
     170            -3/2*a^2 + 3/2
     171        """
     172        h = self.modulus()
    97173        one, a, b = self._f.xgcd(h)
    98         return NumberFieldElement_generic(self._parent, a % h)
     174        return self._new(a % h)
    99175
    100176    def __richcmp__(left, right, int op):
     177        """
     178        EXAMPLES::
     179
     180            sage: K.<a> = NumberField(x^3 -x + 2/3, implementation='generic')
     181            sage: f = a^2 - 2; g = a^2 + 3*a - 2
     182            sage: f < g
     183            True
     184            sage: g > f
     185            True
     186            sage: f == g
     187            False
     188            sage: f != g
     189            True
     190            sage: cmp(f,g)
     191            -1
     192        """
    101193        return (<Element>left)._richcmp(right, op)
    102194
    103195    cdef int _cmp_c_impl(left, Element right) except -2:
    104196        cdef NumberFieldElement_generic _right = right
    105197        return cmp(left._f, (<NumberFieldElement_generic>right)._f)
    106198
     199    cdef bint cmp_eq(left, right):
     200        """
     201        EXAMPLES::
     202       
     203            sage: K.<a> = NumberField(x^3 -x + 2, implementation='generic'); b = K.maximal_order()(a^2-3); b
     204            a^2 - 3
     205            sage: b == b
     206            True
     207            sage: b == (b+1)
     208            False
     209            sage: a == a
     210            True
     211            sage: a == a+1
     212            False
     213        """
     214        return left._f == (<NumberFieldElement_generic>right)._f
     215
    107216    def _integer_(self, Z=None):
     217        """
     218        EXAMPLES::
     219
     220            sage: K.<a> = NumberField(x^3 -x + 2/3, implementation='generic');   ZZ(K(19))
     221            19
     222            sage: ZZ(a)
     223            Traceback (most recent call last):
     224            ...
     225            TypeError: cannot coerce nonconstant polynomial
     226        """
    108227        return Z(self._f)
    109228
    110229    def _rational_(self):
    111         from sage.rings.rational import QQ
     230        """
     231        EXAMPLES::
     232
     233            sage: K.<a> = NumberField(x^3 -x + 2/3, implementation='generic');   b = QQ(K(-19/5));  b
     234            -19/5
     235            sage: b.parent()
     236            Rational Field
     237            sage: QQ(a)
     238            Traceback (most recent call last):
     239            ...
     240            TypeError: not a constant polynomial
     241        """
     242        from sage.rings.rational_field import QQ
    112243        return QQ(self._f)
    113244
    114245   
    115246   
     247cdef class OrderElement_generic(NumberFieldElement_generic):
     248    """
     249    TESTS::
     250       
     251    Checking that the inverse has the right parent::
     252
     253            sage: K.<a> = NumberField(x^3 -x + 2, implementation='generic')
     254            sage: OK = K.ring_of_integers(); a = OK(K.0)
     255            sage: (~a).parent() is K
     256            True
     257            sage: (~a) in OK
     258            False
     259            sage: a^(-1) in OK
     260            False
     261    """
     262    cdef modulus(self):
     263        """
     264        Return the modulus of the ambient number field.  This is used
     265        in basic arithmetic operations.
     266        """
     267        cdef Order_base P = self._parent
     268        cdef ArithmeticImplementation_generic I = P.implementation()
     269        return I.modulus
     270
     271    cdef _new(self, f):
     272        """
     273        Return a new order element defined by f.
     274        """
     275        cdef OrderElement_generic x = PY_NEW(OrderElement_generic)
     276        x._parent = self._parent
     277        x._f = f
     278        return x
     279
     280    cdef _new_nfelt(self, f):
     281        """
     282        Return a new number field element defined by f.
     283        """
     284        cdef NumberFieldElement_generic x = NumberFieldElement_generic._new(self, f)
     285        x._parent = self._parent.number_field()
     286        return x
     287
     288    def __init__(self, parent, f):
     289        """
     290        INPUT:
     291            - ``parent`` -- number field
     292            - ``f`` -- element
     293           
     294        EXAMPLES::
     295
     296            sage: K.<abc> = NumberField(x^7-2/3*x+1, implementation='generic'); abc
     297            abc
     298            sage: type(abc)
     299            <type 'sage.rings.number_field.number_field_element_generic.NumberFieldElement_generic'>
     300        """
     301        self._parent = parent
     302        if isinstance(f, NumberFieldElement_generic):
     303            self._f = (<NumberFieldElement_generic>f)._f
     304        else:
     305            self._f = parent.number_field().polynomial_ring()(f)
     306
     307    cpdef bint is_order_element(self):
     308        """
     309        EXAMPLES::
     310
     311            sage: K.<a> = NumberField(x^3 -x + 2, implementation='generic')
     312            sage: a.is_order_element()
     313            False
     314            sage: K.maximal_order().0.is_order_element()
     315            True
     316        """
     317        return True
     318
     319    cpdef RingElement _mul_(left, RingElement right):
     320        """
     321        EXAMPLES::
     322
     323            sage: K.<a> = NumberField(x^3 -x + 2, implementation='generic'); b = K.maximal_order()(a^2-3); b
     324            a^2 - 3
     325            sage: c = b*b; c
     326            -5*a^2 - 2*a + 9
     327            sage: parent(c)
     328            Maximal Order in Number Field in a with defining polynomial x^3 - x + 2
     329        """
     330        return left._new((left._f * (<OrderElement_generic>right)._f) % left.modulus())
     331
     332    cpdef ModuleElement _add_(left, ModuleElement right):
     333        """
     334        EXAMPLES::
     335
     336            sage: K.<a> = NumberField(x^3 -x + 2, implementation='generic'); b = K.maximal_order()(a^2-3); b
     337            a^2 - 3
     338            sage: c = b + b; c
     339            2*a^2 - 6
     340            sage: parent(c)
     341            Maximal Order in Number Field in a with defining polynomial x^3 - x + 2
     342        """
     343        return left._new(left._f + (<OrderElement_generic>right)._f)
     344
     345    cpdef ModuleElement _sub_(left, ModuleElement right):
     346        """
     347        EXAMPLES::
     348
     349            sage: K.<a> = NumberField(x^3 -x + 2, implementation='generic'); b = K.maximal_order()(a^2-3); b
     350            a^2 - 3
     351            sage: c = b - b; c
     352            0
     353            sage: parent(c)
     354            Maximal Order in Number Field in a with defining polynomial x^3 - x + 2
     355        """
     356        return left._new(left._f - (<OrderElement_generic>right)._f)
     357
     358    def __richcmp__(left, right, int op):
     359        """
     360        """
     361        return (<Element>left)._richcmp(right, op)
     362
     363    cdef int _cmp_c_impl(left, Element right) except -2:
     364        cdef OrderElement_generic _right = right
     365        return cmp(left._f, (<OrderElement_generic>right)._f)
     366
     367    cpdef RingElement _div_(left, RingElement right):
     368        r"""
     369        Implement division, checking that the result has the right parent.
     370       
     371        EXAMPLES::
     372       
     373            sage: K.<a> = NumberField(x^3 - 17, implementation='generic')
     374            sage: OK = K.ring_of_integers(); a = OK(a); type(a)
     375            <type 'sage.rings.number_field.number_field_element_generic.OrderElement_generic'>
     376            sage: (17/a) in OK
     377            True
     378            sage: (17/a).parent() is K
     379            True
     380            sage: (17/(2*a)).parent() is K
     381            True
     382            sage: (17/(2*a)) in OK
     383            False
     384        """     
     385        if left is right: return left._parent.number_field().one_element()
     386        if not (<OrderElement_generic>right)._f: raise ZeroDivisionError
     387        h = left.modulus()
     388        one, a, b = (<NumberFieldElement_generic>right)._f.xgcd(h)
     389        return left._new_nfelt((left._f * a) % h)
     390   
     391    def __invert__(self):
     392        """
     393        EXAMPLES::
     394
     395            sage: K.<a> = NumberField(x^3 -x + 2, implementation='generic'); b = K.maximal_order()(a^2-3); b
     396            a^2 - 3
     397            sage: ~b
     398            -1/4*a^2 + 1/4*a - 1/2
     399            sage: (~b)*b
     400            1
     401            sage: parent(~b)
     402            Number Field in a with defining polynomial x^3 - x + 2
     403        """
     404        h = self.modulus()
     405        one, a, b = self._f.xgcd(h)
     406        return self._new_nfelt(a % h)
  • sage/rings/number_field/number_field_element_libsingular.pxd

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field_element_libsingular.pxd
    a b  
    11from number_field_element cimport NumberFieldElement
    22
    33cdef class NumberFieldElement_libsingular(NumberFieldElement):
    4     pass
     4    cdef _new(self)
  • sage/rings/number_field/number_field_element_ntl.pxd

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field_element_ntl.pxd
    a b  
    2525
    2626    cdef void _invert_c_(self, ZZX_c *num, ZZ_c *den)
    2727    cdef void _reduce_c_(self)
     28    cdef _new(self)
    2829   
    2930   
    3031cdef class NumberFieldElement_ntl_absolute(NumberFieldElement_ntl):
  • sage/rings/number_field/number_field_element_ntl.pyx

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field_element_ntl.pyx
    a b  
    9494   
    9595    ::
    9696   
    97         sage: k.<a> = NumberField(x^3 - 2)
     97        sage: k.<a> = NumberField(x^3 - 2, implementation='ntl')
    9898        sage: loads(dumps(a+1)) == a + 1
    9999        True
    100100    """
     
    110110   
    111111    ::
    112112   
    113         sage: k.<a> = NumberField(x^3 - 2)
     113        sage: k.<a> = NumberField(x^3 - 2, implementation='ntl')
    114114        sage: loads(dumps(a+1)) == a + 1
    115115        True
    116116
    117117    This also gets called for unpickling order elements; we check that #6462 is
    118118    fixed::
    119119
    120         sage: L = NumberField(x^3 - x - 1,'a'); OL = L.maximal_order(); w = OL.0
     120        sage: L = NumberField(x^3 - x - 1,'a', implementation='ntl'); OL = L.maximal_order(); w = OL.0
    121121        sage: loads(dumps(w)) == w
    122122        True
    123123    """
     
    133133   
    134134    EXAMPLES::
    135135   
    136         sage: OE = NumberField(x^3 - x + 2, 'w').ring_of_integers()
     136        sage: OE = NumberField(x^3 - x + 2, 'w', implementation='ntl').ring_of_integers()
    137137        sage: w = OE.ring_generators()[0]
    138138        sage: from sage.rings.number_field.number_field_element import _inverse_mod_generic
    139139        sage: _inverse_mod_generic(w, 13*OE)
     
    167167   
    168168    EXAMPLES::
    169169   
    170         sage: k.<a> = NumberField(x^3 + x + 1)
     170        sage: k.<a> = NumberField(x^3 + x + 1, implementation='ntl')
    171171        sage: a^3
    172172        -a - 1
    173173    """
     
    186186    def __init__(self, parent, f):
    187187        """
    188188        INPUT:
    189        
    190        
    191         -  ``parent`` - a number field
    192        
    193         -  ``f`` - defines an element of a number field.
    194        
     189            -  ``parent`` - a number field
     190            -  ``f`` - defines an element of a number field.
    195191       
    196192        EXAMPLES:
    197193
    198194        The following examples illustrate creation of elements of
    199         number fields, and some basic arithmetic.
     195        number fields using the NTL implementation, and some basic
     196        arithmetic::
    200197       
    201         First we define a polynomial over Q.
    202        
    203         ::
    204        
    205             sage: R.<x> = PolynomialRing(QQ)
    206             sage: f = x^2 + 1
    207        
    208         Next we use f to define the number field.
    209        
    210         ::
    211        
    212             sage: K.<a> = NumberField(f); K
     198            sage: K.<a> = NumberField(x^2 + 1, implementation='ntl'); K
    213199            Number Field in a with defining polynomial x^2 + 1
    214             sage: a = K.gen()
    215200            sage: a^2
    216201            -1
    217202            sage: (a+1)^2
     
    225210       
    226211        ::
    227212       
    228             sage: K.<b> = NumberField(x^3 - 2)
     213            sage: K.<b> = NumberField(x^3 - 2, implementation='ntl')
    229214            sage: b = K.gen()
    230215            sage: b^3
    231216            2
     
    234219       
    235220        This example illustrates save and load::
    236221       
    237             sage: K.<a> = NumberField(x^17 - 2)
     222            sage: K.<a> = NumberField(x^17 - 2, implementation='ntl')
    238223            sage: s = a^15 - 19*a + 3
    239224            sage: loads(s.dumps()) == s
    240225            True
     
    528513            sage: C.<zeta12>=CyclotomicField(12)
    529514            sage: zeta12*zeta12^11
    530515            1
    531             sage: G.<a> = NumberField(x^3 + 2/3*x + 1)
     516            sage: G.<a> = NumberField(x^3 + 2/3*x + 1, implementation='ntl')
    532517            sage: a^3
    533518            -2/3*a - 1
    534519            sage: a^3+a
     
    584569       
    585570        ::
    586571       
    587             sage: G.<a> = NumberField(x^3 + 2/3*x + 1)
     572            sage: G.<a> = NumberField(x^3 + 2/3*x + 1, implementation='ntl')
    588573            sage: a/a
    589574            1
    590575            sage: 1/a
     
    845830   
    846831    EXAMPLES::
    847832   
    848         sage: K.<a> = NumberField(x^2 + 1)
     833        sage: K.<a> = NumberField(x^2 + 1, implementation='ntl')
    849834        sage: O2 = K.order(2*a)
    850835        sage: w = O2.1; w
    851836        2*a
     
    921906       
    922907        EXAMPLES::
    923908       
    924             sage: K = NumberField(x^3 - 17, 'a')
     909            sage: K.<a> = NumberField(x^3 - 17, implementation='ntl')
    925910            sage: OK = K.ring_of_integers()
    926911            sage: a = OK(K.gen())
    927912            sage: (17/a) in OK
     
    952937       
    953938        EXAMPLES::
    954939       
    955             sage: OE = NumberField(x^3 - x + 2, 'w').ring_of_integers()
     940            sage: OE = NumberField(x^3 - x + 2, 'w', implementation='ntl').ring_of_integers()
    956941            sage: w = OE.ring_generators()[0]
    957942            sage: w.inverse_mod(13*OE)
    958943            6*w^2 - 6
     
    975960
    976961        EXAMPLE::
    977962       
    978             sage: K = NumberField(x^3 -x + 2, 'a')
     963            sage: K = NumberField(x^3 -x + 2, 'a', implementation='ntl')
    979964            sage: OK = K.ring_of_integers()
    980965            sage: a = OK(K.gen())
    981966            sage: (~a).parent() is K
     
    10541039       
    10551040        EXAMPLES::
    10561041       
    1057             sage: K1.<a> = NumberField(x^3 - 17)
     1042            sage: K1.<a> = NumberField(x^3 - 17, implementation='ntl')
    10581043            sage: R.<y> = K1[]
    10591044            sage: K2 = K1.extension(y^2 - a, 'b')
    10601045            sage: OK2 = K2.order(K2.gen()) # (not maximal)
     
    10771062
    10781063        EXAMPLES::
    10791064
    1080             sage: K1.<a> = NumberField(x^3 - 17)
     1065            sage: K1.<a> = NumberField(x^3 - 17, implementation='ntl')
    10811066            sage: R.<y> = K1[]
    10821067            sage: K2 = K1.extension(y^2 - a, 'b')
    10831068            sage: OK2 = K2.order(K2.gen()) # (not maximal)
  • sage/rings/number_field/number_field_rel.py

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/number_field_rel.py
    a b  
    140140from sage.rings.number_field.number_field_ideal_rel import NumberFieldFractionalIdeal_rel
    141141from sage.libs.all import pari, pari_gen
    142142
     143import implementation as arithmetic_implementation
     144
    143145QQ = rational_field.RationalField()
    144146ZZ = integer_ring.IntegerRing()
    145147RIF = sage.rings.real_mpfi.RealIntervalField()
     
    174176class NumberField_relative(NumberField_generic):
    175177    """
    176178    INPUT:
    177        
    178     - ``base`` -- the base field
    179     - ``polynomial`` -- must be defined in the ring `K[x]`, where `K` is
    180       the base field.
    181     - ``name`` -- variable name
    182     - ``latex_name`` -- latex variable name
    183     - ``names`` -- alternative to name
    184     - ``check`` -- whether to check irreducibility of polynomial.
     179        - ``base`` -- the base field
     180        - ``polynomial`` -- must be defined in the ring `K[x]`, where `K` is
     181          the base field.
     182        - ``name`` -- variable name
     183        - ``latex_name`` -- latex variable name
     184        - ``names`` -- alternative to name
     185        - ``check`` -- whether to check irreducibility of polynomial.
    185186
    186187    EXAMPLES::
    187188
     
    195196                 implementation='default'):
    196197        r"""
    197198        INPUT:
    198        
    199         - ``base`` -- the base field
    200         - ``polynomial`` -- must be defined in the ring `K[x]`, where `K` is
    201           the base field.
    202         - ``name`` -- variable name
    203         - ``latex_name`` -- latex variable name
    204         - ``names`` -- alternative to name
    205         - ``check`` -- whether to check irreducibility of polynomial.
     199            - ``base`` -- the base field
     200            - ``polynomial`` -- must be defined in the ring `K[x]`, where `K` is
     201              the base field.
     202            - ``name`` -- variable name
     203            - ``latex_name`` -- latex variable name
     204            - ``names`` -- alternative to name
     205            - ``check`` -- whether to check irreducibility of polynomial.
    206206
    207207        EXAMPLES::
    208208
     
    298298        if implementation == 'ntl':
    299299            import number_field_element_ntl
    300300            self._element_class = number_field_element_ntl.NumberFieldElement_ntl_relative
     301            self._order_element_class = number_field_element_ntl.OrderElement_ntl_relative
     302            self._set_implementation(arithmetic_implementation.ArithmeticImplementation_ntl())
    301303        elif implementation == 'generic':
    302304            import number_field_element_generic
    303305            self._element_class = number_field_element_generic.NumberFieldElement_generic
     306            self._order_element_class = number_field_element_generic.OrderElement_generic
     307            self._set_implementation(arithmetic_implementation.ArithmeticImplementation_generic(polynomial))
    304308        elif implementation == 'singular':
    305309            import number_field_element_singular
    306310            self._element_class = number_field_element_singular.NumberFieldElement_singular
     311            self._order_element_class = number_field_element_singular.OrderElement_singular
     312            self._set_implementation(arithmetic_implementation.ArithmeticImplementation_singular())           
    307313        else:
    308             raise ValueError, 'unknown implementation'
     314            raise ValueError, "unknown implementation '%s'"%implementation
     315
     316        assert self.implementation() is not None
    309317
    310318        if check:
    311319            if not self.pari_relative_polynomial().polisirreducible():
     
    9941002            sage: S.coerce(-Integer(2))
    9951003            -2
    9961004            sage: z = S.coerce(-7/8); z, type(z)
    997             (-7/8, <type 'sage.rings.number_field.number_field_element.NumberFieldElement_absolute'>)
     1005            (-7/8, <type 'sage.rings.number_field.number_field_element_ntl.NumberFieldElement_ntl_absolute'>)
    9981006            sage: S.coerce(y) is y
    9991007            True
    10001008
     
    15691577            x^2 + 2*a1*x + a1^2 + 2
    15701578            sage: g.absolute_minpoly()
    15711579            x^8 + 8*x^6 + 30*x^4 - 40*x^2 + 49
     1580
     1581        Another example involving a tower::
     1582
     1583            sage: R.<x> = QQ[]
     1584            sage: F.<a> = NumberField(x^2 - 2)
     1585            sage: L.<b> = F.extension(x^3 - a)
     1586            sage: M.<c> = NumberField(x^2 - x*b^2 + b)
     1587            sage: M.absolute_generator()
     1588            c
    15721589        """
    15731590        try:
    15741591            return self.__abs_gen
    15751592        except AttributeError:
    1576             self.__abs_gen = self._element_class(self, QQ['x'].gen())
     1593            L = self.absolute_field('a')
     1594            t = L.structure()[0](L.gen())
     1595            self.__abs_gen = t
    15771596            return self.__abs_gen
    15781597       
    15791598    def absolute_field(self, names):
  • sage/rings/number_field/order.py

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/order.py
    a b  
    3636    17^45
    3737"""
    3838
    39 from sage.rings.ring import IntegralDomain
     39from order_base import Order_base
    4040from sage.structure.sequence import Sequence
    4141from sage.rings.integer_ring import ZZ
    4242from sage.structure.element import is_Element
     
    4848
    4949from sage.rings.monomials import monomials
    5050
     51from sage.rings.ring import IntegralDomain
    5152
    5253def is_NumberFieldOrder(R):
    5354    r"""
     
    116117    K = NumberField(f, names=names)
    117118    return K.order(K.gens())
    118119
    119 class Order(IntegralDomain):
     120class Order(Order_base):
    120121    r"""
    121122    An order in a number field.
    122123
     
    160161        self._K = K
    161162        self._is_maximal = is_maximal
    162163        IntegralDomain.__init__(self, ZZ, names = K.variable_names(), normalize = False)
     164        self._set_implementation(K.implementation())
    163165
    164166    def fractional_ideal(self, *args, **kwds):
    165167        """
     
    937939        """
    938940        return self.number_field().absolute_degree()
    939941
    940 ##     def absolute_polynomial(self):
    941 ##         """
    942 ##         Returns the absolute polynomial of this order, which is just the absolute polynomial of the number field.
    943 
    944 ##          EXAMPLES:
    945 ##         sage: K.<a, b> = NumberField([x^2 + 1, x^3 + x + 1]); OK = K.maximal_order()
    946 ##         Traceback (most recent call last):
    947 ##         ...
    948 ##         NotImplementedError
    949        
    950 ##         #sage: OK.absolute_polynomial()
    951 ##         #x^6 + 5*x^4 - 2*x^3 + 4*x^2 + 4*x + 1
    952 ##         """
    953 ##         return self.number_field().absolute_polynomial()
    954 
    955 ##     def polynomial(self):
    956 ##         """
    957 ##         Returns the polynomial defining the number field that contains self.
    958 ##         """
    959 ##         return self.number_field().polynomial()
    960 
    961 ##     def polynomial_ntl(self):
    962 ##         """
    963 ##         Return defining polynomial of the parent number field as a
    964 ##         pair, an ntl polynomial and a denominator.
    965 
    966 ##         This is used mainly to implement some internal arithmetic.
    967 
    968 ##         EXAMPLES:
    969 ##             sage: NumberField(x^2 + 1,'a').maximal_order().polynomial_ntl()
    970 ##             ([1 0 1], 1)
    971 ##         """
    972 ##         return self.number_field().polynomial_ntl()
    973 
    974942class AbsoluteOrder(Order):
    975943
    976944    def __init__(self, K, module_rep, is_maximal=None, check=True):
     
    994962            sage: loads(dumps(O)) == O
    995963            True
    996964
    997         Quadratic elements have a special optimized type:
    998            
     965        Quadratic elements have a special optimized type::
     966
     967            sage: R.<a> = EquationOrder(x^2+2); type(a)
     968            <type 'sage.rings.number_field.number_field_element_quadratic.OrderElement_quadratic'>
    999969        """
    1000970        Order.__init__(self, K, is_maximal=is_maximal)
    1001971
    1002         if K.degree() == 2:
    1003             self._element_type = OrderElement_quadratic
    1004         else:
    1005             self._element_type = OrderElement_ntl_absolute
    1006            
     972        self._element_type = K._order_element_class
    1007973        self._module_rep = module_rep
     974       
    1008975        V, from_v, to_v = self._K.vector_space()
    1009976        if check:
    1010977            if not K.is_absolute():
     
    11311098        v = [K(a)._magma_init_(magma) for a in self.gens()]
    11321099        return 'Order([%s])'%(','.join(v))
    11331100
     1101    def is_absolute(self):
     1102        """
     1103        EXAMPLES::
     1104       
     1105            sage: EquationOrder([x^2 + x + 1],'a').is_absolute()
     1106            True
     1107        """
     1108        return True
     1109
    11341110    def discriminant(self):
    11351111        """
    11361112        Return the discriminant of this order.
     
    14181394        #", ".join([str(b) for b in self.basis()]),
    14191395        return "%sRelative Order in %r" % ("Maximal " if self._is_maximal else "", self._K)
    14201396       
     1397    def is_absolute(self):
     1398        """
     1399        EXAMPLES::
     1400
     1401            sage: EquationOrder([x^2 + x + 1, x^3 - 2],'a,b').is_absolute()
     1402            False
     1403        """
     1404        return False
     1405
    14211406    def absolute_order(self, names='z'):
    14221407        """
    14231408        Return underlying absolute order associated to this relative
     
    17321717              allow_subfield = False):
    17331718    """
    17341719    INPUT:
    1735 
    1736     - ``gens`` -- list of elements of an absolute number field that generates an
    1737       order in that number field as a ZZ *module*.
    1738     - ``check_integral`` -- check that each gen is integral
    1739     - ``check_rank`` -- check that the gens span a module of the correct rank
    1740     - ``check_is_ring`` -- check that the module is closed under multiplication
    1741       (this is very expensive)
    1742     - ``is_maximal`` -- bool (or None); set if maximality of the generated order is known                 
     1720        - ``gens`` -- list of elements of an absolute number field that generates an
     1721          order in that number field as a ZZ *module*.
     1722        - ``check_integral`` -- check that each gen is integral
     1723        - ``check_rank`` -- check that the gens span a module of the correct rank
     1724        - ``check_is_ring`` -- check that the module is closed under multiplication
     1725          (this is very expensive)
     1726        - ``is_maximal`` -- bool (or None); set if maximality of the generated order is known                 
    17431727
    17441728    OUTPUT:
    1745    
    1746     an absolute order
     1729        - an absolute order
    17471730
    17481731    EXAMPLES:
     1732   
    17491733    We have to explicitly import the function, since it isn't meant
    17501734    for regular usage::
    17511735
    17521736        sage: from sage.rings.number_field.order import absolute_order_from_module_generators
    1753 
    17541737        sage: K.<a> = NumberField(x^4 - 5)
    17551738        sage: O = K.maximal_order(); O
    17561739        Maximal Order in Number Field in a with defining polynomial x^4 - 5
     
    18581841        alg = [to_V(x) for x in monomials(gens, [f.absolute_minpoly().degree() for f in gens])]
    18591842        if ambient.span(alg) != W:
    18601843            raise ValueError, "the module span of the gens is not closed under multiplication."
    1861        
     1844
    18621845    return AbsoluteOrder(K, W, check=False, is_maximal=is_maximal)  # we have already checked everything
    18631846   
    18641847
  • new file sage/rings/number_field/order_base.pxd

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/order_base.pxd
    - +  
     1from sage.rings.ring cimport IntegralDomain
     2from implementation cimport ArithmeticImplementation
     3
     4cdef class Order_base(IntegralDomain):
     5    cdef ArithmeticImplementation _implementation
     6    cpdef ArithmeticImplementation implementation(self)
     7    cpdef _set_implementation(self, ArithmeticImplementation implementation)
  • new file sage/rings/number_field/order_base.pyx

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/order_base.pyx
    - +  
     1cdef class Order_base(IntegralDomain):
     2    cpdef ArithmeticImplementation implementation(self):
     3        """
     4        EXAMPLES::
     5       
     6            sage: NumberField(x^3+x+1,'c', implementation='ntl').maximal_order().implementation()
     7            Arithmetic implementation 'ntl'
     8            sage: NumberField(x^3+x+1,'c', implementation='generic').maximal_order().implementation()
     9            Arithmetic implementation 'generic'
     10        """
     11        return self._implementation
     12
     13    cpdef _set_implementation(self, ArithmeticImplementation implementation):
     14        """
     15        Do not use this!  It is used internally during object construction.
     16       
     17        EXAMPLES::
     18       
     19            sage: R = NumberField(x^3+x+1,'c', implementation='generic').maximal_order()
     20            sage: R._set_implementation(sage.rings.number_field.implementation.ArithmeticImplementation_singular())
     21            sage: R.implementation()
     22            Arithmetic implementation 'singular'
     23        """
     24        self._implementation = implementation
     25   
  • sage/rings/number_field/small_primes_of_degree_one.py

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/small_primes_of_degree_one.py
    a b  
    4545
    4646EXAMPLES::
    4747
    48     sage: x = ZZ['x'].gen()
    49     sage: F.<a> = NumberField(x^2 - 2, 'a')
     48    sage: R.<x> = ZZ[]
     49    sage: F.<a> = NumberField(x^2 - 2)
    5050    sage: Ps = F.primes_of_degree_one_list(3)
    5151    sage: Ps # random
    5252    [Fractional ideal (2*a + 1), Fractional ideal (-3*a + 1), Fractional ideal (-a + 5)]
     
    5959
    6060The next two examples are for relative number fields.::
    6161
    62     sage: L.<b> = F.extension(x^3 - a, 'b')
     62    sage: L.<b> = F.extension(x^3 - a)
    6363    sage: Ps = L.primes_of_degree_one_list(3)
    6464    sage: Ps # random
    6565    [Fractional ideal (17, b - 5), Fractional ideal (23, b - 4), Fractional ideal (31, b - 2)]
     
    6969    True
    7070    sage: all(P.residue_class_degree() == 1 for P in Ps)
    7171    True
    72     sage: M.<c> = NumberField(x^2 - x*b^2 + b, 'c')
     72    sage: M.<c> = NumberField(x^2 - x*b^2 + b)
    7373    sage: Ps = M.primes_of_degree_one_list(3)
    7474    sage: Ps # random
    7575    [Fractional ideal (17, c - 2), Fractional ideal (c - 1), Fractional ideal (41, c + 15)]
  • sage/rings/number_field/todo.txt

    diff -r e2e7a6415ab6 -r d582c0c29177 sage/rings/number_field/todo.txt
    a b  
    1919   [x] move is_OrderElement
    2020   [x] test
    2121   [x] fill in all the sage: doctests, so all tests pass again
     22   [x] doctest existing code -- make ntl file use implementation='ntl' everywhere.
     23   [x] _quadratic -- make sure datatype right in doctests there.
     24   [x] cython arithmetic implementation object.
    2225
    23    [ ] doctest existing code -- make ntl file use implementation='ntl' everywhere.
     26   [ ] test stabilization
     27 
     28   [ ] generic: get to work in the *relative case*
     29
     30   [ ] rewrite order elements to use implementation, e.g., this line in order.py:
     31       #TODO: fix this
     32       from number_field_element_ntl import OrderElement_absolute, OrderElement_relative
     33
     34   [ ] implement OrderElement_generic, _singular, _flint
     35   [ ] get doctest coverage of 'generic' to 100%
     36   [ ] get implementation = 'generic' to fully work as another default type
     37
     38   [ ] move data out of elements into implementation objects for ntl and quadratic types
     39
     40   [ ] increase coverage to 100%
     41
     42
     43   [ ] fix all INPUT block list formatting
    2444
    2545   [ ] generic elements
     46
    2647   [ ] libsingular elements
    2748   [ ] flint elements
    2849
     50   [ ] rewrite base class "def _lift_cyclotomic_element(self, new_parent, bint check=True, int rel=0):"
     51
    2952   [ ] make default implementation computation be centralized?
    3053
    3154   [ ] fix order.py -- it explicitly references ntl but must not.
    32    [ ] rewrite order elements to use implementation, e.g., this line in order.py:
    33        #TODO: fix this
    34        from number_field_element_ntl import OrderElement_absolute, OrderElement_relative
    3555   [ ] change number_field_element_quadratic to derive from
    3656       number_field_element code (i.e., the abstract base), instead of
    3757       deriving from number_field_element_ntl.
     
    5979       guarantees at least a certain amount of speed.  add this to
    6080       library so that nobody can frack my shizzle up.
    6181   [ ] add proper headers
     82   [ ] add overview docs to each file
     83   [ ] overview of everything
    6284   [ ] rebase
     85   [ ] rebase 2 -- make sure my fast mod-p reduction works.
    6386   [ ] get refereed
    6487   [ ] check through todo
    6588   [ ] cacheing in number_field_element.pxd seems excessive
     89   [ ] suspicious bound:
     90     "   D = (Dpoly.numer() * Dpoly.denom()).squarefree_part(bound=10000) "
     91
    6692[ ] optimize -- creation of the order generated by elements, especially in relative case (use singular?)
    6793[ ] plug in sebastian's code for QQ['x']?
    6894[ ] Organize a similar refactoring for function fields.