zeta.py 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. from __future__ import print_function
  2. from ..libmp.backend import xrange
  3. from .functions import defun, defun_wrapped, defun_static
  4. @defun
  5. def stieltjes(ctx, n, a=1):
  6. n = ctx.convert(n)
  7. a = ctx.convert(a)
  8. if n < 0:
  9. return ctx.bad_domain("Stieltjes constants defined for n >= 0")
  10. if hasattr(ctx, "stieltjes_cache"):
  11. stieltjes_cache = ctx.stieltjes_cache
  12. else:
  13. stieltjes_cache = ctx.stieltjes_cache = {}
  14. if a == 1:
  15. if n == 0:
  16. return +ctx.euler
  17. if n in stieltjes_cache:
  18. prec, s = stieltjes_cache[n]
  19. if prec >= ctx.prec:
  20. return +s
  21. mag = 1
  22. def f(x):
  23. xa = x/a
  24. v = (xa-ctx.j)*ctx.ln(a-ctx.j*x)**n/(1+xa**2)/(ctx.exp(2*ctx.pi*x)-1)
  25. return ctx._re(v) / mag
  26. orig = ctx.prec
  27. try:
  28. # Normalize integrand by approx. magnitude to
  29. # speed up quadrature (which uses absolute error)
  30. if n > 50:
  31. ctx.prec = 20
  32. mag = ctx.quad(f, [0,ctx.inf], maxdegree=3)
  33. ctx.prec = orig + 10 + int(n**0.5)
  34. s = ctx.quad(f, [0,ctx.inf], maxdegree=20)
  35. v = ctx.ln(a)**n/(2*a) - ctx.ln(a)**(n+1)/(n+1) + 2*s/a*mag
  36. finally:
  37. ctx.prec = orig
  38. if a == 1 and ctx.isint(n):
  39. stieltjes_cache[n] = (ctx.prec, v)
  40. return +v
  41. @defun_wrapped
  42. def siegeltheta(ctx, t, derivative=0):
  43. d = int(derivative)
  44. if (t == ctx.inf or t == ctx.ninf):
  45. if d < 2:
  46. if t == ctx.ninf and d == 0:
  47. return ctx.ninf
  48. return ctx.inf
  49. else:
  50. return ctx.zero
  51. if d == 0:
  52. if ctx._im(t):
  53. # XXX: cancellation occurs
  54. a = ctx.loggamma(0.25+0.5j*t)
  55. b = ctx.loggamma(0.25-0.5j*t)
  56. return -ctx.ln(ctx.pi)/2*t - 0.5j*(a-b)
  57. else:
  58. if ctx.isinf(t):
  59. return t
  60. return ctx._im(ctx.loggamma(0.25+0.5j*t)) - ctx.ln(ctx.pi)/2*t
  61. if d > 0:
  62. a = (-0.5j)**(d-1)*ctx.polygamma(d-1, 0.25-0.5j*t)
  63. b = (0.5j)**(d-1)*ctx.polygamma(d-1, 0.25+0.5j*t)
  64. if ctx._im(t):
  65. if d == 1:
  66. return -0.5*ctx.log(ctx.pi)+0.25*(a+b)
  67. else:
  68. return 0.25*(a+b)
  69. else:
  70. if d == 1:
  71. return ctx._re(-0.5*ctx.log(ctx.pi)+0.25*(a+b))
  72. else:
  73. return ctx._re(0.25*(a+b))
  74. @defun_wrapped
  75. def grampoint(ctx, n):
  76. # asymptotic expansion, from
  77. # http://mathworld.wolfram.com/GramPoint.html
  78. g = 2*ctx.pi*ctx.exp(1+ctx.lambertw((8*n+1)/(8*ctx.e)))
  79. return ctx.findroot(lambda t: ctx.siegeltheta(t)-ctx.pi*n, g)
  80. @defun_wrapped
  81. def siegelz(ctx, t, **kwargs):
  82. d = int(kwargs.get("derivative", 0))
  83. t = ctx.convert(t)
  84. t1 = ctx._re(t)
  85. t2 = ctx._im(t)
  86. prec = ctx.prec
  87. try:
  88. if abs(t1) > 500*prec and t2**2 < t1:
  89. v = ctx.rs_z(t, d)
  90. if ctx._is_real_type(t):
  91. return ctx._re(v)
  92. return v
  93. except NotImplementedError:
  94. pass
  95. ctx.prec += 21
  96. e1 = ctx.expj(ctx.siegeltheta(t))
  97. z = ctx.zeta(0.5+ctx.j*t)
  98. if d == 0:
  99. v = e1*z
  100. ctx.prec=prec
  101. if ctx._is_real_type(t):
  102. return ctx._re(v)
  103. return +v
  104. z1 = ctx.zeta(0.5+ctx.j*t, derivative=1)
  105. theta1 = ctx.siegeltheta(t, derivative=1)
  106. if d == 1:
  107. v = ctx.j*e1*(z1+z*theta1)
  108. ctx.prec=prec
  109. if ctx._is_real_type(t):
  110. return ctx._re(v)
  111. return +v
  112. z2 = ctx.zeta(0.5+ctx.j*t, derivative=2)
  113. theta2 = ctx.siegeltheta(t, derivative=2)
  114. comb1 = theta1**2-ctx.j*theta2
  115. if d == 2:
  116. def terms():
  117. return [2*z1*theta1, z2, z*comb1]
  118. v = ctx.sum_accurately(terms, 1)
  119. v = -e1*v
  120. ctx.prec = prec
  121. if ctx._is_real_type(t):
  122. return ctx._re(v)
  123. return +v
  124. ctx.prec += 10
  125. z3 = ctx.zeta(0.5+ctx.j*t, derivative=3)
  126. theta3 = ctx.siegeltheta(t, derivative=3)
  127. comb2 = theta1**3-3*ctx.j*theta1*theta2-theta3
  128. if d == 3:
  129. def terms():
  130. return [3*theta1*z2, 3*z1*comb1, z3+z*comb2]
  131. v = ctx.sum_accurately(terms, 1)
  132. v = -ctx.j*e1*v
  133. ctx.prec = prec
  134. if ctx._is_real_type(t):
  135. return ctx._re(v)
  136. return +v
  137. z4 = ctx.zeta(0.5+ctx.j*t, derivative=4)
  138. theta4 = ctx.siegeltheta(t, derivative=4)
  139. def terms():
  140. return [theta1**4, -6*ctx.j*theta1**2*theta2, -3*theta2**2,
  141. -4*theta1*theta3, ctx.j*theta4]
  142. comb3 = ctx.sum_accurately(terms, 1)
  143. if d == 4:
  144. def terms():
  145. return [6*theta1**2*z2, -6*ctx.j*z2*theta2, 4*theta1*z3,
  146. 4*z1*comb2, z4, z*comb3]
  147. v = ctx.sum_accurately(terms, 1)
  148. v = e1*v
  149. ctx.prec = prec
  150. if ctx._is_real_type(t):
  151. return ctx._re(v)
  152. return +v
  153. if d > 4:
  154. h = lambda x: ctx.siegelz(x, derivative=4)
  155. return ctx.diff(h, t, n=d-4)
  156. _zeta_zeros = [
  157. 14.134725142,21.022039639,25.010857580,30.424876126,32.935061588,
  158. 37.586178159,40.918719012,43.327073281,48.005150881,49.773832478,
  159. 52.970321478,56.446247697,59.347044003,60.831778525,65.112544048,
  160. 67.079810529,69.546401711,72.067157674,75.704690699,77.144840069,
  161. 79.337375020,82.910380854,84.735492981,87.425274613,88.809111208,
  162. 92.491899271,94.651344041,95.870634228,98.831194218,101.317851006,
  163. 103.725538040,105.446623052,107.168611184,111.029535543,111.874659177,
  164. 114.320220915,116.226680321,118.790782866,121.370125002,122.946829294,
  165. 124.256818554,127.516683880,129.578704200,131.087688531,133.497737203,
  166. 134.756509753,138.116042055,139.736208952,141.123707404,143.111845808,
  167. 146.000982487,147.422765343,150.053520421,150.925257612,153.024693811,
  168. 156.112909294,157.597591818,158.849988171,161.188964138,163.030709687,
  169. 165.537069188,167.184439978,169.094515416,169.911976479,173.411536520,
  170. 174.754191523,176.441434298,178.377407776,179.916484020,182.207078484,
  171. 184.874467848,185.598783678,187.228922584,189.416158656,192.026656361,
  172. 193.079726604,195.265396680,196.876481841,198.015309676,201.264751944,
  173. 202.493594514,204.189671803,205.394697202,207.906258888,209.576509717,
  174. 211.690862595,213.347919360,214.547044783,216.169538508,219.067596349,
  175. 220.714918839,221.430705555,224.007000255,224.983324670,227.421444280,
  176. 229.337413306,231.250188700,231.987235253,233.693404179,236.524229666,
  177. ]
  178. def _load_zeta_zeros(url):
  179. import urllib
  180. d = urllib.urlopen(url)
  181. L = [float(x) for x in d.readlines()]
  182. # Sanity check
  183. assert round(L[0]) == 14
  184. _zeta_zeros[:] = L
  185. @defun
  186. def oldzetazero(ctx, n, url='http://www.dtc.umn.edu/~odlyzko/zeta_tables/zeros1'):
  187. n = int(n)
  188. if n < 0:
  189. return ctx.zetazero(-n).conjugate()
  190. if n == 0:
  191. raise ValueError("n must be nonzero")
  192. if n > len(_zeta_zeros) and n <= 100000:
  193. _load_zeta_zeros(url)
  194. if n > len(_zeta_zeros):
  195. raise NotImplementedError("n too large for zetazeros")
  196. return ctx.mpc(0.5, ctx.findroot(ctx.siegelz, _zeta_zeros[n-1]))
  197. @defun_wrapped
  198. def riemannr(ctx, x):
  199. if x == 0:
  200. return ctx.zero
  201. # Check if a simple asymptotic estimate is accurate enough
  202. if abs(x) > 1000:
  203. a = ctx.li(x)
  204. b = 0.5*ctx.li(ctx.sqrt(x))
  205. if abs(b) < abs(a)*ctx.eps:
  206. return a
  207. if abs(x) < 0.01:
  208. # XXX
  209. ctx.prec += int(-ctx.log(abs(x),2))
  210. # Sum Gram's series
  211. s = t = ctx.one
  212. u = ctx.ln(x)
  213. k = 1
  214. while abs(t) > abs(s)*ctx.eps:
  215. t = t * u / k
  216. s += t / (k * ctx._zeta_int(k+1))
  217. k += 1
  218. return s
  219. @defun_static
  220. def primepi(ctx, x):
  221. x = int(x)
  222. if x < 2:
  223. return 0
  224. return len(ctx.list_primes(x))
  225. # TODO: fix the interface wrt contexts
  226. @defun_wrapped
  227. def primepi2(ctx, x):
  228. x = int(x)
  229. if x < 2:
  230. return ctx._iv.zero
  231. if x < 2657:
  232. return ctx._iv.mpf(ctx.primepi(x))
  233. mid = ctx.li(x)
  234. # Schoenfeld's estimate for x >= 2657, assuming RH
  235. err = ctx.sqrt(x,rounding='u')*ctx.ln(x,rounding='u')/8/ctx.pi(rounding='d')
  236. a = ctx.floor((ctx._iv.mpf(mid)-err).a, rounding='d')
  237. b = ctx.ceil((ctx._iv.mpf(mid)+err).b, rounding='u')
  238. return ctx._iv.mpf([a,b])
  239. @defun_wrapped
  240. def primezeta(ctx, s):
  241. if ctx.isnan(s):
  242. return s
  243. if ctx.re(s) <= 0:
  244. raise ValueError("prime zeta function defined only for re(s) > 0")
  245. if s == 1:
  246. return ctx.inf
  247. if s == 0.5:
  248. return ctx.mpc(ctx.ninf, ctx.pi)
  249. r = ctx.re(s)
  250. if r > ctx.prec:
  251. return 0.5**s
  252. else:
  253. wp = ctx.prec + int(r)
  254. def terms():
  255. orig = ctx.prec
  256. # zeta ~ 1+eps; need to set precision
  257. # to get logarithm accurately
  258. k = 0
  259. while 1:
  260. k += 1
  261. u = ctx.moebius(k)
  262. if not u:
  263. continue
  264. ctx.prec = wp
  265. t = u*ctx.ln(ctx.zeta(k*s))/k
  266. if not t:
  267. return
  268. #print ctx.prec, ctx.nstr(t)
  269. ctx.prec = orig
  270. yield t
  271. return ctx.sum_accurately(terms)
  272. # TODO: for bernpoly and eulerpoly, ensure that all exact zeros are covered
  273. @defun_wrapped
  274. def bernpoly(ctx, n, z):
  275. # Slow implementation:
  276. #return sum(ctx.binomial(n,k)*ctx.bernoulli(k)*z**(n-k) for k in xrange(0,n+1))
  277. n = int(n)
  278. if n < 0:
  279. raise ValueError("Bernoulli polynomials only defined for n >= 0")
  280. if z == 0 or (z == 1 and n > 1):
  281. return ctx.bernoulli(n)
  282. if z == 0.5:
  283. return (ctx.ldexp(1,1-n)-1)*ctx.bernoulli(n)
  284. if n <= 3:
  285. if n == 0: return z ** 0
  286. if n == 1: return z - 0.5
  287. if n == 2: return (6*z*(z-1)+1)/6
  288. if n == 3: return z*(z*(z-1.5)+0.5)
  289. if ctx.isinf(z):
  290. return z ** n
  291. if ctx.isnan(z):
  292. return z
  293. if abs(z) > 2:
  294. def terms():
  295. t = ctx.one
  296. yield t
  297. r = ctx.one/z
  298. k = 1
  299. while k <= n:
  300. t = t*(n+1-k)/k*r
  301. if not (k > 2 and k & 1):
  302. yield t*ctx.bernoulli(k)
  303. k += 1
  304. return ctx.sum_accurately(terms) * z**n
  305. else:
  306. def terms():
  307. yield ctx.bernoulli(n)
  308. t = ctx.one
  309. k = 1
  310. while k <= n:
  311. t = t*(n+1-k)/k * z
  312. m = n-k
  313. if not (m > 2 and m & 1):
  314. yield t*ctx.bernoulli(m)
  315. k += 1
  316. return ctx.sum_accurately(terms)
  317. @defun_wrapped
  318. def eulerpoly(ctx, n, z):
  319. n = int(n)
  320. if n < 0:
  321. raise ValueError("Euler polynomials only defined for n >= 0")
  322. if n <= 2:
  323. if n == 0: return z ** 0
  324. if n == 1: return z - 0.5
  325. if n == 2: return z*(z-1)
  326. if ctx.isinf(z):
  327. return z**n
  328. if ctx.isnan(z):
  329. return z
  330. m = n+1
  331. if z == 0:
  332. return -2*(ctx.ldexp(1,m)-1)*ctx.bernoulli(m)/m * z**0
  333. if z == 1:
  334. return 2*(ctx.ldexp(1,m)-1)*ctx.bernoulli(m)/m * z**0
  335. if z == 0.5:
  336. if n % 2:
  337. return ctx.zero
  338. # Use exact code for Euler numbers
  339. if n < 100 or n*ctx.mag(0.46839865*n) < ctx.prec*0.25:
  340. return ctx.ldexp(ctx._eulernum(n), -n)
  341. # http://functions.wolfram.com/Polynomials/EulerE2/06/01/02/01/0002/
  342. def terms():
  343. t = ctx.one
  344. k = 0
  345. w = ctx.ldexp(1,n+2)
  346. while 1:
  347. v = n-k+1
  348. if not (v > 2 and v & 1):
  349. yield (2-w)*ctx.bernoulli(v)*t
  350. k += 1
  351. if k > n:
  352. break
  353. t = t*z*(n-k+2)/k
  354. w *= 0.5
  355. return ctx.sum_accurately(terms) / m
  356. @defun
  357. def eulernum(ctx, n, exact=False):
  358. n = int(n)
  359. if exact:
  360. return int(ctx._eulernum(n))
  361. if n < 100:
  362. return ctx.mpf(ctx._eulernum(n))
  363. if n % 2:
  364. return ctx.zero
  365. return ctx.ldexp(ctx.eulerpoly(n,0.5), n)
  366. # TODO: this should be implemented low-level
  367. def polylog_series(ctx, s, z):
  368. tol = +ctx.eps
  369. l = ctx.zero
  370. k = 1
  371. zk = z
  372. while 1:
  373. term = zk / k**s
  374. l += term
  375. if abs(term) < tol:
  376. break
  377. zk *= z
  378. k += 1
  379. return l
  380. def polylog_continuation(ctx, n, z):
  381. if n < 0:
  382. return z*0
  383. twopij = 2j * ctx.pi
  384. a = -twopij**n/ctx.fac(n) * ctx.bernpoly(n, ctx.ln(z)/twopij)
  385. if ctx._is_real_type(z) and z < 0:
  386. a = ctx._re(a)
  387. if ctx._im(z) < 0 or (ctx._im(z) == 0 and ctx._re(z) >= 1):
  388. a -= twopij*ctx.ln(z)**(n-1)/ctx.fac(n-1)
  389. return a
  390. def polylog_unitcircle(ctx, n, z):
  391. tol = +ctx.eps
  392. if n > 1:
  393. l = ctx.zero
  394. logz = ctx.ln(z)
  395. logmz = ctx.one
  396. m = 0
  397. while 1:
  398. if (n-m) != 1:
  399. term = ctx.zeta(n-m) * logmz / ctx.fac(m)
  400. if term and abs(term) < tol:
  401. break
  402. l += term
  403. logmz *= logz
  404. m += 1
  405. l += ctx.ln(z)**(n-1)/ctx.fac(n-1)*(ctx.harmonic(n-1)-ctx.ln(-ctx.ln(z)))
  406. elif n < 1: # else
  407. l = ctx.fac(-n)*(-ctx.ln(z))**(n-1)
  408. logz = ctx.ln(z)
  409. logkz = ctx.one
  410. k = 0
  411. while 1:
  412. b = ctx.bernoulli(k-n+1)
  413. if b:
  414. term = b*logkz/(ctx.fac(k)*(k-n+1))
  415. if abs(term) < tol:
  416. break
  417. l -= term
  418. logkz *= logz
  419. k += 1
  420. else:
  421. raise ValueError
  422. if ctx._is_real_type(z) and z < 0:
  423. l = ctx._re(l)
  424. return l
  425. def polylog_general(ctx, s, z):
  426. v = ctx.zero
  427. u = ctx.ln(z)
  428. if not abs(u) < 5: # theoretically |u| < 2*pi
  429. j = ctx.j
  430. v = 1-s
  431. y = ctx.ln(-z)/(2*ctx.pi*j)
  432. return ctx.gamma(v)*(j**v*ctx.zeta(v,0.5+y) + j**-v*ctx.zeta(v,0.5-y))/(2*ctx.pi)**v
  433. t = 1
  434. k = 0
  435. while 1:
  436. term = ctx.zeta(s-k) * t
  437. if abs(term) < ctx.eps:
  438. break
  439. v += term
  440. k += 1
  441. t *= u
  442. t /= k
  443. return ctx.gamma(1-s)*(-u)**(s-1) + v
  444. @defun_wrapped
  445. def polylog(ctx, s, z):
  446. s = ctx.convert(s)
  447. z = ctx.convert(z)
  448. if z == 1:
  449. return ctx.zeta(s)
  450. if z == -1:
  451. return -ctx.altzeta(s)
  452. if s == 0:
  453. return z/(1-z)
  454. if s == 1:
  455. return -ctx.ln(1-z)
  456. if s == -1:
  457. return z/(1-z)**2
  458. if abs(z) <= 0.75 or (not ctx.isint(s) and abs(z) < 0.9):
  459. return polylog_series(ctx, s, z)
  460. if abs(z) >= 1.4 and ctx.isint(s):
  461. return (-1)**(s+1)*polylog_series(ctx, s, 1/z) + polylog_continuation(ctx, int(ctx.re(s)), z)
  462. if ctx.isint(s):
  463. return polylog_unitcircle(ctx, int(ctx.re(s)), z)
  464. return polylog_general(ctx, s, z)
  465. @defun_wrapped
  466. def clsin(ctx, s, z, pi=False):
  467. if ctx.isint(s) and s < 0 and int(s) % 2 == 1:
  468. return z*0
  469. if pi:
  470. a = ctx.expjpi(z)
  471. else:
  472. a = ctx.expj(z)
  473. if ctx._is_real_type(z) and ctx._is_real_type(s):
  474. return ctx.im(ctx.polylog(s,a))
  475. b = 1/a
  476. return (-0.5j)*(ctx.polylog(s,a) - ctx.polylog(s,b))
  477. @defun_wrapped
  478. def clcos(ctx, s, z, pi=False):
  479. if ctx.isint(s) and s < 0 and int(s) % 2 == 0:
  480. return z*0
  481. if pi:
  482. a = ctx.expjpi(z)
  483. else:
  484. a = ctx.expj(z)
  485. if ctx._is_real_type(z) and ctx._is_real_type(s):
  486. return ctx.re(ctx.polylog(s,a))
  487. b = 1/a
  488. return 0.5*(ctx.polylog(s,a) + ctx.polylog(s,b))
  489. @defun
  490. def altzeta(ctx, s, **kwargs):
  491. try:
  492. return ctx._altzeta(s, **kwargs)
  493. except NotImplementedError:
  494. return ctx._altzeta_generic(s)
  495. @defun_wrapped
  496. def _altzeta_generic(ctx, s):
  497. if s == 1:
  498. return ctx.ln2 + 0*s
  499. return -ctx.powm1(2, 1-s) * ctx.zeta(s)
  500. @defun
  501. def zeta(ctx, s, a=1, derivative=0, method=None, **kwargs):
  502. d = int(derivative)
  503. if a == 1 and not (d or method):
  504. try:
  505. return ctx._zeta(s, **kwargs)
  506. except NotImplementedError:
  507. pass
  508. s = ctx.convert(s)
  509. prec = ctx.prec
  510. method = kwargs.get('method')
  511. verbose = kwargs.get('verbose')
  512. if (not s) and (not derivative):
  513. return ctx.mpf(0.5) - ctx._convert_param(a)[0]
  514. if a == 1 and method != 'euler-maclaurin':
  515. im = abs(ctx._im(s))
  516. re = abs(ctx._re(s))
  517. #if (im < prec or method == 'borwein') and not derivative:
  518. # try:
  519. # if verbose:
  520. # print "zeta: Attempting to use the Borwein algorithm"
  521. # return ctx._zeta(s, **kwargs)
  522. # except NotImplementedError:
  523. # if verbose:
  524. # print "zeta: Could not use the Borwein algorithm"
  525. # pass
  526. if abs(im) > 500*prec and 10*re < prec and derivative <= 4 or \
  527. method == 'riemann-siegel':
  528. try: # py2.4 compatible try block
  529. try:
  530. if verbose:
  531. print("zeta: Attempting to use the Riemann-Siegel algorithm")
  532. return ctx.rs_zeta(s, derivative, **kwargs)
  533. except NotImplementedError:
  534. if verbose:
  535. print("zeta: Could not use the Riemann-Siegel algorithm")
  536. pass
  537. finally:
  538. ctx.prec = prec
  539. if s == 1:
  540. return ctx.inf
  541. abss = abs(s)
  542. if abss == ctx.inf:
  543. if ctx.re(s) == ctx.inf:
  544. if d == 0:
  545. return ctx.one
  546. return ctx.zero
  547. return s*0
  548. elif ctx.isnan(abss):
  549. return 1/s
  550. if ctx.re(s) > 2*ctx.prec and a == 1 and not derivative:
  551. return ctx.one + ctx.power(2, -s)
  552. return +ctx._hurwitz(s, a, d, **kwargs)
  553. @defun
  554. def _hurwitz(ctx, s, a=1, d=0, **kwargs):
  555. prec = ctx.prec
  556. verbose = kwargs.get('verbose')
  557. try:
  558. extraprec = 10
  559. ctx.prec += extraprec
  560. # We strongly want to special-case rational a
  561. a, atype = ctx._convert_param(a)
  562. if ctx.re(s) < 0:
  563. if verbose:
  564. print("zeta: Attempting reflection formula")
  565. try:
  566. return _hurwitz_reflection(ctx, s, a, d, atype)
  567. except NotImplementedError:
  568. pass
  569. if verbose:
  570. print("zeta: Reflection formula failed")
  571. if verbose:
  572. print("zeta: Using the Euler-Maclaurin algorithm")
  573. while 1:
  574. ctx.prec = prec + extraprec
  575. T1, T2 = _hurwitz_em(ctx, s, a, d, prec+10, verbose)
  576. cancellation = ctx.mag(T1) - ctx.mag(T1+T2)
  577. if verbose:
  578. print("Term 1:", T1)
  579. print("Term 2:", T2)
  580. print("Cancellation:", cancellation, "bits")
  581. if cancellation < extraprec:
  582. return T1 + T2
  583. else:
  584. extraprec = max(2*extraprec, min(cancellation + 5, 100*prec))
  585. if extraprec > kwargs.get('maxprec', 100*prec):
  586. raise ctx.NoConvergence("zeta: too much cancellation")
  587. finally:
  588. ctx.prec = prec
  589. def _hurwitz_reflection(ctx, s, a, d, atype):
  590. # TODO: implement for derivatives
  591. if d != 0:
  592. raise NotImplementedError
  593. res = ctx.re(s)
  594. negs = -s
  595. # Integer reflection formula
  596. if ctx.isnpint(s):
  597. n = int(res)
  598. if n <= 0:
  599. return ctx.bernpoly(1-n, a) / (n-1)
  600. if not (atype == 'Q' or atype == 'Z'):
  601. raise NotImplementedError
  602. t = 1-s
  603. # We now require a to be standardized
  604. v = 0
  605. shift = 0
  606. b = a
  607. while ctx.re(b) > 1:
  608. b -= 1
  609. v -= b**negs
  610. shift -= 1
  611. while ctx.re(b) <= 0:
  612. v += b**negs
  613. b += 1
  614. shift += 1
  615. # Rational reflection formula
  616. try:
  617. p, q = a._mpq_
  618. except:
  619. assert a == int(a)
  620. p = int(a)
  621. q = 1
  622. p += shift*q
  623. assert 1 <= p <= q
  624. g = ctx.fsum(ctx.cospi(t/2-2*k*b)*ctx._hurwitz(t,(k,q)) \
  625. for k in range(1,q+1))
  626. g *= 2*ctx.gamma(t)/(2*ctx.pi*q)**t
  627. v += g
  628. return v
  629. def _hurwitz_em(ctx, s, a, d, prec, verbose):
  630. # May not be converted at this point
  631. a = ctx.convert(a)
  632. tol = -prec
  633. # Estimate number of terms for Euler-Maclaurin summation; could be improved
  634. M1 = 0
  635. M2 = prec // 3
  636. N = M2
  637. lsum = 0
  638. # This speeds up the recurrence for derivatives
  639. if ctx.isint(s):
  640. s = int(ctx._re(s))
  641. s1 = s-1
  642. while 1:
  643. # Truncated L-series
  644. l = ctx._zetasum(s, M1+a, M2-M1-1, [d])[0][0]
  645. #if d:
  646. # l = ctx.fsum((-ctx.ln(n+a))**d * (n+a)**negs for n in range(M1,M2))
  647. #else:
  648. # l = ctx.fsum((n+a)**negs for n in range(M1,M2))
  649. lsum += l
  650. M2a = M2+a
  651. logM2a = ctx.ln(M2a)
  652. logM2ad = logM2a**d
  653. logs = [logM2ad]
  654. logr = 1/logM2a
  655. rM2a = 1/M2a
  656. M2as = M2a**(-s)
  657. if d:
  658. tailsum = ctx.gammainc(d+1, s1*logM2a) / s1**(d+1)
  659. else:
  660. tailsum = 1/((s1)*(M2a)**s1)
  661. tailsum += 0.5 * logM2ad * M2as
  662. U = [1]
  663. r = M2as
  664. fact = 2
  665. for j in range(1, N+1):
  666. # TODO: the following could perhaps be tidied a bit
  667. j2 = 2*j
  668. if j == 1:
  669. upds = [1]
  670. else:
  671. upds = [j2-2, j2-1]
  672. for m in upds:
  673. D = min(m,d+1)
  674. if m <= d:
  675. logs.append(logs[-1] * logr)
  676. Un = [0]*(D+1)
  677. for i in xrange(D): Un[i] = (1-m-s)*U[i]
  678. for i in xrange(1,D+1): Un[i] += (d-(i-1))*U[i-1]
  679. U = Un
  680. r *= rM2a
  681. t = ctx.fdot(U, logs) * r * ctx.bernoulli(j2)/(-fact)
  682. tailsum += t
  683. if ctx.mag(t) < tol:
  684. return lsum, (-1)**d * tailsum
  685. fact *= (j2+1)*(j2+2)
  686. if verbose:
  687. print("Sum range:", M1, M2, "term magnitude", ctx.mag(t), "tolerance", tol)
  688. M1, M2 = M2, M2*2
  689. if ctx.re(s) < 0:
  690. N += N//2
  691. @defun
  692. def _zetasum(ctx, s, a, n, derivatives=[0], reflect=False):
  693. """
  694. Returns [xd0,xd1,...,xdr], [yd0,yd1,...ydr] where
  695. xdk = D^k ( 1/a^s + 1/(a+1)^s + ... + 1/(a+n)^s )
  696. ydk = D^k conj( 1/a^(1-s) + 1/(a+1)^(1-s) + ... + 1/(a+n)^(1-s) )
  697. D^k = kth derivative with respect to s, k ranges over the given list of
  698. derivatives (which should consist of either a single element
  699. or a range 0,1,...r). If reflect=False, the ydks are not computed.
  700. """
  701. #print "zetasum", s, a, n
  702. # don't use the fixed-point code if there are large exponentials
  703. if abs(ctx.re(s)) < 0.5 * ctx.prec:
  704. try:
  705. return ctx._zetasum_fast(s, a, n, derivatives, reflect)
  706. except NotImplementedError:
  707. pass
  708. negs = ctx.fneg(s, exact=True)
  709. have_derivatives = derivatives != [0]
  710. have_one_derivative = len(derivatives) == 1
  711. if not reflect:
  712. if not have_derivatives:
  713. return [ctx.fsum((a+k)**negs for k in xrange(n+1))], []
  714. if have_one_derivative:
  715. d = derivatives[0]
  716. x = ctx.fsum(ctx.ln(a+k)**d * (a+k)**negs for k in xrange(n+1))
  717. return [(-1)**d * x], []
  718. maxd = max(derivatives)
  719. if not have_one_derivative:
  720. derivatives = range(maxd+1)
  721. xs = [ctx.zero for d in derivatives]
  722. if reflect:
  723. ys = [ctx.zero for d in derivatives]
  724. else:
  725. ys = []
  726. for k in xrange(n+1):
  727. w = a + k
  728. xterm = w ** negs
  729. if reflect:
  730. yterm = ctx.conj(ctx.one / (w * xterm))
  731. if have_derivatives:
  732. logw = -ctx.ln(w)
  733. if have_one_derivative:
  734. logw = logw ** maxd
  735. xs[0] += xterm * logw
  736. if reflect:
  737. ys[0] += yterm * logw
  738. else:
  739. t = ctx.one
  740. for d in derivatives:
  741. xs[d] += xterm * t
  742. if reflect:
  743. ys[d] += yterm * t
  744. t *= logw
  745. else:
  746. xs[0] += xterm
  747. if reflect:
  748. ys[0] += yterm
  749. return xs, ys
  750. @defun
  751. def dirichlet(ctx, s, chi=[1], derivative=0):
  752. s = ctx.convert(s)
  753. q = len(chi)
  754. d = int(derivative)
  755. if d > 2:
  756. raise NotImplementedError("arbitrary order derivatives")
  757. prec = ctx.prec
  758. try:
  759. ctx.prec += 10
  760. if s == 1:
  761. have_pole = True
  762. for x in chi:
  763. if x and x != 1:
  764. have_pole = False
  765. h = +ctx.eps
  766. ctx.prec *= 2*(d+1)
  767. s += h
  768. if have_pole:
  769. return +ctx.inf
  770. z = ctx.zero
  771. for p in range(1,q+1):
  772. if chi[p%q]:
  773. if d == 1:
  774. z += chi[p%q] * (ctx.zeta(s, (p,q), 1) - \
  775. ctx.zeta(s, (p,q))*ctx.log(q))
  776. else:
  777. z += chi[p%q] * ctx.zeta(s, (p,q))
  778. z /= q**s
  779. finally:
  780. ctx.prec = prec
  781. return +z
  782. def secondzeta_main_term(ctx, s, a, **kwargs):
  783. tol = ctx.eps
  784. f = lambda n: ctx.gammainc(0.5*s, a*gamm**2, regularized=True)*gamm**(-s)
  785. totsum = term = ctx.zero
  786. mg = ctx.inf
  787. n = 0
  788. while mg > tol:
  789. totsum += term
  790. n += 1
  791. gamm = ctx.im(ctx.zetazero_memoized(n))
  792. term = f(n)
  793. mg = abs(term)
  794. err = 0
  795. if kwargs.get("error"):
  796. sg = ctx.re(s)
  797. err = 0.5*ctx.pi**(-1)*max(1,sg)*a**(sg-0.5)*ctx.log(gamm/(2*ctx.pi))*\
  798. ctx.gammainc(-0.5, a*gamm**2)/abs(ctx.gamma(s/2))
  799. err = abs(err)
  800. return +totsum, err, n
  801. def secondzeta_prime_term(ctx, s, a, **kwargs):
  802. tol = ctx.eps
  803. f = lambda n: ctx.gammainc(0.5*(1-s),0.25*ctx.log(n)**2 * a**(-1))*\
  804. ((0.5*ctx.log(n))**(s-1))*ctx.mangoldt(n)/ctx.sqrt(n)/\
  805. (2*ctx.gamma(0.5*s)*ctx.sqrt(ctx.pi))
  806. totsum = term = ctx.zero
  807. mg = ctx.inf
  808. n = 1
  809. while mg > tol or n < 9:
  810. totsum += term
  811. n += 1
  812. term = f(n)
  813. if term == 0:
  814. mg = ctx.inf
  815. else:
  816. mg = abs(term)
  817. if kwargs.get("error"):
  818. err = mg
  819. return +totsum, err, n
  820. def secondzeta_exp_term(ctx, s, a):
  821. if ctx.isint(s) and ctx.re(s) <= 0:
  822. m = int(round(ctx.re(s)))
  823. if not m & 1:
  824. return ctx.mpf('-0.25')**(-m//2)
  825. tol = ctx.eps
  826. f = lambda n: (0.25*a)**n/((n+0.5*s)*ctx.fac(n))
  827. totsum = ctx.zero
  828. term = f(0)
  829. mg = ctx.inf
  830. n = 0
  831. while mg > tol:
  832. totsum += term
  833. n += 1
  834. term = f(n)
  835. mg = abs(term)
  836. v = a**(0.5*s)*totsum/ctx.gamma(0.5*s)
  837. return v
  838. def secondzeta_singular_term(ctx, s, a, **kwargs):
  839. factor = a**(0.5*(s-1))/(4*ctx.sqrt(ctx.pi)*ctx.gamma(0.5*s))
  840. extraprec = ctx.mag(factor)
  841. ctx.prec += extraprec
  842. factor = a**(0.5*(s-1))/(4*ctx.sqrt(ctx.pi)*ctx.gamma(0.5*s))
  843. tol = ctx.eps
  844. f = lambda n: ctx.bernpoly(n,0.75)*(4*ctx.sqrt(a))**n*\
  845. ctx.gamma(0.5*n)/((s+n-1)*ctx.fac(n))
  846. totsum = ctx.zero
  847. mg1 = ctx.inf
  848. n = 1
  849. term = f(n)
  850. mg2 = abs(term)
  851. while mg2 > tol and mg2 <= mg1:
  852. totsum += term
  853. n += 1
  854. term = f(n)
  855. totsum += term
  856. n +=1
  857. term = f(n)
  858. mg1 = mg2
  859. mg2 = abs(term)
  860. totsum += term
  861. pole = -2*(s-1)**(-2)+(ctx.euler+ctx.log(16*ctx.pi**2*a))*(s-1)**(-1)
  862. st = factor*(pole+totsum)
  863. err = 0
  864. if kwargs.get("error"):
  865. if not ((mg2 > tol) and (mg2 <= mg1)):
  866. if mg2 <= tol:
  867. err = ctx.mpf(10)**int(ctx.log(abs(factor*tol),10))
  868. if mg2 > mg1:
  869. err = ctx.mpf(10)**int(ctx.log(abs(factor*mg1),10))
  870. err = max(err, ctx.eps*1.)
  871. ctx.prec -= extraprec
  872. return +st, err
  873. @defun
  874. def secondzeta(ctx, s, a = 0.015, **kwargs):
  875. r"""
  876. Evaluates the secondary zeta function `Z(s)`, defined for
  877. `\mathrm{Re}(s)>1` by
  878. .. math ::
  879. Z(s) = \sum_{n=1}^{\infty} \frac{1}{\tau_n^s}
  880. where `\frac12+i\tau_n` runs through the zeros of `\zeta(s)` with
  881. imaginary part positive.
  882. `Z(s)` extends to a meromorphic function on `\mathbb{C}` with a
  883. double pole at `s=1` and simple poles at the points `-2n` for
  884. `n=0`, 1, 2, ...
  885. **Examples**
  886. >>> from mpmath import *
  887. >>> mp.pretty = True; mp.dps = 15
  888. >>> secondzeta(2)
  889. 0.023104993115419
  890. >>> xi = lambda s: 0.5*s*(s-1)*pi**(-0.5*s)*gamma(0.5*s)*zeta(s)
  891. >>> Xi = lambda t: xi(0.5+t*j)
  892. >>> chop(-0.5*diff(Xi,0,n=2)/Xi(0))
  893. 0.023104993115419
  894. We may ask for an approximate error value::
  895. >>> secondzeta(0.5+100j, error=True)
  896. ((-0.216272011276718 - 0.844952708937228j), 2.22044604925031e-16)
  897. The function has poles at the negative odd integers,
  898. and dyadic rational values at the negative even integers::
  899. >>> mp.dps = 30
  900. >>> secondzeta(-8)
  901. -0.67236328125
  902. >>> secondzeta(-7)
  903. +inf
  904. **Implementation notes**
  905. The function is computed as sum of four terms `Z(s)=A(s)-P(s)+E(s)-S(s)`
  906. respectively main, prime, exponential and singular terms.
  907. The main term `A(s)` is computed from the zeros of zeta.
  908. The prime term depends on the von Mangoldt function.
  909. The singular term is responsible for the poles of the function.
  910. The four terms depends on a small parameter `a`. We may change the
  911. value of `a`. Theoretically this has no effect on the sum of the four
  912. terms, but in practice may be important.
  913. A smaller value of the parameter `a` makes `A(s)` depend on
  914. a smaller number of zeros of zeta, but `P(s)` uses more values of
  915. von Mangoldt function.
  916. We may also add a verbose option to obtain data about the
  917. values of the four terms.
  918. >>> mp.dps = 10
  919. >>> secondzeta(0.5 + 40j, error=True, verbose=True)
  920. main term = (-30190318549.138656312556 - 13964804384.624622876523j)
  921. computed using 19 zeros of zeta
  922. prime term = (132717176.89212754625045 + 188980555.17563978290601j)
  923. computed using 9 values of the von Mangoldt function
  924. exponential term = (542447428666.07179812536 + 362434922978.80192435203j)
  925. singular term = (512124392939.98154322355 + 348281138038.65531023921j)
  926. ((0.059471043 + 0.3463514534j), 1.455191523e-11)
  927. >>> secondzeta(0.5 + 40j, a=0.04, error=True, verbose=True)
  928. main term = (-151962888.19606243907725 - 217930683.90210294051982j)
  929. computed using 9 zeros of zeta
  930. prime term = (2476659342.3038722372461 + 28711581821.921627163136j)
  931. computed using 37 values of the von Mangoldt function
  932. exponential term = (178506047114.7838188264 + 819674143244.45677330576j)
  933. singular term = (175877424884.22441310708 + 790744630738.28669174871j)
  934. ((0.059471043 + 0.3463514534j), 1.455191523e-11)
  935. Notice the great cancellation between the four terms. Changing `a`, the
  936. four terms are very different numbers but the cancellation gives
  937. the good value of Z(s).
  938. **References**
  939. A. Voros, Zeta functions for the Riemann zeros, Ann. Institute Fourier,
  940. 53, (2003) 665--699.
  941. A. Voros, Zeta functions over Zeros of Zeta Functions, Lecture Notes
  942. of the Unione Matematica Italiana, Springer, 2009.
  943. """
  944. s = ctx.convert(s)
  945. a = ctx.convert(a)
  946. tol = ctx.eps
  947. if ctx.isint(s) and ctx.re(s) <= 1:
  948. if abs(s-1) < tol*1000:
  949. return ctx.inf
  950. m = int(round(ctx.re(s)))
  951. if m & 1:
  952. return ctx.inf
  953. else:
  954. return ((-1)**(-m//2)*\
  955. ctx.fraction(8-ctx.eulernum(-m,exact=True),2**(-m+3)))
  956. prec = ctx.prec
  957. try:
  958. t3 = secondzeta_exp_term(ctx, s, a)
  959. extraprec = max(ctx.mag(t3),0)
  960. ctx.prec += extraprec + 3
  961. t1, r1, gt = secondzeta_main_term(ctx,s,a,error='True', verbose='True')
  962. t2, r2, pt = secondzeta_prime_term(ctx,s,a,error='True', verbose='True')
  963. t4, r4 = secondzeta_singular_term(ctx,s,a,error='True')
  964. t3 = secondzeta_exp_term(ctx, s, a)
  965. err = r1+r2+r4
  966. t = t1-t2+t3-t4
  967. if kwargs.get("verbose"):
  968. print('main term =', t1)
  969. print(' computed using', gt, 'zeros of zeta')
  970. print('prime term =', t2)
  971. print(' computed using', pt, 'values of the von Mangoldt function')
  972. print('exponential term =', t3)
  973. print('singular term =', t4)
  974. finally:
  975. ctx.prec = prec
  976. if kwargs.get("error"):
  977. w = max(ctx.mag(abs(t)),0)
  978. err = max(err*2**w, ctx.eps*1.*2**w)
  979. return +t, err
  980. return +t
  981. @defun_wrapped
  982. def lerchphi(ctx, z, s, a):
  983. r"""
  984. Gives the Lerch transcendent, defined for `|z| < 1` and
  985. `\Re{a} > 0` by
  986. .. math ::
  987. \Phi(z,s,a) = \sum_{k=0}^{\infty} \frac{z^k}{(a+k)^s}
  988. and generally by the recurrence `\Phi(z,s,a) = z \Phi(z,s,a+1) + a^{-s}`
  989. along with the integral representation valid for `\Re{a} > 0`
  990. .. math ::
  991. \Phi(z,s,a) = \frac{1}{2 a^s} +
  992. \int_0^{\infty} \frac{z^t}{(a+t)^s} dt -
  993. 2 \int_0^{\infty} \frac{\sin(t \log z - s
  994. \operatorname{arctan}(t/a)}{(a^2 + t^2)^{s/2}
  995. (e^{2 \pi t}-1)} dt.
  996. The Lerch transcendent generalizes the Hurwitz zeta function :func:`zeta`
  997. (`z = 1`) and the polylogarithm :func:`polylog` (`a = 1`).
  998. **Examples**
  999. Several evaluations in terms of simpler functions::
  1000. >>> from mpmath import *
  1001. >>> mp.dps = 25; mp.pretty = True
  1002. >>> lerchphi(-1,2,0.5); 4*catalan
  1003. 3.663862376708876060218414
  1004. 3.663862376708876060218414
  1005. >>> diff(lerchphi, (-1,-2,1), (0,1,0)); 7*zeta(3)/(4*pi**2)
  1006. 0.2131391994087528954617607
  1007. 0.2131391994087528954617607
  1008. >>> lerchphi(-4,1,1); log(5)/4
  1009. 0.4023594781085250936501898
  1010. 0.4023594781085250936501898
  1011. >>> lerchphi(-3+2j,1,0.5); 2*atanh(sqrt(-3+2j))/sqrt(-3+2j)
  1012. (1.142423447120257137774002 + 0.2118232380980201350495795j)
  1013. (1.142423447120257137774002 + 0.2118232380980201350495795j)
  1014. Evaluation works for complex arguments and `|z| \ge 1`::
  1015. >>> lerchphi(1+2j, 3-j, 4+2j)
  1016. (0.002025009957009908600539469 + 0.003327897536813558807438089j)
  1017. >>> lerchphi(-2,2,-2.5)
  1018. -12.28676272353094275265944
  1019. >>> lerchphi(10,10,10)
  1020. (-4.462130727102185701817349e-11 - 1.575172198981096218823481e-12j)
  1021. >>> lerchphi(10,10,-10.5)
  1022. (112658784011940.5605789002 - 498113185.5756221777743631j)
  1023. Some degenerate cases::
  1024. >>> lerchphi(0,1,2)
  1025. 0.5
  1026. >>> lerchphi(0,1,-2)
  1027. -0.5
  1028. Reduction to simpler functions::
  1029. >>> lerchphi(1, 4.25+1j, 1)
  1030. (1.044674457556746668033975 - 0.04674508654012658932271226j)
  1031. >>> zeta(4.25+1j)
  1032. (1.044674457556746668033975 - 0.04674508654012658932271226j)
  1033. >>> lerchphi(1 - 0.5**10, 4.25+1j, 1)
  1034. (1.044629338021507546737197 - 0.04667768813963388181708101j)
  1035. >>> lerchphi(3, 4, 1)
  1036. (1.249503297023366545192592 - 0.2314252413375664776474462j)
  1037. >>> polylog(4, 3) / 3
  1038. (1.249503297023366545192592 - 0.2314252413375664776474462j)
  1039. >>> lerchphi(3, 4, 1 - 0.5**10)
  1040. (1.253978063946663945672674 - 0.2316736622836535468765376j)
  1041. **References**
  1042. 1. [DLMF]_ section 25.14
  1043. """
  1044. if z == 0:
  1045. return a ** (-s)
  1046. # Faster, but these cases are useful for testing right now
  1047. if z == 1:
  1048. return ctx.zeta(s, a)
  1049. if a == 1:
  1050. return ctx.polylog(s, z) / z
  1051. if ctx.re(a) < 1:
  1052. if ctx.isnpint(a):
  1053. raise ValueError("Lerch transcendent complex infinity")
  1054. m = int(ctx.ceil(1-ctx.re(a)))
  1055. v = ctx.zero
  1056. zpow = ctx.one
  1057. for n in xrange(m):
  1058. v += zpow / (a+n)**s
  1059. zpow *= z
  1060. return zpow * ctx.lerchphi(z,s, a+m) + v
  1061. g = ctx.ln(z)
  1062. v = 1/(2*a**s) + ctx.gammainc(1-s, -a*g) * (-g)**(s-1) / z**a
  1063. h = s / 2
  1064. r = 2*ctx.pi
  1065. f = lambda t: ctx.sin(s*ctx.atan(t/a)-t*g) / \
  1066. ((a**2+t**2)**h * ctx.expm1(r*t))
  1067. v += 2*ctx.quad(f, [0, ctx.inf])
  1068. if not ctx.im(z) and not ctx.im(s) and not ctx.im(a) and ctx.re(z) < 1:
  1069. v = ctx.chop(v)
  1070. return v