Ticket #6046: trac_6046.patch

File trac_6046.patch, 30.2 KB (added by cremona, 4 years ago)

Replaces all earlier patches; based on 4.0.alpha0

  • sage/rings/number_field/number_field.py

    # HG changeset patch
    # User John Cremona <john.cremona@gmail.com>
    # Date 1242575193 -3600
    # Node ID 19b9a330ac4695690b9b72eb47bb6b54271893bf
    # Parent  21c6c829ea32fa36c07b5d55395fa16f1bf8ef96
    * * *
    [mq]: nfht
    * * *
    heights fix
    * * *
    more heights fixes
    * * *
    Refine sorting of primes in primes_above()
    
    diff -r 21c6c829ea32 -r 19b9a330ac46 sage/rings/number_field/number_field.py
    a b  
    18731873           exactly this degree. 
    18741874         
    18751875         
    1876         OUTPUT: A list of prime ideals of self lying over x. If degree is 
    1877         specified and no such ideal exists, returns the empty list. 
     1876        OUTPUT: A list of prime ideals of self lying over x. If degree 
     1877        is specified and no such ideal exists, returns the empty list. 
     1878        The output is sorted by residue degree first, then by 
     1879        underlying prime (or equivalently, by norm). 
    18781880         
    18791881        EXAMPLES:: 
    18801882         
     
    19571959        if degree is not None: 
    19581960            degree = ZZ(degree) 
    19591961        ideal = self.ideal(x) 
    1960         facs = [ (id.residue_class_degree(), id) for id, _ in ideal.factor() ] 
    1961         facs.sort() # sorts on residue_class_degree(), lowest first 
     1962        facs = [ (id.residue_class_degree(), id.absolute_norm(), id) for id, _ in ideal.factor() ] 
     1963        facs.sort() # sorts on residue_class_degree(), lowest first, then by norm 
    19621964        if degree is None: 
    1963             return [ id for d, id in facs ] 
    1964         else: 
    1965             return [ id for d, id in facs if d == degree ] 
     1965            return [ id for d, n, id in facs ] 
     1966        else: 
     1967            return [ id for d, n, id in facs if d == degree ] 
    19661968 
    19671969    def prime_above(self, x, degree=None): 
    19681970        r""" 
     
    52805282 
    52815283    def places(self, all_complex=False, prec=None): 
    52825284        """ 
    5283         Return the collection of all places of self. By default, this 
    5284         returns the set of real places as homomorphisms into RIF first, 
    5285         followed by a choice of one of each pair of complex conjugate 
    5286         homomorphisms into CIF. 
     5285        Return the collection of all infinite places of self. 
     5286 
     5287        By default, this returns the set of real places as 
     5288        homomorphisms into RIF first, followed by a choice of one of 
     5289        each pair of complex conjugate homomorphisms into CIF. 
    52875290         
    52885291        On the other hand, if prec is not None, we simply return places 
    52895292        into RealField(prec) and ComplexField(prec) (or RDF, CDF if 
  • sage/rings/number_field/number_field_element.pyx

    diff -r 21c6c829ea32 -r 19b9a330ac46 sage/rings/number_field/number_field_element.pyx
    a b  
    1111 
    1212- Robert Bradshaw (2007-09-15): specialized classes for relative and 
    1313  absolute elements 
     14 
     15- John Cremona (2009-05-15): added support for local and global 
     16  logarithmic heights. 
     17 
    1418""" 
    1519 
    1620# TODO -- relative extensions need to be completely rewritten, so one 
     
    605609        return self.abs(prec=53, i=0) 
    606610 
    607611    def abs(self, prec=53, i=0): 
    608         """ 
    609         Return the absolute value of this element with respect to the ith 
    610         complex embedding of parent, to the given precision. 
     612        r""" 
     613        Return the absolute value of this element with respect to the 
     614        `i`th complex embedding of parent, to the given precision. 
    611615         
    612         If prec is 53 (the default), then the complex double field is used; 
    613         otherwise the arbitrary precision (but slow) complex field is 
    614         used. 
     616        If prec is 53 (the default), then the complex double field is 
     617        used; otherwise the arbitrary precision (but slow) complex 
     618        field is used. 
    615619         
    616620        INPUT: 
    617621         
     
    653657        P = self.number_field().complex_embeddings(prec)[i] 
    654658        return abs(P(self)) 
    655659 
     660    def abs_non_arch(self, P, prec=None): 
     661        r""" 
     662        Return the non-archimedean absolute value of this element with 
     663        respect to the prime `P`, to the given precision. 
     664         
     665        INPUT: 
     666         
     667        -  ``P`` - a prime ideal of the parent of self 
     668         
     669        - ``prec`` (int) -- desired floating point precision (default: 
     670          default RealField precision). 
     671 
     672        OUTPUT: 
     673 
     674        (real) the non-archimedean absolute value of this element with 
     675        respect to the prime `P`, to the given precision.  This is the 
     676        normalised absolute value, so that the underlying prime number 
     677        `p` has absolute value `1/p`. 
     678 
     679         
     680        EXAMPLES:: 
     681 
     682            sage: K.<a> = NumberField(x^2+5) 
     683            sage: [1/K(2).abs_non_arch(P) for P in K.primes_above(2)] 
     684            [2.00000000000000] 
     685            sage: [1/K(3).abs_non_arch(P) for P in K.primes_above(3)] 
     686            [3.00000000000000, 3.00000000000000] 
     687            sage: [1/K(5).abs_non_arch(P) for P in K.primes_above(5)] 
     688            [5.00000000000000] 
     689 
     690        A relative example:: 
     691 
     692            sage: L.<b> = K.extension(x^2-5) 
     693            sage: [b.abs_non_arch(P) for P in L.primes_above(b)] 
     694            [0.447213595499958, 0.447213595499958] 
     695        """ 
     696        from sage.rings.real_mpfr import RealField 
     697        if prec is None: 
     698            R = RealField() 
     699        else: 
     700            R = RealField(prec) 
     701 
     702        if self.is_zero(): 
     703            return R.zero_element() 
     704        val = self.valuation(P) 
     705        nP = P.residue_class_degree()*P.absolute_ramification_index() 
     706        return R(P.absolute_norm()) ** (-R(val) / R(nP)) 
     707 
    656708    def coordinates_in_terms_of_powers(self): 
    657709        r""" 
    658710        Let `\alpha` be self. Return a Python function that takes 
     
    16031655            sage: a.multiplicative_order() 
    16041656            +Infinity 
    16051657 
    1606         An example in a relative extension: 
     1658        An example in a relative extension:: 
    16071659     
    16081660            sage: K.<a, b> = NumberField([x^2 + x + 1, x^2 - 3]) 
    16091661            sage: z = (a - 1)*b/3 
     
    19872039            1 
    19882040            sage: type(b.valuation(P)) 
    19892041            <type 'sage.rings.integer.Integer'> 
     2042 
     2043        The function can be applied to elements in relative number fields:: 
     2044 
     2045            sage: L.<b> = K.extension(x^2 - 3) 
     2046            sage: [L(6).valuation(P) for P in L.primes_above(6)] 
     2047            [4, 2, 2] 
    19902048        """ 
    19912049        from number_field_ideal import is_NumberFieldIdeal 
    19922050        from sage.rings.infinity import infinity 
     
    20022060            return infinity 
    20032061        return Integer_sage(self.number_field()._pari_().elementval(self._pari_(), P._pari_prime)) 
    20042062 
     2063    def local_height(self, P, prec=None, weighted=False): 
     2064        r""" 
     2065        Returns the local height of self at a given prime ideal `P`. 
     2066         
     2067        INPUT: 
     2068         
     2069         
     2070        -  ``P`` - a prime ideal of the parent of self 
     2071         
     2072        - ``prec`` (int) -- desired floating point precision (defult: 
     2073          default RealField precision). 
     2074 
     2075        - ``weighted`` (bool, default False) -- if True, apply local 
     2076          degree weighting. 
     2077 
     2078        OUTPUT: 
     2079 
     2080        (real) The local height of this number field element at the 
     2081        place `P`.  If ``weighted`` is True, this is multiplied by the 
     2082        local degree (as required for global heights). 
     2083         
     2084        EXAMPLES:: 
     2085         
     2086            sage: R.<x> = QQ[] 
     2087            sage: K.<a> = NumberField(x^4+3*x^2-17) 
     2088            sage: P = K.ideal(61).factor()[0][0] 
     2089            sage: b = 1/(a^2 + 30) 
     2090            sage: b.local_height(P) 
     2091            4.11087386417331 
     2092            sage: b.local_height(P, weighted=True) 
     2093            8.22174772834662 
     2094            sage: b.local_height(P, 200) 
     2095            4.1108738641733112487513891034256147463156817430812610629374 
     2096            sage: (b^2).local_height(P) 
     2097            8.22174772834662 
     2098            sage: (b^-1).local_height(P) 
     2099            0.000000000000000 
     2100 
     2101        A relative example:: 
     2102 
     2103            sage: PK.<y> = K[] 
     2104            sage: L.<c> = NumberField(y^2 + a) 
     2105            sage: L(1/4).local_height(L.ideal(2, c-a+1)) 
     2106            1.38629436111989 
     2107        """ 
     2108        if self.valuation(P) >= 0: ## includes the case self=0 
     2109            from sage.rings.real_mpfr import RealField 
     2110            if prec is None: 
     2111                return RealField().zero_element() 
     2112            else: 
     2113                return RealField(prec).zero_element() 
     2114        ht = self.abs_non_arch(P,prec).log() 
     2115        if not weighted: 
     2116            return ht 
     2117        nP = P.residue_class_degree()*P.absolute_ramification_index() 
     2118        return nP*ht 
     2119 
     2120    def local_height_arch(self, i, prec=None, weighted=False): 
     2121        r""" 
     2122        Returns the local height of self at the `i`'th infinite place. 
     2123         
     2124        INPUT: 
     2125         
     2126         
     2127        - ``i`` (int) - an integer in ``range(r+s)`` where `(r,s)` is the 
     2128           signature of the parent field (so `n=r+2s` is the degree). 
     2129         
     2130        - ``prec`` (int) -- desired floating point precision (default: 
     2131          default RealField precision). 
     2132 
     2133        - ``weighted`` (bool, default False) -- if True, apply local 
     2134          degree weighting, i.e. double the value for complex places. 
     2135 
     2136        OUTPUT: 
     2137 
     2138        (real) The archimedean local height of this number field 
     2139        element at the `i`'th infinite place.  If ``weighted`` is 
     2140        True, this is multiplied by the local degree (as required for 
     2141        global heights), i.e. 1 for real places and 2 for complex 
     2142        places. 
     2143         
     2144        EXAMPLES:: 
     2145         
     2146            sage: R.<x> = QQ[] 
     2147            sage: K.<a> = NumberField(x^4+3*x^2-17) 
     2148            sage: [p.codomain() for p in K.places()] 
     2149            [Real Field with 106 bits of precision, 
     2150            Real Field with 106 bits of precision, 
     2151            Complex Field with 53 bits of precision] 
     2152            sage: [a.local_height_arch(i) for i in range(3)] 
     2153            [0.5301924545717755083366563897519, 
     2154            0.5301924545717755083366563897519, 
     2155            0.886414217456333] 
     2156            sage: [a.local_height_arch(i, weighted=True) for i in range(3)] 
     2157            [0.5301924545717755083366563897519, 
     2158            0.5301924545717755083366563897519, 
     2159            1.77282843491267] 
     2160     
     2161        A relative example:: 
     2162 
     2163            sage: L.<b, c> = NumberFieldTower([x^2 - 5, x^3 + x + 3]) 
     2164            sage: [(b + c).local_height_arch(i) for i in range(4)] 
     2165            [1.238223390757884911842206617439, 
     2166            0.02240347229957875780769746914391, 
     2167            0.780028961749618, 
     2168            1.16048938497298] 
     2169        """ 
     2170        K = self.number_field() 
     2171        emb = K.places(prec=prec)[i] 
     2172        a = emb(self).abs() 
     2173        Kv = emb.codomain() 
     2174        if a <= Kv.one_element(): 
     2175            return Kv.zero_element() 
     2176        ht = a.log() 
     2177        from sage.rings.real_mpfr import is_RealField 
     2178        if weighted and not is_RealField(Kv): 
     2179            ht*=2 
     2180        return ht 
     2181 
     2182    def global_height_non_arch(self, prec=None): 
     2183        """ 
     2184        Returns the total non-archimedean component of the height of self. 
     2185         
     2186        INPUT: 
     2187         
     2188        - ``prec`` (int) -- desired floating point precision (defult: 
     2189          default RealField precision). 
     2190 
     2191        OUTPUT: 
     2192 
     2193        (real) The total non-archimedean component of the height of 
     2194        this number field element; that is, the sum of the local 
     2195        heights at all finite places, weighted by the local degrees. 
     2196 
     2197        ALGORITHM: 
     2198 
     2199        An alternative formula is `\log(d)` where `d` is the norm of 
     2200        the denominator ideal; this is used to avoid factorization. 
     2201         
     2202        EXAMPLES:: 
     2203         
     2204            sage: R.<x> = QQ[] 
     2205            sage: K.<a> = NumberField(x^4+3*x^2-17) 
     2206            sage: b = a/6 
     2207            sage: b.global_height_non_arch() 
     2208            7.16703787691222 
     2209             
     2210        Check that this is equal to the sum of the non-archimedean 
     2211        local heights:: 
     2212 
     2213            sage: [b.local_height(P) for P in b.support()] 
     2214            [0.000000000000000, 0.693147180559945, 1.09861228866811, 1.09861228866811] 
     2215            sage: [b.local_height(P, weighted=True) for P in b.support()] 
     2216            [0.000000000000000, 2.77258872223978, 2.19722457733622, 2.19722457733622] 
     2217            sage: sum([b.local_height(P,weighted=True) for P in b.support()]) 
     2218            7.16703787691222 
     2219     
     2220        A relative example:: 
     2221 
     2222            sage: PK.<y> = K[] 
     2223            sage: L.<c> = NumberField(y^2 + a) 
     2224            sage: (c/10).global_height_non_arch() 
     2225            18.4206807439524 
     2226        """ 
     2227        from sage.rings.real_mpfr import RealField 
     2228        if prec is None: 
     2229            R = RealField() 
     2230        else: 
     2231            R = RealField(prec) 
     2232        if self.is_zero(): 
     2233            return R.zero_element() 
     2234        return R(self.denominator_ideal().absolute_norm()).log() 
     2235 
     2236    def global_height_arch(self, prec=None): 
     2237        """ 
     2238        Returns the total archimedean component of the height of self. 
     2239         
     2240        INPUT: 
     2241         
     2242        - ``prec`` (int) -- desired floating point precision (defult: 
     2243          default RealField precision). 
     2244 
     2245        OUTPUT: 
     2246 
     2247        (real) The total archimedean component of the height of 
     2248        this number field element; that is, the sum of the local 
     2249        heights at all infinite places. 
     2250         
     2251        EXAMPLES:: 
     2252         
     2253            sage: R.<x> = QQ[] 
     2254            sage: K.<a> = NumberField(x^4+3*x^2-17) 
     2255            sage: b = a/2 
     2256            sage: b.global_height_arch() 
     2257            0.38653407379277... 
     2258        """ 
     2259        r,s = self.number_field().signature() 
     2260        hts = [self.local_height_arch(i, prec, weighted=True) for i in range(r+s)] 
     2261        return sum(hts, hts[0].parent().zero_element()) 
     2262 
     2263    def global_height(self, prec=None): 
     2264        """ 
     2265        Returns the absolute logarithmic height of this number field element. 
     2266         
     2267        INPUT: 
     2268         
     2269        - ``prec`` (int) -- desired floating point precision (defult: 
     2270          default RealField precision). 
     2271 
     2272        OUTPUT: 
     2273 
     2274        (real) The absolute logarithmic height of this number field 
     2275        element; that is, the sum of the local heights at all finite 
     2276        and infinite places, with the contributions from the infinite 
     2277        places scaled by the degree to make the result independent of 
     2278        the parent field. 
     2279         
     2280        EXAMPLES:: 
     2281         
     2282            sage: R.<x> = QQ[] 
     2283            sage: K.<a> = NumberField(x^4+3*x^2-17) 
     2284            sage: b = a/2 
     2285            sage: b.global_height() 
     2286            2.869222240687... 
     2287            sage: b.global_height(prec=200) 
     2288            2.8692222406879748488543678846959454765968722137813736080066 
     2289 
     2290        The global height of an algebraic number is absolute, i.e. it 
     2291        does not depend on th parent field:: 
     2292 
     2293            sage: QQ(6).global_height() 
     2294            1.79175946922805 
     2295            sage: K(6).global_height() 
     2296            1.79175946922805 
     2297 
     2298            sage: L.<b> = NumberField((a^2).minpoly()) 
     2299            sage: L.degree() 
     2300            2 
     2301            sage: b.global_height() # element of L (degree 2 field) 
     2302            1.41660667202811 
     2303            sage: (a^2).global_height() # element of K (degree 4 field) 
     2304            1.41660667202811 
     2305        """ 
     2306        return self.global_height_non_arch(prec)+self.global_height_arch(prec)/self.number_field().absolute_degree() 
     2307 
     2308    def numerator_ideal(self): 
     2309        """ 
     2310        Return the numerator ideal of this number field element. 
     2311 
     2312        .. note:: 
     2313 
     2314           A ValueError will be saised if this function is called on 
     2315           0. 
     2316 
     2317        .. note:: 
     2318 
     2319           See also ``denominator_ideal()`` 
     2320         
     2321        OUTPUT: 
     2322 
     2323        (integral ideal) The numerator ideal `N` of this element, 
     2324        where for a non-zero number field element `a`, the principal 
     2325        ideal generated by `a` has the form `N/D` where `N` and `D` 
     2326        are coprime integral ideals.  An error is raised if the 
     2327        element is zero. 
     2328         
     2329        EXAMPLES:: 
     2330         
     2331            sage: K.<a> = NumberField(x^2+5) 
     2332            sage: b = (1+a)/2 
     2333            sage: b.norm() 
     2334            3/2 
     2335            sage: N = b.numerator_ideal(); N 
     2336            Fractional ideal (3, a + 1) 
     2337            sage: N.norm() 
     2338            3 
     2339            sage: (1/b).numerator_ideal() 
     2340            Fractional ideal (2, a + 1) 
     2341         
     2342        TESTS: 
     2343 
     2344        Undefined for 0:: 
     2345         
     2346            sage: K(0).numerator_ideal() 
     2347            Traceback (most recent call last): 
     2348            ... 
     2349            ValueError: numerator ideal of 0 is not defined. 
     2350        """ 
     2351        if self.is_zero(): 
     2352            raise ValueError, "numerator ideal of 0 is not defined." 
     2353        return self.number_field().ideal(self).numerator() 
     2354 
     2355    def denominator_ideal(self): 
     2356        """ 
     2357        Return the denominator ideal of this number field element. 
     2358 
     2359        .. note:: 
     2360 
     2361           A ValueError will be saised if this function is called on 
     2362           0. 
     2363 
     2364        .. note:: 
     2365 
     2366           See also ``numerator_ideal()`` 
     2367         
     2368        OUTPUT: 
     2369 
     2370        (integral ideal) The denominator ideal `D` of this element, 
     2371        where for a non-zero number field element `a`, the principal 
     2372        ideal generated by `a` has the form `N/D` where `N` and `D` 
     2373        are coprime integral ideals.  An error is raised if the 
     2374        element is zero. 
     2375         
     2376        EXAMPLES:: 
     2377         
     2378            sage: K.<a> = NumberField(x^2+5) 
     2379            sage: b = (1+a)/2 
     2380            sage: b.norm() 
     2381            3/2 
     2382            sage: D = b.denominator_ideal(); D 
     2383            Fractional ideal (2, a + 1) 
     2384            sage: D.norm() 
     2385            2 
     2386            sage: (1/b).denominator_ideal() 
     2387            Fractional ideal (3, a + 1) 
     2388         
     2389        TESTS: 
     2390 
     2391        Undefined for 0:: 
     2392         
     2393            sage: K(0).denominator_ideal() 
     2394            Traceback (most recent call last): 
     2395            ... 
     2396            ValueError: denominator ideal of 0 is not defined. 
     2397        """ 
     2398        if self.is_zero(): 
     2399            raise ValueError, "denominator ideal of 0 is not defined." 
     2400        return self.number_field().ideal(self).denominator() 
     2401 
    20052402    def support(self): 
    20062403        """ 
    20072404        Return the support of this number field element. 
  • sage/rings/number_field/number_field_ideal_rel.py

    diff -r 21c6c829ea32 -r 19b9a330ac46 sage/rings/number_field/number_field_ideal_rel.py
    a b  
    456456            sage: K.ideal(13).is_prime() 
    457457            False 
    458458        """ 
    459         return self.absolute_ideal().is_prime() 
     459        try: 
     460            return self._pari_prime is not None 
     461        except AttributeError: 
     462            abs_ideal = self.absolute_ideal() 
     463            _ = abs_ideal.is_prime() 
     464            self._pari_prime = abs_ideal._pari_prime 
     465            return self._pari_prime is not None 
    460466 
    461467    def is_integral(self): 
    462468        """ 
  • sage/rings/number_field/number_field_rel.py

    diff -r 21c6c829ea32 -r 19b9a330ac46 sage/rings/number_field/number_field_rel.py
    a b  
    15681568                                        check=False, universe=self.Hom(self)) 
    15691569        return self.__automorphisms 
    15701570 
     1571    def places(self, all_complex=False, prec=None): 
     1572        """ 
     1573        Return the collection of all infinite places of self. 
     1574 
     1575        By default, this returns the set of real places as 
     1576        homomorphisms into RIF first, followed by a choice of one of 
     1577        each pair of complex conjugate homomorphisms into CIF. 
     1578         
     1579        On the other hand, if prec is not None, we simply return places 
     1580        into RealField(prec) and ComplexField(prec) (or RDF, CDF if 
     1581        prec=53). 
     1582         
     1583        There is an optional flag all_complex, which defaults to False. If 
     1584        all_complex is True, then the real embeddings are returned as 
     1585        embeddings into CIF instead of RIF. 
     1586         
     1587        EXAMPLES:: 
     1588 
     1589            sage: L.<b, c> = NumberFieldTower([x^2 - 5, x^3 + x + 3]) 
     1590            sage: L.places() 
     1591            [Relative number field morphism: 
     1592            From: Number Field in b with defining polynomial x^2 - 5 over its base field 
     1593            To:   Real Field with 106 bits of precision 
     1594            Defn: b |--> -2.236067977499789696409173668937 
     1595            c |--> -1.213411662762229634132131377426, 
     1596            Relative number field morphism: 
     1597            From: Number Field in b with defining polynomial x^2 - 5 over its base field 
     1598            To:   Real Field with 106 bits of precision 
     1599            Defn: b |--> 2.236067977499789696411548005367 
     1600            c |--> -1.213411662762229634130492421800, 
     1601            Relative number field morphism: 
     1602            From: Number Field in b with defining polynomial x^2 - 5 over its base field 
     1603            To:   Complex Field with 53 bits of precision 
     1604            Defn: b |--> -2.23606797749979 - 2.22044604925031e-16*I 
     1605            c |--> 0.606705831381115 - 1.45061224918844*I, 
     1606            Relative number field morphism: 
     1607            From: Number Field in b with defining polynomial x^2 - 5 over its base field 
     1608            To:   Complex Field with 53 bits of precision 
     1609            Defn: b |--> 2.23606797749979 - 4.44089209850063e-16*I 
     1610            c |--> 0.606705831381115 - 1.45061224918844*I]         
     1611        """ 
     1612        L = self.absolute_field('a') 
     1613        pl = L.places(all_complex, prec) 
     1614        return [self.hom(p, p.codomain()) for p in pl] 
     1615         
     1616 
    15711617    def absolute_different(self): 
    15721618        r""" 
    15731619        Return the absolute different of this relative number field as 
  • sage/rings/rational.pyx

    diff -r 21c6c829ea32 -r 19b9a330ac46 sage/rings/rational.pyx
    a b  
    1919  multiplicative_order, is_one; optimized __nonzero__ ; documented: 
    2020  lcm,gcd 
    2121 
     22- John Cremona (2009-05-15): added support for local and global 
     23  logarithmic heights. 
     24 
    2225TESTS:: 
    2326 
    2427    sage: a = -2/3 
     
    729732            return Rational(1) 
    730733 
    731734    def valuation(self, p): 
    732         """ 
    733         Return the largest power of p that divides self. 
     735        r""" 
     736        Return the power of ``p`` in the factorization of self. 
    734737         
    735738        INPUT: 
    736739         
    737740         
    738741        -  ``p`` - a prime number 
     742 
     743        OUTPUT: 
     744 
     745        (integer or infinity) Infinity if self is zero, otherwise the 
     746        (positive or negative) integer `e` such that self = `m*p^e` 
     747        with `m` coprime to `p`. 
     748 
     749        .. note:: 
     750 
     751           See also ``val_unit()`` which returns the pair `(e,m)`. 
    739752         
    740          
    741         EXAMPLES:: 
     753        Examples:: 
    742754         
    743755            sage: x = -5/9 
    744756            sage: x.valuation(5) 
     
    757769        """ 
    758770        return self.numerator().valuation(p) - self.denominator().valuation(p) 
    759771         
     772    def local_height(self, p, prec=None): 
     773        r""" 
     774        Returns the local height of this rational number at the prime `p`. 
     775         
     776        INPUT: 
     777         
     778         
     779        -  ``p`` - a prime number 
     780         
     781        - ``prec`` (int) -- desired floating point precision (defult: 
     782          default RealField precision). 
     783 
     784        OUTPUT: 
     785 
     786        (real) The local height of this rational number at the 
     787        prime `p`. 
     788         
     789        EXAMPLES:: 
     790         
     791            sage: a = QQ(25/6) 
     792            sage: a.local_height(2) 
     793            0.693147180559945 
     794            sage: a.local_height(3) 
     795            1.09861228866811 
     796            sage: a.local_height(5) 
     797            0.000000000000000 
     798        """ 
     799        from sage.rings.real_mpfr import RealField 
     800        if prec is None: 
     801            R = RealField() 
     802        else: 
     803            R = RealField(prec) 
     804        if self.is_zero(): 
     805            return R.zero_element() 
     806        val = self.valuation(p) 
     807        if val >= 0: 
     808            return R.zero_element() 
     809        return -val * R(p).log() 
     810 
     811    def local_height_arch(self, prec=None): 
     812        r""" 
     813        Returns the archimdean local height of this rational number at the infinite place. 
     814         
     815        INPUT: 
     816         
     817         
     818        - ``prec`` (int) -- desired floating point precision (defult: 
     819          default RealField precision). 
     820 
     821        OUTPUT: 
     822 
     823        (real) The local height of this rational number `x` at the 
     824        unique infinite place of `\QQ`, which is 
     825        `\max(\log(|x|),0)`. 
     826         
     827        EXAMPLES:: 
     828         
     829            sage: a = QQ(6/25) 
     830            sage: a.local_height_arch() 
     831            0.000000000000000 
     832            sage: (1/a).local_height_arch() 
     833            1.42711635564015 
     834            sage: (1/a).local_height_arch(100) 
     835            1.4271163556401457483890413081 
     836        """ 
     837        from sage.rings.real_mpfr import RealField 
     838        if prec is None: 
     839            R = RealField() 
     840        else: 
     841            R = RealField(prec) 
     842        a = self.abs() 
     843        if a <= 1: 
     844            return R.zero_element() 
     845        return R(a).log() 
     846 
     847    def global_height_non_arch(self, prec=None): 
     848        r""" 
     849        Returns the total non-archimedean component of the height of this rational number. 
     850         
     851        INPUT: 
     852         
     853        - ``prec`` (int) -- desired floating point precision (defult: 
     854          default RealField precision). 
     855 
     856        OUTPUT: 
     857 
     858        (real) The total non-archimedean component of the height of 
     859        this rational number. 
     860 
     861        ALGORITHM: 
     862         
     863        This is the sum of the local heights at all primes `p`, which 
     864        may be computed without fatorization as the log of the 
     865        denominator. 
     866         
     867        EXAMPLES:: 
     868         
     869            sage: a = QQ(5/6) 
     870            sage: a.support() 
     871            [2, 3, 5] 
     872            sage: a.global_height_non_arch() 
     873            1.79175946922805 
     874            sage: [a.local_height(p) for p in a.support()] 
     875            [0.693147180559945, 1.09861228866811, 0.000000000000000] 
     876            sage: sum([a.local_height(p) for p in a.support()]) 
     877            1.79175946922805 
     878        """ 
     879        from sage.rings.real_mpfr import RealField 
     880        if prec is None: 
     881            R = RealField() 
     882        else: 
     883            R = RealField(prec) 
     884        d = self.denominator() 
     885        if d.is_one(): 
     886            return R.zero_element() 
     887        return R(d).log() 
     888 
     889    def global_height_arch(self, prec=None): 
     890        r""" 
     891        Returns the total archimedean component of the height of this rational number. 
     892         
     893        INPUT: 
     894         
     895        - ``prec`` (int) -- desired floating point precision (defult: 
     896          default RealField precision). 
     897 
     898        OUTPUT: 
     899 
     900        (real) The total archimedean component of the height of 
     901        this rational number. 
     902 
     903        ALGORITHM: 
     904         
     905        Since `\QQ` has only one infinite place this is just the value 
     906        of the local height at that place.  This separate function is 
     907        included for compatibility with number fields. 
     908         
     909        EXAMPLES:: 
     910         
     911            sage: a = QQ(6/25) 
     912            sage: a.global_height_arch() 
     913            0.000000000000000 
     914            sage: (1/a).global_height_arch() 
     915            1.42711635564015 
     916            sage: (1/a).global_height_arch(100) 
     917            1.4271163556401457483890413081 
     918        """ 
     919        return self.local_height_arch(prec) 
     920 
     921    def global_height(self, prec=None): 
     922        r""" 
     923        Returns the absolute logarithmic height of this rational number. 
     924         
     925        INPUT: 
     926         
     927        - ``prec`` (int) -- desired floating point precision (defult: 
     928          default RealField precision). 
     929 
     930        OUTPUT: 
     931 
     932        (real) The absolute logarithmic height of this rational 
     933        number. 
     934 
     935        ALGORITHM: 
     936         
     937        The height is the sum of the total archimedean and 
     938        non-archimedean components, which is equal to 
     939        `\max(\log(n),\log(d))` where `n,d` are the numerator and 
     940        denominator of the rational number. 
     941         
     942        EXAMPLES:: 
     943         
     944            sage: a = QQ(6/25) 
     945            sage: a.global_height_arch() + a.global_height_non_arch() 
     946            3.21887582486820 
     947            sage: a.global_height() 
     948            3.21887582486820 
     949            sage: (1/a).global_height() 
     950            3.21887582486820 
     951            sage: QQ(0).global_height() 
     952            0.000000000000000 
     953            sage: QQ(1).global_height() 
     954            0.000000000000000 
     955        """ 
     956        from sage.rings.real_mpfr import RealField 
     957        if prec is None: 
     958            R = RealField() 
     959        else: 
     960            R = RealField(prec) 
     961        return R(max(self.numerator().abs(),self.denominator())).log() 
     962 
    760963    def is_square(self): 
    761964        """ 
    762965        Return whether or not this rational number is a square. 
     
    12291432        Return the period of the repeating part of the decimal expansion of 
    12301433        this rational number. 
    12311434         
    1232         ALGORITHM: When a rational number `n/d` with 
    1233         `(n,d)==1` is expanded, the period begins after `s` 
    1234         terms and has length `t`, where `s` and `t` 
    1235         are the smallest numbers satisfying 
    1236         `10^s=10^(s+t) (mod d)`. When `d` is coprime to 10, 
    1237         this becomes a purely periodic decimal with 
    1238         `10^t=1 (mod d)`. (Lehmer 1941 and Mathworld). 
     1435        ALGORITHM: When a rational number `n/d` with `(n,d)==1` is 
     1436        expanded, the period begins after `s` terms and has length 
     1437        `t`, where `s` and `t` are the smallest numbers satisfying 
     1438        `10^s=10^{s+t} (\mod d)`. In general if `d=2^a3^bm` where `m` 
     1439        is coprime to 10, then `s=\max(a,b)` and `t` is the order of 
     1440        10 modulo `d`. 
    12391441         
    12401442        EXAMPLES:: 
    12411443         
     
    12591461        """ 
    12601462        cdef unsigned int alpha, beta 
    12611463        d = self.denominator() 
    1262         alpha = d.valuation(2) 
    1263         beta = d.valuation(5) 
    1264         P = d.parent() 
    1265         if alpha > 0 or beta > 0: 
    1266             d = d//(P(2)**alpha * P(5)**beta) 
     1464        alpha, d = d.val_unit(2) 
     1465        beta, d  = d.val_unit(5) 
    12671466        from sage.rings.integer_mod import Mod 
    1268         a = Mod(P(10),d) 
    1269         return a.multiplicative_order() 
     1467        return Mod(ZZ(10),d).multiplicative_order() 
    12701468 
    12711469    def nth_root(self, int n): 
    12721470        r""" 
     
    23602558        AUTHORS: 
    23612559 
    23622560        - Naqi Jaffery (2006-03-05): examples 
     2561 
     2562        .. note:: 
     2563 
     2564           For the logarithmic height, use ``global_height()``. 
     2565 
    23632566        """ 
    23642567        x = abs(self.numer()) 
    23652568        if x > self.denom():