zeta_functions.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. """ Riemann zeta and related function. """
  2. from sympy.core.add import Add
  3. from sympy.core.cache import cacheit
  4. from sympy.core.function import ArgumentIndexError, expand_mul, Function
  5. from sympy.core.numbers import pi, I, Integer
  6. from sympy.core.relational import Eq
  7. from sympy.core.singleton import S
  8. from sympy.core.symbol import Dummy
  9. from sympy.core.sympify import sympify
  10. from sympy.functions.combinatorial.numbers import bernoulli, factorial, genocchi, harmonic
  11. from sympy.functions.elementary.complexes import re, unpolarify, Abs, polar_lift
  12. from sympy.functions.elementary.exponential import log, exp_polar, exp
  13. from sympy.functions.elementary.integers import ceiling, floor
  14. from sympy.functions.elementary.miscellaneous import sqrt
  15. from sympy.functions.elementary.piecewise import Piecewise
  16. from sympy.polys.polytools import Poly
  17. ###############################################################################
  18. ###################### LERCH TRANSCENDENT #####################################
  19. ###############################################################################
  20. class lerchphi(Function):
  21. r"""
  22. Lerch transcendent (Lerch phi function).
  23. Explanation
  24. ===========
  25. For $\operatorname{Re}(a) > 0$, $|z| < 1$ and $s \in \mathbb{C}$, the
  26. Lerch transcendent is defined as
  27. .. math :: \Phi(z, s, a) = \sum_{n=0}^\infty \frac{z^n}{(n + a)^s},
  28. where the standard branch of the argument is used for $n + a$,
  29. and by analytic continuation for other values of the parameters.
  30. A commonly used related function is the Lerch zeta function, defined by
  31. .. math:: L(q, s, a) = \Phi(e^{2\pi i q}, s, a).
  32. **Analytic Continuation and Branching Behavior**
  33. It can be shown that
  34. .. math:: \Phi(z, s, a) = z\Phi(z, s, a+1) + a^{-s}.
  35. This provides the analytic continuation to $\operatorname{Re}(a) \le 0$.
  36. Assume now $\operatorname{Re}(a) > 0$. The integral representation
  37. .. math:: \Phi_0(z, s, a) = \int_0^\infty \frac{t^{s-1} e^{-at}}{1 - ze^{-t}}
  38. \frac{\mathrm{d}t}{\Gamma(s)}
  39. provides an analytic continuation to $\mathbb{C} - [1, \infty)$.
  40. Finally, for $x \in (1, \infty)$ we find
  41. .. math:: \lim_{\epsilon \to 0^+} \Phi_0(x + i\epsilon, s, a)
  42. -\lim_{\epsilon \to 0^+} \Phi_0(x - i\epsilon, s, a)
  43. = \frac{2\pi i \log^{s-1}{x}}{x^a \Gamma(s)},
  44. using the standard branch for both $\log{x}$ and
  45. $\log{\log{x}}$ (a branch of $\log{\log{x}}$ is needed to
  46. evaluate $\log{x}^{s-1}$).
  47. This concludes the analytic continuation. The Lerch transcendent is thus
  48. branched at $z \in \{0, 1, \infty\}$ and
  49. $a \in \mathbb{Z}_{\le 0}$. For fixed $z, a$ outside these
  50. branch points, it is an entire function of $s$.
  51. Examples
  52. ========
  53. The Lerch transcendent is a fairly general function, for this reason it does
  54. not automatically evaluate to simpler functions. Use ``expand_func()`` to
  55. achieve this.
  56. If $z=1$, the Lerch transcendent reduces to the Hurwitz zeta function:
  57. >>> from sympy import lerchphi, expand_func
  58. >>> from sympy.abc import z, s, a
  59. >>> expand_func(lerchphi(1, s, a))
  60. zeta(s, a)
  61. More generally, if $z$ is a root of unity, the Lerch transcendent
  62. reduces to a sum of Hurwitz zeta functions:
  63. >>> expand_func(lerchphi(-1, s, a))
  64. zeta(s, a/2)/2**s - zeta(s, a/2 + 1/2)/2**s
  65. If $a=1$, the Lerch transcendent reduces to the polylogarithm:
  66. >>> expand_func(lerchphi(z, s, 1))
  67. polylog(s, z)/z
  68. More generally, if $a$ is rational, the Lerch transcendent reduces
  69. to a sum of polylogarithms:
  70. >>> from sympy import S
  71. >>> expand_func(lerchphi(z, s, S(1)/2))
  72. 2**(s - 1)*(polylog(s, sqrt(z))/sqrt(z) -
  73. polylog(s, sqrt(z)*exp_polar(I*pi))/sqrt(z))
  74. >>> expand_func(lerchphi(z, s, S(3)/2))
  75. -2**s/z + 2**(s - 1)*(polylog(s, sqrt(z))/sqrt(z) -
  76. polylog(s, sqrt(z)*exp_polar(I*pi))/sqrt(z))/z
  77. The derivatives with respect to $z$ and $a$ can be computed in
  78. closed form:
  79. >>> lerchphi(z, s, a).diff(z)
  80. (-a*lerchphi(z, s, a) + lerchphi(z, s - 1, a))/z
  81. >>> lerchphi(z, s, a).diff(a)
  82. -s*lerchphi(z, s + 1, a)
  83. See Also
  84. ========
  85. polylog, zeta
  86. References
  87. ==========
  88. .. [1] Bateman, H.; Erdelyi, A. (1953), Higher Transcendental Functions,
  89. Vol. I, New York: McGraw-Hill. Section 1.11.
  90. .. [2] https://dlmf.nist.gov/25.14
  91. .. [3] https://en.wikipedia.org/wiki/Lerch_transcendent
  92. """
  93. def _eval_expand_func(self, **hints):
  94. z, s, a = self.args
  95. if z == 1:
  96. return zeta(s, a)
  97. if s.is_Integer and s <= 0:
  98. t = Dummy('t')
  99. p = Poly((t + a)**(-s), t)
  100. start = 1/(1 - t)
  101. res = S.Zero
  102. for c in reversed(p.all_coeffs()):
  103. res += c*start
  104. start = t*start.diff(t)
  105. return res.subs(t, z)
  106. if a.is_Rational:
  107. # See section 18 of
  108. # Kelly B. Roach. Hypergeometric Function Representations.
  109. # In: Proceedings of the 1997 International Symposium on Symbolic and
  110. # Algebraic Computation, pages 205-211, New York, 1997. ACM.
  111. # TODO should something be polarified here?
  112. add = S.Zero
  113. mul = S.One
  114. # First reduce a to the interaval (0, 1]
  115. if a > 1:
  116. n = floor(a)
  117. if n == a:
  118. n -= 1
  119. a -= n
  120. mul = z**(-n)
  121. add = Add(*[-z**(k - n)/(a + k)**s for k in range(n)])
  122. elif a <= 0:
  123. n = floor(-a) + 1
  124. a += n
  125. mul = z**n
  126. add = Add(*[z**(n - 1 - k)/(a - k - 1)**s for k in range(n)])
  127. m, n = S([a.p, a.q])
  128. zet = exp_polar(2*pi*I/n)
  129. root = z**(1/n)
  130. up_zet = unpolarify(zet)
  131. addargs = []
  132. for k in range(n):
  133. p = polylog(s, zet**k*root)
  134. if isinstance(p, polylog):
  135. p = p._eval_expand_func(**hints)
  136. addargs.append(p/(up_zet**k*root)**m)
  137. return add + mul*n**(s - 1)*Add(*addargs)
  138. # TODO use minpoly instead of ad-hoc methods when issue 5888 is fixed
  139. if isinstance(z, exp) and (z.args[0]/(pi*I)).is_Rational or z in [-1, I, -I]:
  140. # TODO reference?
  141. if z == -1:
  142. p, q = S([1, 2])
  143. elif z == I:
  144. p, q = S([1, 4])
  145. elif z == -I:
  146. p, q = S([-1, 4])
  147. else:
  148. arg = z.args[0]/(2*pi*I)
  149. p, q = S([arg.p, arg.q])
  150. return Add(*[exp(2*pi*I*k*p/q)/q**s*zeta(s, (k + a)/q)
  151. for k in range(q)])
  152. return lerchphi(z, s, a)
  153. def fdiff(self, argindex=1):
  154. z, s, a = self.args
  155. if argindex == 3:
  156. return -s*lerchphi(z, s + 1, a)
  157. elif argindex == 1:
  158. return (lerchphi(z, s - 1, a) - a*lerchphi(z, s, a))/z
  159. else:
  160. raise ArgumentIndexError
  161. def _eval_rewrite_helper(self, target):
  162. res = self._eval_expand_func()
  163. if res.has(target):
  164. return res
  165. else:
  166. return self
  167. def _eval_rewrite_as_zeta(self, z, s, a, **kwargs):
  168. return self._eval_rewrite_helper(zeta)
  169. def _eval_rewrite_as_polylog(self, z, s, a, **kwargs):
  170. return self._eval_rewrite_helper(polylog)
  171. ###############################################################################
  172. ###################### POLYLOGARITHM ##########################################
  173. ###############################################################################
  174. class polylog(Function):
  175. r"""
  176. Polylogarithm function.
  177. Explanation
  178. ===========
  179. For $|z| < 1$ and $s \in \mathbb{C}$, the polylogarithm is
  180. defined by
  181. .. math:: \operatorname{Li}_s(z) = \sum_{n=1}^\infty \frac{z^n}{n^s},
  182. where the standard branch of the argument is used for $n$. It admits
  183. an analytic continuation which is branched at $z=1$ (notably not on the
  184. sheet of initial definition), $z=0$ and $z=\infty$.
  185. The name polylogarithm comes from the fact that for $s=1$, the
  186. polylogarithm is related to the ordinary logarithm (see examples), and that
  187. .. math:: \operatorname{Li}_{s+1}(z) =
  188. \int_0^z \frac{\operatorname{Li}_s(t)}{t} \mathrm{d}t.
  189. The polylogarithm is a special case of the Lerch transcendent:
  190. .. math:: \operatorname{Li}_{s}(z) = z \Phi(z, s, 1).
  191. Examples
  192. ========
  193. For $z \in \{0, 1, -1\}$, the polylogarithm is automatically expressed
  194. using other functions:
  195. >>> from sympy import polylog
  196. >>> from sympy.abc import s
  197. >>> polylog(s, 0)
  198. 0
  199. >>> polylog(s, 1)
  200. zeta(s)
  201. >>> polylog(s, -1)
  202. -dirichlet_eta(s)
  203. If $s$ is a negative integer, $0$ or $1$, the polylogarithm can be
  204. expressed using elementary functions. This can be done using
  205. ``expand_func()``:
  206. >>> from sympy import expand_func
  207. >>> from sympy.abc import z
  208. >>> expand_func(polylog(1, z))
  209. -log(1 - z)
  210. >>> expand_func(polylog(0, z))
  211. z/(1 - z)
  212. The derivative with respect to $z$ can be computed in closed form:
  213. >>> polylog(s, z).diff(z)
  214. polylog(s - 1, z)/z
  215. The polylogarithm can be expressed in terms of the lerch transcendent:
  216. >>> from sympy import lerchphi
  217. >>> polylog(s, z).rewrite(lerchphi)
  218. z*lerchphi(z, s, 1)
  219. See Also
  220. ========
  221. zeta, lerchphi
  222. """
  223. @classmethod
  224. def eval(cls, s, z):
  225. if z.is_number:
  226. if z is S.One:
  227. return zeta(s)
  228. elif z is S.NegativeOne:
  229. return -dirichlet_eta(s)
  230. elif z is S.Zero:
  231. return S.Zero
  232. elif s == 2:
  233. dilogtable = _dilogtable()
  234. if z in dilogtable:
  235. return dilogtable[z]
  236. if z.is_zero:
  237. return S.Zero
  238. # Make an effort to determine if z is 1 to avoid replacing into
  239. # expression with singularity
  240. zone = z.equals(S.One)
  241. if zone:
  242. return zeta(s)
  243. elif zone is False:
  244. # For s = 0 or -1 use explicit formulas to evaluate, but
  245. # automatically expanding polylog(1, z) to -log(1-z) seems
  246. # undesirable for summation methods based on hypergeometric
  247. # functions
  248. if s is S.Zero:
  249. return z/(1 - z)
  250. elif s is S.NegativeOne:
  251. return z/(1 - z)**2
  252. if s.is_zero:
  253. return z/(1 - z)
  254. # polylog is branched, but not over the unit disk
  255. if z.has(exp_polar, polar_lift) and (zone or (Abs(z) <= S.One) == True):
  256. return cls(s, unpolarify(z))
  257. def fdiff(self, argindex=1):
  258. s, z = self.args
  259. if argindex == 2:
  260. return polylog(s - 1, z)/z
  261. raise ArgumentIndexError
  262. def _eval_rewrite_as_lerchphi(self, s, z, **kwargs):
  263. return z*lerchphi(z, s, 1)
  264. def _eval_expand_func(self, **hints):
  265. s, z = self.args
  266. if s == 1:
  267. return -log(1 - z)
  268. if s.is_Integer and s <= 0:
  269. u = Dummy('u')
  270. start = u/(1 - u)
  271. for _ in range(-s):
  272. start = u*start.diff(u)
  273. return expand_mul(start).subs(u, z)
  274. return polylog(s, z)
  275. def _eval_is_zero(self):
  276. z = self.args[1]
  277. if z.is_zero:
  278. return True
  279. def _eval_nseries(self, x, n, logx, cdir=0):
  280. from sympy.series.order import Order
  281. nu, z = self.args
  282. z0 = z.subs(x, 0)
  283. if z0 is S.NaN:
  284. z0 = z.limit(x, 0, dir='-' if re(cdir).is_negative else '+')
  285. if z0.is_zero:
  286. # In case of powers less than 1, number of terms need to be computed
  287. # separately to avoid repeated callings of _eval_nseries with wrong n
  288. try:
  289. _, exp = z.leadterm(x)
  290. except (ValueError, NotImplementedError):
  291. return self
  292. if exp.is_positive:
  293. newn = ceiling(n/exp)
  294. o = Order(x**n, x)
  295. r = z._eval_nseries(x, n, logx, cdir).removeO()
  296. if r is S.Zero:
  297. return o
  298. term = r
  299. s = [term]
  300. for k in range(2, newn):
  301. term *= r
  302. s.append(term/k**nu)
  303. return Add(*s) + o
  304. return super(polylog, self)._eval_nseries(x, n, logx, cdir)
  305. ###############################################################################
  306. ###################### HURWITZ GENERALIZED ZETA FUNCTION ######################
  307. ###############################################################################
  308. class zeta(Function):
  309. r"""
  310. Hurwitz zeta function (or Riemann zeta function).
  311. Explanation
  312. ===========
  313. For $\operatorname{Re}(a) > 0$ and $\operatorname{Re}(s) > 1$, this
  314. function is defined as
  315. .. math:: \zeta(s, a) = \sum_{n=0}^\infty \frac{1}{(n + a)^s},
  316. where the standard choice of argument for $n + a$ is used. For fixed
  317. $a$ not a nonpositive integer the Hurwitz zeta function admits a
  318. meromorphic continuation to all of $\mathbb{C}$; it is an unbranched
  319. function with a simple pole at $s = 1$.
  320. The Hurwitz zeta function is a special case of the Lerch transcendent:
  321. .. math:: \zeta(s, a) = \Phi(1, s, a).
  322. This formula defines an analytic continuation for all possible values of
  323. $s$ and $a$ (also $\operatorname{Re}(a) < 0$), see the documentation of
  324. :class:`lerchphi` for a description of the branching behavior.
  325. If no value is passed for $a$ a default value of $a = 1$ is assumed,
  326. yielding the Riemann zeta function.
  327. Examples
  328. ========
  329. For $a = 1$ the Hurwitz zeta function reduces to the famous Riemann
  330. zeta function:
  331. .. math:: \zeta(s, 1) = \zeta(s) = \sum_{n=1}^\infty \frac{1}{n^s}.
  332. >>> from sympy import zeta
  333. >>> from sympy.abc import s
  334. >>> zeta(s, 1)
  335. zeta(s)
  336. >>> zeta(s)
  337. zeta(s)
  338. The Riemann zeta function can also be expressed using the Dirichlet eta
  339. function:
  340. >>> from sympy import dirichlet_eta
  341. >>> zeta(s).rewrite(dirichlet_eta)
  342. dirichlet_eta(s)/(1 - 2**(1 - s))
  343. The Riemann zeta function at nonnegative even and negative integer
  344. values is related to the Bernoulli numbers and polynomials:
  345. >>> zeta(2)
  346. pi**2/6
  347. >>> zeta(4)
  348. pi**4/90
  349. >>> zeta(0)
  350. -1/2
  351. >>> zeta(-1)
  352. -1/12
  353. >>> zeta(-4)
  354. 0
  355. The specific formulae are:
  356. .. math:: \zeta(2n) = -\frac{(2\pi i)^{2n} B_{2n}}{2(2n)!}
  357. .. math:: \zeta(-n,a) = -\frac{B_{n+1}(a)}{n+1}
  358. No closed-form expressions are known at positive odd integers, but
  359. numerical evaluation is possible:
  360. >>> zeta(3).n()
  361. 1.20205690315959
  362. The derivative of $\zeta(s, a)$ with respect to $a$ can be computed:
  363. >>> from sympy.abc import a
  364. >>> zeta(s, a).diff(a)
  365. -s*zeta(s + 1, a)
  366. However the derivative with respect to $s$ has no useful closed form
  367. expression:
  368. >>> zeta(s, a).diff(s)
  369. Derivative(zeta(s, a), s)
  370. The Hurwitz zeta function can be expressed in terms of the Lerch
  371. transcendent, :class:`~.lerchphi`:
  372. >>> from sympy import lerchphi
  373. >>> zeta(s, a).rewrite(lerchphi)
  374. lerchphi(1, s, a)
  375. See Also
  376. ========
  377. dirichlet_eta, lerchphi, polylog
  378. References
  379. ==========
  380. .. [1] https://dlmf.nist.gov/25.11
  381. .. [2] https://en.wikipedia.org/wiki/Hurwitz_zeta_function
  382. """
  383. @classmethod
  384. def eval(cls, s, a=None):
  385. if a is S.One:
  386. return cls(s)
  387. elif s is S.NaN or a is S.NaN:
  388. return S.NaN
  389. elif s is S.One:
  390. return S.ComplexInfinity
  391. elif s is S.Infinity:
  392. return S.One
  393. elif a is S.Infinity:
  394. return S.Zero
  395. sint = s.is_Integer
  396. if a is None:
  397. a = S.One
  398. if sint and s.is_nonpositive:
  399. return bernoulli(1-s, a) / (s-1)
  400. elif a is S.One:
  401. if sint and s.is_even:
  402. return -(2*pi*I)**s * bernoulli(s) / (2*factorial(s))
  403. elif sint and a.is_Integer and a.is_positive:
  404. return cls(s) - harmonic(a-1, s)
  405. elif a.is_Integer and a.is_nonpositive and \
  406. (s.is_integer is False or s.is_nonpositive is False):
  407. return S.NaN
  408. def _eval_rewrite_as_bernoulli(self, s, a=1, **kwargs):
  409. if a == 1 and s.is_integer and s.is_nonnegative and s.is_even:
  410. return -(2*pi*I)**s * bernoulli(s) / (2*factorial(s))
  411. return bernoulli(1-s, a) / (s-1)
  412. def _eval_rewrite_as_dirichlet_eta(self, s, a=1, **kwargs):
  413. if a != 1:
  414. return self
  415. s = self.args[0]
  416. return dirichlet_eta(s)/(1 - 2**(1 - s))
  417. def _eval_rewrite_as_lerchphi(self, s, a=1, **kwargs):
  418. return lerchphi(1, s, a)
  419. def _eval_is_finite(self):
  420. arg_is_one = (self.args[0] - 1).is_zero
  421. if arg_is_one is not None:
  422. return not arg_is_one
  423. def _eval_expand_func(self, **hints):
  424. s = self.args[0]
  425. a = self.args[1] if len(self.args) > 1 else S.One
  426. if a.is_integer:
  427. if a.is_positive:
  428. return zeta(s) - harmonic(a-1, s)
  429. if a.is_nonpositive and (s.is_integer is False or
  430. s.is_nonpositive is False):
  431. return S.NaN
  432. return self
  433. def fdiff(self, argindex=1):
  434. if len(self.args) == 2:
  435. s, a = self.args
  436. else:
  437. s, a = self.args + (1,)
  438. if argindex == 2:
  439. return -s*zeta(s + 1, a)
  440. else:
  441. raise ArgumentIndexError
  442. def _eval_as_leading_term(self, x, logx=None, cdir=0):
  443. if len(self.args) == 2:
  444. s, a = self.args
  445. else:
  446. s, a = self.args + (S.One,)
  447. try:
  448. c, e = a.leadterm(x)
  449. except NotImplementedError:
  450. return self
  451. if e.is_negative and not s.is_positive:
  452. raise NotImplementedError
  453. return super(zeta, self)._eval_as_leading_term(x, logx, cdir)
  454. class dirichlet_eta(Function):
  455. r"""
  456. Dirichlet eta function.
  457. Explanation
  458. ===========
  459. For $\operatorname{Re}(s) > 0$ and $0 < x \le 1$, this function is defined as
  460. .. math:: \eta(s, a) = \sum_{n=0}^\infty \frac{(-1)^n}{(n+a)^s}.
  461. It admits a unique analytic continuation to all of $\mathbb{C}$ for any
  462. fixed $a$ not a nonpositive integer. It is an entire, unbranched function.
  463. It can be expressed using the Hurwitz zeta function as
  464. .. math:: \eta(s, a) = \zeta(s,a) - 2^{1-s} \zeta\left(s, \frac{a+1}{2}\right)
  465. and using the generalized Genocchi function as
  466. .. math:: \eta(s, a) = \frac{G(1-s, a)}{2(s-1)}.
  467. In both cases the limiting value of $\log2 - \psi(a) + \psi\left(\frac{a+1}{2}\right)$
  468. is used when $s = 1$.
  469. Examples
  470. ========
  471. >>> from sympy import dirichlet_eta, zeta
  472. >>> from sympy.abc import s
  473. >>> dirichlet_eta(s).rewrite(zeta)
  474. Piecewise((log(2), Eq(s, 1)), ((1 - 2**(1 - s))*zeta(s), True))
  475. See Also
  476. ========
  477. zeta
  478. References
  479. ==========
  480. .. [1] https://en.wikipedia.org/wiki/Dirichlet_eta_function
  481. .. [2] Peter Luschny, "An introduction to the Bernoulli function",
  482. https://arxiv.org/abs/2009.06743
  483. """
  484. @classmethod
  485. def eval(cls, s, a=None):
  486. if a is S.One:
  487. return cls(s)
  488. if a is None:
  489. if s == 1:
  490. return log(2)
  491. z = zeta(s)
  492. if not z.has(zeta):
  493. return (1 - 2**(1-s)) * z
  494. return
  495. elif s == 1:
  496. from sympy.functions.special.gamma_functions import digamma
  497. return log(2) - digamma(a) + digamma((a+1)/2)
  498. z1 = zeta(s, a)
  499. z2 = zeta(s, (a+1)/2)
  500. if not z1.has(zeta) and not z2.has(zeta):
  501. return z1 - 2**(1-s) * z2
  502. def _eval_rewrite_as_zeta(self, s, a=1, **kwargs):
  503. from sympy.functions.special.gamma_functions import digamma
  504. if a == 1:
  505. return Piecewise((log(2), Eq(s, 1)), ((1 - 2**(1-s)) * zeta(s), True))
  506. return Piecewise((log(2) - digamma(a) + digamma((a+1)/2), Eq(s, 1)),
  507. (zeta(s, a) - 2**(1-s) * zeta(s, (a+1)/2), True))
  508. def _eval_rewrite_as_genocchi(self, s, a=S.One, **kwargs):
  509. from sympy.functions.special.gamma_functions import digamma
  510. return Piecewise((log(2) - digamma(a) + digamma((a+1)/2), Eq(s, 1)),
  511. (genocchi(1-s, a) / (2 * (s-1)), True))
  512. def _eval_evalf(self, prec):
  513. if all(i.is_number for i in self.args):
  514. return self.rewrite(zeta)._eval_evalf(prec)
  515. class riemann_xi(Function):
  516. r"""
  517. Riemann Xi function.
  518. Examples
  519. ========
  520. The Riemann Xi function is closely related to the Riemann zeta function.
  521. The zeros of Riemann Xi function are precisely the non-trivial zeros
  522. of the zeta function.
  523. >>> from sympy import riemann_xi, zeta
  524. >>> from sympy.abc import s
  525. >>> riemann_xi(s).rewrite(zeta)
  526. s*(s - 1)*gamma(s/2)*zeta(s)/(2*pi**(s/2))
  527. References
  528. ==========
  529. .. [1] https://en.wikipedia.org/wiki/Riemann_Xi_function
  530. """
  531. @classmethod
  532. def eval(cls, s):
  533. from sympy.functions.special.gamma_functions import gamma
  534. z = zeta(s)
  535. if s in (S.Zero, S.One):
  536. return S.Half
  537. if not isinstance(z, zeta):
  538. return s*(s - 1)*gamma(s/2)*z/(2*pi**(s/2))
  539. def _eval_rewrite_as_zeta(self, s, **kwargs):
  540. from sympy.functions.special.gamma_functions import gamma
  541. return s*(s - 1)*gamma(s/2)*zeta(s)/(2*pi**(s/2))
  542. class stieltjes(Function):
  543. r"""
  544. Represents Stieltjes constants, $\gamma_{k}$ that occur in
  545. Laurent Series expansion of the Riemann zeta function.
  546. Examples
  547. ========
  548. >>> from sympy import stieltjes
  549. >>> from sympy.abc import n, m
  550. >>> stieltjes(n)
  551. stieltjes(n)
  552. The zero'th stieltjes constant:
  553. >>> stieltjes(0)
  554. EulerGamma
  555. >>> stieltjes(0, 1)
  556. EulerGamma
  557. For generalized stieltjes constants:
  558. >>> stieltjes(n, m)
  559. stieltjes(n, m)
  560. Constants are only defined for integers >= 0:
  561. >>> stieltjes(-1)
  562. zoo
  563. References
  564. ==========
  565. .. [1] https://en.wikipedia.org/wiki/Stieltjes_constants
  566. """
  567. @classmethod
  568. def eval(cls, n, a=None):
  569. if a is not None:
  570. a = sympify(a)
  571. if a is S.NaN:
  572. return S.NaN
  573. if a.is_Integer and a.is_nonpositive:
  574. return S.ComplexInfinity
  575. if n.is_Number:
  576. if n is S.NaN:
  577. return S.NaN
  578. elif n < 0:
  579. return S.ComplexInfinity
  580. elif not n.is_Integer:
  581. return S.ComplexInfinity
  582. elif n is S.Zero and a in [None, 1]:
  583. return S.EulerGamma
  584. if n.is_extended_negative:
  585. return S.ComplexInfinity
  586. if n.is_zero and a in [None, 1]:
  587. return S.EulerGamma
  588. if n.is_integer == False:
  589. return S.ComplexInfinity
  590. @cacheit
  591. def _dilogtable():
  592. return {
  593. S.Half: pi**2/12 - log(2)**2/2,
  594. Integer(2) : pi**2/4 - I*pi*log(2),
  595. -(sqrt(5) - 1)/2 : -pi**2/15 + log((sqrt(5)-1)/2)**2/2,
  596. -(sqrt(5) + 1)/2 : -pi**2/10 - log((sqrt(5)+1)/2)**2,
  597. (3 - sqrt(5))/2 : pi**2/15 - log((sqrt(5)-1)/2)**2,
  598. (sqrt(5) - 1)/2 : pi**2/10 - log((sqrt(5)-1)/2)**2,
  599. I : I*S.Catalan - pi**2/48,
  600. -I : -I*S.Catalan - pi**2/48,
  601. 1 - I : pi**2/16 - I*S.Catalan - pi*I/4*log(2),
  602. 1 + I : pi**2/16 + I*S.Catalan + pi*I/4*log(2),
  603. (1 - I)/2 : -log(2)**2/8 + pi*I*log(2)/8 + 5*pi**2/96 - I*S.Catalan
  604. }