gammazeta.py 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  1. """
  2. -----------------------------------------------------------------------
  3. This module implements gamma- and zeta-related functions:
  4. * Bernoulli numbers
  5. * Factorials
  6. * The gamma function
  7. * Polygamma functions
  8. * Harmonic numbers
  9. * The Riemann zeta function
  10. * Constants related to these functions
  11. -----------------------------------------------------------------------
  12. """
  13. import math
  14. import sys
  15. from .backend import xrange
  16. from .backend import MPZ, MPZ_ZERO, MPZ_ONE, MPZ_THREE, gmpy
  17. from .libintmath import list_primes, ifac, ifac2, moebius
  18. from .libmpf import (\
  19. round_floor, round_ceiling, round_down, round_up,
  20. round_nearest, round_fast,
  21. lshift, sqrt_fixed, isqrt_fast,
  22. fzero, fone, fnone, fhalf, ftwo, finf, fninf, fnan,
  23. from_int, to_int, to_fixed, from_man_exp, from_rational,
  24. mpf_pos, mpf_neg, mpf_abs, mpf_add, mpf_sub,
  25. mpf_mul, mpf_mul_int, mpf_div, mpf_sqrt, mpf_pow_int,
  26. mpf_rdiv_int,
  27. mpf_perturb, mpf_le, mpf_lt, mpf_gt, mpf_shift,
  28. negative_rnd, reciprocal_rnd,
  29. bitcount, to_float, mpf_floor, mpf_sign, ComplexResult
  30. )
  31. from .libelefun import (\
  32. constant_memo,
  33. def_mpf_constant,
  34. mpf_pi, pi_fixed, ln2_fixed, log_int_fixed, mpf_ln2,
  35. mpf_exp, mpf_log, mpf_pow, mpf_cosh,
  36. mpf_cos_sin, mpf_cosh_sinh, mpf_cos_sin_pi, mpf_cos_pi, mpf_sin_pi,
  37. ln_sqrt2pi_fixed, mpf_ln_sqrt2pi, sqrtpi_fixed, mpf_sqrtpi,
  38. cos_sin_fixed, exp_fixed
  39. )
  40. from .libmpc import (\
  41. mpc_zero, mpc_one, mpc_half, mpc_two,
  42. mpc_abs, mpc_shift, mpc_pos, mpc_neg,
  43. mpc_add, mpc_sub, mpc_mul, mpc_div,
  44. mpc_add_mpf, mpc_mul_mpf, mpc_div_mpf, mpc_mpf_div,
  45. mpc_mul_int, mpc_pow_int,
  46. mpc_log, mpc_exp, mpc_pow,
  47. mpc_cos_pi, mpc_sin_pi,
  48. mpc_reciprocal, mpc_square,
  49. mpc_sub_mpf
  50. )
  51. # Catalan's constant is computed using Lupas's rapidly convergent series
  52. # (listed on http://mathworld.wolfram.com/CatalansConstant.html)
  53. # oo
  54. # ___ n-1 8n 2 3 2
  55. # 1 \ (-1) 2 (40n - 24n + 3) [(2n)!] (n!)
  56. # K = --- ) -----------------------------------------
  57. # 64 /___ 3 2
  58. # n (2n-1) [(4n)!]
  59. # n = 1
  60. @constant_memo
  61. def catalan_fixed(prec):
  62. prec = prec + 20
  63. a = one = MPZ_ONE << prec
  64. s, t, n = 0, 1, 1
  65. while t:
  66. a *= 32 * n**3 * (2*n-1)
  67. a //= (3-16*n+16*n**2)**2
  68. t = a * (-1)**(n-1) * (40*n**2-24*n+3) // (n**3 * (2*n-1))
  69. s += t
  70. n += 1
  71. return s >> (20 + 6)
  72. # Khinchin's constant is relatively difficult to compute. Here
  73. # we use the rational zeta series
  74. # oo 2*n-1
  75. # ___ ___
  76. # \ ` zeta(2*n)-1 \ ` (-1)^(k+1)
  77. # log(K)*log(2) = ) ------------ ) ----------
  78. # /___. n /___. k
  79. # n = 1 k = 1
  80. # which adds half a digit per term. The essential trick for achieving
  81. # reasonable efficiency is to recycle both the values of the zeta
  82. # function (essentially Bernoulli numbers) and the partial terms of
  83. # the inner sum.
  84. # An alternative might be to use K = 2*exp[1/log(2) X] where
  85. # / 1 1 [ pi*x*(1-x^2) ]
  86. # X = | ------ log [ ------------ ].
  87. # / 0 x(1+x) [ sin(pi*x) ]
  88. # and integrate numerically. In practice, this seems to be slightly
  89. # slower than the zeta series at high precision.
  90. @constant_memo
  91. def khinchin_fixed(prec):
  92. wp = int(prec + prec**0.5 + 15)
  93. s = MPZ_ZERO
  94. fac = from_int(4)
  95. t = ONE = MPZ_ONE << wp
  96. pi = mpf_pi(wp)
  97. pipow = twopi2 = mpf_shift(mpf_mul(pi, pi, wp), 2)
  98. n = 1
  99. while 1:
  100. zeta2n = mpf_abs(mpf_bernoulli(2*n, wp))
  101. zeta2n = mpf_mul(zeta2n, pipow, wp)
  102. zeta2n = mpf_div(zeta2n, fac, wp)
  103. zeta2n = to_fixed(zeta2n, wp)
  104. term = (((zeta2n - ONE) * t) // n) >> wp
  105. if term < 100:
  106. break
  107. #if not n % 10:
  108. # print n, math.log(int(abs(term)))
  109. s += term
  110. t += ONE//(2*n+1) - ONE//(2*n)
  111. n += 1
  112. fac = mpf_mul_int(fac, (2*n)*(2*n-1), wp)
  113. pipow = mpf_mul(pipow, twopi2, wp)
  114. s = (s << wp) // ln2_fixed(wp)
  115. K = mpf_exp(from_man_exp(s, -wp), wp)
  116. K = to_fixed(K, prec)
  117. return K
  118. # Glaisher's constant is defined as A = exp(1/2 - zeta'(-1)).
  119. # One way to compute it would be to perform direct numerical
  120. # differentiation, but computing arbitrary Riemann zeta function
  121. # values at high precision is expensive. We instead use the formula
  122. # A = exp((6 (-zeta'(2))/pi^2 + log 2 pi + gamma)/12)
  123. # and compute zeta'(2) from the series representation
  124. # oo
  125. # ___
  126. # \ log k
  127. # -zeta'(2) = ) -----
  128. # /___ 2
  129. # k
  130. # k = 2
  131. # This series converges exceptionally slowly, but can be accelerated
  132. # using Euler-Maclaurin formula. The important insight is that the
  133. # E-M integral can be done in closed form and that the high order
  134. # are given by
  135. # n / \
  136. # d | log x | a + b log x
  137. # --- | ----- | = -----------
  138. # n | 2 | 2 + n
  139. # dx \ x / x
  140. # where a and b are integers given by a simple recurrence. Note
  141. # that just one logarithm is needed. However, lots of integer
  142. # logarithms are required for the initial summation.
  143. # This algorithm could possibly be turned into a faster algorithm
  144. # for general evaluation of zeta(s) or zeta'(s); this should be
  145. # looked into.
  146. @constant_memo
  147. def glaisher_fixed(prec):
  148. wp = prec + 30
  149. # Number of direct terms to sum before applying the Euler-Maclaurin
  150. # formula to the tail. TODO: choose more intelligently
  151. N = int(0.33*prec + 5)
  152. ONE = MPZ_ONE << wp
  153. # Euler-Maclaurin, step 1: sum log(k)/k**2 for k from 2 to N-1
  154. s = MPZ_ZERO
  155. for k in range(2, N):
  156. #print k, N
  157. s += log_int_fixed(k, wp) // k**2
  158. logN = log_int_fixed(N, wp)
  159. #logN = to_fixed(mpf_log(from_int(N), wp+20), wp)
  160. # E-M step 2: integral of log(x)/x**2 from N to inf
  161. s += (ONE + logN) // N
  162. # E-M step 3: endpoint correction term f(N)/2
  163. s += logN // (N**2 * 2)
  164. # E-M step 4: the series of derivatives
  165. pN = N**3
  166. a = 1
  167. b = -2
  168. j = 3
  169. fac = from_int(2)
  170. k = 1
  171. while 1:
  172. # D(2*k-1) * B(2*k) / fac(2*k) [D(n) = nth derivative]
  173. D = ((a << wp) + b*logN) // pN
  174. D = from_man_exp(D, -wp)
  175. B = mpf_bernoulli(2*k, wp)
  176. term = mpf_mul(B, D, wp)
  177. term = mpf_div(term, fac, wp)
  178. term = to_fixed(term, wp)
  179. if abs(term) < 100:
  180. break
  181. #if not k % 10:
  182. # print k, math.log(int(abs(term)), 10)
  183. s -= term
  184. # Advance derivative twice
  185. a, b, pN, j = b-a*j, -j*b, pN*N, j+1
  186. a, b, pN, j = b-a*j, -j*b, pN*N, j+1
  187. k += 1
  188. fac = mpf_mul_int(fac, (2*k)*(2*k-1), wp)
  189. # A = exp((6*s/pi**2 + log(2*pi) + euler)/12)
  190. pi = pi_fixed(wp)
  191. s *= 6
  192. s = (s << wp) // (pi**2 >> wp)
  193. s += euler_fixed(wp)
  194. s += to_fixed(mpf_log(from_man_exp(2*pi, -wp), wp), wp)
  195. s //= 12
  196. A = mpf_exp(from_man_exp(s, -wp), wp)
  197. return to_fixed(A, prec)
  198. # Apery's constant can be computed using the very rapidly convergent
  199. # series
  200. # oo
  201. # ___ 2 10
  202. # \ n 205 n + 250 n + 77 (n!)
  203. # zeta(3) = ) (-1) ------------------- ----------
  204. # /___ 64 5
  205. # n = 0 ((2n+1)!)
  206. @constant_memo
  207. def apery_fixed(prec):
  208. prec += 20
  209. d = MPZ_ONE << prec
  210. term = MPZ(77) << prec
  211. n = 1
  212. s = MPZ_ZERO
  213. while term:
  214. s += term
  215. d *= (n**10)
  216. d //= (((2*n+1)**5) * (2*n)**5)
  217. term = (-1)**n * (205*(n**2) + 250*n + 77) * d
  218. n += 1
  219. return s >> (20 + 6)
  220. """
  221. Euler's constant (gamma) is computed using the Brent-McMillan formula,
  222. gamma ~= I(n)/J(n) - log(n), where
  223. I(n) = sum_{k=0,1,2,...} (n**k / k!)**2 * H(k)
  224. J(n) = sum_{k=0,1,2,...} (n**k / k!)**2
  225. H(k) = 1 + 1/2 + 1/3 + ... + 1/k
  226. The error is bounded by O(exp(-4n)). Choosing n to be a power
  227. of two, 2**p, the logarithm becomes particularly easy to calculate.[1]
  228. We use the formulation of Algorithm 3.9 in [2] to make the summation
  229. more efficient.
  230. Reference:
  231. [1] Xavier Gourdon & Pascal Sebah, The Euler constant: gamma
  232. http://numbers.computation.free.fr/Constants/Gamma/gamma.pdf
  233. [2] [BorweinBailey]_
  234. """
  235. @constant_memo
  236. def euler_fixed(prec):
  237. extra = 30
  238. prec += extra
  239. # choose p such that exp(-4*(2**p)) < 2**-n
  240. p = int(math.log((prec/4) * math.log(2), 2)) + 1
  241. n = 2**p
  242. A = U = -p*ln2_fixed(prec)
  243. B = V = MPZ_ONE << prec
  244. k = 1
  245. while 1:
  246. B = B*n**2//k**2
  247. A = (A*n**2//k + B)//k
  248. U += A
  249. V += B
  250. if max(abs(A), abs(B)) < 100:
  251. break
  252. k += 1
  253. return (U<<(prec-extra))//V
  254. # Use zeta accelerated formulas for the Mertens and twin
  255. # prime constants; see
  256. # http://mathworld.wolfram.com/MertensConstant.html
  257. # http://mathworld.wolfram.com/TwinPrimesConstant.html
  258. @constant_memo
  259. def mertens_fixed(prec):
  260. wp = prec + 20
  261. m = 2
  262. s = mpf_euler(wp)
  263. while 1:
  264. t = mpf_zeta_int(m, wp)
  265. if t == fone:
  266. break
  267. t = mpf_log(t, wp)
  268. t = mpf_mul_int(t, moebius(m), wp)
  269. t = mpf_div(t, from_int(m), wp)
  270. s = mpf_add(s, t)
  271. m += 1
  272. return to_fixed(s, prec)
  273. @constant_memo
  274. def twinprime_fixed(prec):
  275. def I(n):
  276. return sum(moebius(d)<<(n//d) for d in xrange(1,n+1) if not n%d)//n
  277. wp = 2*prec + 30
  278. res = fone
  279. primes = [from_rational(1,p,wp) for p in [2,3,5,7]]
  280. ppowers = [mpf_mul(p,p,wp) for p in primes]
  281. n = 2
  282. while 1:
  283. a = mpf_zeta_int(n, wp)
  284. for i in range(4):
  285. a = mpf_mul(a, mpf_sub(fone, ppowers[i]), wp)
  286. ppowers[i] = mpf_mul(ppowers[i], primes[i], wp)
  287. a = mpf_pow_int(a, -I(n), wp)
  288. if mpf_pos(a, prec+10, 'n') == fone:
  289. break
  290. #from libmpf import to_str
  291. #print n, to_str(mpf_sub(fone, a), 6)
  292. res = mpf_mul(res, a, wp)
  293. n += 1
  294. res = mpf_mul(res, from_int(3*15*35), wp)
  295. res = mpf_div(res, from_int(4*16*36), wp)
  296. return to_fixed(res, prec)
  297. mpf_euler = def_mpf_constant(euler_fixed)
  298. mpf_apery = def_mpf_constant(apery_fixed)
  299. mpf_khinchin = def_mpf_constant(khinchin_fixed)
  300. mpf_glaisher = def_mpf_constant(glaisher_fixed)
  301. mpf_catalan = def_mpf_constant(catalan_fixed)
  302. mpf_mertens = def_mpf_constant(mertens_fixed)
  303. mpf_twinprime = def_mpf_constant(twinprime_fixed)
  304. #-----------------------------------------------------------------------#
  305. # #
  306. # Bernoulli numbers #
  307. # #
  308. #-----------------------------------------------------------------------#
  309. MAX_BERNOULLI_CACHE = 3000
  310. r"""
  311. Small Bernoulli numbers and factorials are used in numerous summations,
  312. so it is critical for speed that sequential computation is fast and that
  313. values are cached up to a fairly high threshold.
  314. On the other hand, we also want to support fast computation of isolated
  315. large numbers. Currently, no such acceleration is provided for integer
  316. factorials (though it is for large floating-point factorials, which are
  317. computed via gamma if the precision is low enough).
  318. For sequential computation of Bernoulli numbers, we use Ramanujan's formula
  319. / n + 3 \
  320. B = (A(n) - S(n)) / | |
  321. n \ n /
  322. where A(n) = (n+3)/3 when n = 0 or 2 (mod 6), A(n) = -(n+3)/6
  323. when n = 4 (mod 6), and
  324. [n/6]
  325. ___
  326. \ / n + 3 \
  327. S(n) = ) | | * B
  328. /___ \ n - 6*k / n-6*k
  329. k = 1
  330. For isolated large Bernoulli numbers, we use the Riemann zeta function
  331. to calculate a numerical value for B_n. The von Staudt-Clausen theorem
  332. can then be used to optionally find the exact value of the
  333. numerator and denominator.
  334. """
  335. bernoulli_cache = {}
  336. f3 = from_int(3)
  337. f6 = from_int(6)
  338. def bernoulli_size(n):
  339. """Accurately estimate the size of B_n (even n > 2 only)"""
  340. lgn = math.log(n,2)
  341. return int(2.326 + 0.5*lgn + n*(lgn - 4.094))
  342. BERNOULLI_PREC_CUTOFF = bernoulli_size(MAX_BERNOULLI_CACHE)
  343. def mpf_bernoulli(n, prec, rnd=None):
  344. """Computation of Bernoulli numbers (numerically)"""
  345. if n < 2:
  346. if n < 0:
  347. raise ValueError("Bernoulli numbers only defined for n >= 0")
  348. if n == 0:
  349. return fone
  350. if n == 1:
  351. return mpf_neg(fhalf)
  352. # For odd n > 1, the Bernoulli numbers are zero
  353. if n & 1:
  354. return fzero
  355. # If precision is extremely high, we can save time by computing
  356. # the Bernoulli number at a lower precision that is sufficient to
  357. # obtain the exact fraction, round to the exact fraction, and
  358. # convert the fraction back to an mpf value at the original precision
  359. if prec > BERNOULLI_PREC_CUTOFF and prec > bernoulli_size(n)*1.1 + 1000:
  360. p, q = bernfrac(n)
  361. return from_rational(p, q, prec, rnd or round_floor)
  362. if n > MAX_BERNOULLI_CACHE:
  363. return mpf_bernoulli_huge(n, prec, rnd)
  364. wp = prec + 30
  365. # Reuse nearby precisions
  366. wp += 32 - (prec & 31)
  367. cached = bernoulli_cache.get(wp)
  368. if cached:
  369. numbers, state = cached
  370. if n in numbers:
  371. if not rnd:
  372. return numbers[n]
  373. return mpf_pos(numbers[n], prec, rnd)
  374. m, bin, bin1 = state
  375. if n - m > 10:
  376. return mpf_bernoulli_huge(n, prec, rnd)
  377. else:
  378. if n > 10:
  379. return mpf_bernoulli_huge(n, prec, rnd)
  380. numbers = {0:fone}
  381. m, bin, bin1 = state = [2, MPZ(10), MPZ_ONE]
  382. bernoulli_cache[wp] = (numbers, state)
  383. while m <= n:
  384. #print m
  385. case = m % 6
  386. # Accurately estimate size of B_m so we can use
  387. # fixed point math without using too much precision
  388. szbm = bernoulli_size(m)
  389. s = 0
  390. sexp = max(0, szbm) - wp
  391. if m < 6:
  392. a = MPZ_ZERO
  393. else:
  394. a = bin1
  395. for j in xrange(1, m//6+1):
  396. usign, uman, uexp, ubc = u = numbers[m-6*j]
  397. if usign:
  398. uman = -uman
  399. s += lshift(a*uman, uexp-sexp)
  400. # Update inner binomial coefficient
  401. j6 = 6*j
  402. a *= ((m-5-j6)*(m-4-j6)*(m-3-j6)*(m-2-j6)*(m-1-j6)*(m-j6))
  403. a //= ((4+j6)*(5+j6)*(6+j6)*(7+j6)*(8+j6)*(9+j6))
  404. if case == 0: b = mpf_rdiv_int(m+3, f3, wp)
  405. if case == 2: b = mpf_rdiv_int(m+3, f3, wp)
  406. if case == 4: b = mpf_rdiv_int(-m-3, f6, wp)
  407. s = from_man_exp(s, sexp, wp)
  408. b = mpf_div(mpf_sub(b, s, wp), from_int(bin), wp)
  409. numbers[m] = b
  410. m += 2
  411. # Update outer binomial coefficient
  412. bin = bin * ((m+2)*(m+3)) // (m*(m-1))
  413. if m > 6:
  414. bin1 = bin1 * ((2+m)*(3+m)) // ((m-7)*(m-6))
  415. state[:] = [m, bin, bin1]
  416. return numbers[n]
  417. def mpf_bernoulli_huge(n, prec, rnd=None):
  418. wp = prec + 10
  419. piprec = wp + int(math.log(n,2))
  420. v = mpf_gamma_int(n+1, wp)
  421. v = mpf_mul(v, mpf_zeta_int(n, wp), wp)
  422. v = mpf_mul(v, mpf_pow_int(mpf_pi(piprec), -n, wp))
  423. v = mpf_shift(v, 1-n)
  424. if not n & 3:
  425. v = mpf_neg(v)
  426. return mpf_pos(v, prec, rnd or round_fast)
  427. def bernfrac(n):
  428. r"""
  429. Returns a tuple of integers `(p, q)` such that `p/q = B_n` exactly,
  430. where `B_n` denotes the `n`-th Bernoulli number. The fraction is
  431. always reduced to lowest terms. Note that for `n > 1` and `n` odd,
  432. `B_n = 0`, and `(0, 1)` is returned.
  433. **Examples**
  434. The first few Bernoulli numbers are exactly::
  435. >>> from mpmath import *
  436. >>> for n in range(15):
  437. ... p, q = bernfrac(n)
  438. ... print("%s %s/%s" % (n, p, q))
  439. ...
  440. 0 1/1
  441. 1 -1/2
  442. 2 1/6
  443. 3 0/1
  444. 4 -1/30
  445. 5 0/1
  446. 6 1/42
  447. 7 0/1
  448. 8 -1/30
  449. 9 0/1
  450. 10 5/66
  451. 11 0/1
  452. 12 -691/2730
  453. 13 0/1
  454. 14 7/6
  455. This function works for arbitrarily large `n`::
  456. >>> p, q = bernfrac(10**4)
  457. >>> print(q)
  458. 2338224387510
  459. >>> print(len(str(p)))
  460. 27692
  461. >>> mp.dps = 15
  462. >>> print(mpf(p) / q)
  463. -9.04942396360948e+27677
  464. >>> print(bernoulli(10**4))
  465. -9.04942396360948e+27677
  466. .. note ::
  467. :func:`~mpmath.bernoulli` computes a floating-point approximation
  468. directly, without computing the exact fraction first.
  469. This is much faster for large `n`.
  470. **Algorithm**
  471. :func:`~mpmath.bernfrac` works by computing the value of `B_n` numerically
  472. and then using the von Staudt-Clausen theorem [1] to reconstruct
  473. the exact fraction. For large `n`, this is significantly faster than
  474. computing `B_1, B_2, \ldots, B_2` recursively with exact arithmetic.
  475. The implementation has been tested for `n = 10^m` up to `m = 6`.
  476. In practice, :func:`~mpmath.bernfrac` appears to be about three times
  477. slower than the specialized program calcbn.exe [2]
  478. **References**
  479. 1. MathWorld, von Staudt-Clausen Theorem:
  480. http://mathworld.wolfram.com/vonStaudt-ClausenTheorem.html
  481. 2. The Bernoulli Number Page:
  482. http://www.bernoulli.org/
  483. """
  484. n = int(n)
  485. if n < 3:
  486. return [(1, 1), (-1, 2), (1, 6)][n]
  487. if n & 1:
  488. return (0, 1)
  489. q = 1
  490. for k in list_primes(n+1):
  491. if not (n % (k-1)):
  492. q *= k
  493. prec = bernoulli_size(n) + int(math.log(q,2)) + 20
  494. b = mpf_bernoulli(n, prec)
  495. p = mpf_mul(b, from_int(q))
  496. pint = to_int(p, round_nearest)
  497. return (pint, q)
  498. #-----------------------------------------------------------------------#
  499. # #
  500. # Polygamma functions #
  501. # #
  502. #-----------------------------------------------------------------------#
  503. r"""
  504. For all polygamma (psi) functions, we use the Euler-Maclaurin summation
  505. formula. It looks slightly different in the m = 0 and m > 0 cases.
  506. For m = 0, we have
  507. oo
  508. ___ B
  509. (0) 1 \ 2 k -2 k
  510. psi (z) ~ log z + --- - ) ------ z
  511. 2 z /___ (2 k)!
  512. k = 1
  513. Experiment shows that the minimum term of the asymptotic series
  514. reaches 2^(-p) when Re(z) > 0.11*p. So we simply use the recurrence
  515. for psi (equivalent, in fact, to summing to the first few terms
  516. directly before applying E-M) to obtain z large enough.
  517. Since, very crudely, log z ~= 1 for Re(z) > 1, we can use
  518. fixed-point arithmetic (if z is extremely large, log(z) itself
  519. is a sufficient approximation, so we can stop there already).
  520. For Re(z) << 0, we could use recurrence, but this is of course
  521. inefficient for large negative z, so there we use the
  522. reflection formula instead.
  523. For m > 0, we have
  524. N - 1
  525. ___
  526. ~~~(m) [ \ 1 ] 1 1
  527. psi (z) ~ [ ) -------- ] + ---------- + -------- +
  528. [ /___ m+1 ] m+1 m
  529. k = 1 (z+k) ] 2 (z+N) m (z+N)
  530. oo
  531. ___ B
  532. \ 2 k (m+1) (m+2) ... (m+2k-1)
  533. + ) ------ ------------------------
  534. /___ (2 k)! m + 2 k
  535. k = 1 (z+N)
  536. where ~~~ denotes the function rescaled by 1/((-1)^(m+1) m!).
  537. Here again N is chosen to make z+N large enough for the minimum
  538. term in the last series to become smaller than eps.
  539. TODO: the current estimation of N for m > 0 is *very suboptimal*.
  540. TODO: implement the reflection formula for m > 0, Re(z) << 0.
  541. It is generally a combination of multiple cotangents. Need to
  542. figure out a reasonably simple way to generate these formulas
  543. on the fly.
  544. TODO: maybe use exact algorithms to compute psi for integral
  545. and certain rational arguments, as this can be much more
  546. efficient. (On the other hand, the availability of these
  547. special values provides a convenient way to test the general
  548. algorithm.)
  549. """
  550. # Harmonic numbers are just shifted digamma functions
  551. # We should calculate these exactly when x is an integer
  552. # and when doing so is faster.
  553. def mpf_harmonic(x, prec, rnd):
  554. if x in (fzero, fnan, finf):
  555. return x
  556. a = mpf_psi0(mpf_add(fone, x, prec+5), prec)
  557. return mpf_add(a, mpf_euler(prec+5, rnd), prec, rnd)
  558. def mpc_harmonic(z, prec, rnd):
  559. if z[1] == fzero:
  560. return (mpf_harmonic(z[0], prec, rnd), fzero)
  561. a = mpc_psi0(mpc_add_mpf(z, fone, prec+5), prec)
  562. return mpc_add_mpf(a, mpf_euler(prec+5, rnd), prec, rnd)
  563. def mpf_psi0(x, prec, rnd=round_fast):
  564. """
  565. Computation of the digamma function (psi function of order 0)
  566. of a real argument.
  567. """
  568. sign, man, exp, bc = x
  569. wp = prec + 10
  570. if not man:
  571. if x == finf: return x
  572. if x == fninf or x == fnan: return fnan
  573. if x == fzero or (exp >= 0 and sign):
  574. raise ValueError("polygamma pole")
  575. # Near 0 -- fixed-point arithmetic becomes bad
  576. if exp+bc < -5:
  577. v = mpf_psi0(mpf_add(x, fone, prec, rnd), prec, rnd)
  578. return mpf_sub(v, mpf_div(fone, x, wp, rnd), prec, rnd)
  579. # Reflection formula
  580. if sign and exp+bc > 3:
  581. c, s = mpf_cos_sin_pi(x, wp)
  582. q = mpf_mul(mpf_div(c, s, wp), mpf_pi(wp), wp)
  583. p = mpf_psi0(mpf_sub(fone, x, wp), wp)
  584. return mpf_sub(p, q, prec, rnd)
  585. # The logarithmic term is accurate enough
  586. if (not sign) and bc + exp > wp:
  587. return mpf_log(mpf_sub(x, fone, wp), prec, rnd)
  588. # Initial recurrence to obtain a large enough x
  589. m = to_int(x)
  590. n = int(0.11*wp) + 2
  591. s = MPZ_ZERO
  592. x = to_fixed(x, wp)
  593. one = MPZ_ONE << wp
  594. if m < n:
  595. for k in xrange(m, n):
  596. s -= (one << wp) // x
  597. x += one
  598. x -= one
  599. # Logarithmic term
  600. s += to_fixed(mpf_log(from_man_exp(x, -wp, wp), wp), wp)
  601. # Endpoint term in Euler-Maclaurin expansion
  602. s += (one << wp) // (2*x)
  603. # Euler-Maclaurin remainder sum
  604. x2 = (x*x) >> wp
  605. t = one
  606. prev = 0
  607. k = 1
  608. while 1:
  609. t = (t*x2) >> wp
  610. bsign, bman, bexp, bbc = mpf_bernoulli(2*k, wp)
  611. offset = (bexp + 2*wp)
  612. if offset >= 0: term = (bman << offset) // (t*(2*k))
  613. else: term = (bman >> (-offset)) // (t*(2*k))
  614. if k & 1: s -= term
  615. else: s += term
  616. if k > 2 and term >= prev:
  617. break
  618. prev = term
  619. k += 1
  620. return from_man_exp(s, -wp, wp, rnd)
  621. def mpc_psi0(z, prec, rnd=round_fast):
  622. """
  623. Computation of the digamma function (psi function of order 0)
  624. of a complex argument.
  625. """
  626. re, im = z
  627. # Fall back to the real case
  628. if im == fzero:
  629. return (mpf_psi0(re, prec, rnd), fzero)
  630. wp = prec + 20
  631. sign, man, exp, bc = re
  632. # Reflection formula
  633. if sign and exp+bc > 3:
  634. c = mpc_cos_pi(z, wp)
  635. s = mpc_sin_pi(z, wp)
  636. q = mpc_mul_mpf(mpc_div(c, s, wp), mpf_pi(wp), wp)
  637. p = mpc_psi0(mpc_sub(mpc_one, z, wp), wp)
  638. return mpc_sub(p, q, prec, rnd)
  639. # Just the logarithmic term
  640. if (not sign) and bc + exp > wp:
  641. return mpc_log(mpc_sub(z, mpc_one, wp), prec, rnd)
  642. # Initial recurrence to obtain a large enough z
  643. w = to_int(re)
  644. n = int(0.11*wp) + 2
  645. s = mpc_zero
  646. if w < n:
  647. for k in xrange(w, n):
  648. s = mpc_sub(s, mpc_reciprocal(z, wp), wp)
  649. z = mpc_add_mpf(z, fone, wp)
  650. z = mpc_sub(z, mpc_one, wp)
  651. # Logarithmic and endpoint term
  652. s = mpc_add(s, mpc_log(z, wp), wp)
  653. s = mpc_add(s, mpc_div(mpc_half, z, wp), wp)
  654. # Euler-Maclaurin remainder sum
  655. z2 = mpc_square(z, wp)
  656. t = mpc_one
  657. prev = mpc_zero
  658. szprev = fzero
  659. k = 1
  660. eps = mpf_shift(fone, -wp+2)
  661. while 1:
  662. t = mpc_mul(t, z2, wp)
  663. bern = mpf_bernoulli(2*k, wp)
  664. term = mpc_mpf_div(bern, mpc_mul_int(t, 2*k, wp), wp)
  665. s = mpc_sub(s, term, wp)
  666. szterm = mpc_abs(term, 10)
  667. if k > 2 and (mpf_le(szterm, eps) or mpf_le(szprev, szterm)):
  668. break
  669. prev = term
  670. szprev = szterm
  671. k += 1
  672. return s
  673. # Currently unoptimized
  674. def mpf_psi(m, x, prec, rnd=round_fast):
  675. """
  676. Computation of the polygamma function of arbitrary integer order
  677. m >= 0, for a real argument x.
  678. """
  679. if m == 0:
  680. return mpf_psi0(x, prec, rnd=round_fast)
  681. return mpc_psi(m, (x, fzero), prec, rnd)[0]
  682. def mpc_psi(m, z, prec, rnd=round_fast):
  683. """
  684. Computation of the polygamma function of arbitrary integer order
  685. m >= 0, for a complex argument z.
  686. """
  687. if m == 0:
  688. return mpc_psi0(z, prec, rnd)
  689. re, im = z
  690. wp = prec + 20
  691. sign, man, exp, bc = re
  692. if not im[1]:
  693. if im in (finf, fninf, fnan):
  694. return (fnan, fnan)
  695. if not man:
  696. if re == finf and im == fzero:
  697. return (fzero, fzero)
  698. if re == fnan:
  699. return (fnan, fnan)
  700. # Recurrence
  701. w = to_int(re)
  702. n = int(0.4*wp + 4*m)
  703. s = mpc_zero
  704. if w < n:
  705. for k in xrange(w, n):
  706. t = mpc_pow_int(z, -m-1, wp)
  707. s = mpc_add(s, t, wp)
  708. z = mpc_add_mpf(z, fone, wp)
  709. zm = mpc_pow_int(z, -m, wp)
  710. z2 = mpc_pow_int(z, -2, wp)
  711. # 1/m*(z+N)^m
  712. integral_term = mpc_div_mpf(zm, from_int(m), wp)
  713. s = mpc_add(s, integral_term, wp)
  714. # 1/2*(z+N)^(-(m+1))
  715. s = mpc_add(s, mpc_mul_mpf(mpc_div(zm, z, wp), fhalf, wp), wp)
  716. a = m + 1
  717. b = 2
  718. k = 1
  719. # Important: we want to sum up to the *relative* error,
  720. # not the absolute error, because psi^(m)(z) might be tiny
  721. magn = mpc_abs(s, 10)
  722. magn = magn[2]+magn[3]
  723. eps = mpf_shift(fone, magn-wp+2)
  724. while 1:
  725. zm = mpc_mul(zm, z2, wp)
  726. bern = mpf_bernoulli(2*k, wp)
  727. scal = mpf_mul_int(bern, a, wp)
  728. scal = mpf_div(scal, from_int(b), wp)
  729. term = mpc_mul_mpf(zm, scal, wp)
  730. s = mpc_add(s, term, wp)
  731. szterm = mpc_abs(term, 10)
  732. if k > 2 and mpf_le(szterm, eps):
  733. break
  734. #print k, to_str(szterm, 10), to_str(eps, 10)
  735. a *= (m+2*k)*(m+2*k+1)
  736. b *= (2*k+1)*(2*k+2)
  737. k += 1
  738. # Scale and sign factor
  739. v = mpc_mul_mpf(s, mpf_gamma(from_int(m+1), wp), prec, rnd)
  740. if not (m & 1):
  741. v = mpf_neg(v[0]), mpf_neg(v[1])
  742. return v
  743. #-----------------------------------------------------------------------#
  744. # #
  745. # Riemann zeta function #
  746. # #
  747. #-----------------------------------------------------------------------#
  748. r"""
  749. We use zeta(s) = eta(s) / (1 - 2**(1-s)) and Borwein's approximation
  750. n-1
  751. ___ k
  752. -1 \ (-1) (d_k - d_n)
  753. eta(s) ~= ---- ) ------------------
  754. d_n /___ s
  755. k = 0 (k + 1)
  756. where
  757. k
  758. ___ i
  759. \ (n + i - 1)! 4
  760. d_k = n ) ---------------.
  761. /___ (n - i)! (2i)!
  762. i = 0
  763. If s = a + b*I, the absolute error for eta(s) is bounded by
  764. 3 (1 + 2|b|)
  765. ------------ * exp(|b| pi/2)
  766. n
  767. (3+sqrt(8))
  768. Disregarding the linear term, we have approximately,
  769. log(err) ~= log(exp(1.58*|b|)) - log(5.8**n)
  770. log(err) ~= 1.58*|b| - log(5.8)*n
  771. log(err) ~= 1.58*|b| - 1.76*n
  772. log2(err) ~= 2.28*|b| - 2.54*n
  773. So for p bits, we should choose n > (p + 2.28*|b|) / 2.54.
  774. References:
  775. -----------
  776. Peter Borwein, "An Efficient Algorithm for the Riemann Zeta Function"
  777. http://www.cecm.sfu.ca/personal/pborwein/PAPERS/P117.ps
  778. http://en.wikipedia.org/wiki/Dirichlet_eta_function
  779. """
  780. borwein_cache = {}
  781. def borwein_coefficients(n):
  782. if n in borwein_cache:
  783. return borwein_cache[n]
  784. ds = [MPZ_ZERO] * (n+1)
  785. d = MPZ_ONE
  786. s = ds[0] = MPZ_ONE
  787. for i in range(1, n+1):
  788. d = d * 4 * (n+i-1) * (n-i+1)
  789. d //= ((2*i) * ((2*i)-1))
  790. s += d
  791. ds[i] = s
  792. borwein_cache[n] = ds
  793. return ds
  794. ZETA_INT_CACHE_MAX_PREC = 1000
  795. zeta_int_cache = {}
  796. def mpf_zeta_int(s, prec, rnd=round_fast):
  797. """
  798. Optimized computation of zeta(s) for an integer s.
  799. """
  800. wp = prec + 20
  801. s = int(s)
  802. if s in zeta_int_cache and zeta_int_cache[s][0] >= wp:
  803. return mpf_pos(zeta_int_cache[s][1], prec, rnd)
  804. if s < 2:
  805. if s == 1:
  806. raise ValueError("zeta(1) pole")
  807. if not s:
  808. return mpf_neg(fhalf)
  809. return mpf_div(mpf_bernoulli(-s+1, wp), from_int(s-1), prec, rnd)
  810. # 2^-s term vanishes?
  811. if s >= wp:
  812. return mpf_perturb(fone, 0, prec, rnd)
  813. # 5^-s term vanishes?
  814. elif s >= wp*0.431:
  815. t = one = 1 << wp
  816. t += 1 << (wp - s)
  817. t += one // (MPZ_THREE ** s)
  818. t += 1 << max(0, wp - s*2)
  819. return from_man_exp(t, -wp, prec, rnd)
  820. else:
  821. # Fast enough to sum directly?
  822. # Even better, we use the Euler product (idea stolen from pari)
  823. m = (float(wp)/(s-1) + 1)
  824. if m < 30:
  825. needed_terms = int(2.0**m + 1)
  826. if needed_terms < int(wp/2.54 + 5) / 10:
  827. t = fone
  828. for k in list_primes(needed_terms):
  829. #print k, needed_terms
  830. powprec = int(wp - s*math.log(k,2))
  831. if powprec < 2:
  832. break
  833. a = mpf_sub(fone, mpf_pow_int(from_int(k), -s, powprec), wp)
  834. t = mpf_mul(t, a, wp)
  835. return mpf_div(fone, t, wp)
  836. # Use Borwein's algorithm
  837. n = int(wp/2.54 + 5)
  838. d = borwein_coefficients(n)
  839. t = MPZ_ZERO
  840. s = MPZ(s)
  841. for k in xrange(n):
  842. t += (((-1)**k * (d[k] - d[n])) << wp) // (k+1)**s
  843. t = (t << wp) // (-d[n])
  844. t = (t << wp) // ((1 << wp) - (1 << (wp+1-s)))
  845. if (s in zeta_int_cache and zeta_int_cache[s][0] < wp) or (s not in zeta_int_cache):
  846. zeta_int_cache[s] = (wp, from_man_exp(t, -wp-wp))
  847. return from_man_exp(t, -wp-wp, prec, rnd)
  848. def mpf_zeta(s, prec, rnd=round_fast, alt=0):
  849. sign, man, exp, bc = s
  850. if not man:
  851. if s == fzero:
  852. if alt:
  853. return fhalf
  854. else:
  855. return mpf_neg(fhalf)
  856. if s == finf:
  857. return fone
  858. return fnan
  859. wp = prec + 20
  860. # First term vanishes?
  861. if (not sign) and (exp + bc > (math.log(wp,2) + 2)):
  862. return mpf_perturb(fone, alt, prec, rnd)
  863. # Optimize for integer arguments
  864. elif exp >= 0:
  865. if alt:
  866. if s == fone:
  867. return mpf_ln2(prec, rnd)
  868. z = mpf_zeta_int(to_int(s), wp, negative_rnd[rnd])
  869. q = mpf_sub(fone, mpf_pow(ftwo, mpf_sub(fone, s, wp), wp), wp)
  870. return mpf_mul(z, q, prec, rnd)
  871. else:
  872. return mpf_zeta_int(to_int(s), prec, rnd)
  873. # Negative: use the reflection formula
  874. # Borwein only proves the accuracy bound for x >= 1/2. However, based on
  875. # tests, the accuracy without reflection is quite good even some distance
  876. # to the left of 1/2. XXX: verify this.
  877. if sign:
  878. # XXX: could use the separate refl. formula for Dirichlet eta
  879. if alt:
  880. q = mpf_sub(fone, mpf_pow(ftwo, mpf_sub(fone, s, wp), wp), wp)
  881. return mpf_mul(mpf_zeta(s, wp), q, prec, rnd)
  882. # XXX: -1 should be done exactly
  883. y = mpf_sub(fone, s, 10*wp)
  884. a = mpf_gamma(y, wp)
  885. b = mpf_zeta(y, wp)
  886. c = mpf_sin_pi(mpf_shift(s, -1), wp)
  887. wp2 = wp + max(0,exp+bc)
  888. pi = mpf_pi(wp+wp2)
  889. d = mpf_div(mpf_pow(mpf_shift(pi, 1), s, wp2), pi, wp2)
  890. return mpf_mul(a,mpf_mul(b,mpf_mul(c,d,wp),wp),prec,rnd)
  891. # Near pole
  892. r = mpf_sub(fone, s, wp)
  893. asign, aman, aexp, abc = mpf_abs(r)
  894. pole_dist = -2*(aexp+abc)
  895. if pole_dist > wp:
  896. if alt:
  897. return mpf_ln2(prec, rnd)
  898. else:
  899. q = mpf_neg(mpf_div(fone, r, wp))
  900. return mpf_add(q, mpf_euler(wp), prec, rnd)
  901. else:
  902. wp += max(0, pole_dist)
  903. t = MPZ_ZERO
  904. #wp += 16 - (prec & 15)
  905. # Use Borwein's algorithm
  906. n = int(wp/2.54 + 5)
  907. d = borwein_coefficients(n)
  908. t = MPZ_ZERO
  909. sf = to_fixed(s, wp)
  910. ln2 = ln2_fixed(wp)
  911. for k in xrange(n):
  912. u = (-sf*log_int_fixed(k+1, wp, ln2)) >> wp
  913. #esign, eman, eexp, ebc = mpf_exp(u, wp)
  914. #offset = eexp + wp
  915. #if offset >= 0:
  916. # w = ((d[k] - d[n]) * eman) << offset
  917. #else:
  918. # w = ((d[k] - d[n]) * eman) >> (-offset)
  919. eman = exp_fixed(u, wp, ln2)
  920. w = (d[k] - d[n]) * eman
  921. if k & 1:
  922. t -= w
  923. else:
  924. t += w
  925. t = t // (-d[n])
  926. t = from_man_exp(t, -wp, wp)
  927. if alt:
  928. return mpf_pos(t, prec, rnd)
  929. else:
  930. q = mpf_sub(fone, mpf_pow(ftwo, mpf_sub(fone, s, wp), wp), wp)
  931. return mpf_div(t, q, prec, rnd)
  932. def mpc_zeta(s, prec, rnd=round_fast, alt=0, force=False):
  933. re, im = s
  934. if im == fzero:
  935. return mpf_zeta(re, prec, rnd, alt), fzero
  936. # slow for large s
  937. if (not force) and mpf_gt(mpc_abs(s, 10), from_int(prec)):
  938. raise NotImplementedError
  939. wp = prec + 20
  940. # Near pole
  941. r = mpc_sub(mpc_one, s, wp)
  942. asign, aman, aexp, abc = mpc_abs(r, 10)
  943. pole_dist = -2*(aexp+abc)
  944. if pole_dist > wp:
  945. if alt:
  946. q = mpf_ln2(wp)
  947. y = mpf_mul(q, mpf_euler(wp), wp)
  948. g = mpf_shift(mpf_mul(q, q, wp), -1)
  949. g = mpf_sub(y, g)
  950. z = mpc_mul_mpf(r, mpf_neg(g), wp)
  951. z = mpc_add_mpf(z, q, wp)
  952. return mpc_pos(z, prec, rnd)
  953. else:
  954. q = mpc_neg(mpc_div(mpc_one, r, wp))
  955. q = mpc_add_mpf(q, mpf_euler(wp), wp)
  956. return mpc_pos(q, prec, rnd)
  957. else:
  958. wp += max(0, pole_dist)
  959. # Reflection formula. To be rigorous, we should reflect to the left of
  960. # re = 1/2 (see comments for mpf_zeta), but this leads to unnecessary
  961. # slowdown for interesting values of s
  962. if mpf_lt(re, fzero):
  963. # XXX: could use the separate refl. formula for Dirichlet eta
  964. if alt:
  965. q = mpc_sub(mpc_one, mpc_pow(mpc_two, mpc_sub(mpc_one, s, wp),
  966. wp), wp)
  967. return mpc_mul(mpc_zeta(s, wp), q, prec, rnd)
  968. # XXX: -1 should be done exactly
  969. y = mpc_sub(mpc_one, s, 10*wp)
  970. a = mpc_gamma(y, wp)
  971. b = mpc_zeta(y, wp)
  972. c = mpc_sin_pi(mpc_shift(s, -1), wp)
  973. rsign, rman, rexp, rbc = re
  974. isign, iman, iexp, ibc = im
  975. mag = max(rexp+rbc, iexp+ibc)
  976. wp2 = wp + max(0, mag)
  977. pi = mpf_pi(wp+wp2)
  978. pi2 = (mpf_shift(pi, 1), fzero)
  979. d = mpc_div_mpf(mpc_pow(pi2, s, wp2), pi, wp2)
  980. return mpc_mul(a,mpc_mul(b,mpc_mul(c,d,wp),wp),prec,rnd)
  981. n = int(wp/2.54 + 5)
  982. n += int(0.9*abs(to_int(im)))
  983. d = borwein_coefficients(n)
  984. ref = to_fixed(re, wp)
  985. imf = to_fixed(im, wp)
  986. tre = MPZ_ZERO
  987. tim = MPZ_ZERO
  988. one = MPZ_ONE << wp
  989. one_2wp = MPZ_ONE << (2*wp)
  990. critical_line = re == fhalf
  991. ln2 = ln2_fixed(wp)
  992. pi2 = pi_fixed(wp-1)
  993. wp2 = wp+wp
  994. for k in xrange(n):
  995. log = log_int_fixed(k+1, wp, ln2)
  996. # A square root is much cheaper than an exp
  997. if critical_line:
  998. w = one_2wp // isqrt_fast((k+1) << wp2)
  999. else:
  1000. w = exp_fixed((-ref*log) >> wp, wp)
  1001. if k & 1:
  1002. w *= (d[n] - d[k])
  1003. else:
  1004. w *= (d[k] - d[n])
  1005. wre, wim = cos_sin_fixed((-imf*log)>>wp, wp, pi2)
  1006. tre += (w * wre) >> wp
  1007. tim += (w * wim) >> wp
  1008. tre //= (-d[n])
  1009. tim //= (-d[n])
  1010. tre = from_man_exp(tre, -wp, wp)
  1011. tim = from_man_exp(tim, -wp, wp)
  1012. if alt:
  1013. return mpc_pos((tre, tim), prec, rnd)
  1014. else:
  1015. q = mpc_sub(mpc_one, mpc_pow(mpc_two, r, wp), wp)
  1016. return mpc_div((tre, tim), q, prec, rnd)
  1017. def mpf_altzeta(s, prec, rnd=round_fast):
  1018. return mpf_zeta(s, prec, rnd, 1)
  1019. def mpc_altzeta(s, prec, rnd=round_fast):
  1020. return mpc_zeta(s, prec, rnd, 1)
  1021. # Not optimized currently
  1022. mpf_zetasum = None
  1023. def pow_fixed(x, n, wp):
  1024. if n == 1:
  1025. return x
  1026. y = MPZ_ONE << wp
  1027. while n:
  1028. if n & 1:
  1029. y = (y*x) >> wp
  1030. n -= 1
  1031. x = (x*x) >> wp
  1032. n //= 2
  1033. return y
  1034. # TODO: optimize / cleanup interface / unify with list_primes
  1035. sieve_cache = []
  1036. primes_cache = []
  1037. mult_cache = []
  1038. def primesieve(n):
  1039. global sieve_cache, primes_cache, mult_cache
  1040. if n < len(sieve_cache):
  1041. sieve = sieve_cache#[:n+1]
  1042. primes = primes_cache[:primes_cache.index(max(sieve))+1]
  1043. mult = mult_cache#[:n+1]
  1044. return sieve, primes, mult
  1045. sieve = [0] * (n+1)
  1046. mult = [0] * (n+1)
  1047. primes = list_primes(n)
  1048. for p in primes:
  1049. #sieve[p::p] = p
  1050. for k in xrange(p,n+1,p):
  1051. sieve[k] = p
  1052. for i, p in enumerate(sieve):
  1053. if i >= 2:
  1054. m = 1
  1055. n = i // p
  1056. while not n % p:
  1057. n //= p
  1058. m += 1
  1059. mult[i] = m
  1060. sieve_cache = sieve
  1061. primes_cache = primes
  1062. mult_cache = mult
  1063. return sieve, primes, mult
  1064. def zetasum_sieved(critical_line, sre, sim, a, n, wp):
  1065. if a < 1:
  1066. raise ValueError("a cannot be less than 1")
  1067. sieve, primes, mult = primesieve(a+n)
  1068. basic_powers = {}
  1069. one = MPZ_ONE << wp
  1070. one_2wp = MPZ_ONE << (2*wp)
  1071. wp2 = wp+wp
  1072. ln2 = ln2_fixed(wp)
  1073. pi2 = pi_fixed(wp-1)
  1074. for p in primes:
  1075. if p*2 > a+n:
  1076. break
  1077. log = log_int_fixed(p, wp, ln2)
  1078. cos, sin = cos_sin_fixed((-sim*log)>>wp, wp, pi2)
  1079. if critical_line:
  1080. u = one_2wp // isqrt_fast(p<<wp2)
  1081. else:
  1082. u = exp_fixed((-sre*log)>>wp, wp)
  1083. pre = (u*cos) >> wp
  1084. pim = (u*sin) >> wp
  1085. basic_powers[p] = [(pre, pim)]
  1086. tre, tim = pre, pim
  1087. for m in range(1,int(math.log(a+n,p)+0.01)+1):
  1088. tre, tim = ((pre*tre-pim*tim)>>wp), ((pim*tre+pre*tim)>>wp)
  1089. basic_powers[p].append((tre,tim))
  1090. xre = MPZ_ZERO
  1091. xim = MPZ_ZERO
  1092. if a == 1:
  1093. xre += one
  1094. aa = max(a,2)
  1095. for k in xrange(aa, a+n+1):
  1096. p = sieve[k]
  1097. if p in basic_powers:
  1098. m = mult[k]
  1099. tre, tim = basic_powers[p][m-1]
  1100. while 1:
  1101. k //= p**m
  1102. if k == 1:
  1103. break
  1104. p = sieve[k]
  1105. m = mult[k]
  1106. pre, pim = basic_powers[p][m-1]
  1107. tre, tim = ((pre*tre-pim*tim)>>wp), ((pim*tre+pre*tim)>>wp)
  1108. else:
  1109. log = log_int_fixed(k, wp, ln2)
  1110. cos, sin = cos_sin_fixed((-sim*log)>>wp, wp, pi2)
  1111. if critical_line:
  1112. u = one_2wp // isqrt_fast(k<<wp2)
  1113. else:
  1114. u = exp_fixed((-sre*log)>>wp, wp)
  1115. tre = (u*cos) >> wp
  1116. tim = (u*sin) >> wp
  1117. xre += tre
  1118. xim += tim
  1119. return xre, xim
  1120. # Set to something large to disable
  1121. ZETASUM_SIEVE_CUTOFF = 10
  1122. def mpc_zetasum(s, a, n, derivatives, reflect, prec):
  1123. """
  1124. Fast version of mp._zetasum, assuming s = complex, a = integer.
  1125. """
  1126. wp = prec + 10
  1127. derivatives = list(derivatives)
  1128. have_derivatives = derivatives != [0]
  1129. have_one_derivative = len(derivatives) == 1
  1130. # parse s
  1131. sre, sim = s
  1132. critical_line = (sre == fhalf)
  1133. sre = to_fixed(sre, wp)
  1134. sim = to_fixed(sim, wp)
  1135. if a > 0 and n > ZETASUM_SIEVE_CUTOFF and not have_derivatives \
  1136. and not reflect and (n < 4e7 or sys.maxsize > 2**32):
  1137. re, im = zetasum_sieved(critical_line, sre, sim, a, n, wp)
  1138. xs = [(from_man_exp(re, -wp, prec, 'n'), from_man_exp(im, -wp, prec, 'n'))]
  1139. return xs, []
  1140. maxd = max(derivatives)
  1141. if not have_one_derivative:
  1142. derivatives = range(maxd+1)
  1143. # x_d = 0, y_d = 0
  1144. xre = [MPZ_ZERO for d in derivatives]
  1145. xim = [MPZ_ZERO for d in derivatives]
  1146. if reflect:
  1147. yre = [MPZ_ZERO for d in derivatives]
  1148. yim = [MPZ_ZERO for d in derivatives]
  1149. else:
  1150. yre = yim = []
  1151. one = MPZ_ONE << wp
  1152. one_2wp = MPZ_ONE << (2*wp)
  1153. ln2 = ln2_fixed(wp)
  1154. pi2 = pi_fixed(wp-1)
  1155. wp2 = wp+wp
  1156. for w in xrange(a, a+n+1):
  1157. log = log_int_fixed(w, wp, ln2)
  1158. cos, sin = cos_sin_fixed((-sim*log)>>wp, wp, pi2)
  1159. if critical_line:
  1160. u = one_2wp // isqrt_fast(w<<wp2)
  1161. else:
  1162. u = exp_fixed((-sre*log)>>wp, wp)
  1163. xterm_re = (u * cos) >> wp
  1164. xterm_im = (u * sin) >> wp
  1165. if reflect:
  1166. reciprocal = (one_2wp // (u*w))
  1167. yterm_re = (reciprocal * cos) >> wp
  1168. yterm_im = (reciprocal * sin) >> wp
  1169. if have_derivatives:
  1170. if have_one_derivative:
  1171. log = pow_fixed(log, maxd, wp)
  1172. xre[0] += (xterm_re * log) >> wp
  1173. xim[0] += (xterm_im * log) >> wp
  1174. if reflect:
  1175. yre[0] += (yterm_re * log) >> wp
  1176. yim[0] += (yterm_im * log) >> wp
  1177. else:
  1178. t = MPZ_ONE << wp
  1179. for d in derivatives:
  1180. xre[d] += (xterm_re * t) >> wp
  1181. xim[d] += (xterm_im * t) >> wp
  1182. if reflect:
  1183. yre[d] += (yterm_re * t) >> wp
  1184. yim[d] += (yterm_im * t) >> wp
  1185. t = (t * log) >> wp
  1186. else:
  1187. xre[0] += xterm_re
  1188. xim[0] += xterm_im
  1189. if reflect:
  1190. yre[0] += yterm_re
  1191. yim[0] += yterm_im
  1192. if have_derivatives:
  1193. if have_one_derivative:
  1194. if maxd % 2:
  1195. xre[0] = -xre[0]
  1196. xim[0] = -xim[0]
  1197. if reflect:
  1198. yre[0] = -yre[0]
  1199. yim[0] = -yim[0]
  1200. else:
  1201. xre = [(-1)**d * xre[d] for d in derivatives]
  1202. xim = [(-1)**d * xim[d] for d in derivatives]
  1203. if reflect:
  1204. yre = [(-1)**d * yre[d] for d in derivatives]
  1205. yim = [(-1)**d * yim[d] for d in derivatives]
  1206. xs = [(from_man_exp(xa, -wp, prec, 'n'), from_man_exp(xb, -wp, prec, 'n'))
  1207. for (xa, xb) in zip(xre, xim)]
  1208. ys = [(from_man_exp(ya, -wp, prec, 'n'), from_man_exp(yb, -wp, prec, 'n'))
  1209. for (ya, yb) in zip(yre, yim)]
  1210. return xs, ys
  1211. #-----------------------------------------------------------------------#
  1212. # #
  1213. # The gamma function (NEW IMPLEMENTATION) #
  1214. # #
  1215. #-----------------------------------------------------------------------#
  1216. # Higher means faster, but more precomputation time
  1217. MAX_GAMMA_TAYLOR_PREC = 5000
  1218. # Need to derive higher bounds for Taylor series to go higher
  1219. assert MAX_GAMMA_TAYLOR_PREC < 15000
  1220. # Use Stirling's series if abs(x) > beta*prec
  1221. # Important: must be large enough for convergence!
  1222. GAMMA_STIRLING_BETA = 0.2
  1223. SMALL_FACTORIAL_CACHE_SIZE = 150
  1224. gamma_taylor_cache = {}
  1225. gamma_stirling_cache = {}
  1226. small_factorial_cache = [from_int(ifac(n)) for \
  1227. n in range(SMALL_FACTORIAL_CACHE_SIZE+1)]
  1228. def zeta_array(N, prec):
  1229. """
  1230. zeta(n) = A * pi**n / n! + B
  1231. where A is a rational number (A = Bernoulli number
  1232. for n even) and B is an infinite sum over powers of exp(2*pi).
  1233. (B = 0 for n even).
  1234. TODO: this is currently only used for gamma, but could
  1235. be very useful elsewhere.
  1236. """
  1237. extra = 30
  1238. wp = prec+extra
  1239. zeta_values = [MPZ_ZERO] * (N+2)
  1240. pi = pi_fixed(wp)
  1241. # STEP 1:
  1242. one = MPZ_ONE << wp
  1243. zeta_values[0] = -one//2
  1244. f_2pi = mpf_shift(mpf_pi(wp),1)
  1245. exp_2pi_k = exp_2pi = mpf_exp(f_2pi, wp)
  1246. # Compute exponential series
  1247. # Store values of 1/(exp(2*pi*k)-1),
  1248. # exp(2*pi*k)/(exp(2*pi*k)-1)**2, 1/(exp(2*pi*k)-1)**2
  1249. # pi*k*exp(2*pi*k)/(exp(2*pi*k)-1)**2
  1250. exps3 = []
  1251. k = 1
  1252. while 1:
  1253. tp = wp - 9*k
  1254. if tp < 1:
  1255. break
  1256. # 1/(exp(2*pi*k-1)
  1257. q1 = mpf_div(fone, mpf_sub(exp_2pi_k, fone, tp), tp)
  1258. # pi*k*exp(2*pi*k)/(exp(2*pi*k)-1)**2
  1259. q2 = mpf_mul(exp_2pi_k, mpf_mul(q1,q1,tp), tp)
  1260. q1 = to_fixed(q1, wp)
  1261. q2 = to_fixed(q2, wp)
  1262. q2 = (k * q2 * pi) >> wp
  1263. exps3.append((q1, q2))
  1264. # Multiply for next round
  1265. exp_2pi_k = mpf_mul(exp_2pi_k, exp_2pi, wp)
  1266. k += 1
  1267. # Exponential sum
  1268. for n in xrange(3, N+1, 2):
  1269. s = MPZ_ZERO
  1270. k = 1
  1271. for e1, e2 in exps3:
  1272. if n%4 == 3:
  1273. t = e1 // k**n
  1274. else:
  1275. U = (n-1)//4
  1276. t = (e1 + e2//U) // k**n
  1277. if not t:
  1278. break
  1279. s += t
  1280. k += 1
  1281. zeta_values[n] = -2*s
  1282. # Even zeta values
  1283. B = [mpf_abs(mpf_bernoulli(k,wp)) for k in xrange(N+2)]
  1284. pi_pow = fpi = mpf_pow_int(mpf_shift(mpf_pi(wp), 1), 2, wp)
  1285. pi_pow = mpf_div(pi_pow, from_int(4), wp)
  1286. for n in xrange(2,N+2,2):
  1287. z = mpf_mul(B[n], pi_pow, wp)
  1288. zeta_values[n] = to_fixed(z, wp)
  1289. pi_pow = mpf_mul(pi_pow, fpi, wp)
  1290. pi_pow = mpf_div(pi_pow, from_int((n+1)*(n+2)), wp)
  1291. # Zeta sum
  1292. reciprocal_pi = (one << wp) // pi
  1293. for n in xrange(3, N+1, 4):
  1294. U = (n-3)//4
  1295. s = zeta_values[4*U+4]*(4*U+7)//4
  1296. for k in xrange(1, U+1):
  1297. s -= (zeta_values[4*k] * zeta_values[4*U+4-4*k]) >> wp
  1298. zeta_values[n] += (2*s*reciprocal_pi) >> wp
  1299. for n in xrange(5, N+1, 4):
  1300. U = (n-1)//4
  1301. s = zeta_values[4*U+2]*(2*U+1)
  1302. for k in xrange(1, 2*U+1):
  1303. s += ((-1)**k*2*k* zeta_values[2*k] * zeta_values[4*U+2-2*k])>>wp
  1304. zeta_values[n] += ((s*reciprocal_pi)>>wp)//(2*U)
  1305. return [x>>extra for x in zeta_values]
  1306. def gamma_taylor_coefficients(inprec):
  1307. """
  1308. Gives the Taylor coefficients of 1/gamma(1+x) as
  1309. a list of fixed-point numbers. Enough coefficients are returned
  1310. to ensure that the series converges to the given precision
  1311. when x is in [0.5, 1.5].
  1312. """
  1313. # Reuse nearby cache values (small case)
  1314. if inprec < 400:
  1315. prec = inprec + (10-(inprec%10))
  1316. elif inprec < 1000:
  1317. prec = inprec + (30-(inprec%30))
  1318. else:
  1319. prec = inprec
  1320. if prec in gamma_taylor_cache:
  1321. return gamma_taylor_cache[prec], prec
  1322. # Experimentally determined bounds
  1323. if prec < 1000:
  1324. N = int(prec**0.76 + 2)
  1325. else:
  1326. # Valid to at least 15000 bits
  1327. N = int(prec**0.787 + 2)
  1328. # Reuse higher precision values
  1329. for cprec in gamma_taylor_cache:
  1330. if cprec > prec:
  1331. coeffs = [x>>(cprec-prec) for x in gamma_taylor_cache[cprec][-N:]]
  1332. if inprec < 1000:
  1333. gamma_taylor_cache[prec] = coeffs
  1334. return coeffs, prec
  1335. # Cache at a higher precision (large case)
  1336. if prec > 1000:
  1337. prec = int(prec * 1.2)
  1338. wp = prec + 20
  1339. A = [0] * N
  1340. A[0] = MPZ_ZERO
  1341. A[1] = MPZ_ONE << wp
  1342. A[2] = euler_fixed(wp)
  1343. # SLOW, reference implementation
  1344. #zeta_values = [0,0]+[to_fixed(mpf_zeta_int(k,wp),wp) for k in xrange(2,N)]
  1345. zeta_values = zeta_array(N, wp)
  1346. for k in xrange(3, N):
  1347. a = (-A[2]*A[k-1])>>wp
  1348. for j in xrange(2,k):
  1349. a += ((-1)**j * zeta_values[j] * A[k-j]) >> wp
  1350. a //= (1-k)
  1351. A[k] = a
  1352. A = [a>>20 for a in A]
  1353. A = A[::-1]
  1354. A = A[:-1]
  1355. gamma_taylor_cache[prec] = A
  1356. #return A, prec
  1357. return gamma_taylor_coefficients(inprec)
  1358. def gamma_fixed_taylor(xmpf, x, wp, prec, rnd, type):
  1359. # Determine nearest multiple of N/2
  1360. #n = int(x >> (wp-1))
  1361. #steps = (n-1)>>1
  1362. nearest_int = ((x >> (wp-1)) + MPZ_ONE) >> 1
  1363. one = MPZ_ONE << wp
  1364. coeffs, cwp = gamma_taylor_coefficients(wp)
  1365. if nearest_int > 0:
  1366. r = one
  1367. for i in xrange(nearest_int-1):
  1368. x -= one
  1369. r = (r*x) >> wp
  1370. x -= one
  1371. p = MPZ_ZERO
  1372. for c in coeffs:
  1373. p = c + ((x*p)>>wp)
  1374. p >>= (cwp-wp)
  1375. if type == 0:
  1376. return from_man_exp((r<<wp)//p, -wp, prec, rnd)
  1377. if type == 2:
  1378. return mpf_shift(from_rational(p, (r<<wp), prec, rnd), wp)
  1379. if type == 3:
  1380. return mpf_log(mpf_abs(from_man_exp((r<<wp)//p, -wp)), prec, rnd)
  1381. else:
  1382. r = one
  1383. for i in xrange(-nearest_int):
  1384. r = (r*x) >> wp
  1385. x += one
  1386. p = MPZ_ZERO
  1387. for c in coeffs:
  1388. p = c + ((x*p)>>wp)
  1389. p >>= (cwp-wp)
  1390. if wp - bitcount(abs(x)) > 10:
  1391. # pass very close to 0, so do floating-point multiply
  1392. g = mpf_add(xmpf, from_int(-nearest_int)) # exact
  1393. r = from_man_exp(p*r,-wp-wp)
  1394. r = mpf_mul(r, g, wp)
  1395. if type == 0:
  1396. return mpf_div(fone, r, prec, rnd)
  1397. if type == 2:
  1398. return mpf_pos(r, prec, rnd)
  1399. if type == 3:
  1400. return mpf_log(mpf_abs(mpf_div(fone, r, wp)), prec, rnd)
  1401. else:
  1402. r = from_man_exp(x*p*r,-3*wp)
  1403. if type == 0: return mpf_div(fone, r, prec, rnd)
  1404. if type == 2: return mpf_pos(r, prec, rnd)
  1405. if type == 3: return mpf_neg(mpf_log(mpf_abs(r), prec, rnd))
  1406. def stirling_coefficient(n):
  1407. if n in gamma_stirling_cache:
  1408. return gamma_stirling_cache[n]
  1409. p, q = bernfrac(n)
  1410. q *= MPZ(n*(n-1))
  1411. gamma_stirling_cache[n] = p, q, bitcount(abs(p)), bitcount(q)
  1412. return gamma_stirling_cache[n]
  1413. def real_stirling_series(x, prec):
  1414. """
  1415. Sums the rational part of Stirling's expansion,
  1416. log(sqrt(2*pi)) - z + 1/(12*z) - 1/(360*z^3) + ...
  1417. """
  1418. t = (MPZ_ONE<<(prec+prec)) // x # t = 1/x
  1419. u = (t*t)>>prec # u = 1/x**2
  1420. s = ln_sqrt2pi_fixed(prec) - x
  1421. # Add initial terms of Stirling's series
  1422. s += t//12; t = (t*u)>>prec
  1423. s -= t//360; t = (t*u)>>prec
  1424. s += t//1260; t = (t*u)>>prec
  1425. s -= t//1680; t = (t*u)>>prec
  1426. if not t: return s
  1427. s += t//1188; t = (t*u)>>prec
  1428. s -= 691*t//360360; t = (t*u)>>prec
  1429. s += t//156; t = (t*u)>>prec
  1430. if not t: return s
  1431. s -= 3617*t//122400; t = (t*u)>>prec
  1432. s += 43867*t//244188; t = (t*u)>>prec
  1433. s -= 174611*t//125400; t = (t*u)>>prec
  1434. if not t: return s
  1435. k = 22
  1436. # From here on, the coefficients are growing, so we
  1437. # have to keep t at a roughly constant size
  1438. usize = bitcount(abs(u))
  1439. tsize = bitcount(abs(t))
  1440. texp = 0
  1441. while 1:
  1442. p, q, pb, qb = stirling_coefficient(k)
  1443. term_mag = tsize + pb + texp
  1444. shift = -texp
  1445. m = pb - term_mag
  1446. if m > 0 and shift < m:
  1447. p >>= m
  1448. shift -= m
  1449. m = tsize - term_mag
  1450. if m > 0 and shift < m:
  1451. w = t >> m
  1452. shift -= m
  1453. else:
  1454. w = t
  1455. term = (t*p//q) >> shift
  1456. if not term:
  1457. break
  1458. s += term
  1459. t = (t*u) >> usize
  1460. texp -= (prec - usize)
  1461. k += 2
  1462. return s
  1463. def complex_stirling_series(x, y, prec):
  1464. # t = 1/z
  1465. _m = (x*x + y*y) >> prec
  1466. tre = (x << prec) // _m
  1467. tim = (-y << prec) // _m
  1468. # u = 1/z**2
  1469. ure = (tre*tre - tim*tim) >> prec
  1470. uim = tim*tre >> (prec-1)
  1471. # s = log(sqrt(2*pi)) - z
  1472. sre = ln_sqrt2pi_fixed(prec) - x
  1473. sim = -y
  1474. # Add initial terms of Stirling's series
  1475. sre += tre//12; sim += tim//12;
  1476. tre, tim = ((tre*ure-tim*uim)>>prec), ((tre*uim+tim*ure)>>prec)
  1477. sre -= tre//360; sim -= tim//360;
  1478. tre, tim = ((tre*ure-tim*uim)>>prec), ((tre*uim+tim*ure)>>prec)
  1479. sre += tre//1260; sim += tim//1260;
  1480. tre, tim = ((tre*ure-tim*uim)>>prec), ((tre*uim+tim*ure)>>prec)
  1481. sre -= tre//1680; sim -= tim//1680;
  1482. tre, tim = ((tre*ure-tim*uim)>>prec), ((tre*uim+tim*ure)>>prec)
  1483. if abs(tre) + abs(tim) < 5: return sre, sim
  1484. sre += tre//1188; sim += tim//1188;
  1485. tre, tim = ((tre*ure-tim*uim)>>prec), ((tre*uim+tim*ure)>>prec)
  1486. sre -= 691*tre//360360; sim -= 691*tim//360360;
  1487. tre, tim = ((tre*ure-tim*uim)>>prec), ((tre*uim+tim*ure)>>prec)
  1488. sre += tre//156; sim += tim//156;
  1489. tre, tim = ((tre*ure-tim*uim)>>prec), ((tre*uim+tim*ure)>>prec)
  1490. if abs(tre) + abs(tim) < 5: return sre, sim
  1491. sre -= 3617*tre//122400; sim -= 3617*tim//122400;
  1492. tre, tim = ((tre*ure-tim*uim)>>prec), ((tre*uim+tim*ure)>>prec)
  1493. sre += 43867*tre//244188; sim += 43867*tim//244188;
  1494. tre, tim = ((tre*ure-tim*uim)>>prec), ((tre*uim+tim*ure)>>prec)
  1495. sre -= 174611*tre//125400; sim -= 174611*tim//125400;
  1496. tre, tim = ((tre*ure-tim*uim)>>prec), ((tre*uim+tim*ure)>>prec)
  1497. if abs(tre) + abs(tim) < 5: return sre, sim
  1498. k = 22
  1499. # From here on, the coefficients are growing, so we
  1500. # have to keep t at a roughly constant size
  1501. usize = bitcount(max(abs(ure), abs(uim)))
  1502. tsize = bitcount(max(abs(tre), abs(tim)))
  1503. texp = 0
  1504. while 1:
  1505. p, q, pb, qb = stirling_coefficient(k)
  1506. term_mag = tsize + pb + texp
  1507. shift = -texp
  1508. m = pb - term_mag
  1509. if m > 0 and shift < m:
  1510. p >>= m
  1511. shift -= m
  1512. m = tsize - term_mag
  1513. if m > 0 and shift < m:
  1514. wre = tre >> m
  1515. wim = tim >> m
  1516. shift -= m
  1517. else:
  1518. wre = tre
  1519. wim = tim
  1520. termre = (tre*p//q) >> shift
  1521. termim = (tim*p//q) >> shift
  1522. if abs(termre) + abs(termim) < 5:
  1523. break
  1524. sre += termre
  1525. sim += termim
  1526. tre, tim = ((tre*ure - tim*uim)>>usize), \
  1527. ((tre*uim + tim*ure)>>usize)
  1528. texp -= (prec - usize)
  1529. k += 2
  1530. return sre, sim
  1531. def mpf_gamma(x, prec, rnd='d', type=0):
  1532. """
  1533. This function implements multipurpose evaluation of the gamma
  1534. function, G(x), as well as the following versions of the same:
  1535. type = 0 -- G(x) [standard gamma function]
  1536. type = 1 -- G(x+1) = x*G(x+1) = x! [factorial]
  1537. type = 2 -- 1/G(x) [reciprocal gamma function]
  1538. type = 3 -- log(|G(x)|) [log-gamma function, real part]
  1539. """
  1540. # Specal values
  1541. sign, man, exp, bc = x
  1542. if not man:
  1543. if x == fzero:
  1544. if type == 1: return fone
  1545. if type == 2: return fzero
  1546. raise ValueError("gamma function pole")
  1547. if x == finf:
  1548. if type == 2: return fzero
  1549. return finf
  1550. return fnan
  1551. # First of all, for log gamma, numbers can be well beyond the fixed-point
  1552. # range, so we must take care of huge numbers before e.g. trying
  1553. # to convert x to the nearest integer
  1554. if type == 3:
  1555. wp = prec+20
  1556. if exp+bc > wp and not sign:
  1557. return mpf_sub(mpf_mul(x, mpf_log(x, wp), wp), x, prec, rnd)
  1558. # We strongly want to special-case small integers
  1559. is_integer = exp >= 0
  1560. if is_integer:
  1561. # Poles
  1562. if sign:
  1563. if type == 2:
  1564. return fzero
  1565. raise ValueError("gamma function pole")
  1566. # n = x
  1567. n = man << exp
  1568. if n < SMALL_FACTORIAL_CACHE_SIZE:
  1569. if type == 0:
  1570. return mpf_pos(small_factorial_cache[n-1], prec, rnd)
  1571. if type == 1:
  1572. return mpf_pos(small_factorial_cache[n], prec, rnd)
  1573. if type == 2:
  1574. return mpf_div(fone, small_factorial_cache[n-1], prec, rnd)
  1575. if type == 3:
  1576. return mpf_log(small_factorial_cache[n-1], prec, rnd)
  1577. else:
  1578. # floor(abs(x))
  1579. n = int(man >> (-exp))
  1580. # Estimate size and precision
  1581. # Estimate log(gamma(|x|),2) as x*log(x,2)
  1582. mag = exp + bc
  1583. gamma_size = n*mag
  1584. if type == 3:
  1585. wp = prec + 20
  1586. else:
  1587. wp = prec + bitcount(gamma_size) + 20
  1588. # Very close to 0, pole
  1589. if mag < -wp:
  1590. if type == 0:
  1591. return mpf_sub(mpf_div(fone,x, wp),mpf_shift(fone,-wp),prec,rnd)
  1592. if type == 1: return mpf_sub(fone, x, prec, rnd)
  1593. if type == 2: return mpf_add(x, mpf_shift(fone,mag-wp), prec, rnd)
  1594. if type == 3: return mpf_neg(mpf_log(mpf_abs(x), prec, rnd))
  1595. # From now on, we assume having a gamma function
  1596. if type == 1:
  1597. return mpf_gamma(mpf_add(x, fone), prec, rnd, 0)
  1598. # Special case integers (those not small enough to be caught above,
  1599. # but still small enough for an exact factorial to be faster
  1600. # than an approximate algorithm), and half-integers
  1601. if exp >= -1:
  1602. if is_integer:
  1603. if gamma_size < 10*wp:
  1604. if type == 0:
  1605. return from_int(ifac(n-1), prec, rnd)
  1606. if type == 2:
  1607. return from_rational(MPZ_ONE, ifac(n-1), prec, rnd)
  1608. if type == 3:
  1609. return mpf_log(from_int(ifac(n-1)), prec, rnd)
  1610. # half-integer
  1611. if n < 100 or gamma_size < 10*wp:
  1612. if sign:
  1613. w = sqrtpi_fixed(wp)
  1614. if n % 2: f = ifac2(2*n+1)
  1615. else: f = -ifac2(2*n+1)
  1616. if type == 0:
  1617. return mpf_shift(from_rational(w, f, prec, rnd), -wp+n+1)
  1618. if type == 2:
  1619. return mpf_shift(from_rational(f, w, prec, rnd), wp-n-1)
  1620. if type == 3:
  1621. return mpf_log(mpf_shift(from_rational(w, abs(f),
  1622. prec, rnd), -wp+n+1), prec, rnd)
  1623. elif n == 0:
  1624. if type == 0: return mpf_sqrtpi(prec, rnd)
  1625. if type == 2: return mpf_div(fone, mpf_sqrtpi(wp), prec, rnd)
  1626. if type == 3: return mpf_log(mpf_sqrtpi(wp), prec, rnd)
  1627. else:
  1628. w = sqrtpi_fixed(wp)
  1629. w = from_man_exp(w * ifac2(2*n-1), -wp-n)
  1630. if type == 0: return mpf_pos(w, prec, rnd)
  1631. if type == 2: return mpf_div(fone, w, prec, rnd)
  1632. if type == 3: return mpf_log(mpf_abs(w), prec, rnd)
  1633. # Convert to fixed point
  1634. offset = exp + wp
  1635. if offset >= 0: absxman = man << offset
  1636. else: absxman = man >> (-offset)
  1637. # For log gamma, provide accurate evaluation for x = 1+eps and 2+eps
  1638. if type == 3 and not sign:
  1639. one = MPZ_ONE << wp
  1640. one_dist = abs(absxman-one)
  1641. two_dist = abs(absxman-2*one)
  1642. cancellation = (wp - bitcount(min(one_dist, two_dist)))
  1643. if cancellation > 10:
  1644. xsub1 = mpf_sub(fone, x)
  1645. xsub2 = mpf_sub(ftwo, x)
  1646. xsub1mag = xsub1[2]+xsub1[3]
  1647. xsub2mag = xsub2[2]+xsub2[3]
  1648. if xsub1mag < -wp:
  1649. return mpf_mul(mpf_euler(wp), mpf_sub(fone, x), prec, rnd)
  1650. if xsub2mag < -wp:
  1651. return mpf_mul(mpf_sub(fone, mpf_euler(wp)),
  1652. mpf_sub(x, ftwo), prec, rnd)
  1653. # Proceed but increase precision
  1654. wp += max(-xsub1mag, -xsub2mag)
  1655. offset = exp + wp
  1656. if offset >= 0: absxman = man << offset
  1657. else: absxman = man >> (-offset)
  1658. # Use Taylor series if appropriate
  1659. n_for_stirling = int(GAMMA_STIRLING_BETA*wp)
  1660. if n < max(100, n_for_stirling) and wp < MAX_GAMMA_TAYLOR_PREC:
  1661. if sign:
  1662. absxman = -absxman
  1663. return gamma_fixed_taylor(x, absxman, wp, prec, rnd, type)
  1664. # Use Stirling's series
  1665. # First ensure that |x| is large enough for rapid convergence
  1666. xorig = x
  1667. # Argument reduction
  1668. r = 0
  1669. if n < n_for_stirling:
  1670. r = one = MPZ_ONE << wp
  1671. d = n_for_stirling - n
  1672. for k in xrange(d):
  1673. r = (r * absxman) >> wp
  1674. absxman += one
  1675. x = xabs = from_man_exp(absxman, -wp)
  1676. if sign:
  1677. x = mpf_neg(x)
  1678. else:
  1679. xabs = mpf_abs(x)
  1680. # Asymptotic series
  1681. y = real_stirling_series(absxman, wp)
  1682. u = to_fixed(mpf_log(xabs, wp), wp)
  1683. u = ((absxman - (MPZ_ONE<<(wp-1))) * u) >> wp
  1684. y += u
  1685. w = from_man_exp(y, -wp)
  1686. # Compute final value
  1687. if sign:
  1688. # Reflection formula
  1689. A = mpf_mul(mpf_sin_pi(xorig, wp), xorig, wp)
  1690. B = mpf_neg(mpf_pi(wp))
  1691. if type == 0 or type == 2:
  1692. A = mpf_mul(A, mpf_exp(w, wp))
  1693. if r:
  1694. B = mpf_mul(B, from_man_exp(r, -wp), wp)
  1695. if type == 0:
  1696. return mpf_div(B, A, prec, rnd)
  1697. if type == 2:
  1698. return mpf_div(A, B, prec, rnd)
  1699. if type == 3:
  1700. if r:
  1701. B = mpf_mul(B, from_man_exp(r, -wp), wp)
  1702. A = mpf_add(mpf_log(mpf_abs(A), wp), w, wp)
  1703. return mpf_sub(mpf_log(mpf_abs(B), wp), A, prec, rnd)
  1704. else:
  1705. if type == 0:
  1706. if r:
  1707. return mpf_div(mpf_exp(w, wp),
  1708. from_man_exp(r, -wp), prec, rnd)
  1709. return mpf_exp(w, prec, rnd)
  1710. if type == 2:
  1711. if r:
  1712. return mpf_div(from_man_exp(r, -wp),
  1713. mpf_exp(w, wp), prec, rnd)
  1714. return mpf_exp(mpf_neg(w), prec, rnd)
  1715. if type == 3:
  1716. if r:
  1717. return mpf_sub(w, mpf_log(from_man_exp(r,-wp), wp), prec, rnd)
  1718. return mpf_pos(w, prec, rnd)
  1719. def mpc_gamma(z, prec, rnd='d', type=0):
  1720. a, b = z
  1721. asign, aman, aexp, abc = a
  1722. bsign, bman, bexp, bbc = b
  1723. if b == fzero:
  1724. # Imaginary part on negative half-axis for log-gamma function
  1725. if type == 3 and asign:
  1726. re = mpf_gamma(a, prec, rnd, 3)
  1727. n = (-aman) >> (-aexp)
  1728. im = mpf_mul_int(mpf_pi(prec+10), n, prec, rnd)
  1729. return re, im
  1730. return mpf_gamma(a, prec, rnd, type), fzero
  1731. # Some kind of complex inf/nan
  1732. if (not aman and aexp) or (not bman and bexp):
  1733. return (fnan, fnan)
  1734. # Initial working precision
  1735. wp = prec + 20
  1736. amag = aexp+abc
  1737. bmag = bexp+bbc
  1738. if aman:
  1739. mag = max(amag, bmag)
  1740. else:
  1741. mag = bmag
  1742. # Close to 0
  1743. if mag < -8:
  1744. if mag < -wp:
  1745. # 1/gamma(z) = z + euler*z^2 + O(z^3)
  1746. v = mpc_add(z, mpc_mul_mpf(mpc_mul(z,z,wp),mpf_euler(wp),wp), wp)
  1747. if type == 0: return mpc_reciprocal(v, prec, rnd)
  1748. if type == 1: return mpc_div(z, v, prec, rnd)
  1749. if type == 2: return mpc_pos(v, prec, rnd)
  1750. if type == 3: return mpc_log(mpc_reciprocal(v, prec), prec, rnd)
  1751. elif type != 1:
  1752. wp += (-mag)
  1753. # Handle huge log-gamma values; must do this before converting to
  1754. # a fixed-point value. TODO: determine a precise cutoff of validity
  1755. # depending on amag and bmag
  1756. if type == 3 and mag > wp and ((not asign) or (bmag >= amag)):
  1757. return mpc_sub(mpc_mul(z, mpc_log(z, wp), wp), z, prec, rnd)
  1758. # From now on, we assume having a gamma function
  1759. if type == 1:
  1760. return mpc_gamma((mpf_add(a, fone), b), prec, rnd, 0)
  1761. an = abs(to_int(a))
  1762. bn = abs(to_int(b))
  1763. absn = max(an, bn)
  1764. gamma_size = absn*mag
  1765. if type == 3:
  1766. pass
  1767. else:
  1768. wp += bitcount(gamma_size)
  1769. # Reflect to the right half-plane. Note that Stirling's expansion
  1770. # is valid in the left half-plane too, as long as we're not too close
  1771. # to the real axis, but in order to use this argument reduction
  1772. # in the negative direction must be implemented.
  1773. #need_reflection = asign and ((bmag < 0) or (amag-bmag > 4))
  1774. need_reflection = asign
  1775. zorig = z
  1776. if need_reflection:
  1777. z = mpc_neg(z)
  1778. asign, aman, aexp, abc = a = z[0]
  1779. bsign, bman, bexp, bbc = b = z[1]
  1780. # Imaginary part very small compared to real one?
  1781. yfinal = 0
  1782. balance_prec = 0
  1783. if bmag < -10:
  1784. # Check z ~= 1 and z ~= 2 for loggamma
  1785. if type == 3:
  1786. zsub1 = mpc_sub_mpf(z, fone)
  1787. if zsub1[0] == fzero:
  1788. cancel1 = -bmag
  1789. else:
  1790. cancel1 = -max(zsub1[0][2]+zsub1[0][3], bmag)
  1791. if cancel1 > wp:
  1792. pi = mpf_pi(wp)
  1793. x = mpc_mul_mpf(zsub1, pi, wp)
  1794. x = mpc_mul(x, x, wp)
  1795. x = mpc_div_mpf(x, from_int(12), wp)
  1796. y = mpc_mul_mpf(zsub1, mpf_neg(mpf_euler(wp)), wp)
  1797. yfinal = mpc_add(x, y, wp)
  1798. if not need_reflection:
  1799. return mpc_pos(yfinal, prec, rnd)
  1800. elif cancel1 > 0:
  1801. wp += cancel1
  1802. zsub2 = mpc_sub_mpf(z, ftwo)
  1803. if zsub2[0] == fzero:
  1804. cancel2 = -bmag
  1805. else:
  1806. cancel2 = -max(zsub2[0][2]+zsub2[0][3], bmag)
  1807. if cancel2 > wp:
  1808. pi = mpf_pi(wp)
  1809. t = mpf_sub(mpf_mul(pi, pi), from_int(6))
  1810. x = mpc_mul_mpf(mpc_mul(zsub2, zsub2, wp), t, wp)
  1811. x = mpc_div_mpf(x, from_int(12), wp)
  1812. y = mpc_mul_mpf(zsub2, mpf_sub(fone, mpf_euler(wp)), wp)
  1813. yfinal = mpc_add(x, y, wp)
  1814. if not need_reflection:
  1815. return mpc_pos(yfinal, prec, rnd)
  1816. elif cancel2 > 0:
  1817. wp += cancel2
  1818. if bmag < -wp:
  1819. # Compute directly from the real gamma function.
  1820. pp = 2*(wp+10)
  1821. aabs = mpf_abs(a)
  1822. eps = mpf_shift(fone, amag-wp)
  1823. x1 = mpf_gamma(aabs, pp, type=type)
  1824. x2 = mpf_gamma(mpf_add(aabs, eps), pp, type=type)
  1825. xprime = mpf_div(mpf_sub(x2, x1, pp), eps, pp)
  1826. y = mpf_mul(b, xprime, prec, rnd)
  1827. yfinal = (x1, y)
  1828. # Note: we still need to use the reflection formula for
  1829. # near-poles, and the correct branch of the log-gamma function
  1830. if not need_reflection:
  1831. return mpc_pos(yfinal, prec, rnd)
  1832. else:
  1833. balance_prec += (-bmag)
  1834. wp += balance_prec
  1835. n_for_stirling = int(GAMMA_STIRLING_BETA*wp)
  1836. need_reduction = absn < n_for_stirling
  1837. afix = to_fixed(a, wp)
  1838. bfix = to_fixed(b, wp)
  1839. r = 0
  1840. if not yfinal:
  1841. zprered = z
  1842. # Argument reduction
  1843. if absn < n_for_stirling:
  1844. absn = complex(an, bn)
  1845. d = int((1 + n_for_stirling**2 - bn**2)**0.5 - an)
  1846. rre = one = MPZ_ONE << wp
  1847. rim = MPZ_ZERO
  1848. for k in xrange(d):
  1849. rre, rim = ((afix*rre-bfix*rim)>>wp), ((afix*rim + bfix*rre)>>wp)
  1850. afix += one
  1851. r = from_man_exp(rre, -wp), from_man_exp(rim, -wp)
  1852. a = from_man_exp(afix, -wp)
  1853. z = a, b
  1854. yre, yim = complex_stirling_series(afix, bfix, wp)
  1855. # (z-1/2)*log(z) + S
  1856. lre, lim = mpc_log(z, wp)
  1857. lre = to_fixed(lre, wp)
  1858. lim = to_fixed(lim, wp)
  1859. yre = ((lre*afix - lim*bfix)>>wp) - (lre>>1) + yre
  1860. yim = ((lre*bfix + lim*afix)>>wp) - (lim>>1) + yim
  1861. y = from_man_exp(yre, -wp), from_man_exp(yim, -wp)
  1862. if r and type == 3:
  1863. # If re(z) > 0 and abs(z) <= 4, the branches of loggamma(z)
  1864. # and log(gamma(z)) coincide. Otherwise, use the zeroth order
  1865. # Stirling expansion to compute the correct imaginary part.
  1866. y = mpc_sub(y, mpc_log(r, wp), wp)
  1867. zfa = to_float(zprered[0])
  1868. zfb = to_float(zprered[1])
  1869. zfabs = math.hypot(zfa,zfb)
  1870. #if not (zfa > 0.0 and zfabs <= 4):
  1871. yfb = to_float(y[1])
  1872. u = math.atan2(zfb, zfa)
  1873. if zfabs <= 0.5:
  1874. gi = 0.577216*zfb - u
  1875. else:
  1876. gi = -zfb - 0.5*u + zfa*u + zfb*math.log(zfabs)
  1877. n = int(math.floor((gi-yfb)/(2*math.pi)+0.5))
  1878. y = (y[0], mpf_add(y[1], mpf_mul_int(mpf_pi(wp), 2*n, wp), wp))
  1879. if need_reflection:
  1880. if type == 0 or type == 2:
  1881. A = mpc_mul(mpc_sin_pi(zorig, wp), zorig, wp)
  1882. B = (mpf_neg(mpf_pi(wp)), fzero)
  1883. if yfinal:
  1884. if type == 2:
  1885. A = mpc_div(A, yfinal, wp)
  1886. else:
  1887. A = mpc_mul(A, yfinal, wp)
  1888. else:
  1889. A = mpc_mul(A, mpc_exp(y, wp), wp)
  1890. if r:
  1891. B = mpc_mul(B, r, wp)
  1892. if type == 0: return mpc_div(B, A, prec, rnd)
  1893. if type == 2: return mpc_div(A, B, prec, rnd)
  1894. # Reflection formula for the log-gamma function with correct branch
  1895. # http://functions.wolfram.com/GammaBetaErf/LogGamma/16/01/01/0006/
  1896. # LogGamma[z] == -LogGamma[-z] - Log[-z] +
  1897. # Sign[Im[z]] Floor[Re[z]] Pi I + Log[Pi] -
  1898. # Log[Sin[Pi (z - Floor[Re[z]])]] -
  1899. # Pi I (1 - Abs[Sign[Im[z]]]) Abs[Floor[Re[z]]]
  1900. if type == 3:
  1901. if yfinal:
  1902. s1 = mpc_neg(yfinal)
  1903. else:
  1904. s1 = mpc_neg(y)
  1905. # s -= log(-z)
  1906. s1 = mpc_sub(s1, mpc_log(mpc_neg(zorig), wp), wp)
  1907. # floor(re(z))
  1908. rezfloor = mpf_floor(zorig[0])
  1909. imzsign = mpf_sign(zorig[1])
  1910. pi = mpf_pi(wp)
  1911. t = mpf_mul(pi, rezfloor)
  1912. t = mpf_mul_int(t, imzsign, wp)
  1913. s1 = (s1[0], mpf_add(s1[1], t, wp))
  1914. s1 = mpc_add_mpf(s1, mpf_log(pi, wp), wp)
  1915. t = mpc_sin_pi(mpc_sub_mpf(zorig, rezfloor), wp)
  1916. t = mpc_log(t, wp)
  1917. s1 = mpc_sub(s1, t, wp)
  1918. # Note: may actually be unused, because we fall back
  1919. # to the mpf_ function for real arguments
  1920. if not imzsign:
  1921. t = mpf_mul(pi, mpf_floor(rezfloor), wp)
  1922. s1 = (s1[0], mpf_sub(s1[1], t, wp))
  1923. return mpc_pos(s1, prec, rnd)
  1924. else:
  1925. if type == 0:
  1926. if r:
  1927. return mpc_div(mpc_exp(y, wp), r, prec, rnd)
  1928. return mpc_exp(y, prec, rnd)
  1929. if type == 2:
  1930. if r:
  1931. return mpc_div(r, mpc_exp(y, wp), prec, rnd)
  1932. return mpc_exp(mpc_neg(y), prec, rnd)
  1933. if type == 3:
  1934. return mpc_pos(y, prec, rnd)
  1935. def mpf_factorial(x, prec, rnd='d'):
  1936. return mpf_gamma(x, prec, rnd, 1)
  1937. def mpc_factorial(x, prec, rnd='d'):
  1938. return mpc_gamma(x, prec, rnd, 1)
  1939. def mpf_rgamma(x, prec, rnd='d'):
  1940. return mpf_gamma(x, prec, rnd, 2)
  1941. def mpc_rgamma(x, prec, rnd='d'):
  1942. return mpc_gamma(x, prec, rnd, 2)
  1943. def mpf_loggamma(x, prec, rnd='d'):
  1944. sign, man, exp, bc = x
  1945. if sign:
  1946. raise ComplexResult
  1947. return mpf_gamma(x, prec, rnd, 3)
  1948. def mpc_loggamma(z, prec, rnd='d'):
  1949. a, b = z
  1950. asign, aman, aexp, abc = a
  1951. bsign, bman, bexp, bbc = b
  1952. if b == fzero and asign:
  1953. re = mpf_gamma(a, prec, rnd, 3)
  1954. n = (-aman) >> (-aexp)
  1955. im = mpf_mul_int(mpf_pi(prec+10), n, prec, rnd)
  1956. return re, im
  1957. return mpc_gamma(z, prec, rnd, 3)
  1958. def mpf_gamma_int(n, prec, rnd=round_fast):
  1959. if n < SMALL_FACTORIAL_CACHE_SIZE:
  1960. return mpf_pos(small_factorial_cache[n-1], prec, rnd)
  1961. return mpf_gamma(from_int(n), prec, rnd)