Changeset 5243:9c6972d1055c


Ignore:
Timestamp:
07/01/07 14:15:09 (6 years ago)
Author:
William Stein <wstein@…>
Branch:
default
Message:

Reorganize and fix a bunch of basic arithmetic; make Li much much faster.

Location:
sage
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sage/functions/all.py

    r4270 r5243  
    66from transcendental import (exponential_integral_1, 
    77                            gamma, gamma_inc, incomplete_gamma, 
    8                             zeta, zeta_symmetric) 
     8                            zeta, zeta_symmetric, 
     9                            Li, 
     10                            prime_pi,  
     11                            number_of_primes) 
    912 
    1013#from elementary import (cosine, sine, exponential, 
  • sage/functions/constants.py

    r4858 r5243  
    465465pi = Pi() 
    466466 
     467python_complex_i = complex(0,1) 
    467468 
    468469class I_class(Constant): 
     
    526527        return C.gen() 
    527528 
     529    def __complex__(self): 
     530        return python_complex_i 
     531 
    528532    def _real_double_(self, R): 
    529533        raise TypeError         
  • sage/functions/transcendental.py

    r4055 r5243  
    1919 
    2020import  sage.libs.pari.all 
    21 pari = sage.libs.pari.all.pari 
     21from sage.libs.pari.all import pari, PariError 
    2222import sage.rings.complex_field as complex_field 
    2323import sage.rings.real_mpfr as real_field 
     24import sage.rings.real_double as real_double 
    2425import sage.rings.complex_number 
    25  
    26 from sage.rings.all import is_RealNumber, RealField, is_ComplexNumber, ComplexField 
     26from sage.gsl.integration import numerical_integral 
     27 
     28from sage.rings.all import (is_RealNumber, RealField, 
     29                            is_ComplexNumber, ComplexField, 
     30                            ZZ) 
    2731 
    2832def __prep_num(x): 
     
    219223#    """ 
    220224#   return real_field.RealField(prec).pi() 
     225 
     226 
     227 
     228 
     229def Li(x, eps_rel=None, err_bound=False): 
     230    r""" 
     231    Return value of the function Li(x) as a real double field element. 
     232 
     233    This is the function 
     234    $$ 
     235       \int_2^{x} dt / \log(t). 
     236    $$ 
     237     
     238    The function Li(x) is an approximation for the number 
     239    of primes up to $x$.  In fact, the famous Riemann 
     240    Hypothesis is equivalent to the statement that for 
     241    $x \geq 2.01$ we have 
     242    $$ 
     243        |\pi(x) - Li(x)| \leq \sqrt{x} \log(x). 
     244    $$ 
     245    For ``small'' $x$, $Li(x)$ is always slightly bigger than 
     246    $\pi(x)$.  However it is a theorem that there are (very large, 
     247    e.g., around $10^{316}$) values of $x$ so that $\pi(x) > Li(x)$. 
     248    See ``A new bound for the smallest x with $\pi(x) > li(x)$'', 
     249    Bays and Hudson, Mathematics of Computation, 69 (2000) 1285--1296. 
     250     
     251    ALGORITHM: Computed numerically using GSL. 
     252 
     253    INPUT: 
     254        x -- a real number >= 2. 
     255 
     256    OUTPUT: 
     257        x -- a real double 
     258 
     259    EXAMPLES: 
     260        sage: Li(2) 
     261        0.0 
     262        sage: Li(5) 
     263        2.58942452992 
     264        sage: Li(1000) 
     265        176.56449421 
     266        sage: Li(10^5) 
     267        9628.76383727 
     268        sage: prime_pi(10^5) 
     269        9592 
     270        sage: Li(1) 
     271        Traceback (most recent call last): 
     272        ... 
     273        ValueError: Li only defined for x at least 2.     
     274 
     275        sage: for n in range(1,7): 
     276        ...    print '%-10s%-10s%-20s'%(10^n, prime_pi(10^n), Li(10^n)) 
     277        10        4         5.12043572467        
     278        100       25        29.080977804         
     279        1000      168       176.56449421         
     280        10000     1229      1245.09205212        
     281        100000    9592      9628.76383727        
     282        1000000   78498     78626.5039957            
     283    """ 
     284    x = float(x) 
     285    if x < 2: 
     286        raise ValueError, "Li only defined for x at least 2." 
     287    if eps_rel: 
     288        ans = numerical_integral(_one_over_log, 2, float(x), 
     289                             eps_rel=eps_rel) 
     290    else: 
     291        ans = numerical_integral(_one_over_log, 2, float(x)) 
     292    if err_bound: 
     293        return real_double.RDF(ans[0]), ans[1] 
     294    else: 
     295        return real_double.RDF(ans[0]) 
     296    # Old PARI version -- much much slower 
     297    #x = RDF(x) 
     298    #return RDF(gp('intnum(t=2,%s,1/log(t))'%x)) 
     299 
     300import math 
     301def _one_over_log(t): 
     302    return 1/math.log(t)     
     303 
     304def prime_pi(x): 
     305    """ 
     306    Return the number of primes $\leq x$. 
     307 
     308    EXAMPLES: 
     309        sage: prime_pi(7) 
     310        4 
     311        sage: prime_pi(100) 
     312        25 
     313        sage: prime_pi(1000) 
     314        168 
     315        sage: prime_pi(100000) 
     316        9592 
     317        sage: prime_pi(0.5) 
     318        0 
     319        sage: prime_pi(-10) 
     320        0 
     321    """ 
     322    if x < 2: 
     323        return ZZ(0) 
     324    try: 
     325        return ZZ(pari(x).primepi()) 
     326    except PariError: 
     327        pari.init_primes(pari(x)+1) 
     328        return ZZ(pari(x).primepi()) 
     329 
     330number_of_primes = prime_pi 
     331 
     332 
  • sage/rings/arith.py

    r4880 r5243  
    207207    else: 
    208208        raise ValueError, "invalid choice of algorithm" 
    209  
    210 def Li(x): 
    211     r""" 
    212     Return value of the function Li(x), which is by definition 
    213     $$ 
    214        \int_2^{x} dt / \log(t). 
    215     $$ 
    216      
    217     The function Li(x) is an approximation for the number 
    218     of primes up to $x$.  In fact, the famous Riemann 
    219     Hypothesis is equivalent to the statement that for 
    220     $x \geq 2.01$ we have 
    221     $$ 
    222         |\pi(x) - Li(x)| \leq \sqrt{x} \log(x). 
    223     $$ 
    224     For ``small'' $x$, $Li(x)$ is always slightly bigger than 
    225     $\pi(x)$.  However it is a theorem that there are (very large, 
    226     e.g., around $10^{316}$) values of $x$ so that $\pi(x) > Li(x)$. 
    227     See ``A new bound for the smallest x with $\pi(x) > li(x)$'', 
    228     Bays and Hudson, Mathematics of Computation, 69 (2000) 1285--1296. 
    229      
    230     ALGORITHM: Computed numerically using PARI. 
    231  
    232     INPUT: 
    233         x -- a real number >= 2. 
    234  
    235     OUTPUT: 
    236         x -- a real double 
    237  
    238     EXAMPLES: 
    239         sage: pari.init_primes(10^6)   # needed to compute prime_pi(10^6) 
    240         sage: for n in range(1,7): 
    241         ...    print '%-10s%-10s%-20s'%(10^n, prime_pi(10^n), Li(10^n)) 
    242         10        4         5.12043572467        
    243         100       25        29.080977804         
    244         1000      168       176.56449421         
    245         10000     1229      1245.09205212        
    246         100000    9592      9628.76383727        
    247         1000000   78498     78626.5039957            
    248     """ 
    249     from real_double import RDF 
    250     x = RDF(x) 
    251     return RDF(gp('intnum(t=2,%s,1/log(t))'%x))     
    252  
    253 def prime_pi(x): 
    254     """ 
    255     Return the number of primes $\leq x$. 
    256  
    257     EXAMPLES: 
    258         sage: prime_pi(7) 
    259         4 
    260         sage: prime_pi(100) 
    261         25 
    262         sage: prime_pi(1000) 
    263         168 
    264         sage: prime_pi(100000) 
    265         9592 
    266         sage: prime_pi(0.5) 
    267         0 
    268         sage: prime_pi(-10) 
    269         0 
    270     """ 
    271     if x < 2: 
    272         return integer_ring.ZZ(0) 
    273     try: 
    274         return integer_ring.ZZ(pari(x).primepi()) 
    275     except PariError: 
    276         pari.init_primes(pari(x)+1) 
    277         return integer_ring.ZZ(pari(x).primepi()) 
    278  
    279 number_of_primes = prime_pi 
    280209 
    281210 
     
    22072136                return lower 
    22082137            upper = mediant 
    2209  
    2210 def number_of_partitions(n): 
    2211     """ 
    2212     Return the number of partitions of the integer $n$. 
    2213  
    2214     To compute all the partitions of $n$ use \code{partitions(n)}. 
    2215  
    2216     EXAMPLES: 
    2217         sage: number_of_partitions(3) 
    2218         3 
    2219         sage: number_of_partitions(10) 
    2220         42 
    2221         sage: number_of_partitions(40) 
    2222         37338 
    2223         sage: number_of_partitions(100) 
    2224         190569292 
    2225         sage: number_of_partitions(-5) 
    2226         0 
    2227         sage: number_of_partitions(0) 
    2228         1 
    2229     """ 
    2230     ZZ = integer_ring.ZZ 
    2231     return ZZ(pari(ZZ(n)).numbpart()) 
    2232  
    2233 def partitions(n): 
    2234     """ 
    2235     Generator of all the partitions of the integer $n$. 
    2236  
    2237     To compute the number of partitions of $n$ use 
    2238     \code{number_of_partitions(n)}. 
    2239  
    2240     INPUT: 
    2241         n -- int 
    2242  
    2243     EXAMPLES: 
    2244         >> partitions(3) 
    2245         <generator object at 0xab3b3eac> 
    2246         sage: list(partitions(3)) 
    2247         [(1, 1, 1), (1, 2), (3,)] 
    2248  
    2249     AUTHOR: David Eppstein, Jan Van lent, George Yoshida; Python Cookbook 2, Recipe 19.16. 
    2250     """ 
    2251     n == integer_ring.ZZ(n) 
    2252     # base case of the recursion: zero is the sum of the empty tuple 
    2253     if n == 0: 
    2254         yield ( ) 
    2255         return 
    2256     # modify the partitions of n-1 to form the partitions of n 
    2257     for p in partitions(n-1): 
    2258         yield (1,) + p 
    2259         if p and (len(p) < 2 or p[1] > p[0]): 
    2260             yield (p[0] + 1,) + p[1:] 
    2261  
    22622138 
    22632139 
Note: See TracChangeset for help on using the changeset viewer.