Changeset 5243:9c6972d1055c
- Timestamp:
- 07/01/07 14:15:09 (6 years ago)
- Branch:
- default
- Location:
- sage
- Files:
-
- 4 edited
-
functions/all.py (modified) (1 diff)
-
functions/constants.py (modified) (2 diffs)
-
functions/transcendental.py (modified) (2 diffs)
-
rings/arith.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/functions/all.py
r4270 r5243 6 6 from transcendental import (exponential_integral_1, 7 7 gamma, gamma_inc, incomplete_gamma, 8 zeta, zeta_symmetric) 8 zeta, zeta_symmetric, 9 Li, 10 prime_pi, 11 number_of_primes) 9 12 10 13 #from elementary import (cosine, sine, exponential, -
sage/functions/constants.py
r4858 r5243 465 465 pi = Pi() 466 466 467 python_complex_i = complex(0,1) 467 468 468 469 class I_class(Constant): … … 526 527 return C.gen() 527 528 529 def __complex__(self): 530 return python_complex_i 531 528 532 def _real_double_(self, R): 529 533 raise TypeError -
sage/functions/transcendental.py
r4055 r5243 19 19 20 20 import sage.libs.pari.all 21 pari = sage.libs.pari.all.pari 21 from sage.libs.pari.all import pari, PariError 22 22 import sage.rings.complex_field as complex_field 23 23 import sage.rings.real_mpfr as real_field 24 import sage.rings.real_double as real_double 24 25 import sage.rings.complex_number 25 26 from sage.rings.all import is_RealNumber, RealField, is_ComplexNumber, ComplexField 26 from sage.gsl.integration import numerical_integral 27 28 from sage.rings.all import (is_RealNumber, RealField, 29 is_ComplexNumber, ComplexField, 30 ZZ) 27 31 28 32 def __prep_num(x): … … 219 223 # """ 220 224 # return real_field.RealField(prec).pi() 225 226 227 228 229 def 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 300 import math 301 def _one_over_log(t): 302 return 1/math.log(t) 303 304 def 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 330 number_of_primes = prime_pi 331 332 -
sage/rings/arith.py
r4880 r5243 207 207 else: 208 208 raise ValueError, "invalid choice of algorithm" 209 210 def Li(x):211 r"""212 Return value of the function Li(x), which is by definition213 $$214 \int_2^{x} dt / \log(t).215 $$216 217 The function Li(x) is an approximation for the number218 of primes up to $x$. In fact, the famous Riemann219 Hypothesis is equivalent to the statement that for220 $x \geq 2.01$ we have221 $$222 |\pi(x) - Li(x)| \leq \sqrt{x} \log(x).223 $$224 For ``small'' $x$, $Li(x)$ is always slightly bigger than225 $\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 double237 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.12043572467243 100 25 29.080977804244 1000 168 176.56449421245 10000 1229 1245.09205212246 100000 9592 9628.76383727247 1000000 78498 78626.5039957248 """249 from real_double import RDF250 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 4260 sage: prime_pi(100)261 25262 sage: prime_pi(1000)263 168264 sage: prime_pi(100000)265 9592266 sage: prime_pi(0.5)267 0268 sage: prime_pi(-10)269 0270 """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_pi280 209 281 210 … … 2207 2136 return lower 2208 2137 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 32219 sage: number_of_partitions(10)2220 422221 sage: number_of_partitions(40)2222 373382223 sage: number_of_partitions(100)2224 1905692922225 sage: number_of_partitions(-5)2226 02227 sage: number_of_partitions(0)2228 12229 """2230 ZZ = integer_ring.ZZ2231 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$ use2238 \code{number_of_partitions(n)}.2239 2240 INPUT:2241 n -- int2242 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 tuple2253 if n == 0:2254 yield ( )2255 return2256 # modify the partitions of n-1 to form the partitions of n2257 for p in partitions(n-1):2258 yield (1,) + p2259 if p and (len(p) < 2 or p[1] > p[0]):2260 yield (p[0] + 1,) + p[1:]2261 2262 2138 2263 2139
Note: See TracChangeset
for help on using the changeset viewer.
