rszeta.py 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. """
  2. ---------------------------------------------------------------------
  3. .. sectionauthor:: Juan Arias de Reyna <arias@us.es>
  4. This module implements zeta-related functions using the Riemann-Siegel
  5. expansion: zeta_offline(s,k=0)
  6. * coef(J, eps): Need in the computation of Rzeta(s,k)
  7. * Rzeta_simul(s, der=0) computes Rzeta^(k)(s) and Rzeta^(k)(1-s) simultaneously
  8. for 0 <= k <= der. Used by zeta_offline and z_offline
  9. * Rzeta_set(s, derivatives) computes Rzeta^(k)(s) for given derivatives, used by
  10. z_half(t,k) and zeta_half
  11. * z_offline(w,k): Z(w) and its derivatives of order k <= 4
  12. * z_half(t,k): Z(t) (Riemann Siegel function) and its derivatives of order k <= 4
  13. * zeta_offline(s): zeta(s) and its derivatives of order k<= 4
  14. * zeta_half(1/2+it,k): zeta(s) and its derivatives of order k<= 4
  15. * rs_zeta(s,k=0) Computes zeta^(k)(s) Unifies zeta_half and zeta_offline
  16. * rs_z(w,k=0) Computes Z^(k)(w) Unifies z_offline and z_half
  17. ----------------------------------------------------------------------
  18. This program uses Riemann-Siegel expansion even to compute
  19. zeta(s) on points s = sigma + i t with sigma arbitrary not
  20. necessarily equal to 1/2.
  21. It is founded on a new deduction of the formula, with rigorous
  22. and sharp bounds for the terms and rest of this expansion.
  23. More information on the papers:
  24. J. Arias de Reyna, High Precision Computation of Riemann's
  25. Zeta Function by the Riemann-Siegel Formula I, II
  26. We refer to them as I, II.
  27. In them we shall find detailed explanation of all the
  28. procedure.
  29. The program uses Riemann-Siegel expansion.
  30. This is useful when t is big, ( say t > 10000 ).
  31. The precision is limited, roughly it can compute zeta(sigma+it)
  32. with an error less than exp(-c t) for some constant c depending
  33. on sigma. The program gives an error when the Riemann-Siegel
  34. formula can not compute to the wanted precision.
  35. """
  36. import math
  37. class RSCache(object):
  38. def __init__(ctx):
  39. ctx._rs_cache = [0, 10, {}, {}]
  40. from .functions import defun
  41. #-------------------------------------------------------------------------------#
  42. # #
  43. # coef(ctx, J, eps, _cache=[0, 10, {} ] ) #
  44. # #
  45. #-------------------------------------------------------------------------------#
  46. # This function computes the coefficients c[n] defined on (I, equation (47))
  47. # but see also (II, section 3.14).
  48. #
  49. # Since these coefficients are very difficult to compute we save the values
  50. # in a cache. So if we compute several values of the functions Rzeta(s) for
  51. # near values of s, we do not recompute these coefficients.
  52. #
  53. # c[n] are the Taylor coefficients of the function:
  54. #
  55. # F(z):= (exp(pi*j*(z*z/2+3/8))-j* sqrt(2) cos(pi*z/2))/(2*cos(pi *z))
  56. #
  57. #
  58. def _coef(ctx, J, eps):
  59. r"""
  60. Computes the coefficients `c_n` for `0\le n\le 2J` with error less than eps
  61. **Definition**
  62. The coefficients c_n are defined by
  63. .. math ::
  64. \begin{equation}
  65. F(z)=\frac{e^{\pi i
  66. \bigl(\frac{z^2}{2}+\frac38\bigr)}-i\sqrt{2}\cos\frac{\pi}{2}z}{2\cos\pi
  67. z}=\sum_{n=0}^\infty c_{2n} z^{2n}
  68. \end{equation}
  69. they are computed applying the relation
  70. .. math ::
  71. \begin{multline}
  72. c_{2n}=-\frac{i}{\sqrt{2}}\Bigl(\frac{\pi}{2}\Bigr)^{2n}
  73. \sum_{k=0}^n\frac{(-1)^k}{(2k)!}
  74. 2^{2n-2k}\frac{(-1)^{n-k}E_{2n-2k}}{(2n-2k)!}+\\
  75. +e^{3\pi i/8}\sum_{j=0}^n(-1)^j\frac{
  76. E_{2j}}{(2j)!}\frac{i^{n-j}\pi^{n+j}}{(n-j)!2^{n-j+1}}.
  77. \end{multline}
  78. """
  79. newJ = J+2 # compute more coefficients that are needed
  80. neweps6 = eps/2. # compute with a slight more precision that are needed
  81. # PREPARATION FOR THE COMPUTATION OF V(N) AND W(N)
  82. # See II Section 3.16
  83. #
  84. # Computing the exponent wpvw of the error II equation (81)
  85. wpvw = max(ctx.mag(10*(newJ+3)), 4*newJ+5-ctx.mag(neweps6))
  86. # Preparation of Euler numbers (we need until the 2*RS_NEWJ)
  87. E = ctx._eulernum(2*newJ)
  88. # Now we have in the cache all the needed Euler numbers.
  89. #
  90. # Computing the powers of pi
  91. #
  92. # We need to compute the powers pi**n for 1<= n <= 2*J
  93. # with relative error less than 2**(-wpvw)
  94. # it is easy to show that this is obtained
  95. # taking wppi as the least d with
  96. # 2**d>40*J and 2**d> 4.24 *newJ + 2**wpvw
  97. # In II Section 3.9 we need also that
  98. # wppi > wptcoef[0], and that the powers
  99. # here computed 0<= k <= 2*newJ are more
  100. # than those needed there that are 2*L-2.
  101. # so we need J >= L this will be checked
  102. # before computing tcoef[]
  103. wppi = max(ctx.mag(40*newJ), ctx.mag(newJ)+3 +wpvw)
  104. ctx.prec = wppi
  105. pipower = {}
  106. pipower[0] = ctx.one
  107. pipower[1] = ctx.pi
  108. for n in range(2,2*newJ+1):
  109. pipower[n] = pipower[n-1]*ctx.pi
  110. # COMPUTING THE COEFFICIENTS v(n) AND w(n)
  111. # see II equation (61) and equations (81) and (82)
  112. ctx.prec = wpvw+2
  113. v={}
  114. w={}
  115. for n in range(0,newJ+1):
  116. va = (-1)**n * ctx._eulernum(2*n)
  117. va = ctx.mpf(va)/ctx.fac(2*n)
  118. v[n]=va*pipower[2*n]
  119. for n in range(0,2*newJ+1):
  120. wa = ctx.one/ctx.fac(n)
  121. wa=wa/(2**n)
  122. w[n]=wa*pipower[n]
  123. # COMPUTATION OF THE CONVOLUTIONS RS_P1 AND RS_P2
  124. # See II Section 3.16
  125. ctx.prec = 15
  126. wpp1a = 9 - ctx.mag(neweps6)
  127. P1 = {}
  128. for n in range(0,newJ+1):
  129. ctx.prec = 15
  130. wpp1 = max(ctx.mag(10*(n+4)),4*n+wpp1a)
  131. ctx.prec = wpp1
  132. sump = 0
  133. for k in range(0,n+1):
  134. sump += ((-1)**k) * v[k]*w[2*n-2*k]
  135. P1[n]=((-1)**(n+1))*ctx.j*sump
  136. P2={}
  137. for n in range(0,newJ+1):
  138. ctx.prec = 15
  139. wpp2 = max(ctx.mag(10*(n+4)),4*n+wpp1a)
  140. ctx.prec = wpp2
  141. sump = 0
  142. for k in range(0,n+1):
  143. sump += (ctx.j**(n-k)) * v[k]*w[n-k]
  144. P2[n]=sump
  145. # COMPUTING THE COEFFICIENTS c[2n]
  146. # See II Section 3.14
  147. ctx.prec = 15
  148. wpc0 = 5 - ctx.mag(neweps6)
  149. wpc = max(6,4*newJ+wpc0)
  150. ctx.prec = wpc
  151. mu = ctx.sqrt(ctx.mpf('2'))/2
  152. nu = ctx.expjpi(3./8)/2
  153. c={}
  154. for n in range(0,newJ):
  155. ctx.prec = 15
  156. wpc = max(6,4*n+wpc0)
  157. ctx.prec = wpc
  158. c[2*n] = mu*P1[n]+nu*P2[n]
  159. for n in range(1,2*newJ,2):
  160. c[n] = 0
  161. return [newJ, neweps6, c, pipower]
  162. def coef(ctx, J, eps):
  163. _cache = ctx._rs_cache
  164. if J <= _cache[0] and eps >= _cache[1]:
  165. return _cache[2], _cache[3]
  166. orig = ctx._mp.prec
  167. try:
  168. data = _coef(ctx._mp, J, eps)
  169. finally:
  170. ctx._mp.prec = orig
  171. if ctx is not ctx._mp:
  172. data[2] = dict((k,ctx.convert(v)) for (k,v) in data[2].items())
  173. data[3] = dict((k,ctx.convert(v)) for (k,v) in data[3].items())
  174. ctx._rs_cache[:] = data
  175. return ctx._rs_cache[2], ctx._rs_cache[3]
  176. #-------------------------------------------------------------------------------#
  177. # #
  178. # Rzeta_simul(s,k=0) #
  179. # #
  180. #-------------------------------------------------------------------------------#
  181. # This function return a list with the values:
  182. # Rzeta(sigma+it), conj(Rzeta(1-sigma+it)),Rzeta'(sigma+it), conj(Rzeta'(1-sigma+it)),
  183. # .... , Rzeta^{(k)}(sigma+it), conj(Rzeta^{(k)}(1-sigma+it))
  184. #
  185. # Useful to compute the function zeta(s) and Z(w) or its derivatives.
  186. #
  187. def aux_M_Fp(ctx, xA, xeps4, a, xB1, xL):
  188. # COMPUTING M NUMBER OF DERIVATIVES Fp[m] TO COMPUTE
  189. # See II Section 3.11 equations (47) and (48)
  190. aux1 = 126.0657606*xA/xeps4 # 126.06.. = 316/sqrt(2*pi)
  191. aux1 = ctx.ln(aux1)
  192. aux2 = (2*ctx.ln(ctx.pi)+ctx.ln(xB1)+ctx.ln(a))/3 -ctx.ln(2*ctx.pi)/2
  193. m = 3*xL-3
  194. aux3= (ctx.loggamma(m+1)-ctx.loggamma(m/3.0+2))/2 -ctx.loggamma((m+1)/2.)
  195. while((aux1 < m*aux2+ aux3)and (m>1)):
  196. m = m - 1
  197. aux3 = (ctx.loggamma(m+1)-ctx.loggamma(m/3.0+2))/2 -ctx.loggamma((m+1)/2.)
  198. xM = m
  199. return xM
  200. def aux_J_needed(ctx, xA, xeps4, a, xB1, xM):
  201. # DETERMINATION OF J THE NUMBER OF TERMS NEEDED
  202. # IN THE TAYLOR SERIES OF F.
  203. # See II Section 3.11 equation (49))
  204. # Only determine one
  205. h1 = xeps4/(632*xA)
  206. h2 = xB1*a * 126.31337419529260248 # = pi^2*e^2*sqrt(3)
  207. h2 = h1 * ctx.power((h2/xM**2),(xM-1)/3) / xM
  208. h3 = min(h1,h2)
  209. return h3
  210. def Rzeta_simul(ctx, s, der=0):
  211. # First we take the value of ctx.prec
  212. wpinitial = ctx.prec
  213. # INITIALIZATION
  214. # Take the real and imaginary part of s
  215. t = ctx._im(s)
  216. xsigma = ctx._re(s)
  217. ysigma = 1 - xsigma
  218. # Now compute several parameter that appear on the program
  219. ctx.prec = 15
  220. a = ctx.sqrt(t/(2*ctx.pi))
  221. xasigma = a ** xsigma
  222. yasigma = a ** ysigma
  223. # We need a simple bound A1 < asigma (see II Section 3.1 and 3.3)
  224. xA1=ctx.power(2, ctx.mag(xasigma)-1)
  225. yA1=ctx.power(2, ctx.mag(yasigma)-1)
  226. # We compute various epsilon's (see II end of Section 3.1)
  227. eps = ctx.power(2, -wpinitial)
  228. eps1 = eps/6.
  229. xeps2 = eps * xA1/3.
  230. yeps2 = eps * yA1/3.
  231. # COMPUTING SOME COEFFICIENTS THAT DEPENDS
  232. # ON sigma
  233. # constant b and c (see I Theorem 2 formula (26) )
  234. # coefficients A and B1 (see I Section 6.1 equation (50))
  235. #
  236. # here we not need high precision
  237. ctx.prec = 15
  238. if xsigma > 0:
  239. xb = 2.
  240. xc = math.pow(9,xsigma)/4.44288
  241. # 4.44288 =(math.sqrt(2)*math.pi)
  242. xA = math.pow(9,xsigma)
  243. xB1 = 1
  244. else:
  245. xb = 2.25158 # math.sqrt( (3-2* math.log(2))*math.pi )
  246. xc = math.pow(2,-xsigma)/4.44288
  247. xA = math.pow(2,-xsigma)
  248. xB1 = 1.10789 # = 2*sqrt(1-log(2))
  249. if(ysigma > 0):
  250. yb = 2.
  251. yc = math.pow(9,ysigma)/4.44288
  252. # 4.44288 =(math.sqrt(2)*math.pi)
  253. yA = math.pow(9,ysigma)
  254. yB1 = 1
  255. else:
  256. yb = 2.25158 # math.sqrt( (3-2* math.log(2))*math.pi )
  257. yc = math.pow(2,-ysigma)/4.44288
  258. yA = math.pow(2,-ysigma)
  259. yB1 = 1.10789 # = 2*sqrt(1-log(2))
  260. # COMPUTING L THE NUMBER OF TERMS NEEDED IN THE RIEMANN-SIEGEL
  261. # CORRECTION
  262. # See II Section 3.2
  263. ctx.prec = 15
  264. xL = 1
  265. while 3*xc*ctx.gamma(xL*0.5) * ctx.power(xb*a,-xL) >= xeps2:
  266. xL = xL+1
  267. xL = max(2,xL)
  268. yL = 1
  269. while 3*yc*ctx.gamma(yL*0.5) * ctx.power(yb*a,-yL) >= yeps2:
  270. yL = yL+1
  271. yL = max(2,yL)
  272. # The number L has to satify some conditions.
  273. # If not RS can not compute Rzeta(s) with the prescribed precision
  274. # (see II, Section 3.2 condition (20) ) and
  275. # (II, Section 3.3 condition (22) ). Also we have added
  276. # an additional technical condition in Section 3.17 Proposition 17
  277. if ((3*xL >= 2*a*a/25.) or (3*xL+2+xsigma<0) or (abs(xsigma) > a/2.) or \
  278. (3*yL >= 2*a*a/25.) or (3*yL+2+ysigma<0) or (abs(ysigma) > a/2.)):
  279. ctx.prec = wpinitial
  280. raise NotImplementedError("Riemann-Siegel can not compute with such precision")
  281. # We take the maximum of the two values
  282. L = max(xL, yL)
  283. # INITIALIZATION (CONTINUATION)
  284. #
  285. # eps3 is the constant defined on (II, Section 3.5 equation (27) )
  286. # each term of the RS correction must be computed with error <= eps3
  287. xeps3 = xeps2/(4*xL)
  288. yeps3 = yeps2/(4*yL)
  289. # eps4 is defined on (II Section 3.6 equation (30) )
  290. # each component of the formula (II Section 3.6 equation (29) )
  291. # must be computed with error <= eps4
  292. xeps4 = xeps3/(3*xL)
  293. yeps4 = yeps3/(3*yL)
  294. # COMPUTING M NUMBER OF DERIVATIVES Fp[m] TO COMPUTE
  295. xM = aux_M_Fp(ctx, xA, xeps4, a, xB1, xL)
  296. yM = aux_M_Fp(ctx, yA, yeps4, a, yB1, yL)
  297. M = max(xM, yM)
  298. # COMPUTING NUMBER OF TERMS J NEEDED
  299. h3 = aux_J_needed(ctx, xA, xeps4, a, xB1, xM)
  300. h4 = aux_J_needed(ctx, yA, yeps4, a, yB1, yM)
  301. h3 = min(h3,h4)
  302. J = 12
  303. jvalue = (2*ctx.pi)**J / ctx.gamma(J+1)
  304. while jvalue > h3:
  305. J = J+1
  306. jvalue = (2*ctx.pi)*jvalue/J
  307. # COMPUTING eps5[m] for 1 <= m <= 21
  308. # See II Section 10 equation (43)
  309. # We choose the minimum of the two possibilities
  310. eps5={}
  311. xforeps5 = math.pi*math.pi*xB1*a
  312. yforeps5 = math.pi*math.pi*yB1*a
  313. for m in range(0,22):
  314. xaux1 = math.pow(xforeps5, m/3)/(316.*xA)
  315. yaux1 = math.pow(yforeps5, m/3)/(316.*yA)
  316. aux1 = min(xaux1, yaux1)
  317. aux2 = ctx.gamma(m+1)/ctx.gamma(m/3.0+0.5)
  318. aux2 = math.sqrt(aux2)
  319. eps5[m] = (aux1*aux2*min(xeps4,yeps4))
  320. # COMPUTING wpfp
  321. # See II Section 3.13 equation (59)
  322. twenty = min(3*L-3, 21)+1
  323. aux = 6812*J
  324. wpfp = ctx.mag(44*J)
  325. for m in range(0,twenty):
  326. wpfp = max(wpfp, ctx.mag(aux*ctx.gamma(m+1)/eps5[m]))
  327. # COMPUTING N AND p
  328. # See II Section
  329. ctx.prec = wpfp + ctx.mag(t)+20
  330. a = ctx.sqrt(t/(2*ctx.pi))
  331. N = ctx.floor(a)
  332. p = 1-2*(a-N)
  333. # now we get a rounded version of p
  334. # to the precision wpfp
  335. # this possibly is not necessary
  336. num=ctx.floor(p*(ctx.mpf('2')**wpfp))
  337. difference = p * (ctx.mpf('2')**wpfp)-num
  338. if (difference < 0.5):
  339. num = num
  340. else:
  341. num = num+1
  342. p = ctx.convert(num * (ctx.mpf('2')**(-wpfp)))
  343. # COMPUTING THE COEFFICIENTS c[n] = cc[n]
  344. # We shall use the notation cc[n], since there is
  345. # a constant that is called c
  346. # See II Section 3.14
  347. # We compute the coefficients and also save then in a
  348. # cache. The bulk of the computation is passed to
  349. # the function coef()
  350. #
  351. # eps6 is defined in II Section 3.13 equation (58)
  352. eps6 = ctx.power(ctx.convert(2*ctx.pi), J)/(ctx.gamma(J+1)*3*J)
  353. # Now we compute the coefficients
  354. cc = {}
  355. cont = {}
  356. cont, pipowers = coef(ctx, J, eps6)
  357. cc=cont.copy() # we need a copy since we have to change his values.
  358. Fp={} # this is the adequate locus of this
  359. for n in range(M, 3*L-2):
  360. Fp[n] = 0
  361. Fp={}
  362. ctx.prec = wpfp
  363. for m in range(0,M+1):
  364. sumP = 0
  365. for k in range(2*J-m-1,-1,-1):
  366. sumP = (sumP * p)+ cc[k]
  367. Fp[m] = sumP
  368. # preparation of the new coefficients
  369. for k in range(0,2*J-m-1):
  370. cc[k] = (k+1)* cc[k+1]
  371. # COMPUTING THE NUMBERS xd[u,n,k], yd[u,n,k]
  372. # See II Section 3.17
  373. #
  374. # First we compute the working precisions xwpd[k]
  375. # Se II equation (92)
  376. xwpd={}
  377. d1 = max(6,ctx.mag(40*L*L))
  378. xd2 = 13+ctx.mag((1+abs(xsigma))*xA)-ctx.mag(xeps4)-1
  379. xconst = ctx.ln(8/(ctx.pi*ctx.pi*a*a*xB1*xB1)) /2
  380. for n in range(0,L):
  381. xd3 = ctx.mag(ctx.sqrt(ctx.gamma(n-0.5)))-ctx.floor(n*xconst)+xd2
  382. xwpd[n]=max(xd3,d1)
  383. # procedure of II Section 3.17
  384. ctx.prec = xwpd[1]+10
  385. xpsigma = 1-(2*xsigma)
  386. xd = {}
  387. xd[0,0,-2]=0; xd[0,0,-1]=0; xd[0,0,0]=1; xd[0,0,1]=0
  388. xd[0,-1,-2]=0; xd[0,-1,-1]=0; xd[0,-1,0]=1; xd[0,-1,1]=0
  389. for n in range(1,L):
  390. ctx.prec = xwpd[n]+10
  391. for k in range(0,3*n//2+1):
  392. m = 3*n-2*k
  393. if(m!=0):
  394. m1 = ctx.one/m
  395. c1= m1/4
  396. c2=(xpsigma*m1)/2
  397. c3=-(m+1)
  398. xd[0,n,k]=c3*xd[0,n-1,k-2]+c1*xd[0,n-1,k]+c2*xd[0,n-1,k-1]
  399. else:
  400. xd[0,n,k]=0
  401. for r in range(0,k):
  402. add=xd[0,n,r]*(ctx.mpf('1.0')*ctx.fac(2*k-2*r)/ctx.fac(k-r))
  403. xd[0,n,k] -= ((-1)**(k-r))*add
  404. xd[0,n,-2]=0; xd[0,n,-1]=0; xd[0,n,3*n//2+1]=0
  405. for mu in range(-2,der+1):
  406. for n in range(-2,L):
  407. for k in range(-3,max(1,3*n//2+2)):
  408. if( (mu<0)or (n<0) or(k<0)or (k>3*n//2)):
  409. xd[mu,n,k] = 0
  410. for mu in range(1,der+1):
  411. for n in range(0,L):
  412. ctx.prec = xwpd[n]+10
  413. for k in range(0,3*n//2+1):
  414. aux=(2*mu-2)*xd[mu-2,n-2,k-3]+2*(xsigma+n-2)*xd[mu-1,n-2,k-3]
  415. xd[mu,n,k] = aux - xd[mu-1,n-1,k-1]
  416. # Now we compute the working precisions ywpd[k]
  417. # Se II equation (92)
  418. ywpd={}
  419. d1 = max(6,ctx.mag(40*L*L))
  420. yd2 = 13+ctx.mag((1+abs(ysigma))*yA)-ctx.mag(yeps4)-1
  421. yconst = ctx.ln(8/(ctx.pi*ctx.pi*a*a*yB1*yB1)) /2
  422. for n in range(0,L):
  423. yd3 = ctx.mag(ctx.sqrt(ctx.gamma(n-0.5)))-ctx.floor(n*yconst)+yd2
  424. ywpd[n]=max(yd3,d1)
  425. # procedure of II Section 3.17
  426. ctx.prec = ywpd[1]+10
  427. ypsigma = 1-(2*ysigma)
  428. yd = {}
  429. yd[0,0,-2]=0; yd[0,0,-1]=0; yd[0,0,0]=1; yd[0,0,1]=0
  430. yd[0,-1,-2]=0; yd[0,-1,-1]=0; yd[0,-1,0]=1; yd[0,-1,1]=0
  431. for n in range(1,L):
  432. ctx.prec = ywpd[n]+10
  433. for k in range(0,3*n//2+1):
  434. m = 3*n-2*k
  435. if(m!=0):
  436. m1 = ctx.one/m
  437. c1= m1/4
  438. c2=(ypsigma*m1)/2
  439. c3=-(m+1)
  440. yd[0,n,k]=c3*yd[0,n-1,k-2]+c1*yd[0,n-1,k]+c2*yd[0,n-1,k-1]
  441. else:
  442. yd[0,n,k]=0
  443. for r in range(0,k):
  444. add=yd[0,n,r]*(ctx.mpf('1.0')*ctx.fac(2*k-2*r)/ctx.fac(k-r))
  445. yd[0,n,k] -= ((-1)**(k-r))*add
  446. yd[0,n,-2]=0; yd[0,n,-1]=0; yd[0,n,3*n//2+1]=0
  447. for mu in range(-2,der+1):
  448. for n in range(-2,L):
  449. for k in range(-3,max(1,3*n//2+2)):
  450. if( (mu<0)or (n<0) or(k<0)or (k>3*n//2)):
  451. yd[mu,n,k] = 0
  452. for mu in range(1,der+1):
  453. for n in range(0,L):
  454. ctx.prec = ywpd[n]+10
  455. for k in range(0,3*n//2+1):
  456. aux=(2*mu-2)*yd[mu-2,n-2,k-3]+2*(ysigma+n-2)*yd[mu-1,n-2,k-3]
  457. yd[mu,n,k] = aux - yd[mu-1,n-1,k-1]
  458. # COMPUTING THE COEFFICIENTS xtcoef[k,l]
  459. # See II Section 3.9
  460. #
  461. # computing the needed wp
  462. xwptcoef={}
  463. xwpterm={}
  464. ctx.prec = 15
  465. c1 = ctx.mag(40*(L+2))
  466. xc2 = ctx.mag(68*(L+2)*xA)
  467. xc4 = ctx.mag(xB1*a*math.sqrt(ctx.pi))-1
  468. for k in range(0,L):
  469. xc3 = xc2 - k*xc4+ctx.mag(ctx.fac(k+0.5))/2.
  470. xwptcoef[k] = (max(c1,xc3-ctx.mag(xeps4)+1)+1 +20)*1.5
  471. xwpterm[k] = (max(c1,ctx.mag(L+2)+xc3-ctx.mag(xeps3)+1)+1 +20)
  472. ywptcoef={}
  473. ywpterm={}
  474. ctx.prec = 15
  475. c1 = ctx.mag(40*(L+2))
  476. yc2 = ctx.mag(68*(L+2)*yA)
  477. yc4 = ctx.mag(yB1*a*math.sqrt(ctx.pi))-1
  478. for k in range(0,L):
  479. yc3 = yc2 - k*yc4+ctx.mag(ctx.fac(k+0.5))/2.
  480. ywptcoef[k] = ((max(c1,yc3-ctx.mag(yeps4)+1))+10)*1.5
  481. ywpterm[k] = (max(c1,ctx.mag(L+2)+yc3-ctx.mag(yeps3)+1)+1)+10
  482. # check of power of pi
  483. # computing the fortcoef[mu,k,ell]
  484. xfortcoef={}
  485. for mu in range(0,der+1):
  486. for k in range(0,L):
  487. for ell in range(-2,3*k//2+1):
  488. xfortcoef[mu,k,ell]=0
  489. for mu in range(0,der+1):
  490. for k in range(0,L):
  491. ctx.prec = xwptcoef[k]
  492. for ell in range(0,3*k//2+1):
  493. xfortcoef[mu,k,ell]=xd[mu,k,ell]*Fp[3*k-2*ell]/pipowers[2*k-ell]
  494. xfortcoef[mu,k,ell]=xfortcoef[mu,k,ell]/((2*ctx.j)**ell)
  495. def trunc_a(t):
  496. wp = ctx.prec
  497. ctx.prec = wp + 2
  498. aa = ctx.sqrt(t/(2*ctx.pi))
  499. ctx.prec = wp
  500. return aa
  501. # computing the tcoef[k,ell]
  502. xtcoef={}
  503. for mu in range(0,der+1):
  504. for k in range(0,L):
  505. for ell in range(-2,3*k//2+1):
  506. xtcoef[mu,k,ell]=0
  507. ctx.prec = max(xwptcoef[0],ywptcoef[0])+3
  508. aa= trunc_a(t)
  509. la = -ctx.ln(aa)
  510. for chi in range(0,der+1):
  511. for k in range(0,L):
  512. ctx.prec = xwptcoef[k]
  513. for ell in range(0,3*k//2+1):
  514. xtcoef[chi,k,ell] =0
  515. for mu in range(0, chi+1):
  516. tcoefter=ctx.binomial(chi,mu)*ctx.power(la,mu)*xfortcoef[chi-mu,k,ell]
  517. xtcoef[chi,k,ell] += tcoefter
  518. # COMPUTING THE COEFFICIENTS ytcoef[k,l]
  519. # See II Section 3.9
  520. #
  521. # computing the needed wp
  522. # check of power of pi
  523. # computing the fortcoef[mu,k,ell]
  524. yfortcoef={}
  525. for mu in range(0,der+1):
  526. for k in range(0,L):
  527. for ell in range(-2,3*k//2+1):
  528. yfortcoef[mu,k,ell]=0
  529. for mu in range(0,der+1):
  530. for k in range(0,L):
  531. ctx.prec = ywptcoef[k]
  532. for ell in range(0,3*k//2+1):
  533. yfortcoef[mu,k,ell]=yd[mu,k,ell]*Fp[3*k-2*ell]/pipowers[2*k-ell]
  534. yfortcoef[mu,k,ell]=yfortcoef[mu,k,ell]/((2*ctx.j)**ell)
  535. # computing the tcoef[k,ell]
  536. ytcoef={}
  537. for chi in range(0,der+1):
  538. for k in range(0,L):
  539. for ell in range(-2,3*k//2+1):
  540. ytcoef[chi,k,ell]=0
  541. for chi in range(0,der+1):
  542. for k in range(0,L):
  543. ctx.prec = ywptcoef[k]
  544. for ell in range(0,3*k//2+1):
  545. ytcoef[chi,k,ell] =0
  546. for mu in range(0, chi+1):
  547. tcoefter=ctx.binomial(chi,mu)*ctx.power(la,mu)*yfortcoef[chi-mu,k,ell]
  548. ytcoef[chi,k,ell] += tcoefter
  549. # COMPUTING tv[k,ell]
  550. # See II Section 3.8
  551. #
  552. # a has a good value
  553. ctx.prec = max(xwptcoef[0], ywptcoef[0])+2
  554. av = {}
  555. av[0] = 1
  556. av[1] = av[0]/a
  557. ctx.prec = max(xwptcoef[0],ywptcoef[0])
  558. for k in range(2,L):
  559. av[k] = av[k-1] * av[1]
  560. # Computing the quotients
  561. xtv = {}
  562. for chi in range(0,der+1):
  563. for k in range(0,L):
  564. ctx.prec = xwptcoef[k]
  565. for ell in range(0,3*k//2+1):
  566. xtv[chi,k,ell] = xtcoef[chi,k,ell]* av[k]
  567. # Computing the quotients
  568. ytv = {}
  569. for chi in range(0,der+1):
  570. for k in range(0,L):
  571. ctx.prec = ywptcoef[k]
  572. for ell in range(0,3*k//2+1):
  573. ytv[chi,k,ell] = ytcoef[chi,k,ell]* av[k]
  574. # COMPUTING THE TERMS xterm[k]
  575. # See II Section 3.6
  576. xterm = {}
  577. for chi in range(0,der+1):
  578. for n in range(0,L):
  579. ctx.prec = xwpterm[n]
  580. te = 0
  581. for k in range(0, 3*n//2+1):
  582. te += xtv[chi,n,k]
  583. xterm[chi,n] = te
  584. # COMPUTING THE TERMS yterm[k]
  585. # See II Section 3.6
  586. yterm = {}
  587. for chi in range(0,der+1):
  588. for n in range(0,L):
  589. ctx.prec = ywpterm[n]
  590. te = 0
  591. for k in range(0, 3*n//2+1):
  592. te += ytv[chi,n,k]
  593. yterm[chi,n] = te
  594. # COMPUTING rssum
  595. # See II Section 3.5
  596. xrssum={}
  597. ctx.prec=15
  598. xrsbound = math.sqrt(ctx.pi) * xc /(xb*a)
  599. ctx.prec=15
  600. xwprssum = ctx.mag(4.4*((L+3)**2)*xrsbound / xeps2)
  601. xwprssum = max(xwprssum, ctx.mag(10*(L+1)))
  602. ctx.prec = xwprssum
  603. for chi in range(0,der+1):
  604. xrssum[chi] = 0
  605. for k in range(1,L+1):
  606. xrssum[chi] += xterm[chi,L-k]
  607. yrssum={}
  608. ctx.prec=15
  609. yrsbound = math.sqrt(ctx.pi) * yc /(yb*a)
  610. ctx.prec=15
  611. ywprssum = ctx.mag(4.4*((L+3)**2)*yrsbound / yeps2)
  612. ywprssum = max(ywprssum, ctx.mag(10*(L+1)))
  613. ctx.prec = ywprssum
  614. for chi in range(0,der+1):
  615. yrssum[chi] = 0
  616. for k in range(1,L+1):
  617. yrssum[chi] += yterm[chi,L-k]
  618. # COMPUTING S3
  619. # See II Section 3.19
  620. ctx.prec = 15
  621. A2 = 2**(max(ctx.mag(abs(xrssum[0])), ctx.mag(abs(yrssum[0]))))
  622. eps8 = eps/(3*A2)
  623. T = t *ctx.ln(t/(2*ctx.pi))
  624. xwps3 = 5 + ctx.mag((1+(2/eps8)*ctx.power(a,-xsigma))*T)
  625. ywps3 = 5 + ctx.mag((1+(2/eps8)*ctx.power(a,-ysigma))*T)
  626. ctx.prec = max(xwps3, ywps3)
  627. tpi = t/(2*ctx.pi)
  628. arg = (t/2)*ctx.ln(tpi)-(t/2)-ctx.pi/8
  629. U = ctx.expj(-arg)
  630. a = trunc_a(t)
  631. xasigma = ctx.power(a, -xsigma)
  632. yasigma = ctx.power(a, -ysigma)
  633. xS3 = ((-1)**(N-1)) * xasigma * U
  634. yS3 = ((-1)**(N-1)) * yasigma * U
  635. # COMPUTING S1 the zetasum
  636. # See II Section 3.18
  637. ctx.prec = 15
  638. xwpsum = 4+ ctx.mag((N+ctx.power(N,1-xsigma))*ctx.ln(N) /eps1)
  639. ywpsum = 4+ ctx.mag((N+ctx.power(N,1-ysigma))*ctx.ln(N) /eps1)
  640. wpsum = max(xwpsum, ywpsum)
  641. ctx.prec = wpsum +10
  642. '''
  643. # This can be improved
  644. xS1={}
  645. yS1={}
  646. for chi in range(0,der+1):
  647. xS1[chi] = 0
  648. yS1[chi] = 0
  649. for n in range(1,int(N)+1):
  650. ln = ctx.ln(n)
  651. xexpn = ctx.exp(-ln*(xsigma+ctx.j*t))
  652. yexpn = ctx.conj(1/(n*xexpn))
  653. for chi in range(0,der+1):
  654. pown = ctx.power(-ln, chi)
  655. xterm = pown*xexpn
  656. yterm = pown*yexpn
  657. xS1[chi] += xterm
  658. yS1[chi] += yterm
  659. '''
  660. xS1, yS1 = ctx._zetasum(s, 1, int(N)-1, range(0,der+1), True)
  661. # END OF COMPUTATION of xrz, yrz
  662. # See II Section 3.1
  663. ctx.prec = 15
  664. xabsS1 = abs(xS1[der])
  665. xabsS2 = abs(xrssum[der] * xS3)
  666. xwpend = max(6, wpinitial+ctx.mag(6*(3*xabsS1+7*xabsS2) ) )
  667. ctx.prec = xwpend
  668. xrz={}
  669. for chi in range(0,der+1):
  670. xrz[chi] = xS1[chi]+xrssum[chi]*xS3
  671. ctx.prec = 15
  672. yabsS1 = abs(yS1[der])
  673. yabsS2 = abs(yrssum[der] * yS3)
  674. ywpend = max(6, wpinitial+ctx.mag(6*(3*yabsS1+7*yabsS2) ) )
  675. ctx.prec = ywpend
  676. yrz={}
  677. for chi in range(0,der+1):
  678. yrz[chi] = yS1[chi]+yrssum[chi]*yS3
  679. yrz[chi] = ctx.conj(yrz[chi])
  680. ctx.prec = wpinitial
  681. return xrz, yrz
  682. def Rzeta_set(ctx, s, derivatives=[0]):
  683. r"""
  684. Computes several derivatives of the auxiliary function of Riemann `R(s)`.
  685. **Definition**
  686. The function is defined by
  687. .. math ::
  688. \begin{equation}
  689. {\mathop{\mathcal R }\nolimits}(s)=
  690. \int_{0\swarrow1}\frac{x^{-s} e^{\pi i x^2}}{e^{\pi i x}-
  691. e^{-\pi i x}}\,dx
  692. \end{equation}
  693. To this function we apply the Riemann-Siegel expansion.
  694. """
  695. der = max(derivatives)
  696. # First we take the value of ctx.prec
  697. # During the computation we will change ctx.prec, and finally we will
  698. # restaurate the initial value
  699. wpinitial = ctx.prec
  700. # Take the real and imaginary part of s
  701. t = ctx._im(s)
  702. sigma = ctx._re(s)
  703. # Now compute several parameter that appear on the program
  704. ctx.prec = 15
  705. a = ctx.sqrt(t/(2*ctx.pi)) # Careful
  706. asigma = ctx.power(a, sigma) # Careful
  707. # We need a simple bound A1 < asigma (see II Section 3.1 and 3.3)
  708. A1 = ctx.power(2, ctx.mag(asigma)-1)
  709. # We compute various epsilon's (see II end of Section 3.1)
  710. eps = ctx.power(2, -wpinitial)
  711. eps1 = eps/6.
  712. eps2 = eps * A1/3.
  713. # COMPUTING SOME COEFFICIENTS THAT DEPENDS
  714. # ON sigma
  715. # constant b and c (see I Theorem 2 formula (26) )
  716. # coefficients A and B1 (see I Section 6.1 equation (50))
  717. # here we not need high precision
  718. ctx.prec = 15
  719. if sigma > 0:
  720. b = 2.
  721. c = math.pow(9,sigma)/4.44288
  722. # 4.44288 =(math.sqrt(2)*math.pi)
  723. A = math.pow(9,sigma)
  724. B1 = 1
  725. else:
  726. b = 2.25158 # math.sqrt( (3-2* math.log(2))*math.pi )
  727. c = math.pow(2,-sigma)/4.44288
  728. A = math.pow(2,-sigma)
  729. B1 = 1.10789 # = 2*sqrt(1-log(2))
  730. # COMPUTING L THE NUMBER OF TERMS NEEDED IN THE RIEMANN-SIEGEL
  731. # CORRECTION
  732. # See II Section 3.2
  733. ctx.prec = 15
  734. L = 1
  735. while 3*c*ctx.gamma(L*0.5) * ctx.power(b*a,-L) >= eps2:
  736. L = L+1
  737. L = max(2,L)
  738. # The number L has to satify some conditions.
  739. # If not RS can not compute Rzeta(s) with the prescribed precision
  740. # (see II, Section 3.2 condition (20) ) and
  741. # (II, Section 3.3 condition (22) ). Also we have added
  742. # an additional technical condition in Section 3.17 Proposition 17
  743. if ((3*L >= 2*a*a/25.) or (3*L+2+sigma<0) or (abs(sigma)> a/2.)):
  744. #print 'Error Riemann-Siegel can not compute with such precision'
  745. ctx.prec = wpinitial
  746. raise NotImplementedError("Riemann-Siegel can not compute with such precision")
  747. # INITIALIZATION (CONTINUATION)
  748. #
  749. # eps3 is the constant defined on (II, Section 3.5 equation (27) )
  750. # each term of the RS correction must be computed with error <= eps3
  751. eps3 = eps2/(4*L)
  752. # eps4 is defined on (II Section 3.6 equation (30) )
  753. # each component of the formula (II Section 3.6 equation (29) )
  754. # must be computed with error <= eps4
  755. eps4 = eps3/(3*L)
  756. # COMPUTING M. NUMBER OF DERIVATIVES Fp[m] TO COMPUTE
  757. M = aux_M_Fp(ctx, A, eps4, a, B1, L)
  758. Fp = {}
  759. for n in range(M, 3*L-2):
  760. Fp[n] = 0
  761. # But I have not seen an instance of M != 3*L-3
  762. #
  763. # DETERMINATION OF J THE NUMBER OF TERMS NEEDED
  764. # IN THE TAYLOR SERIES OF F.
  765. # See II Section 3.11 equation (49))
  766. h1 = eps4/(632*A)
  767. h2 = ctx.pi*ctx.pi*B1*a *ctx.sqrt(3)*math.e*math.e
  768. h2 = h1 * ctx.power((h2/M**2),(M-1)/3) / M
  769. h3 = min(h1,h2)
  770. J=12
  771. jvalue = (2*ctx.pi)**J / ctx.gamma(J+1)
  772. while jvalue > h3:
  773. J = J+1
  774. jvalue = (2*ctx.pi)*jvalue/J
  775. # COMPUTING eps5[m] for 1 <= m <= 21
  776. # See II Section 10 equation (43)
  777. eps5={}
  778. foreps5 = math.pi*math.pi*B1*a
  779. for m in range(0,22):
  780. aux1 = math.pow(foreps5, m/3)/(316.*A)
  781. aux2 = ctx.gamma(m+1)/ctx.gamma(m/3.0+0.5)
  782. aux2 = math.sqrt(aux2)
  783. eps5[m] = aux1*aux2*eps4
  784. # COMPUTING wpfp
  785. # See II Section 3.13 equation (59)
  786. twenty = min(3*L-3, 21)+1
  787. aux = 6812*J
  788. wpfp = ctx.mag(44*J)
  789. for m in range(0, twenty):
  790. wpfp = max(wpfp, ctx.mag(aux*ctx.gamma(m+1)/eps5[m]))
  791. # COMPUTING N AND p
  792. # See II Section
  793. ctx.prec = wpfp + ctx.mag(t) + 20
  794. a = ctx.sqrt(t/(2*ctx.pi))
  795. N = ctx.floor(a)
  796. p = 1-2*(a-N)
  797. # now we get a rounded version of p to the precision wpfp
  798. # this possibly is not necessary
  799. num = ctx.floor(p*(ctx.mpf(2)**wpfp))
  800. difference = p * (ctx.mpf(2)**wpfp)-num
  801. if difference < 0.5:
  802. num = num
  803. else:
  804. num = num+1
  805. p = ctx.convert(num * (ctx.mpf(2)**(-wpfp)))
  806. # COMPUTING THE COEFFICIENTS c[n] = cc[n]
  807. # We shall use the notation cc[n], since there is
  808. # a constant that is called c
  809. # See II Section 3.14
  810. # We compute the coefficients and also save then in a
  811. # cache. The bulk of the computation is passed to
  812. # the function coef()
  813. #
  814. # eps6 is defined in II Section 3.13 equation (58)
  815. eps6 = ctx.power(2*ctx.pi, J)/(ctx.gamma(J+1)*3*J)
  816. # Now we compute the coefficients
  817. cc={}
  818. cont={}
  819. cont, pipowers = coef(ctx, J, eps6)
  820. cc = cont.copy() # we need a copy since we have
  821. Fp={}
  822. for n in range(M, 3*L-2):
  823. Fp[n] = 0
  824. ctx.prec = wpfp
  825. for m in range(0,M+1):
  826. sumP = 0
  827. for k in range(2*J-m-1,-1,-1):
  828. sumP = (sumP * p) + cc[k]
  829. Fp[m] = sumP
  830. # preparation of the new coefficients
  831. for k in range(0, 2*J-m-1):
  832. cc[k] = (k+1) * cc[k+1]
  833. # COMPUTING THE NUMBERS d[n,k]
  834. # See II Section 3.17
  835. # First we compute the working precisions wpd[k]
  836. # Se II equation (92)
  837. wpd = {}
  838. d1 = max(6, ctx.mag(40*L*L))
  839. d2 = 13+ctx.mag((1+abs(sigma))*A)-ctx.mag(eps4)-1
  840. const = ctx.ln(8/(ctx.pi*ctx.pi*a*a*B1*B1)) /2
  841. for n in range(0,L):
  842. d3 = ctx.mag(ctx.sqrt(ctx.gamma(n-0.5)))-ctx.floor(n*const)+d2
  843. wpd[n] = max(d3,d1)
  844. # procedure of II Section 3.17
  845. ctx.prec = wpd[1]+10
  846. psigma = 1-(2*sigma)
  847. d = {}
  848. d[0,0,-2]=0; d[0,0,-1]=0; d[0,0,0]=1; d[0,0,1]=0
  849. d[0,-1,-2]=0; d[0,-1,-1]=0; d[0,-1,0]=1; d[0,-1,1]=0
  850. for n in range(1,L):
  851. ctx.prec = wpd[n]+10
  852. for k in range(0,3*n//2+1):
  853. m = 3*n-2*k
  854. if (m!=0):
  855. m1 = ctx.one/m
  856. c1 = m1/4
  857. c2 = (psigma*m1)/2
  858. c3 = -(m+1)
  859. d[0,n,k] = c3*d[0,n-1,k-2]+c1*d[0,n-1,k]+c2*d[0,n-1,k-1]
  860. else:
  861. d[0,n,k]=0
  862. for r in range(0,k):
  863. add = d[0,n,r]*(ctx.one*ctx.fac(2*k-2*r)/ctx.fac(k-r))
  864. d[0,n,k] -= ((-1)**(k-r))*add
  865. d[0,n,-2]=0; d[0,n,-1]=0; d[0,n,3*n//2+1]=0
  866. for mu in range(-2,der+1):
  867. for n in range(-2,L):
  868. for k in range(-3,max(1,3*n//2+2)):
  869. if ((mu<0)or (n<0) or(k<0)or (k>3*n//2)):
  870. d[mu,n,k] = 0
  871. for mu in range(1,der+1):
  872. for n in range(0,L):
  873. ctx.prec = wpd[n]+10
  874. for k in range(0,3*n//2+1):
  875. aux=(2*mu-2)*d[mu-2,n-2,k-3]+2*(sigma+n-2)*d[mu-1,n-2,k-3]
  876. d[mu,n,k] = aux - d[mu-1,n-1,k-1]
  877. # COMPUTING THE COEFFICIENTS t[k,l]
  878. # See II Section 3.9
  879. #
  880. # computing the needed wp
  881. wptcoef = {}
  882. wpterm = {}
  883. ctx.prec = 15
  884. c1 = ctx.mag(40*(L+2))
  885. c2 = ctx.mag(68*(L+2)*A)
  886. c4 = ctx.mag(B1*a*math.sqrt(ctx.pi))-1
  887. for k in range(0,L):
  888. c3 = c2 - k*c4+ctx.mag(ctx.fac(k+0.5))/2.
  889. wptcoef[k] = max(c1,c3-ctx.mag(eps4)+1)+1 +10
  890. wpterm[k] = max(c1,ctx.mag(L+2)+c3-ctx.mag(eps3)+1)+1 +10
  891. # check of power of pi
  892. # computing the fortcoef[mu,k,ell]
  893. fortcoef={}
  894. for mu in derivatives:
  895. for k in range(0,L):
  896. for ell in range(-2,3*k//2+1):
  897. fortcoef[mu,k,ell]=0
  898. for mu in derivatives:
  899. for k in range(0,L):
  900. ctx.prec = wptcoef[k]
  901. for ell in range(0,3*k//2+1):
  902. fortcoef[mu,k,ell]=d[mu,k,ell]*Fp[3*k-2*ell]/pipowers[2*k-ell]
  903. fortcoef[mu,k,ell]=fortcoef[mu,k,ell]/((2*ctx.j)**ell)
  904. def trunc_a(t):
  905. wp = ctx.prec
  906. ctx.prec = wp + 2
  907. aa = ctx.sqrt(t/(2*ctx.pi))
  908. ctx.prec = wp
  909. return aa
  910. # computing the tcoef[chi,k,ell]
  911. tcoef={}
  912. for chi in derivatives:
  913. for k in range(0,L):
  914. for ell in range(-2,3*k//2+1):
  915. tcoef[chi,k,ell]=0
  916. ctx.prec = wptcoef[0]+3
  917. aa = trunc_a(t)
  918. la = -ctx.ln(aa)
  919. for chi in derivatives:
  920. for k in range(0,L):
  921. ctx.prec = wptcoef[k]
  922. for ell in range(0,3*k//2+1):
  923. tcoef[chi,k,ell] = 0
  924. for mu in range(0, chi+1):
  925. tcoefter = ctx.binomial(chi,mu) * la**mu * \
  926. fortcoef[chi-mu,k,ell]
  927. tcoef[chi,k,ell] += tcoefter
  928. # COMPUTING tv[k,ell]
  929. # See II Section 3.8
  930. # Computing the powers av[k] = a**(-k)
  931. ctx.prec = wptcoef[0] + 2
  932. # a has a good value of a.
  933. # See II Section 3.6
  934. av = {}
  935. av[0] = 1
  936. av[1] = av[0]/a
  937. ctx.prec = wptcoef[0]
  938. for k in range(2,L):
  939. av[k] = av[k-1] * av[1]
  940. # Computing the quotients
  941. tv = {}
  942. for chi in derivatives:
  943. for k in range(0,L):
  944. ctx.prec = wptcoef[k]
  945. for ell in range(0,3*k//2+1):
  946. tv[chi,k,ell] = tcoef[chi,k,ell]* av[k]
  947. # COMPUTING THE TERMS term[k]
  948. # See II Section 3.6
  949. term = {}
  950. for chi in derivatives:
  951. for n in range(0,L):
  952. ctx.prec = wpterm[n]
  953. te = 0
  954. for k in range(0, 3*n//2+1):
  955. te += tv[chi,n,k]
  956. term[chi,n] = te
  957. # COMPUTING rssum
  958. # See II Section 3.5
  959. rssum={}
  960. ctx.prec=15
  961. rsbound = math.sqrt(ctx.pi) * c /(b*a)
  962. ctx.prec=15
  963. wprssum = ctx.mag(4.4*((L+3)**2)*rsbound / eps2)
  964. wprssum = max(wprssum, ctx.mag(10*(L+1)))
  965. ctx.prec = wprssum
  966. for chi in derivatives:
  967. rssum[chi] = 0
  968. for k in range(1,L+1):
  969. rssum[chi] += term[chi,L-k]
  970. # COMPUTING S3
  971. # See II Section 3.19
  972. ctx.prec = 15
  973. A2 = 2**(ctx.mag(rssum[0]))
  974. eps8 = eps/(3* A2)
  975. T = t * ctx.ln(t/(2*ctx.pi))
  976. wps3 = 5 + ctx.mag((1+(2/eps8)*ctx.power(a,-sigma))*T)
  977. ctx.prec = wps3
  978. tpi = t/(2*ctx.pi)
  979. arg = (t/2)*ctx.ln(tpi)-(t/2)-ctx.pi/8
  980. U = ctx.expj(-arg)
  981. a = trunc_a(t)
  982. asigma = ctx.power(a, -sigma)
  983. S3 = ((-1)**(N-1)) * asigma * U
  984. # COMPUTING S1 the zetasum
  985. # See II Section 3.18
  986. ctx.prec = 15
  987. wpsum = 4 + ctx.mag((N+ctx.power(N,1-sigma))*ctx.ln(N)/eps1)
  988. ctx.prec = wpsum + 10
  989. '''
  990. # This can be improved
  991. S1 = {}
  992. for chi in derivatives:
  993. S1[chi] = 0
  994. for n in range(1,int(N)+1):
  995. ln = ctx.ln(n)
  996. expn = ctx.exp(-ln*(sigma+ctx.j*t))
  997. for chi in derivatives:
  998. term = ctx.power(-ln, chi)*expn
  999. S1[chi] += term
  1000. '''
  1001. S1 = ctx._zetasum(s, 1, int(N)-1, derivatives)[0]
  1002. # END OF COMPUTATION
  1003. # See II Section 3.1
  1004. ctx.prec = 15
  1005. absS1 = abs(S1[der])
  1006. absS2 = abs(rssum[der] * S3)
  1007. wpend = max(6, wpinitial + ctx.mag(6*(3*absS1+7*absS2)))
  1008. ctx.prec = wpend
  1009. rz = {}
  1010. for chi in derivatives:
  1011. rz[chi] = S1[chi]+rssum[chi]*S3
  1012. ctx.prec = wpinitial
  1013. return rz
  1014. def z_half(ctx,t,der=0):
  1015. r"""
  1016. z_half(t,der=0) Computes Z^(der)(t)
  1017. """
  1018. s=ctx.mpf('0.5')+ctx.j*t
  1019. wpinitial = ctx.prec
  1020. ctx.prec = 15
  1021. tt = t/(2*ctx.pi)
  1022. wptheta = wpinitial +1 + ctx.mag(3*(tt**1.5)*ctx.ln(tt))
  1023. wpz = wpinitial + 1 + ctx.mag(12*tt*ctx.ln(tt))
  1024. ctx.prec = wptheta
  1025. theta = ctx.siegeltheta(t)
  1026. ctx.prec = wpz
  1027. rz = Rzeta_set(ctx,s, range(der+1))
  1028. if der > 0: ps1 = ctx._re(ctx.psi(0,s/2)/2 - ctx.ln(ctx.pi)/2)
  1029. if der > 1: ps2 = ctx._re(ctx.j*ctx.psi(1,s/2)/4)
  1030. if der > 2: ps3 = ctx._re(-ctx.psi(2,s/2)/8)
  1031. if der > 3: ps4 = ctx._re(-ctx.j*ctx.psi(3,s/2)/16)
  1032. exptheta = ctx.expj(theta)
  1033. if der == 0:
  1034. z = 2*exptheta*rz[0]
  1035. if der == 1:
  1036. zf = 2j*exptheta
  1037. z = zf*(ps1*rz[0]+rz[1])
  1038. if der == 2:
  1039. zf = 2 * exptheta
  1040. z = -zf*(2*rz[1]*ps1+rz[0]*ps1**2+rz[2]-ctx.j*rz[0]*ps2)
  1041. if der == 3:
  1042. zf = -2j*exptheta
  1043. z = 3*rz[1]*ps1**2+rz[0]*ps1**3+3*ps1*rz[2]
  1044. z = zf*(z-3j*rz[1]*ps2-3j*rz[0]*ps1*ps2+rz[3]-rz[0]*ps3)
  1045. if der == 4:
  1046. zf = 2*exptheta
  1047. z = 4*rz[1]*ps1**3+rz[0]*ps1**4+6*ps1**2*rz[2]
  1048. z = z-12j*rz[1]*ps1*ps2-6j*rz[0]*ps1**2*ps2-6j*rz[2]*ps2-3*rz[0]*ps2*ps2
  1049. z = z + 4*ps1*rz[3]-4*rz[1]*ps3-4*rz[0]*ps1*ps3+rz[4]+ctx.j*rz[0]*ps4
  1050. z = zf*z
  1051. ctx.prec = wpinitial
  1052. return ctx._re(z)
  1053. def zeta_half(ctx, s, k=0):
  1054. """
  1055. zeta_half(s,k=0) Computes zeta^(k)(s) when Re s = 0.5
  1056. """
  1057. wpinitial = ctx.prec
  1058. sigma = ctx._re(s)
  1059. t = ctx._im(s)
  1060. #--- compute wptheta, wpR, wpbasic ---
  1061. ctx.prec = 53
  1062. # X see II Section 3.21 (109) and (110)
  1063. if sigma > 0:
  1064. X = ctx.sqrt(abs(s))
  1065. else:
  1066. X = (2*ctx.pi)**(sigma-1) * abs(1-s)**(0.5-sigma)
  1067. # M1 see II Section 3.21 (111) and (112)
  1068. if sigma > 0:
  1069. M1 = 2*ctx.sqrt(t/(2*ctx.pi))
  1070. else:
  1071. M1 = 4 * t * X
  1072. # T see II Section 3.21 (113)
  1073. abst = abs(0.5-s)
  1074. T = 2* abst*math.log(abst)
  1075. # computing wpbasic, wptheta, wpR see II Section 3.21
  1076. wpbasic = max(6,3+ctx.mag(t))
  1077. wpbasic2 = 2+ctx.mag(2.12*M1+21.2*M1*X+1.3*M1*X*T)+wpinitial+1
  1078. wpbasic = max(wpbasic, wpbasic2)
  1079. wptheta = max(4, 3+ctx.mag(2.7*M1*X)+wpinitial+1)
  1080. wpR = 3+ctx.mag(1.1+2*X)+wpinitial+1
  1081. ctx.prec = wptheta
  1082. theta = ctx.siegeltheta(t-ctx.j*(sigma-ctx.mpf('0.5')))
  1083. if k > 0: ps1 = (ctx._re(ctx.psi(0,s/2)))/2 - ctx.ln(ctx.pi)/2
  1084. if k > 1: ps2 = -(ctx._im(ctx.psi(1,s/2)))/4
  1085. if k > 2: ps3 = -(ctx._re(ctx.psi(2,s/2)))/8
  1086. if k > 3: ps4 = (ctx._im(ctx.psi(3,s/2)))/16
  1087. ctx.prec = wpR
  1088. xrz = Rzeta_set(ctx,s,range(k+1))
  1089. yrz={}
  1090. for chi in range(0,k+1):
  1091. yrz[chi] = ctx.conj(xrz[chi])
  1092. ctx.prec = wpbasic
  1093. exptheta = ctx.expj(-2*theta)
  1094. if k==0:
  1095. zv = xrz[0]+exptheta*yrz[0]
  1096. if k==1:
  1097. zv1 = -yrz[1] - 2*yrz[0]*ps1
  1098. zv = xrz[1] + exptheta*zv1
  1099. if k==2:
  1100. zv1 = 4*yrz[1]*ps1+4*yrz[0]*(ps1**2)+yrz[2]+2j*yrz[0]*ps2
  1101. zv = xrz[2]+exptheta*zv1
  1102. if k==3:
  1103. zv1 = -12*yrz[1]*ps1**2-8*yrz[0]*ps1**3-6*yrz[2]*ps1-6j*yrz[1]*ps2
  1104. zv1 = zv1 - 12j*yrz[0]*ps1*ps2-yrz[3]+2*yrz[0]*ps3
  1105. zv = xrz[3]+exptheta*zv1
  1106. if k == 4:
  1107. zv1 = 32*yrz[1]*ps1**3 +16*yrz[0]*ps1**4+24*yrz[2]*ps1**2
  1108. zv1 = zv1 +48j*yrz[1]*ps1*ps2+48j*yrz[0]*(ps1**2)*ps2
  1109. zv1 = zv1+12j*yrz[2]*ps2-12*yrz[0]*ps2**2+8*yrz[3]*ps1-8*yrz[1]*ps3
  1110. zv1 = zv1-16*yrz[0]*ps1*ps3+yrz[4]-2j*yrz[0]*ps4
  1111. zv = xrz[4]+exptheta*zv1
  1112. ctx.prec = wpinitial
  1113. return zv
  1114. def zeta_offline(ctx, s, k=0):
  1115. """
  1116. Computes zeta^(k)(s) off the line
  1117. """
  1118. wpinitial = ctx.prec
  1119. sigma = ctx._re(s)
  1120. t = ctx._im(s)
  1121. #--- compute wptheta, wpR, wpbasic ---
  1122. ctx.prec = 53
  1123. # X see II Section 3.21 (109) and (110)
  1124. if sigma > 0:
  1125. X = ctx.power(abs(s), 0.5)
  1126. else:
  1127. X = ctx.power(2*ctx.pi, sigma-1)*ctx.power(abs(1-s),0.5-sigma)
  1128. # M1 see II Section 3.21 (111) and (112)
  1129. if (sigma > 0):
  1130. M1 = 2*ctx.sqrt(t/(2*ctx.pi))
  1131. else:
  1132. M1 = 4 * t * X
  1133. # M2 see II Section 3.21 (111) and (112)
  1134. if (1-sigma > 0):
  1135. M2 = 2*ctx.sqrt(t/(2*ctx.pi))
  1136. else:
  1137. M2 = 4*t*ctx.power(2*ctx.pi, -sigma)*ctx.power(abs(s),sigma-0.5)
  1138. # T see II Section 3.21 (113)
  1139. abst = abs(0.5-s)
  1140. T = 2* abst*math.log(abst)
  1141. # computing wpbasic, wptheta, wpR see II Section 3.21
  1142. wpbasic = max(6,3+ctx.mag(t))
  1143. wpbasic2 = 2+ctx.mag(2.12*M1+21.2*M2*X+1.3*M2*X*T)+wpinitial+1
  1144. wpbasic = max(wpbasic, wpbasic2)
  1145. wptheta = max(4, 3+ctx.mag(2.7*M2*X)+wpinitial+1)
  1146. wpR = 3+ctx.mag(1.1+2*X)+wpinitial+1
  1147. ctx.prec = wptheta
  1148. theta = ctx.siegeltheta(t-ctx.j*(sigma-ctx.mpf('0.5')))
  1149. s1 = s
  1150. s2 = ctx.conj(1-s1)
  1151. ctx.prec = wpR
  1152. xrz, yrz = Rzeta_simul(ctx, s, k)
  1153. if k > 0: ps1 = (ctx.psi(0,s1/2)+ctx.psi(0,(1-s1)/2))/4 - ctx.ln(ctx.pi)/2
  1154. if k > 1: ps2 = ctx.j*(ctx.psi(1,s1/2)-ctx.psi(1,(1-s1)/2))/8
  1155. if k > 2: ps3 = -(ctx.psi(2,s1/2)+ctx.psi(2,(1-s1)/2))/16
  1156. if k > 3: ps4 = -ctx.j*(ctx.psi(3,s1/2)-ctx.psi(3,(1-s1)/2))/32
  1157. ctx.prec = wpbasic
  1158. exptheta = ctx.expj(-2*theta)
  1159. if k == 0:
  1160. zv = xrz[0]+exptheta*yrz[0]
  1161. if k == 1:
  1162. zv1 = -yrz[1]-2*yrz[0]*ps1
  1163. zv = xrz[1]+exptheta*zv1
  1164. if k == 2:
  1165. zv1 = 4*yrz[1]*ps1+4*yrz[0]*(ps1**2) +yrz[2]+2j*yrz[0]*ps2
  1166. zv = xrz[2]+exptheta*zv1
  1167. if k == 3:
  1168. zv1 = -12*yrz[1]*ps1**2 -8*yrz[0]*ps1**3-6*yrz[2]*ps1-6j*yrz[1]*ps2
  1169. zv1 = zv1 - 12j*yrz[0]*ps1*ps2-yrz[3]+2*yrz[0]*ps3
  1170. zv = xrz[3]+exptheta*zv1
  1171. if k == 4:
  1172. zv1 = 32*yrz[1]*ps1**3 +16*yrz[0]*ps1**4+24*yrz[2]*ps1**2
  1173. zv1 = zv1 +48j*yrz[1]*ps1*ps2+48j*yrz[0]*(ps1**2)*ps2
  1174. zv1 = zv1+12j*yrz[2]*ps2-12*yrz[0]*ps2**2+8*yrz[3]*ps1-8*yrz[1]*ps3
  1175. zv1 = zv1-16*yrz[0]*ps1*ps3+yrz[4]-2j*yrz[0]*ps4
  1176. zv = xrz[4]+exptheta*zv1
  1177. ctx.prec = wpinitial
  1178. return zv
  1179. def z_offline(ctx, w, k=0):
  1180. r"""
  1181. Computes Z(w) and its derivatives off the line
  1182. """
  1183. s = ctx.mpf('0.5')+ctx.j*w
  1184. s1 = s
  1185. s2 = ctx.conj(1-s1)
  1186. wpinitial = ctx.prec
  1187. ctx.prec = 35
  1188. # X see II Section 3.21 (109) and (110)
  1189. # M1 see II Section 3.21 (111) and (112)
  1190. if (ctx._re(s1) >= 0):
  1191. M1 = 2*ctx.sqrt(ctx._im(s1)/(2 * ctx.pi))
  1192. X = ctx.sqrt(abs(s1))
  1193. else:
  1194. X = (2*ctx.pi)**(ctx._re(s1)-1) * abs(1-s1)**(0.5-ctx._re(s1))
  1195. M1 = 4 * ctx._im(s1)*X
  1196. # M2 see II Section 3.21 (111) and (112)
  1197. if (ctx._re(s2) >= 0):
  1198. M2 = 2*ctx.sqrt(ctx._im(s2)/(2 * ctx.pi))
  1199. else:
  1200. M2 = 4 * ctx._im(s2)*(2*ctx.pi)**(ctx._re(s2)-1)*abs(1-s2)**(0.5-ctx._re(s2))
  1201. # T see II Section 3.21 Prop. 27
  1202. T = 2*abs(ctx.siegeltheta(w))
  1203. # defining some precisions
  1204. # see II Section 3.22 (115), (116), (117)
  1205. aux1 = ctx.sqrt(X)
  1206. aux2 = aux1*(M1+M2)
  1207. aux3 = 3 +wpinitial
  1208. wpbasic = max(6, 3+ctx.mag(T), ctx.mag(aux2*(26+2*T))+aux3)
  1209. wptheta = max(4,ctx.mag(2.04*aux2)+aux3)
  1210. wpR = ctx.mag(4*aux1)+aux3
  1211. # now the computations
  1212. ctx.prec = wptheta
  1213. theta = ctx.siegeltheta(w)
  1214. ctx.prec = wpR
  1215. xrz, yrz = Rzeta_simul(ctx,s,k)
  1216. pta = 0.25 + 0.5j*w
  1217. ptb = 0.25 - 0.5j*w
  1218. if k > 0: ps1 = 0.25*(ctx.psi(0,pta)+ctx.psi(0,ptb)) - ctx.ln(ctx.pi)/2
  1219. if k > 1: ps2 = (1j/8)*(ctx.psi(1,pta)-ctx.psi(1,ptb))
  1220. if k > 2: ps3 = (-1./16)*(ctx.psi(2,pta)+ctx.psi(2,ptb))
  1221. if k > 3: ps4 = (-1j/32)*(ctx.psi(3,pta)-ctx.psi(3,ptb))
  1222. ctx.prec = wpbasic
  1223. exptheta = ctx.expj(theta)
  1224. if k == 0:
  1225. zv = exptheta*xrz[0]+yrz[0]/exptheta
  1226. j = ctx.j
  1227. if k == 1:
  1228. zv = j*exptheta*(xrz[1]+xrz[0]*ps1)-j*(yrz[1]+yrz[0]*ps1)/exptheta
  1229. if k == 2:
  1230. zv = exptheta*(-2*xrz[1]*ps1-xrz[0]*ps1**2-xrz[2]+j*xrz[0]*ps2)
  1231. zv =zv + (-2*yrz[1]*ps1-yrz[0]*ps1**2-yrz[2]-j*yrz[0]*ps2)/exptheta
  1232. if k == 3:
  1233. zv1 = -3*xrz[1]*ps1**2-xrz[0]*ps1**3-3*xrz[2]*ps1+j*3*xrz[1]*ps2
  1234. zv1 = (zv1+ 3j*xrz[0]*ps1*ps2-xrz[3]+xrz[0]*ps3)*j*exptheta
  1235. zv2 = 3*yrz[1]*ps1**2+yrz[0]*ps1**3+3*yrz[2]*ps1+j*3*yrz[1]*ps2
  1236. zv2 = j*(zv2 + 3j*yrz[0]*ps1*ps2+ yrz[3]-yrz[0]*ps3)/exptheta
  1237. zv = zv1+zv2
  1238. if k == 4:
  1239. zv1 = 4*xrz[1]*ps1**3+xrz[0]*ps1**4 + 6*xrz[2]*ps1**2
  1240. zv1 = zv1-12j*xrz[1]*ps1*ps2-6j*xrz[0]*ps1**2*ps2-6j*xrz[2]*ps2
  1241. zv1 = zv1-3*xrz[0]*ps2*ps2+4*xrz[3]*ps1-4*xrz[1]*ps3-4*xrz[0]*ps1*ps3
  1242. zv1 = zv1+xrz[4]+j*xrz[0]*ps4
  1243. zv2 = 4*yrz[1]*ps1**3+yrz[0]*ps1**4 + 6*yrz[2]*ps1**2
  1244. zv2 = zv2+12j*yrz[1]*ps1*ps2+6j*yrz[0]*ps1**2*ps2+6j*yrz[2]*ps2
  1245. zv2 = zv2-3*yrz[0]*ps2*ps2+4*yrz[3]*ps1-4*yrz[1]*ps3-4*yrz[0]*ps1*ps3
  1246. zv2 = zv2+yrz[4]-j*yrz[0]*ps4
  1247. zv = exptheta*zv1+zv2/exptheta
  1248. ctx.prec = wpinitial
  1249. return zv
  1250. @defun
  1251. def rs_zeta(ctx, s, derivative=0, **kwargs):
  1252. if derivative > 4:
  1253. raise NotImplementedError
  1254. s = ctx.convert(s)
  1255. re = ctx._re(s); im = ctx._im(s)
  1256. if im < 0:
  1257. z = ctx.conj(ctx.rs_zeta(ctx.conj(s), derivative))
  1258. return z
  1259. critical_line = (re == 0.5)
  1260. if critical_line:
  1261. return zeta_half(ctx, s, derivative)
  1262. else:
  1263. return zeta_offline(ctx, s, derivative)
  1264. @defun
  1265. def rs_z(ctx, w, derivative=0):
  1266. w = ctx.convert(w)
  1267. re = ctx._re(w); im = ctx._im(w)
  1268. if re < 0:
  1269. return rs_z(ctx, -w, derivative)
  1270. critical_line = (im == 0)
  1271. if critical_line :
  1272. return z_half(ctx, w, derivative)
  1273. else:
  1274. return z_offline(ctx, w, derivative)