Ticket #9400: 9400_maximize_at_primes.patch

File 9400_maximize_at_primes.patch, 31.2 KB (added by jdemeyer, 10 years ago)

Patch for the "maximize at primes" part, rebased to sage-4.6.prealpha1 (see #9343)

  • sage/rings/number_field/number_field.py

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1282167950 -7200
    # Node ID 386ca091266034e666f83b261f71313134a66896
    # Parent  5bf585a85edbee49811c2f07ddf33549f6ad6682
    [mq]: 9400_maximize_at_primes
    
    diff -r 5bf585a85edb -r 386ca0912660 sage/rings/number_field/number_field.py
    a b  
    248248
    249249
    250250_nf_cache = {}
    251 def NumberField(polynomial, name=None, check=True, names=None, cache=True, embedding=None, latex_name=None):
     251def NumberField(polynomial, name=None, check=True, names=None, cache=True,
     252                embedding=None, latex_name=None,
     253                assume_disc_small=False,
     254                maximize_at_primes=None):
    252255    r"""
    253256    Return *the* number field defined by the given irreducible
    254257    polynomial and with variable with the given name. If check is True
     
    257260   
    258261    INPUT:
    259262   
    260     -  ``polynomial`` - a polynomial over `\QQ` or a number
    261        field, or a list of polynomials.
    262     -  ``name`` - a string (default: 'a'), the name of the
    263        generator
    264     -  ``check`` - bool (default: True); do type checking
    265        and irreducibility checking.
    266     -  ``embedding`` - image of the generator in an ambient
    267        field (default: None)
     263        - ``polynomial`` - a polynomial over `\QQ` or a number field,
     264          or a list of polynomials.
     265        - ``name`` - a string (default: 'a'), the name of the generator
     266        - ``check`` - bool (default: True); do type checking and
     267          irreducibility checking.
     268        - ``embedding`` - image of the generator in an ambient field
     269          (default: None)
     270        - ``assume_disc_small`` -- (default: False); if True, assume
     271          that no square of a prime greater than PARI's primelimit
     272          (which should be 500000); only applies for absolute fields
     273          at present.
     274        - ``maximize_at_primes`` -- (default: None) None or a list of
     275          primes; if not None, then the maximal order is computed by
     276          maximizing only at the primes in this list, which completely
     277          avoids having to factor the discriminant, but of course can
     278          lead to wrong results; only applies for absolute fields at
     279          present.
    268280   
    269281    EXAMPLES::
    270282   
     
    454466
    455467    if cache:
    456468        key = (polynomial, polynomial.base_ring(), name, latex_name,
    457                embedding, embedding.parent() if embedding is not None else None)
     469               embedding, embedding.parent() if embedding is not None else None,
     470               assume_disc_small, None if maximize_at_primes is None else tuple(maximize_at_primes))
    458471        if _nf_cache.has_key(key):
    459472            K = _nf_cache[key]()
    460473            if not K is None: return K
     
    466479        return S
    467480
    468481    if polynomial.degree() == 2:
    469         K = NumberField_quadratic(polynomial, name, latex_name, check, embedding)
     482        K = NumberField_quadratic(polynomial, name, latex_name, check, embedding,
     483             assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes)
    470484    else:
    471         K = NumberField_absolute(polynomial, name, latex_name, check, embedding)
     485        K = NumberField_absolute(polynomial, name, latex_name, check, embedding,
     486             assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes)
    472487
    473488    if cache:
    474489        _nf_cache[key] = weakref.ref(K)
     
    926941    """
    927942    def __init__(self, polynomial, name,
    928943                 latex_name=None, check=True, embedding=None,
    929                  category = None):
     944                 category = None,
     945                 assume_disc_small=False, maximize_at_primes=None):
    930946        """
    931947        Create a number field.
    932948       
     
    978994            ...
    979995            TypeError: polynomial must be defined over rational field
    980996        """
    981 
     997        self._assume_disc_small = assume_disc_small
     998        self._maximize_at_primes = maximize_at_primes
    982999        from sage.categories.number_fields import NumberFields
    9831000        default_category = NumberFields()
    9841001        if category is None:
     
    21652182       
    21662183            sage: x = ZZ['x'].gen()
    21672184            sage: F.<t> = NumberField(x^3 - 2)
    2168        
     2185           
    21692186        ::
    21702187       
    21712188            sage: P2 = F.prime_above(2)
     
    24152432            Traceback (most recent call last):
    24162433            ...
    24172434            TypeError: Unable to coerce number field defined by non-integral polynomial to PARI.
     2435
     2436        We illustrate the maximize_at_primes and assume_disc_small parameters::
     2437       
     2438            sage: p = next_prime(10^40); q = next_prime(p)
     2439            sage: K.<a> = NumberField(x^2 - p*q, maximize_at_primes=[p])
     2440
     2441        The following would take a very long time without the
     2442        maximize_at_primes option above::
     2443       
     2444            sage: K.pari_nf()
     2445            [x^2 - 100000000000000000000...]
     2446
     2447        I'm not sure this really illustrates anything::
     2448       
     2449            sage: K.<a> = NumberField(x^2 - 3*5*7*11*13, assume_disc_small=True)
     2450            sage: K.pari_nf()
     2451            [x^2 - 15015, [2, 0], 60060, 1, ...]
    24182452        """
    24192453        if self.absolute_polynomial().denominator() != 1:
    24202454            raise TypeError, "Unable to coerce number field defined by non-integral polynomial to PARI."
     
    24222456            return self.__pari_nf
    24232457        except AttributeError:
    24242458            f = self.pari_polynomial()
    2425             self.__pari_nf = f.nfinit()
     2459            if self._maximize_at_primes:
     2460                v = self._normalize_prime_list(self._maximize_at_primes)
     2461                m = self._pari_disc_factorization_matrix(v)
     2462                s = str(f)
     2463                # we use a string since the Sage wrapping of the pari
     2464                # c library nfinit doesn't support the third option.
     2465                k = pari('nfinit([%s,nfbasis(%s,0,%s)])'%(s,s,str(m)))
     2466            elif self._assume_disc_small:
     2467                k = f.nfinit(1)
     2468            else:
     2469                k = f.nfinit()
     2470            self.__pari_nf = k
    24262471            return self.__pari_nf
    24272472
    24282473    def _pari_init_(self):
     
    24412486            [x^2 + x + 1, [0, 1], -3, 1, ... [1, x], [1, 0; 0, 1], [1, 0, 0, -1; 0, 1, 1, -1]]
    24422487            sage: pari(k)
    24432488            [x^2 + x + 1, [0, 1], -3, 1, ...[1, x], [1, 0; 0, 1], [1, 0, 0, -1; 0, 1, 1, -1]]
    2444         """
     2489
     2490        We illustrate the maximize_at_primes parameter::
     2491       
     2492            sage: p = next_prime(10^40); q = next_prime(p)
     2493            sage: K.<a> = NumberField(x^2 - p*q, maximize_at_primes=[p,q])
     2494
     2495        Note that nfbasis is explicitly passed in::
     2496       
     2497            sage: K._pari_init_()
     2498            'nfinit(x^2 - 100000000000000000000000000000000000002600000000000000000000000000000000000016819,nfbasis(x^2 - 100000000000000000000000000000000000002600000000000000000000000000000000000016819,0,[10000000000000000000000000000000000000121, 1; 10000000000000000000000000000000000000139, 1]))'
     2499
     2500
     2501        We illustrate the assume_disc_small parameter (note the 1 option to nfinit)::
     2502       
     2503            sage: K.<a> = NumberField(x^2 - 3*5*7*11*13, assume_disc_small=True)
     2504            sage: K._pari_init_()
     2505            'nfinit(x^2 - 15015,1)'
     2506        """
     2507        f = str(self.pari_polynomial())
    24452508        if self.absolute_polynomial().denominator() != 1:
    24462509            raise TypeError, "Unable to coerce number field defined by non-integral polynomial to PARI."
    2447         return 'nfinit(%s)'%self.pari_polynomial()
    2448 
     2510        if self._maximize_at_primes:
     2511            v = self._normalize_prime_list(self._maximize_at_primes)
     2512            m = self._pari_disc_factorization_matrix(v)
     2513            return 'nfinit(%s,nfbasis(%s,0,%s))'%(f,f,str(m))
     2514        elif self._assume_disc_small:
     2515            return 'nfinit(%s,1)'%f
     2516        else:
     2517            return 'nfinit(%s)'%f
     2518           
    24492519    def pari_bnf(self, certify=False, units=True):
    24502520        """
    24512521        PARI big number field corresponding to this field.
     
    36543724       
    36553725        ALGORITHM: Uses the pari library.
    36563726        """
    3657         return self.maximal_order().basis()
     3727        return self.maximal_order(v=v).basis()
    36583728   
    36593729    def _compute_integral_basis(self, v=None):
    36603730        """
     
    36893759            sage: K.integral_basis()
    36903760            [1, 1/2*a^2 + 1/2*a, a^2]
    36913761        """
     3762        if (v is None or len(v) == 0) and self._maximize_at_primes:
     3763            v = self._maximize_at_primes
     3764               
    36923765        v = self._normalize_prime_list(v)
    36933766        try:
    36943767            return self._integral_basis_dict[v]
    36953768        except:
    36963769            f = self.pari_polynomial()
    3697 
    36983770            if len(v) == 0:
    3699                 B = f.nfbasis()
    3700             else:
    3701                 m = pari.matrix(len(v), 2)
    3702                 d = f.poldisc()
    3703                 for i in range(len(v)):
    3704                     p = pari(ZZ(v[i]))
    3705                     m[i,0] = p
    3706                     m[i,1] = d.valuation(p)
     3771                B = f.nfbasis(1 if self._assume_disc_small else 0)
     3772            else:
     3773                m = self._pari_disc_factorization_matrix(v)
    37073774                B = f.nfbasis(p = m)
    37083775
    37093776            basis = map(self, B)
    37103777            self._integral_basis_dict[v] = basis
    37113778            return basis
    37123779
     3780    def _pari_disc_factorization_matrix(self, v):
     3781        """
     3782        Returns a PARI matrix representation for the partial
     3783        factorization of the discriminant of the defining polynomial
     3784        of self, defined by the list of primes in the Python list v.
     3785        This function is used internally by the number fields code.
     3786       
     3787        EXAMPLES::
     3788
     3789            sage: x = polygen(QQ,'x')
     3790            sage: f = x^3 + 17*x + 393; f.discriminant().factor()
     3791            -1 * 5^2 * 29 * 5779
     3792            sage: K.<a> = NumberField(f)
     3793            sage: K._pari_disc_factorization_matrix([5,29])
     3794            [5, 2; 29, 1]
     3795        """
     3796        f = self.pari_polynomial()
     3797        m = pari.matrix(len(v), 2)
     3798        d = f.poldisc()
     3799        for i in range(len(v)):
     3800            p = pari(ZZ(v[i]))
     3801            m[i,0] = p
     3802            m[i,1] = d.valuation(p)
     3803        return m
     3804       
     3805
    37133806    def reduced_basis(self, prec=None):
    37143807        r"""
    37153808        This function returns an LLL-reduced basis for the
     
    46804773
    46814774class NumberField_absolute(NumberField_generic):
    46824775
    4683     def __init__(self, polynomial, name, latex_name=None, check=True, embedding=None):
     4776    def __init__(self, polynomial, name, latex_name=None, check=True, embedding=None,
     4777                 assume_disc_small=False, maximize_at_primes=None):
    46844778        """
    46854779        Function to initialize an absolute number field.
    46864780
     
    46924786            <class 'sage.rings.number_field.number_field.NumberField_absolute_with_category'>
    46934787            sage: TestSuite(K).run()
    46944788        """
    4695         NumberField_generic.__init__(self, polynomial, name, latex_name, check, embedding)
     4789        NumberField_generic.__init__(self, polynomial, name, latex_name, check, embedding,
     4790                                     assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes)
    46964791        self._element_class = number_field_element.NumberFieldElement_absolute
    46974792        self._zero_element = self(0)
    46984793        self._one_element =  self(1)
     
    62816376        sage: type(cf3(z1))
    62826377        <type 'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic'>
    62836378    """
    6284     def __init__(self, n, names, embedding=None):
     6379    def __init__(self, n, names, embedding=None, assume_disc_small=False, maximize_at_primes=None):
    62856380        """
    62866381        A cyclotomic field, i.e., a field obtained by adjoining an n-th
    62876382        root of unity to the rational numbers.
     
    63126407                                      name= names,
    63136408                                      latex_name=latex_name,
    63146409                                      check=False,
    6315                                       embedding = embedding)
     6410                                      embedding = embedding,
     6411                                      assume_disc_small=assume_disc_small,
     6412                                      maximize_at_primes=maximize_at_primes)
    63166413        if n%2:
    63176414            self.__zeta_order = 2*n
    63186415        else:
     
    70997196            sage: CyclotomicField(17).integral_basis() == CyclotomicField(17)._compute_integral_basis()
    71007197            True
    71017198        """
    7102         return self.integral_basis()
     7199        return self.integral_basis(v=v)
    71037200       
    71047201               
    71057202    def zeta_order(self):
     
    73067403        sage: QuadraticField(-4, 'b')
    73077404        Number Field in b with defining polynomial x^2 + 4
    73087405    """
    7309     def __init__(self, polynomial, name=None, latex_name=None, check=True, embedding=None):
     7406    def __init__(self, polynomial, name=None, latex_name=None, check=True, embedding=None,
     7407                 assume_disc_small=False, maximize_at_primes=None):
    73107408        """
    73117409        Create a quadratic number field.
    73127410       
     
    73307428
    73317429            sage: TestSuite(k).run()
    73327430        """
    7333         NumberField_absolute.__init__(self, polynomial, name=name, check=check, embedding=embedding, latex_name=latex_name)
     7431        NumberField_absolute.__init__(self, polynomial, name=name, check=check,
     7432                                      embedding=embedding, latex_name=latex_name,
     7433                                      assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes)
    73347434        self._element_class = number_field_element_quadratic.NumberFieldElement_quadratic
    73357435        c, b, a = [rational.Rational(t) for t in self.defining_polynomial().list()]
    73367436        # set the generator
  • sage/rings/residue_field.pyx

    diff -r 5bf585a85edb -r 386ca0912660 sage/rings/residue_field.pyx
    a b  
    1414    841
    1515
    1616We reduce mod a prime for which the ring of integers is not
    17 monogenic (i.e., 2 is an essential discriminant divisor):
     17monogenic (i.e., 2 is an essential discriminant divisor)::
     18
    1819    sage: K.<a> = NumberField(x^3 + x^2 - 2*x + 8)
    1920    sage: F = K.factor(2); F
    2021    (Fractional ideal (1/2*a^2 - 1/2*a + 1)) * (Fractional ideal (a^2 - 2*a + 3)) * (Fractional ideal (3/2*a^2 - 5/2*a + 4))
     
    2526    sage: F[2][0].residue_field()
    2627    Residue field of Fractional ideal (3/2*a^2 - 5/2*a + 4)
    2728
    28 We can also form residue fields from ZZ:
     29We can also form residue fields from ZZ::
     30
    2931    sage: ZZ.residue_field(17)
    3032    Residue field of Integers modulo 17
    3133
     
    3537    -- John Cremona (2008-9): extend reduction maps to the whole valuation ring
    3638                              add support for residue fields of ZZ
    3739
    38 TESTS:
     40TESTS::
     41
    3942    sage: K.<z> = CyclotomicField(7)
    4043    sage: P = K.factor(17)[0][0]
    4144    sage: ff = K.residue_field(P)
     
    4346    sage: parent(a*a)
    4447    Residue field in zbar of Fractional ideal (17)
    4548
    46 Reducing a curve modulo a prime:
     49Reducing a curve modulo a prime::
     50
    4751    sage: K.<s> = NumberField(x^2+23)
    4852    sage: OK = K.ring_of_integers()
    4953    sage: E = EllipticCurve([0,0,0,K(1),K(5)])
     
    5256    sage: E.base_extend(Fpp)
    5357    Elliptic Curve defined by y^2  = x^3 + x + 5 over Residue field of Fractional ideal (13, s - 4)
    5458
    55 Calculating Groebner bases over various residue fields.  First over a small non-prime field:
     59Calculating Groebner bases over various residue fields.  First over a small non-prime field::
     60
    5661    sage: F1.<u> = NumberField(x^6 + 6*x^5 + 124*x^4 + 452*x^3 + 4336*x^2 + 8200*x + 42316)
    5762    sage: reduct_id = F1.factor(47)[0][0]
    5863    sage: Rf = F1.residue_field(reduct_id)
     
    6772    sage: I.groebner_basis()
    6873    [X + (-19*ubar^2 - 5*ubar - 17)*Y]
    6974
    70 And now over a large prime field:
     75And now over a large prime field::
     76
    7177    sage: x = ZZ['x'].0
    7278    sage: F1.<u> = NumberField(x^2 + 6*x + 324)
    7379    sage: reduct_id = F1.prime_above(next_prime(2^42))
     
    125131    of the ring of integers of a number field.
    126132
    127133    INPUT:
    128         p -- a prime ideal of an order in a number field.
    129         names -- the variable name for the finite field created.
    130                  Defaults to the name of the number field variable but
    131                  with bar placed after it.
    132         check -- whether or not to check if p is prime.
     134   
     135        - ``p`` -- a prime ideal of an order in a number field.
     136        - ``names`` -- the variable name for the finite field created.
     137          Defaults to the name of the number field variable but with
     138          bar placed after it.
     139        - ``check`` -- whether or not to check if p is prime.
    133140
    134141    OUTPUT:
    135          -- The residue field at the prime p.
     142   
     143         - The residue field at the prime p.
    136144
    137     EXAMPLES:
     145    EXAMPLES::
     146   
    138147        sage: K.<a> = NumberField(x^3-7)
    139148        sage: P = K.ideal(29).factor()[0][0]
    140149        sage: ResidueField(P)
    141150        Residue field in abar of Fractional ideal (2*a^2 + 3*a - 10)
    142151
    143     The result is cached:
     152    The result is cached::
     153   
    144154        sage: ResidueField(P) is ResidueField(P)
    145155        True
    146156        sage: k = K.residue_field(P); k
     
    149159        841
    150160
    151161    An example where the generator of the number field doesn't
    152     generate the residue class field.
     162    generate the residue class field::
     163   
    153164        sage: K.<a> = NumberField(x^3-875)
    154165        sage: P = K.ideal(5).factor()[0][0]; k = K.residue_field(P); k
    155166        Residue field in abar of Fractional ideal (5, -2/25*a^2 - 1/5*a + 2)
     
    158169        sage: k.0^3 - 875
    159170        2
    160171
    161     An example where the residue class field is large but of degree 1:
     172    An example where the residue class field is large but of degree 1::
     173   
    162174        sage: K.<a> = NumberField(x^3-875); P = K.ideal(2007).factor()[0][0]; k = K.residue_field(P); k
    163175        Residue field of Fractional ideal (-2/25*a^2 - 2/5*a - 3)
    164176        sage: k(a)
     
    167179        0
    168180
    169181    In this example, 2 is an inessential discriminant divisor, so divides
    170     the index of ZZ[a] in the maximal order for all a.
     182    the index of ZZ[a] in the maximal order for all a::
     183   
    171184        sage: K.<a> = NumberField(x^3 + x^2 - 2*x + 8); P = K.ideal(2).factor()[0][0]; P
    172185        Fractional ideal (1/2*a^2 - 1/2*a + 1)
    173186        sage: F = K.residue_field(P); F
     
    288301    """
    289302    The class representing a generic residue field.
    290303
    291     EXAMPLES:
     304    EXAMPLES::
     305   
    292306        sage: I = QQ[i].factor(2)[0][0]; I
    293307        Fractional ideal (I + 1)
    294308        sage: k = I.residue_field(); k
     
    299313    def __init__(self, p, f, intp):
    300314        """
    301315        INPUT:
    302            p -- the prime (ideal) defining this residue field
    303            f -- the morphism from the order to self.
    304            intp -- the rational prime that p lives over.
     316       
     317           - ``p`` -- the prime (ideal) defining this residue field
     318           - ``f`` -- the morphism from the order to self.
     319           - ``intp`` -- the rational prime that p lives over.
    305320
    306321        EXAMPLES::
    307322
     
    330345        r"""
    331346        Return the maximal ideal that this residue field is the quotient by.
    332347
    333         EXAMPLES:
     348        EXAMPLES::
     349       
    334350            sage: K.<a> = NumberField(x^3 + x + 1)
    335351            sage: P = K.ideal(29).factor()[0][0]
    336352            sage: k = K.residue_field(P) # indirect doctest
     
    346362
    347363    def coerce_map_from_impl(self, R):
    348364        """
    349         EXAMPLES:
     365        EXAMPLES::
     366       
    350367            sage: K.<i> = NumberField(x^2+1)
    351368            sage: P = K.ideal(-3*i-2)
    352369            sage: OK = K.maximal_order()
     
    365382        """
    366383        Returns a string describing this residue field.
    367384
    368         EXAMPLES:
     385        EXAMPLES::
     386       
    369387            sage: K.<a> = NumberField(x^3-7)
    370388            sage: P = K.ideal(29).factor()[0][0]
    371389            sage: k = K.residue_field(P)
     
    384402        Returns a lift of x to the Order, returning a "polynomial" in the
    385403        generator with coefficients between 0 and $p-1$.
    386404
    387         EXAMPLES:
     405        EXAMPLES::
     406       
    388407            sage: K.<a> = NumberField(x^3-7)
    389408            sage: P = K.ideal(29).factor()[0][0]
    390409            sage: k =K.residue_field(P)
     
    406425        Return the partially defined reduction map from the number
    407426        field to this residue class field.
    408427
    409         EXAMPLES:
     428        EXAMPLES::
     429       
    410430            sage: I = QQ[2^(1/3)].factor(2)[0][0]; I
    411431            Fractional ideal (a)
    412432            sage: k = I.residue_field(); k
     
    422442
    423443    def lift_map(self):
    424444        """
    425         EXAMPLES:
     445        EXAMPLES::
     446       
    426447            sage: I = QQ[3^(1/3)].factor(5)[1][0]; I
    427448            Fractional ideal (a - 2)
    428449            sage: k = I.residue_field(); k
     
    443464        Compares two residue fields: they are equal iff the primes
    444465        defining them are equal.
    445466       
    446         EXAMPLES:
     467        EXAMPLES::
     468       
    447469            sage: K.<a> = NumberField(x^3-11)
    448470            sage: F = K.ideal(37).factor(); F
    449471            (Fractional ideal (37, a + 12)) * (Fractional ideal (-2*a + 5)) * (Fractional ideal (37, a + 9))  # 32-bit
     
    464486        r"""
    465487        Return the hash of self.
    466488
    467         EXAMPLES:
     489        EXAMPLES::
     490       
    468491            sage: K.<a> = NumberField(x^3 + x + 1)
    469492            sage: hash(K.residue_field(K.prime_above(17))) # random
    470493            -6463132282686559142
     
    478501    A reduction map from a (subset) of a number field to this residue
    479502    class field.
    480503
    481     EXAMPLES:
     504    EXAMPLES::
     505       
    482506        sage: I = QQ[sqrt(17)].factor(5)[0][0]; I
    483507        Fractional ideal (5)
    484508        sage: k = I.residue_field(); k
     
    490514        """
    491515        Create a reduction map.
    492516
    493         EXAMPLES:
     517        EXAMPLES::
     518       
    494519            sage: K.<a> = NumberField(x^3 + x^2 - 2*x + 8)
    495520            sage: F = K.factor(2)[0][0].residue_field()
    496521            sage: F.reduction_map()
     
    505530        """
    506531        Return the domain of this reduction map.
    507532
    508         EXAMPLES:
     533        EXAMPLES::
     534       
    509535            sage: K.<a> = NumberField(x^3 + x^2 - 2*x + 32)
    510536            sage: F = K.factor(2)[0][0].residue_field()
    511537            sage: F.reduction_map().domain()
     
    517543        """
    518544        Return the codomain of this reduction map.
    519545
    520         EXAMPLES:
     546        EXAMPLES::
     547       
    521548            sage: K.<a> = NumberField(x^3 + 128)
    522549            sage: F = K.factor(2)[0][0].residue_field()
    523550            sage: F.reduction_map().codomain()
     
    532559        If x doesn't map because it has negative valuation, then a
    533560        ZeroDivisionError exception is raised.
    534561
    535         EXAMPLES:
     562        EXAMPLES::
     563       
    536564            sage: K.<a> = NumberField(x^2 + 1)
    537565            sage: F = K.factor(2)[0][0].residue_field()
    538566            sage: r = F.reduction_map(); r
     
    545573            ZeroDivisionError: Cannot reduce field element 1/2*a modulo Fractional ideal (a + 1): it has negative valuation
    546574
    547575        An example to show that the issue raised in trac \#1951
    548         has been fixed.
     576        has been fixed::
     577       
    549578            sage: K.<i> = NumberField(x^2 + 1)
    550579            sage: P1, P2 = [g[0] for g in K.factor(5)]; (P1,P2)
    551580            (Fractional ideal (i + 2), Fractional ideal (-i + 2))
     
    562591            -1
    563592            sage: F2(a)
    564593            Traceback (most recent call last):
     594            ...
    565595            ZeroDivisionError: Cannot reduce field element -2/5*i + 1/5 modulo Fractional ideal (-i + 2): it has negative valuation
    566 
    567596        """
    568597        # The reduction map is just x |--> F(to_vs(x) * (PB**(-1))) if
    569598        # either x is integral or the denominator of x is coprime to
     
    578607            except ZeroDivisionError:
    579608                raise ZeroDivisionError, "Cannot reduce rational %s modulo %s: it has negative valuation"%(x,p)
    580609
    581         dx = x.denominator()
    582         if x.is_integral() or dx.gcd(ZZ(p.absolute_norm())) == 1:
     610        try:
    583611            return self.__F(self.__to_vs(x) * self.__PBinv)
     612        except: pass
    584613
    585614        # Now we do have to work harder...below this point we handle
    586615        # cases which failed before trac 1951 was fixed.
    587616        R = self.__K.ring_of_integers()
    588         dx = R(dx)
     617        dx = R(x.denominator())
    589618        nx = R(dx*x)
    590619        vnx = nx.valuation(p)
    591620        vdx = dx.valuation(p)
     
    593622            return self(0)
    594623        if vnx < vdx:
    595624            raise ZeroDivisionError, "Cannot reduce field element %s modulo %s: it has negative valuation"%(x,p)
     625       
    596626        a = self.__K.uniformizer(p,'negative') ** vnx
    597627        nx /= a
    598628        dx /= a
     
    609639
    610640    def __repr__(self):
    611641        """
    612         EXAMPLES:
     642        EXAMPLES::
     643       
    613644            sage: K.<theta_5> = CyclotomicField(5)
    614645            sage: F = K.factor(7)[0][0].residue_field()
    615646            sage: F.reduction_map().__repr__()
     
    621652    """
    622653    Lifting map from residue class field to number field.
    623654
    624     EXAMPLES:
     655    EXAMPLES::
     656   
    625657        sage: K.<a> = NumberField(x^3 + 2)
    626658        sage: F = K.factor(5)[0][0].residue_field()
    627659        sage: F.degree()
     
    637669        """
    638670        Create a lifting map.
    639671
    640         EXAMPLES:
     672        EXAMPLES::
     673       
    641674            sage: K.<theta_5> = CyclotomicField(5)
    642675            sage: F = K.factor(7)[0][0].residue_field()
    643676            sage: F.lift_map()
     
    666699        """
    667700        Return the codomain of this lifting map.
    668701
    669         EXAMPLES:
     702        EXAMPLES::
     703       
    670704            sage: K.<a> = CyclotomicField(7)
    671705            sage: F = K.factor(5)[0][0].residue_field()
    672706            sage: L = F.lift_map(); L
     
    680714        """
    681715        Lift from this residue class field to the number field.
    682716
    683         EXAMPLES:
     717        EXAMPLES::
     718       
    684719            sage: K.<a> = CyclotomicField(7)
    685720            sage: F = K.factor(5)[0][0].residue_field()
    686721            sage: L = F.lift_map(); L
     
    700735
    701736    def __repr__(self):
    702737        """
    703         EXAMPLES:
     738        EXAMPLES::
     739       
    704740            sage: K.<theta_12> = CyclotomicField(12)
    705741            sage: F.<tmod> = K.factor(7)[0][0].residue_field()
    706742            sage: F.lift_map().__repr__()
     
    716752    The class representing a homomorphism from the order of a number
    717753    field to the residue field at a given prime.
    718754
    719     EXAMPLES:
     755    EXAMPLES::
     756   
    720757        sage: K.<a> = NumberField(x^3-7)
    721758        sage: P  = K.ideal(29).factor()[0][0]
    722759        sage: k  = K.residue_field(P)
     
    738775           im_gen -- The image of the generator of the number field.
    739776
    740777        EXAMPLES:
    741         We create a residue field homomorphism:
     778       
     779        We create a residue field homomorphism::
     780       
    742781            sage: K.<theta> = CyclotomicField(5)
    743782            sage: P = K.factor(7)[0][0]
    744783            sage: P.residue_class_degree()
     
    759798
    760799    cpdef Element _call_(self, x):
    761800        """
    762         Applies this morphism to an element
     801        Applies this morphism to an element.
    763802
    764         EXAMPLES:
     803        EXAMPLES::
     804       
    765805            sage: K.<a> = NumberField(x^3-x+8)
    766806            sage: P = K.ideal(29).factor()[0][0]
    767807            sage: k =K.residue_field(P)
     
    779819        Returns a lift of x to the Order, returning a "polynomial" in
    780820        the generator with coefficients between 0 and p-1.
    781821
    782         EXAMPLES:
     822        EXAMPLES::
     823       
    783824            sage: K.<a> = NumberField(x^3-7)
    784825            sage: P = K.ideal(29).factor()[0][0]
    785826            sage: k = K.residue_field(P)
     
    804845    """
    805846    The class representing residue fields of number fields that have prime order.
    806847
    807     EXAMPLES:
     848    EXAMPLES::
     849   
    808850        sage: R.<x> = QQ[]
    809851        sage: K.<a> = NumberField(x^3-7)
    810852        sage: P = K.ideal(29).factor()[1][0]
     
    828870    def __init__(self, p, name, im_gen = None, intp = None):
    829871        """
    830872        INPUT:
    831            p -- A prime ideal of a number field.
    832            name -- the name of the generator of this extension
    833            im_gen -- the image of the generator of the number field in this finite field.
    834            intp -- the rational prime that p lies over.
     873       
     874           - ``p`` -- A prime ideal of a number field.
     875           - ``name`` -- the name of the generator of this extension
     876           - ``im_gen`` -- the image of the generator of the number field in this finite field.
     877           - ``intp`` -- the rational prime that p lies over.
    835878
    836         EXAMPLES:
     879        EXAMPLES::
     880       
    837881            sage: K.<i> = QuadraticField(-1)
    838882            sage: kk = ResidueField(K.factor(5)[0][0])
    839883            sage: type(kk)
     
    851895    def __call__(self, x):
    852896        """
    853897        INPUT:
    854            x -- something to cast in to self.
     898       
     899           - ``x`` -- something to cast in to self.
    855900
    856         EXAMPLES:
     901        EXAMPLES::
     902       
    857903            sage: R.<x> = QQ[]
    858904            sage: K.<a> = NumberField(x^3-7)
    859905            sage: P = K.ideal(29).factor()[1][0]
     
    881927    """
    882928    The class representing residue fields of number fields that have non-prime order >= 2^16.
    883929
    884     EXAMPLES:
     930    EXAMPLES::
     931   
    885932        sage: K.<a> = NumberField(x^3-7)
    886933        sage: P = K.ideal(923478923).factor()[0][0]
    887934        sage: k = K.residue_field(P)
     
    897944    """
    898945    def __init__(self, p, q, name, g, intp):
    899946        """
    900         EXAMPLES:
    901         We create an ext_pari residue field:
     947        EXAMPLES::
     948       
     949        We create an ext_pari residue field::
     950       
    902951            sage: K.<a> = NumberField(x^3-7)
    903952            sage: P = K.ideal(923478923).factor()[0][0]
    904953            sage: type(P.residue_field())
     
    913962        """
    914963        Coerce x into self.
    915964       
    916         EXAMPLES:
     965        EXAMPLES::
     966       
    917967            sage: K.<aa> = NumberField(x^3 - 2)
    918968            sage: P = K.factor(10007)[0][0]
    919969            sage: P.residue_class_degree()
     
    940990    """
    941991    The class representing residue fields of number fields that have non-prime order < 2**16.
    942992
    943     EXAMPLES:
     993    EXAMPLES::
     994   
    944995        sage: R.<x> = QQ[]
    945996        sage: K.<a> = NumberField(x^3-7)
    946997        sage: P = K.ideal(29).factor()[0][0]
     
    9581009    def __init__(self, p, q, name, g, intp):
    9591010        """
    9601011        INPUT:
    961            p -- the prime ideal defining this residue field
    962            q -- the order of this residue field (a power of intp)
    963            name -- the name of the generator of this extension
    964            g -- the polynomial modulus for this extension
    965            intp -- the rational prime that p lies over.
     1012       
     1013           - ``p`` -- the prime ideal defining this residue field
     1014           - ``q`` -- the order of this residue field (a power of intp)
     1015           - ``name`` -- the name of the generator of this extension
     1016           - ``g`` -- the polynomial modulus for this extension
     1017           - ``intp`` -- the rational prime that p lies over.
    9661018
    967         EXAMPLES:
     1019        EXAMPLES::
     1020       
    9681021            sage: R.<x> = QQ[]
    9691022            sage: K.<a> = NumberField(x^4+3*x^2-17)
    9701023            sage: P = K.ideal(61).factor()[0][0]
     
    9781031    def __call__(self, x):
    9791032        """
    9801033        INPUT:
    981           x -- Something to cast into self.
     1034       
     1035            - x -- Something to cast into self.
    9821036
    983         EXAMPLES:
     1037        EXAMPLES::
     1038       
    9841039            sage: R.<x> = QQ[]
    9851040            sage: K.<a> = NumberField(x^4+3*x^2-17)
    9861041            sage: P = K.ideal(61).factor()[0][0]