
    \h                        S SK Jr  S SKJrJrJrJr  S SKJr  S SK	J
r
JrJr  S SKJrJr  S SKJrJrJrJr  S SKJr  S SKJr  S S	KJrJrJr  S S
KJrJr  S SK J!r!J"r"  S SK#J$r$J%r%  S SK&J'r'  S SK(J)r)J*r*J+r+  S SK,J-r-J.r.  S SK/J0r0J1r1J2r2  S SK3J4r4  S SK5J6r6J7r7  S SK8J9r9  S r: " S S\
5      r; " S S\
5      r< " S S\
5      r= " S S\
5      r> " S S\
5      r? " S S \
5      r@ " S! S"\
5      rA " S# S$\
5      rBg%)&    )prod)AddSDummyexpand_func)Expr)DefinedFunctionArgumentIndexError	PoleError)	fuzzy_and	fuzzy_not)RationalpiooI)Powzeta)erferfcEi)re
unpolarify)explog)ceilingfloor)sqrt)sincoscot)	bernoulliharmonic)	factorialrfRisingFactorial)as_int)mpworkprec)prec_to_dpsc                 :     [        U SS9  g! [         a     gf = f)NF)strictT)r'   
ValueErrorns    _/var/www/auris/envauris/lib/python3.13/site-packages/sympy/functions/special/gamma_functions.pyintliker1      s&    q s   
 
c                      ^  \ rS rSrSrSr\R                  4rSS jr	\
S 5       rS rS rS rS	 rSS
 jrS rSU 4S jjrS rSrU =r$ )gamma"   a  
The gamma function

.. math::
    \Gamma(x) := \int^{\infty}_{0} t^{x-1} e^{-t} \mathrm{d}t.

Explanation
===========

The ``gamma`` function implements the function which passes through the
values of the factorial function (i.e., $\Gamma(n) = (n - 1)!$ when n is
an integer). More generally, $\Gamma(z)$ is defined in the whole complex
plane except at the negative integers where there are simple poles.

Examples
========

>>> from sympy import S, I, pi, gamma
>>> from sympy.abc import x

Several special values are known:

>>> gamma(1)
1
>>> gamma(4)
6
>>> gamma(S(3)/2)
sqrt(pi)/2

The ``gamma`` function obeys the mirror symmetry:

>>> from sympy import conjugate
>>> conjugate(gamma(x))
gamma(conjugate(x))

Differentiation with respect to $x$ is supported:

>>> from sympy import diff
>>> diff(gamma(x), x)
gamma(x)*polygamma(0, x)

Series expansion is also supported:

>>> from sympy import series
>>> series(gamma(x), x, 0, 3)
1/x - EulerGamma + x*(EulerGamma**2/2 + pi**2/12) + x**2*(-EulerGamma*pi**2/12 - zeta(3)/3 - EulerGamma**3/6) + O(x**3)

We can numerically evaluate the ``gamma`` function to arbitrary precision
on the whole complex plane:

>>> gamma(pi).evalf(40)
2.288037795340032417959588909060233922890
>>> gamma(1+I).evalf(20)
0.49801566811835604271 - 0.15494982830181068512*I

See Also
========

lowergamma: Lower incomplete gamma function.
uppergamma: Upper incomplete gamma function.
polygamma: Polygamma function.
loggamma: Log Gamma function.
digamma: Digamma function.
trigamma: Trigamma function.
sympy.functions.special.beta_functions.beta: Euler Beta function.

References
==========

.. [1] https://en.wikipedia.org/wiki/Gamma_function
.. [2] https://dlmf.nist.gov/5
.. [3] https://mathworld.wolfram.com/GammaFunction.html
.. [4] https://functions.wolfram.com/GammaBetaErf/Gamma/

Tc                     US:X  a8  U R                  U R                  S   5      [        SU R                  S   5      -  $ [        X5      eN   r   )funcargs	polygammar
   selfargindexs     r0   fdiffgamma.fdiffr   s?    q=99TYYq\*9Q		!+EEE$T44    c                    UR                   (       Gaf  U[        R                  L a  [        R                  $ U[        L a  [        $ [	        U5      (       a/  UR
                  (       a  [        US-
  5      $ [        R                  $ UR                  (       a  UR                  S:X  a  [        UR                  5      UR                  -  nUR
                  (       a  U[        R                  pCO0US-   =p#US-  S:X  a  [        R                  nO[        R                  nU[        [        SSU-  S5      5      -  nUR
                  (       a  U[!        ["        5      -  SU-  -  $ SU-  [!        ["        5      -  U-  $ g g g )Nr7      r      )	is_Numberr   NaNr   r1   is_positiver$   ComplexInfinityis_RationalqabspOneNegativeOner   ranger   r   )clsargr/   kcoeffs        r0   eval
gamma.evalx   s   ===aee|uu	??$S1W--,,,55A:CEE
cee+A#$aee5 !Aq5A:$%EEE$%MMET%1Q3"233E$T"X~144 !tDH}u44%  ! r@   c                    U R                   S   nUR                  (       a  [        UR                  5      UR                  :  a  [        S5      nUR                  UR                  -  nUR                  XBR                  -  -
  nU R                  X4-   5      R                  5       R                  U[        XRR                  5      5      $ UR                  (       am  UR                  5       u  pgU(       a%  UR                  S:w  a  [        U5      nXh-
  4U-   nUnUR                  " USS06nU R                  U5      [        Xv5      -  $ U R                  " U R                   6 $ )Nr   xr7   reevalF)r9   rH   rJ   rK   rI   r   r8   _eval_expand_funcsubsr   is_Addas_coeff_addr   _new_rawargsr&   )	r<   hintsrP   rV   r/   rK   rR   tailintparts	            r0   rX   gamma._eval_expand_func   s
   iil??355zCEE!#JEESUUNEEAeeGOyy'99;@@HQPUPUDVWW::**,KEA,)D0##T8%8D99T??4#???yy$))$$r@   c                 Z    U R                  U R                  S   R                  5       5      $ Nr   )r8   r9   	conjugater<   s    r0   _eval_conjugategamma._eval_conjugate   s"    yy1//122r@   c                     U R                   S   nUR                  (       a  UR                  (       a  g[        U5      (       a  US::  a  gUR                  (       d  UR
                  (       a  gg )Nr   FT)r9   is_nonpositive
is_integerr1   rF   is_nonintegerr<   rV   s     r0   _eval_is_realgamma._eval_is_real   sH    IIaL1::!q&==AOO ,r@   c                     U R                   S   nUR                  (       a  gUR                  (       a  [        U5      R                  $ g )Nr   T)r9   rF   rj   r   is_evenrk   s     r0   _eval_is_positivegamma._eval_is_positive   s5    IIaL==__8### r@   c                 *    [        [        U5      5      $ N)r   loggamma)r<   zlimitvarkwargss       r0   _eval_rewrite_as_tractable gamma._eval_rewrite_as_tractable   s    8A;r@   c                     [        US-
  5      $ Nr7   )r$   r<   ru   rw   s      r0   _eval_rewrite_as_factorial gamma._eval_rewrite_as_factorial   s    Qr@   c                 8  > U R                   S   R                  US5      nUR                  (       a  US::  d  [        TU ]  XU5      $ U R                   S   U-
  nU R                  US-   5      [        U R                   S   U* S-   5      -  R	                  XU5      $ Nr   r7   )r9   limit
is_Integersuper_eval_nseriesr8   r%   )r<   rV   r/   logxcdirx0t	__class__s          r0   r   gamma._eval_nseries   s    YYq\1%"'7(t44IIaL2		!a% DIIaL2#'!::II!PTUUr@   c                 b   U R                   S   nUR                  US5      nUR                  (       aR  UR                  (       aA  U* n[        R
                  U-  U R                  US-   5      -  nXtU-   R                  U5      -  $ UR                  (       d  U R                  U5      $ [        5       er   )
r9   rY   ri   rh   r   rM   r8   as_leading_termis_infiniter   )r<   rV   r   r   rP   r   r/   ress           r0   _eval_as_leading_termgamma._eval_as_leading_term   s    iilXXa^==R..A--"499QU#33Ca0033399R= kr@    r7   rs   )r   )__name__
__module____qualname____firstlineno____doc__
unbranchedr   rG   _singularitiesr>   classmethodrS   rX   re   rl   rp   rx   r}   r   r   __static_attributes____classcell__r   s   @r0   r3   r3   "   sg    JX J'')N5 5 5@%(3$  V
 
r@   r3   c                   j   ^  \ rS rSrSrSS jr\S 5       rS rS r	S r
U 4S jrS	 rS
 rS rSrU =r$ )
lowergamma   aU  
The lower incomplete gamma function.

Explanation
===========

It can be defined as the meromorphic continuation of

.. math::
    \gamma(s, x) := \int_0^x t^{s-1} e^{-t} \mathrm{d}t = \Gamma(s) - \Gamma(s, x).

This can be shown to be the same as

.. math::
    \gamma(s, x) = \frac{x^s}{s} {}_1F_1\left({s \atop s+1} \middle| -x\right),

where ${}_1F_1$ is the (confluent) hypergeometric function.

Examples
========

>>> from sympy import lowergamma, S
>>> from sympy.abc import s, x
>>> lowergamma(s, x)
lowergamma(s, x)
>>> lowergamma(3, x)
-2*(x**2/2 + x + 1)*exp(-x) + 2
>>> lowergamma(-S(1)/2, x)
-2*sqrt(pi)*erf(sqrt(x)) - 2*exp(-x)/sqrt(x)

See Also
========

gamma: Gamma function.
uppergamma: Upper incomplete gamma function.
polygamma: Polygamma function.
loggamma: Log Gamma function.
digamma: Digamma function.
trigamma: Trigamma function.
sympy.functions.special.beta_functions.beta: Euler Beta function.

References
==========

.. [1] https://en.wikipedia.org/wiki/Incomplete_gamma_function#Lower_incomplete_gamma_function
.. [2] Abramowitz, Milton; Stegun, Irene A., eds. (1965), Chapter 6,
       Section 5, Handbook of Mathematical Functions with Formulas, Graphs,
       and Mathematical Tables
.. [3] https://dlmf.nist.gov/8
.. [4] https://functions.wolfram.com/GammaBetaErf/Gamma2/
.. [5] https://functions.wolfram.com/GammaBetaErf/Gamma3/

c                 0   SSK Jn  US:X  a+  U R                  u  p4[        [	        U5      * 5      XCS-
  -  -  $ US:X  aO  U R                  u  p4[        U5      [        U5      -  [        U5      [        X45      -  -
  U" / SS/SSU// U5      -
  $ [        X5      eNr   )meijergrB   r7   )
sympy.functions.special.hyperr   r9   r   r   r3   digammar   
uppergammar
   r<   r=   r   aru   s        r0   r>   lowergamma.fdiff  s    9q=99DA
1~&qq5z11]99DA8GAJ&Q
10@)@@"q!fq!QiQ78 8 %T44r@   c                    U[         R                  L a  [         R                  $ UR                  5       u  p4UR                  (       a-  UR                  (       a  [        U5      nX2:w  a  [        X5      $ OUR                  (       aY  UR                  (       aH  US:w  aA  S[        -  [        -  U-  [         R                  U* -  -  [        U* 5      -  [        X5      -   $ O1US:w  a+  [        S[        -  [        -  U-  U-  5      [        X5      -  $ UR                  (       Gaq  U[         R                  L a  [         R                  [        U* 5      -
  $ U[         R                  L a$  [!        [        5      [#        [!        U5      5      -  $ UR$                  (       d  SU-  R$                  (       Ga  US-
  nUR                  (       Ga  UR                  (       aU  [        U5      [        U* 5      [        U5      -  ['        [)        U5       Vs/ s H  obU-  [        U5      -  PM     sn6 -  -
  $ [+        U5      [        [         R                  U5      [!        [        5      -  [        U* 5      ['        [)        SU[         R                  -   5       Vs/ s H5  obU[         R                  -
  -  [+        [         R                  U-   5      -  PM7     sn6 -  -
  -  $ UR$                  (       d  [         R                  [         R                  U-
  -  [        -  [#        [!        U5      5      -  [+        SU-
  5      -  [        U* 5      ['        [)        S[-        SS5      U-
  5       Vs/ s H&  obXa-   S-
  -  [+        U5      -  [+        X-   5      -  PM(     sn6 -  -   $ UR.                  (       a  [         R                  $ g s  snf s  snf s  snf )Nr   rB   r7   rC   )r   Zeroextract_branch_factorri   rF   r   r   rh   r   r   rM   r$   r   rD   rL   Halfr   r   r   r   rN   r3   r   is_zero)rO   r   rV   nxr/   brQ   s          r0   rS   lowergamma.eval#  s   " ;66M'')<<AMMABw!!(( \\a..AvtAvax 33IqbMAJqDUUU !VqtAvaxz?:a#444 ;;;AEEzuusA2w&affBxDG,,!A#!1!1!1E===||(|c1"g	!.DslqrsltLultghRSVV_`aVbMbltLuGv.vvv$QxAFFA)>tBx)G#qb'RU  DI  JK  MN  QR  QW  QW  MW  DX  XY  DX~\]`a`f`f\fXghmnontntwxnxhyXy  DX  XY  SZ  KZ  *Z   [  [||==166A:6r9#d1g,FuQQRU|SVY[\Z\V]^a  SX  YZ  \d  ef  hi  \j  mn  \n  So  dp  So  NOhihmpqhqdrsxyzs{d{  }B  CD  CH  }I  eI  So  dp  _q  Wq  q  q9966M  Mv XY dps   	N4<N9-N>c                 h   [        S U R                   5       5      (       a  U R                  S   R                  U5      nU R                  S   R                  U5      n[        U5         [        R
                  " USU5      nS S S 5        [        R                  " WU5      $ U $ ! , (       d  f       N'= f)Nc              3   8   #    U  H  oR                   v   M     g 7frs   	is_number.0rV   s     r0   	<genexpr>)lowergamma._eval_evalf.<locals>.<genexpr>V       .Iq{{I   r   r7   )allr9   
_to_mpmathr)   r(   gammaincr   _from_mpmathr<   precr   ru   r   s        r0   _eval_evalflowergamma._eval_evalfU  s    .DII...		!''-A		!''-A$kk!Q*  $$S$//K	  s   )B##
B1c                     U R                   S   nU[        R                  [        R                  4;  a;  U R	                  U R                   S   R                  5       UR                  5       5      $ g r6   r9   r   r   NegativeInfinityr8   rc   rk   s     r0   re   lowergamma._eval_conjugate_  S    IIaLQVVQ//0099TYYq\335q{{}EE 1r@   c                    U R                   u  p4[        UR                  X5      UR                  X5      /5      nU(       d  U$ UR                  X5      nUR                  (       a!  [        UR
                  UR                  /5      $ UR                  X5      n[        UR                  UR                  [        UR                  5      /5      $ rs   )	r9   r   _eval_is_meromorphicrY   ri   rF   	is_finiter   r   )r<   rV   r   sru   
args_meromz0s0s           r0   r   lowergamma._eval_is_meromorphicd  s     yy 6 6q <""1( * +
VVA\<<ammR\\:;;VVA\",,i

6KLMMr@   c                 0  >^	^
 SSK Jn  U R                  u  m	m
US   [        L ac  T
R	                  U5      (       dM  T
T	-  [        T
* 5      -  n[        U	U
4S j[        US-
  5       5       5      nU" T
T	-  T	U* -  -  5      nXg-  U-   $ [        TU ]%  XX45      $ )Nr   )Oc              3   N   >#    U  H  nTU-  [        TUS -   5      -  v   M     g7f)r7   N)r%   )r   rQ   r   ru   s     r0   r   +lowergamma._eval_aseries.<locals>.<genexpr>z  s$     Cl1a41a!e,ls   "%r7   )
sympy.series.orderr   r9   r   hasr   sumrN   r   _eval_aseries)r<   r/   args0rV   r   r   rR   sum_expror   ru   r   s            @@r0   r   lowergamma._eval_aseriesu  s    (yy18r>!%%((qDaRLECeAElCCH!Q$qA2w,A>A%%w$Qq77r@   c                 0    [        U5      [        X5      -
  $ rs   )r3   r   r<   r   rV   rw   s       r0   _eval_rewrite_as_uppergamma&lowergamma._eval_rewrite_as_uppergamma      Qx*Q***r@   c                     SSK Jn  UR                  (       a  UR                  (       a  U $ U R	                  [
        5      R	                  U5      $ )Nr   expint)'sympy.functions.special.error_functionsr   ri   rh   rewriter   r<   r   rV   rw   r   s        r0   _eval_rewrite_as_expint"lowergamma._eval_rewrite_as_expint  s3    B<<A,,K||J'//77r@   c                 F    U R                   S   nUR                  (       a  gg )Nr7   T)r9   r   rk   s     r0   _eval_is_zerolowergamma._eval_is_zero  s    IIaL99 r@   r   rB   )r   r   r   r   r   r>   r   rS   r   re   r   r   r   r   r   r   r   r   s   @r0   r   r      sM    4n5 / /bF
N"8+8 r@   r   c                   V    \ rS rSrSrSS jrS r\S 5       rS r	S r
S rS	 rS
 rSrg)r   i  a  
The upper incomplete gamma function.

Explanation
===========

It can be defined as the meromorphic continuation of

.. math::
    \Gamma(s, x) := \int_x^\infty t^{s-1} e^{-t} \mathrm{d}t = \Gamma(s) - \gamma(s, x).

where $\gamma(s, x)$ is the lower incomplete gamma function,
:class:`lowergamma`. This can be shown to be the same as

.. math::
    \Gamma(s, x) = \Gamma(s) - \frac{x^s}{s} {}_1F_1\left({s \atop s+1} \middle| -x\right),

where ${}_1F_1$ is the (confluent) hypergeometric function.

The upper incomplete gamma function is also essentially equivalent to the
generalized exponential integral:

.. math::
    \operatorname{E}_{n}(x) = \int_{1}^{\infty}{\frac{e^{-xt}}{t^n} \, dt} = x^{n-1}\Gamma(1-n,x).

Examples
========

>>> from sympy import uppergamma, S
>>> from sympy.abc import s, x
>>> uppergamma(s, x)
uppergamma(s, x)
>>> uppergamma(3, x)
2*(x**2/2 + x + 1)*exp(-x)
>>> uppergamma(-S(1)/2, x)
-2*sqrt(pi)*erfc(sqrt(x)) + 2*exp(-x)/sqrt(x)
>>> uppergamma(-2, x)
expint(3, x)/x**2

See Also
========

gamma: Gamma function.
lowergamma: Lower incomplete gamma function.
polygamma: Polygamma function.
loggamma: Log Gamma function.
digamma: Digamma function.
trigamma: Trigamma function.
sympy.functions.special.beta_functions.beta: Euler Beta function.

References
==========

.. [1] https://en.wikipedia.org/wiki/Incomplete_gamma_function#Upper_incomplete_gamma_function
.. [2] Abramowitz, Milton; Stegun, Irene A., eds. (1965), Chapter 6,
       Section 5, Handbook of Mathematical Functions with Formulas, Graphs,
       and Mathematical Tables
.. [3] https://dlmf.nist.gov/8
.. [4] https://functions.wolfram.com/GammaBetaErf/Gamma2/
.. [5] https://functions.wolfram.com/GammaBetaErf/Gamma3/
.. [6] https://en.wikipedia.org/wiki/Exponential_integral#Relation_with_other_functions

c                    SSK Jn  US:X  a,  U R                  u  p4[        [	        U5      * 5      * XCS-
  -  -  $ US:X  a7  U R                  u  p4[        X45      [        U5      -  U" / SS/SSU// U5      -   $ [        X5      er   )r   r   r9   r   r   r   r   r
   r   s        r0   r>   uppergamma.fdiff  s    9q=99DAA''E
22]99DAa#CF*WR!Q!QBPQ-RRR$T44r@   c                    [        S U R                   5       5      (       a  U R                  S   R                  U5      nU R                  S   R                  U5      n[        U5         [        R
                  " X#[        R                  5      nS S S 5        [        R                  " WU5      $ U $ ! , (       d  f       N'= f)Nc              3   8   #    U  H  oR                   v   M     g 7frs   r   r   s     r0   r   )uppergamma._eval_evalf.<locals>.<genexpr>  r   r   r   r7   )	r   r9   r   r)   r(   r   infr   r   r   s        r0   r   uppergamma._eval_evalf  s    .DII...		!''-A		!''-A$kk!/  $$S$//  s   )&B00
B>c                 	   SSK Jn  UR                  (       ar  U[        R                  L a  [        R                  $ U[
        L a  [        R                  $ UR                  (       a%  [        U5      R                  (       a  [        U5      $ UR                  5       u  pEUR                  (       a-  UR                  (       a  [        U5      nX$:w  a  [        X5      $ OUR                  (       aY  UR                  (       aH  US:w  aA  S[         -  ["        -  U-  [        R$                  U* -  -  ['        U* 5      -  [        X5      -   $ O`US:w  aZ  [        U5      S[)        S[         -  ["        -  U-  U-  5      -
  -  [)        S[         -  ["        -  U-  U-  5      [        X5      -  -   $ UR                  (       Ga  U[        R                  L a  UR                  (       a  [+        U* 5      * $ U[        R,                  L a  [)        U* 5      $ U[        R.                  L a$  [1        [         5      [3        [1        U5      5      -  $ UR4                  (       d  SU-  R4                  (       Ga$  US-
  nUR                  (       Ga  UR                  (       aI  [)        U* 5      ['        U5      -  [7        [9        U5       Vs/ s H  nX'-  ['        U5      -  PM     sn6 -  $ [        U5      [3        [1        U5      5      -  [        R$                  U[        S5      S-  -
  -  [)        U* 5      -  [1        U5      -  [7        [9        U[        R.                  -
  5       Vs/ s H6  n[        [        R.                  * U-
  5      U* U-  -  [        SU-
  5      -  PM8     sn6 -  -   $ UR4                  (       a  U" U* U5      [        U5      US-   -  -  $ UR4                  (       d  [        R$                  [        R.                  U-
  -  [         -  [3        [1        U5      5      -  [        SU-
  5      -  X!-  [)        U* 5      -  [7        [9        [        R.                  U-
  5       Vs/ s H$  nX'-  [        U5      -  [        X-   S-   5      -  PM&     sn6 -  -
  $ UR                  (       a  UR                  (       a  [+        U* 5      * $ UR                  (       a&  [        U5      R                  (       a  [        U5      $ g g s  snf s  snf s  snf )Nr   r   r7   rB   rC   )r   r   rD   r   rE   r   r   r   r   rF   r3   r   ri   r   r   rh   r   r   rM   r$   r   r   rL   r   r   r   r   r   rN   )rO   r   ru   r   r   r/   r   rQ   s           r0   rS   uppergamma.eval  s   B;;AEEzuubvva5$$ 8O '')<<AMMABw!!(( \\a..Av"uQwqy!!44Yr]BZPQEVVV !V8QQrT!VAXaZ01C"Qq
OJqDU4UUU ;;;AFF{q}}A2waeeA2waffBxT!W--!A#!1!1!1E===||"A2w15GLQx>QGO! ?@dYq\>QGO>Q 9R  R R !&a4Q= 8 !AaDF
 ;c1"g EQ O"%16q166z1B(D1BA ).qvvgk(:qb1W(DuQqSz(Q1B(D #E!E!E F \\!1"a=AQ)???||MMAFFQJ7"<T$q']J5QRSTQT:UdS!Wns>CAFFQJ>O5Q>O 67TE!H_uQSQRU|5S>O5Q 0R RR S 99rF7N99A**8O +9%>Q
(D5Qs   4S.
=S3+S8c                     U R                   S   nU[        R                  [        R                  4;  a;  U R	                  U R                   S   R                  5       UR                  5       5      $ g r6   r   r<   ru   s     r0   re   uppergamma._eval_conjugate  r   r@   c                 .    [         R                  XU5      $ rs   )r   r   )r<   rV   r   s      r0   r   uppergamma._eval_is_meromorphic"  s    ..t::r@   c                 0    [        U5      [        X5      -
  $ rs   )r3   r   r   s       r0   _eval_rewrite_as_lowergamma&uppergamma._eval_rewrite_as_lowergamma%  r   r@   c                 B    [        [        U5      5      [        X5      -
  $ rs   )r   rt   r   r   s       r0   rx   %uppergamma._eval_rewrite_as_tractable(  s    8A;*Q"222r@   c                 0    SSK Jn  U" SU-
  U5      X!-  -  $ )Nr   r   r7   )r   r   r   s        r0   r   "uppergamma._eval_rewrite_as_expint+  s    Ba!eQ$$r@   r   Nr   )r   r   r   r   r   r>   r   r   rS   re   r   r   rx   r   r   r   r@   r0   r   r     sA    >B	5 6 6pF
;+3%r@   r   c                   |   ^  \ rS rSrSr\S 5       rS rS rS r	S r
S rS	 rS
 rS rSS jrU 4S jrS rSrU =r$ )r:   i4  a[
  
The function ``polygamma(n, z)`` returns ``log(gamma(z)).diff(n + 1)``.

Explanation
===========

It is a meromorphic function on $\mathbb{C}$ and defined as the $(n+1)$-th
derivative of the logarithm of the gamma function:

.. math::
    \psi^{(n)} (z) := \frac{\mathrm{d}^{n+1}}{\mathrm{d} z^{n+1}} \log\Gamma(z).

For `n` not a nonnegative integer the generalization by Espinosa and Moll [5]_
is used:

.. math:: \psi(s,z) = \frac{\zeta'(s+1, z) + (\gamma + \psi(-s)) \zeta(s+1, z)}
    {\Gamma(-s)}

Examples
========

Several special values are known:

>>> from sympy import S, polygamma
>>> polygamma(0, 1)
-EulerGamma
>>> polygamma(0, 1/S(2))
-2*log(2) - EulerGamma
>>> polygamma(0, 1/S(3))
-log(3) - sqrt(3)*pi/6 - EulerGamma - log(sqrt(3))
>>> polygamma(0, 1/S(4))
-pi/2 - log(4) - log(2) - EulerGamma
>>> polygamma(0, 2)
1 - EulerGamma
>>> polygamma(0, 23)
19093197/5173168 - EulerGamma

>>> from sympy import oo, I
>>> polygamma(0, oo)
oo
>>> polygamma(0, -oo)
oo
>>> polygamma(0, I*oo)
oo
>>> polygamma(0, -I*oo)
oo

Differentiation with respect to $x$ is supported:

>>> from sympy import Symbol, diff
>>> x = Symbol("x")
>>> diff(polygamma(0, x), x)
polygamma(1, x)
>>> diff(polygamma(0, x), x, 2)
polygamma(2, x)
>>> diff(polygamma(0, x), x, 3)
polygamma(3, x)
>>> diff(polygamma(1, x), x)
polygamma(2, x)
>>> diff(polygamma(1, x), x, 2)
polygamma(3, x)
>>> diff(polygamma(2, x), x)
polygamma(3, x)
>>> diff(polygamma(2, x), x, 2)
polygamma(4, x)

>>> n = Symbol("n")
>>> diff(polygamma(n, x), x)
polygamma(n + 1, x)
>>> diff(polygamma(n, x), x, 2)
polygamma(n + 2, x)

We can rewrite ``polygamma`` functions in terms of harmonic numbers:

>>> from sympy import harmonic
>>> polygamma(0, x).rewrite(harmonic)
harmonic(x - 1) - EulerGamma
>>> polygamma(2, x).rewrite(harmonic)
2*harmonic(x - 1, 3) - 2*zeta(3)
>>> ni = Symbol("n", integer=True)
>>> polygamma(ni, x).rewrite(harmonic)
(-1)**(n + 1)*(-harmonic(x - 1, n + 1) + zeta(n + 1))*factorial(n)

See Also
========

gamma: Gamma function.
lowergamma: Lower incomplete gamma function.
uppergamma: Upper incomplete gamma function.
loggamma: Log Gamma function.
digamma: Digamma function.
trigamma: Trigamma function.
sympy.functions.special.beta_functions.beta: Euler Beta function.

References
==========

.. [1] https://en.wikipedia.org/wiki/Polygamma_function
.. [2] https://mathworld.wolfram.com/PolygammaFunction.html
.. [3] https://functions.wolfram.com/GammaBetaErf/PolyGamma/
.. [4] https://functions.wolfram.com/GammaBetaErf/PolyGamma2/
.. [5] O. Espinosa and V. Moll, "A generalized polygamma function",
       *Integral Transforms and Special Functions* (2004), 101-115.

c                    U[         R                  L d  U[         R                  L a  [         R                  $ U[        L a'  UR                  (       a  [        $ [         R                  $ UR
                  (       a!  UR                  (       a  [         R                  $ U[         R                  L a!  [        U5      [        S[        -  5      S-  -
  $ UR                  (       a  U[        * L d$  UR                  [        5      [        [        * 4;   a  [        $ UR
                  (       a  [        US-
  5      [         R                  -
  $ UR                   (       a;  UR#                  5       u  p4US::  a"  [%        ['        [         R                  USS95      $ g g UR(                  (       a  UR*                  (       a  [-        U5      nX%:w  a  ['        X5      $ UR
                  (       a2  [         R                  US-   -  [/        U5      -  [1        US-   U5      -  $ U[         R2                  L a=  [         R                  US-   -  [/        U5      -  SUS-   -  S-
  -  [1        US-   5      -  $ g g g )NrB   r7      F)evaluate)r   rE   r   r   r   r   rh   rG   rM   rt   r   r   extract_multiplicativelyr   r#   
EulerGammarH   as_numer_denomr   r:   ri   is_nonnegativer   r$   r   r   )rO   r/   ru   rK   rI   nzs         r0   rS   polygamma.eval  s   :aee55L"W2..\\a..$$$!--A;QrTQ..YYRCx155a8R"IE	!}q||33'')6&yU'KLL 	  \\a..ABw ''||}}qs+il:T!A#q\IIaff}}qs+il:a!A#hqjIDQRSTQTIUU  /\r@   c                     U R                   S   R                  (       a   U R                   S   R                  (       a  gg g )Nr   r7   T)r9   rF   rd   s    r0   rl   polygamma._eval_is_real  s/    99Q<##		!(@(@ )A#r@   c                     U R                   S   n[        UR                  UR                  /5      n[        UR                  [        U5      /5      $ r{   )r9   r   is_negativeri   
is_complexr   )r<   ru   is_negative_integers      r0   _eval_is_complexpolygamma._eval_is_complex  sA    IIaL'(EF!,,	2E(FGHHr@   c                     U R                   u  pUR                  (       aH  UR                  (       a  UR                  (       a  gUR                  (       a  UR                  (       a  gg g g NTF)r9   rF   is_oddis_realro   r<   r/   ru   s      r0   rp   polygamma._eval_is_positive  s@    yy==xxAIIyyQ]] +y r@   c                     U R                   u  pUR                  (       aH  UR                  (       a  UR                  (       a  gUR                  (       a  UR                  (       a  gg g g r  )r9   rF   ro   r  r  r  s      r0   _eval_is_negativepolygamma._eval_is_negative  s@    yy==yyQ]]xxAII &x r@   c                    U R                   u  p#UR                  (       Ga  UR                  (       Ga  UR                  (       a  UR                   S   nUR                  (       a  US-   * nUS:  a<  [	        [        S[        U5      S-   5       Vs/ s H  n[        X6-
  U5      PM     sn6 nO9[	        [        [        U* 5      5       Vs/ s H  n[        X6-   U5      PM     sn6 * n[        X#U-
  5      [        R                  U-  [        U5      -  U-  -   $ OUR                  (       a  UR                  5       u  pCUR                  (       aw  UR                  (       af  [        [        U5      5       Vs/ s H  n[        X#[        Xd5      -   5      PM     nnUS:X  a  [	        U6 U-  [!        U5      -   $ [	        U6 XBS-   -  -  $ X4-  nUS:X  GaE  UR"                  (       Ga3  UR%                  5       u  p[        R&                  * [(        [+        U[(        -  U	-  5      -  S-  -
  [!        U	5      -
  [	        [        SU	5       V
s/ s H@  n
[-        SU
-  [(        -  U-  U	-  5      [!        S[/        U
[(        -  U	-  5      -  5      -  PMB     sn
6 -   nUS:  a9  [1        U5      nX2-
  nU[	        [        U5       V
s/ s H
  n
SX-   -  PM     sn
6 -   $ US:  a@  [1        SU-
  5      nX2-   nU[	        [        U5       V
s/ s H  n
SUS-
  U
-
  -  PM     sn
6 -
  $ US:X  a!  [3        U5      [!        S[(        -  5      S-  -
  $ UR4                  SL d  UR                  SL at  [7        S5      n[9        X5      R;                  U5      R=                  XS-   5      nU[        R&                  [?        U* 5      -   [9        US-   U5      -  -   [A        U* 5      -  $ [        X#5      $ s  snf s  snf s  snf s  sn
f s  sn
f s  sn
f )Nr   r7   rB   Fr   )!r9   r   r  rZ   r   rN   intr   r:   r   rM   r$   is_Mulas_two_termsrF   r   r   rH   r  r
  r   r!   r    r   r   rt   ri   r   r   diffrY   r   r3   )r<   r]   r/   ru   rR   eir^   rK   rI   rQ   part_1r   r   dzts                  r0   rX   polygamma._eval_expand_func  s   yy<<<A,,,xxq	##a%Aqy"/4QE
Q/G%I/G! &)E1&&/G%I  J !$/4S%[/A&C/A! '*E1'&/A&C !D  D$QE	2Q]]A5EiPQl5RSW5WWW $ >>+##(9(9,1#e*,=?,=q &aX." *" #,=  ?Av"Dz%/#e*<<"Dz%a%.88
6ammm##%DA ll]R#a"fqj/%9A%==AFNSTUWXkZk#a!ebj1nq()CCB
O0C,DDkZJ\ \F 1u!HUE!H%EHqa26lH%E FFFQ!a%LUa%I1a26A:&6%I JJJ7A;QrTQ..<<5 A$4$4$=c
Aq*//!$))!qS1C1<<'1"+5ac1EEPQrRRU%I&C? [
 &F &Js%   OO2!O  AO%
5O*
7O/
c                     UR                   (       aD  UR                  (       a2  [        R                  US-   -  [	        U5      -  [        US-   U5      -  $ g g r{   )ri   rF   r   rM   r$   r   r<   r/   ru   rw   s       r0   _eval_rewrite_as_zetapolygamma._eval_rewrite_as_zeta  sA    <<AMM==1q5))A,6tAE1~EE *<r@   c                    UR                   (       at  UR                  (       a  [        US-
  5      [        R                  -
  $ [        R
                  US-   -  [        U5      -  [        US-   5      [        US-
  US-   5      -
  -  $ g r{   )ri   r   r#   r   r
  rM   r$   r   r,  s       r0   _eval_rewrite_as_harmonic#polygamma._eval_rewrite_as_harmonic  so    <<yyA55}}qs+il:d1Q3i(STUVSVXYZ[X[J\>\]]	 r@   c                 (   SSK Jn  U R                   Vs/ s H  oUR                  U5      PM     snu  pgU" Xq5      nUS:X  a<  UR	                  SU-  5      (       a#  Uc  [        U5      OUnUR                  5       U-  $ U R                  Xg5      $ s  snf )Nr   Orderr7   )r   r4  r9   r   containsr   getnr8   )	r<   rV   r   r   r4  r   r/   ru   r   s	            r0   r   polygamma._eval_as_leading_term  sz    ,.2ii8i!!!$i8!K6ajj1oo!\3q6tD668d?"99Q?" 9s   Bc                 d    US:X  a   U R                   S S u  p#[        US-   U5      $ [        X5      eNrB   r7   )r9   r:   r
   )r<   r=   r/   ru   s       r0   r>   polygamma.fdiff   s6    q=99Ra=DAQUA&&$T44r@   c                 0  > SSK Jn  US   [        :w  d<  U R                  S   R                  (       a  U R                  S   R
                  (       d  [        TU ]  XX45      $ U R                  S   nU R                  S   nUS:X  a  [        U5      SSU-  -  -
  nS n	US:  a  U" SU-  U5      n	Oa[        US-   S-  5      n
[        SU
5       Vs/ s H   n[        SU-  5      SU-  USU-  -  -  -  PM"     nnU[        U6 -  nU" SXa-  -  U5      n	UR                  X1U5      U	-   $ [        U5      nXU-  SU-  -  -   n[        US-   S-  5      n
[        SU
5       HI  nUSU-  U-   S-
  -  SU-  U-   S-
  -  SU-  SU-  S-
  -  -  nU[        SU-  5      U-  USU-  -  -  -  nMK     U" SUSU
-  -  -  U5      n	US:X  a  U" SU-  U5      n	OUS:X  a  U" SUS-  -  U5      n	UR                  XaU5      U	-   nSSU-  U-  -  U-  R                  X1U5      $ s  snf )Nr   r3  r7   rB   r!  )r   r4  r   r9   r   r  r   r   r   r   rN   r"   r   r   r3   )r<   r/   r   rV   r   r4  ru   Nrr   mrQ   lface0r   s                  r0   r   polygamma._eval_aseries'  sC   ,8r>1((TYYq\-H-H7(1;;IIaLIIaL6 AAaC AA1u!A#qMQUQJ'>CAqkJkYqs^qs1qs8|4kJS!W!AD&!$??1.22 (Cuac{"BQ
#A1a[1Q37Q;'1q15!A#!aIi!nS(QqS11 ! aAaCj!$AAv!A#qMa!AqD&!$  t,q0A"Q$NQ&55aDAA- Ks   'Hc                    [        S U R                   5       5      (       d  g U R                  S   R                  US-   5      nU R                  S   R                  US-   5      n[        R                  " U5      (       a  US::  a  [
        R                  $ [        US-   5         [        R                  " U5      (       a  US:  a  [        R                  " X#5      nO{[        R                  " US-   U5      n[        R                  " US-   US5      nU[        R                  [        R                  " U* 5      -   U-  -   [        R                  " U* 5      -  nS S S 5        [        R                  " WU5      $ ! , (       d  f       N%= f)Nc              3   8   #    U  H  oR                   v   M     g 7frs   r   )r   r'  s     r0   r   (polygamma._eval_evalf.<locals>.<genexpr>Q  s     2	1;;	r   r      r7   )r   r9   r   r(   isintr   rG   r)   r:   r   eulerr   rgammar   r   )r<   r   r   ru   r   ztr)  s          r0   r   polygamma._eval_evalfP  s   2		222IIaL##DG,IIaL##DG,88A;;16$$$d2gxx{{qAvll1(WWQqS!_ggac1a(bhhQB72==A2N    d++ s   $B4E77
Fr   r   )r   r   r   r   r   r   rS   rl   r  rp   r  rX   r-  r0  r   r>   r   r   r   r   r   s   @r0   r:   r:   4  sb    hT V V:I
3jF^#5'BR, ,r@   r:   c                   n   ^  \ rS rSrSr\S 5       rS rSU 4S jjrU 4S jr	S r
S rS	 rSS
 jrSrU =r$ )rt   ia  a	  
The ``loggamma`` function implements the logarithm of the
gamma function (i.e., $\log\Gamma(x)$).

Examples
========

Several special values are known. For numerical integral
arguments we have:

>>> from sympy import loggamma
>>> loggamma(-2)
oo
>>> loggamma(0)
oo
>>> loggamma(1)
0
>>> loggamma(2)
0
>>> loggamma(3)
log(2)

And for symbolic values:

>>> from sympy import Symbol
>>> n = Symbol("n", integer=True, positive=True)
>>> loggamma(n)
log(gamma(n))
>>> loggamma(-n)
oo

For half-integral values:

>>> from sympy import S
>>> loggamma(S(5)/2)
log(3*sqrt(pi)/4)
>>> loggamma(n/2)
log(2**(1 - n)*sqrt(pi)*gamma(n)/gamma(n/2 + 1/2))

And general rational arguments:

>>> from sympy import expand_func
>>> L = loggamma(S(16)/3)
>>> expand_func(L).doit()
-5*log(3) + loggamma(1/3) + log(4) + log(7) + log(10) + log(13)
>>> L = loggamma(S(19)/4)
>>> expand_func(L).doit()
-4*log(4) + loggamma(3/4) + log(3) + log(7) + log(11) + log(15)
>>> L = loggamma(S(23)/7)
>>> expand_func(L).doit()
-3*log(7) + log(2) + loggamma(2/7) + log(9) + log(16)

The ``loggamma`` function has the following limits towards infinity:

>>> from sympy import oo
>>> loggamma(oo)
oo
>>> loggamma(-oo)
zoo

The ``loggamma`` function obeys the mirror symmetry
if $x \in \mathbb{C} \setminus \{-\infty, 0\}$:

>>> from sympy.abc import x
>>> from sympy import conjugate
>>> conjugate(loggamma(x))
loggamma(conjugate(x))

Differentiation with respect to $x$ is supported:

>>> from sympy import diff
>>> diff(loggamma(x), x)
polygamma(0, x)

Series expansion is also supported:

>>> from sympy import series
>>> series(loggamma(x), x, 0, 4).cancel()
-log(x) - EulerGamma*x + pi**2*x**2/12 - x**3*zeta(3)/3 + O(x**4)

We can numerically evaluate the ``loggamma`` function
to arbitrary precision on the whole complex plane:

>>> from sympy import I
>>> loggamma(5).evalf(30)
3.17805383034794561964694160130
>>> loggamma(I).evalf(20)
-0.65092319930185633889 - 1.8724366472624298171*I

See Also
========

gamma: Gamma function.
lowergamma: Lower incomplete gamma function.
uppergamma: Upper incomplete gamma function.
polygamma: Polygamma function.
digamma: Digamma function.
trigamma: Trigamma function.
sympy.functions.special.beta_functions.beta: Euler Beta function.

References
==========

.. [1] https://en.wikipedia.org/wiki/Gamma_function
.. [2] https://dlmf.nist.gov/5
.. [3] https://mathworld.wolfram.com/LogGammaFunction.html
.. [4] https://functions.wolfram.com/GammaBetaErf/LogGamma/

c                 V   UR                   (       a=  UR                  (       a  [        $ UR                  (       a  [	        [        U5      5      $ OUR                  (       av  UR                  5       u  p#UR                  (       aS  US:X  aM  [	        [        [        5      SSU-
  -  -  [        U5      -  [        US-   [        R                  -  5      -  5      $ U[        L a  [        $ [        U5      [        L a  [        R                  $ U[        R                  L a  [        R                  $ g r9  )ri   rh   r   rF   r   r3   is_rationalr  r   r   r   r   rJ   rG   rE   )rO   ru   rK   rI   s       r0   rS   loggamma.eval  s    <<	58}$ ]]##%DA}}a48a!a%j058;eQUAFFN>SSTT7IVr\$$$:55L r@   c                 z   SSK Jn  U R                  S   nUR                  (       Ga  UR	                  5       u  pEXE-  nXFU-  -
  nUR
                  (       a  UR
                  (       a  XE:  a  [        S5      nUR
                  (       a;  [        XE-  5      U[        U5      -  -
  U" [        US-
  U-  U-   5      USU45      -   $ UR                  (       aI  [        XE-  5      U[        U5      -  -
  [        [        -  U-  -   U" [        Xu-  U-
  5      USU* 45      -
  $ UR                  (       a  [        XE-  5      $ U $ )Nr   SumrQ   r7   )sympy.concrete.summationsrR  r9   rH   r  rF   r   rt   r   r  r   r   r   )r<   r]   rR  ru   rK   rI   r/   rQ   s           r0   rX   loggamma._eval_expand_func  s   1IIaL===##%DA AaCA}}15#J==#AE?Qs1vX5CQ	A<NQRTUWXPY8ZZZ]]#AE?Qs1vX51Q>Sq\TUWX[\Z\S]A^^^YY#AE?*r@   c                    > U R                   S   R                  US5      nUR                  (       a+  U R                  " U R                   6 nUR	                  XU5      $ [
        TU ]  XU5      $ rb   )r9   r   r   _eval_rewrite_as_intractabler   r   )r<   rV   r/   r   r   r   fr   s          r0   r   loggamma._eval_nseries  s[    YYq\1%::11499=A??1..w$Q400r@   c                   > SSK Jn  US   [        :w  a  [        TU ]  XX45      $ U R
                  S   n[        U5      U[        R                  -
  -  U-
  [        S[        -  5      S-  -   n[        SU5       Vs/ s H,  n[        SU-  5      SU-  SU-  S-
  -  USU-  S-
  -  -  -  PM.     n	nS n
US:X  a
  U" SU5      n
OU" SXa-  -  U5      n
U[        U	6 -   R                  X1U5      U
-   $ s  snf )Nr   r3  rB   r7   )r   r4  r   r   r   r9   r   r   r   r   rN   r"   r   r   )r<   r/   r   rV   r   r4  ru   r=  rQ   r?  r   r   s              r0   r   loggamma._eval_aseries  s    ,8r>7(1;;IIaLFAJ!#c!B$ik1DI!QKPKqYqs^qsAaC!G}Q1q\9:KP6aAafa ACG**16:: Qs   :3C-c                 *    [        [        U5      5      $ rs   )r   r3   r|   s      r0   rV  %loggamma._eval_rewrite_as_intractable  s    58}r@   c                 j    U R                   S   nUR                  (       a  gUR                  (       a  gg )Nr   TF)r9   rF   rh   r   s     r0   rl   loggamma._eval_is_real  s*    IIaL== r@   c                     U R                   S   nU[        R                  [        R                  4;  a  U R	                  UR                  5       5      $ g rb   r   r   s     r0   re   loggamma._eval_conjugate  s@    IIaLQVVQ//0099Q[[]++ 1r@   c                 V    US:X  a  [        SU R                  S   5      $ [        X5      er6   )r:   r9   r
   r;   s     r0   r>   loggamma.fdiff  s)    q=Q		!--$T44r@   r   rb   r   )r   r   r   r   r   r   rS   rX   r   r   rV  rl   re   r>   r   r   r   s   @r0   rt   rt   a  sF    lZ  &*1;,
5 5r@   rt   c                   h    \ rS rSrSrS rSS jrS rS rS r	S r
\S	 5       rS
 rS rS rS rSrg)r   i$  a  
The ``digamma`` function is the first derivative of the ``loggamma``
function

.. math::
    \psi(x) := \frac{\mathrm{d}}{\mathrm{d} z} \log\Gamma(z)
            = \frac{\Gamma'(z)}{\Gamma(z) }.

In this case, ``digamma(z) = polygamma(0, z)``.

Examples
========

>>> from sympy import digamma
>>> digamma(0)
zoo
>>> from sympy import Symbol
>>> z = Symbol('z')
>>> digamma(z)
polygamma(0, z)

To retain ``digamma`` as it is:

>>> digamma(0, evaluate=False)
digamma(0)
>>> digamma(z, evaluate=False)
digamma(z)

See Also
========

gamma: Gamma function.
lowergamma: Lower incomplete gamma function.
uppergamma: Upper incomplete gamma function.
polygamma: Polygamma function.
loggamma: Log Gamma function.
trigamma: Trigamma function.
sympy.functions.special.beta_functions.beta: Euler Beta function.

References
==========

.. [1] https://en.wikipedia.org/wiki/Digamma_function
.. [2] https://mathworld.wolfram.com/DigammaFunction.html
.. [3] https://functions.wolfram.com/GammaBetaErf/PolyGamma2/

c                 h    U R                   S   n[        U5      n[        SU5      R                  US9$ )Nr   r.   r9   r*   r:   evalfr<   r   ru   nprecs       r0   r   digamma._eval_evalfT  3    IIaLD!A$$u$--r@   c                 T    U R                   S   n[        SU5      R                  5       $ rb   r9   r:   r>   r<   r=   ru   s      r0   r>   digamma.fdiffY  $    IIaLA$$&&r@   c                 L    U R                   S   n[        SU5      R                  $ rb   r9   r:   r  r   s     r0   rl   digamma._eval_is_real]  !    IIaLA&&&r@   c                 L    U R                   S   n[        SU5      R                  $ rb   r9   r:   rF   r   s     r0   rp   digamma._eval_is_positivea  !    IIaLA***r@   c                 L    U R                   S   n[        SU5      R                  $ rb   r9   r:   r  r   s     r0   r  digamma._eval_is_negativee  rw  r@   c                 x    U R                  [        5      n[        R                  /U-   nUR	                  XX45      $ rs   )r   r:   r   r   r   r<   r/   r   rV   r   as_polygammas         r0   r   digamma._eval_aseriesi  s3    ||I.	E!))!A<<r@   c                     [        SU5      $ rb   r:   rO   ru   s     r0   rS   digamma.evaln      Ar@   c                 R    U R                   S   n[        SU5      R                  SS9$ )Nr   Tr8   r9   r:   expandr<   r]   ru   s      r0   rX   digamma._eval_expand_funcr  )    IIaLA%%4%00r@   c                 @    [        US-
  5      [        R                  -
  $ r{   )r#   r   r
  r|   s      r0   r0  !digamma._eval_rewrite_as_harmonicv  s    A--r@   c                     [        SU5      $ rb   r  r|   s      r0   _eval_rewrite_as_polygamma"digamma._eval_rewrite_as_polygammay      Ar@   c                 V    U R                   S   n[        SU5      R                  U5      $ rb   r9   r:   r   r<   rV   r   r   ru   s        r0   r   digamma._eval_as_leading_term|  &    IIaLA..q11r@   r   Nr   )r   r   r   r   r   r   r>   rl   rp   r  r   r   rS   rX   r0  r  r   r   r   r@   r0   r   r   $  sN    .^.
''++=
  1.2r@   r   c                   n    \ rS rSrSrS rSS jrS rS rS r	S r
\S	 5       rS
 rS rS rS rS rSrg)trigammai  a  
The ``trigamma`` function is the second derivative of the ``loggamma``
function

.. math::
    \psi^{(1)}(z) := \frac{\mathrm{d}^{2}}{\mathrm{d} z^{2}} \log\Gamma(z).

In this case, ``trigamma(z) = polygamma(1, z)``.

Examples
========

>>> from sympy import trigamma
>>> trigamma(0)
zoo
>>> from sympy import Symbol
>>> z = Symbol('z')
>>> trigamma(z)
polygamma(1, z)

To retain ``trigamma`` as it is:

>>> trigamma(0, evaluate=False)
trigamma(0)
>>> trigamma(z, evaluate=False)
trigamma(z)


See Also
========

gamma: Gamma function.
lowergamma: Lower incomplete gamma function.
uppergamma: Upper incomplete gamma function.
polygamma: Polygamma function.
loggamma: Log Gamma function.
digamma: Digamma function.
sympy.functions.special.beta_functions.beta: Euler Beta function.

References
==========

.. [1] https://en.wikipedia.org/wiki/Trigamma_function
.. [2] https://mathworld.wolfram.com/TrigammaFunction.html
.. [3] https://functions.wolfram.com/GammaBetaErf/PolyGamma2/

c                 h    U R                   S   n[        U5      n[        SU5      R                  US9$ )Nr   r7   r.   re  rg  s       r0   r   trigamma._eval_evalf  rj  r@   c                 T    U R                   S   n[        SU5      R                  5       $ r   rl  rm  s      r0   r>   trigamma.fdiff  ro  r@   c                 L    U R                   S   n[        SU5      R                  $ r   rq  r   s     r0   rl   trigamma._eval_is_real  rs  r@   c                 L    U R                   S   n[        SU5      R                  $ r   ru  r   s     r0   rp   trigamma._eval_is_positive  rw  r@   c                 L    U R                   S   n[        SU5      R                  $ r   ry  r   s     r0   r  trigamma._eval_is_negative  rw  r@   c                 x    U R                  [        5      n[        R                  /U-   nUR	                  XX45      $ rs   )r   r:   r   rL   r   r|  s         r0   r   trigamma._eval_aseries  s3    ||I.5 ))!A<<r@   c                     [        SU5      $ r{   r  r  s     r0   rS   trigamma.eval  r  r@   c                 R    U R                   S   n[        SU5      R                  SS9$ )Nr   r7   Tr  r  r  s      r0   rX   trigamma._eval_expand_func  r  r@   c                     [        SU5      $ )NrB   r   r|   s      r0   r-  trigamma._eval_rewrite_as_zeta  s    Aqzr@   c                     [        SU5      $ r{   r  r|   s      r0   r  #trigamma._eval_rewrite_as_polygamma  r  r@   c                 <    [        US-
  S5      * [        S-  S-  -   $ )Nr7   rB   r  )r#   r   r|   s      r0   r0  "trigamma._eval_rewrite_as_harmonic  s#    Q""RUQY..r@   c                 V    U R                   S   n[        SU5      R                  U5      $ r   r  r  s        r0   r   trigamma._eval_as_leading_term  r  r@   r   Nr   )r   r   r   r   r   r   r>   rl   rp   r  r   r   rS   rX   r-  r  r0  r   r   r   r@   r0   r  r    sS    .^.
''++=
  1/2r@   r  c                   B    \ rS rSrSrSrS
S jr\S 5       rS r	S r
Srg	)
multigammai  a?  
The multivariate gamma function is a generalization of the gamma function

.. math::
    \Gamma_p(z) = \pi^{p(p-1)/4}\prod_{k=1}^p \Gamma[z + (1 - k)/2].

In a special case, ``multigamma(x, 1) = gamma(x)``.

Examples
========

>>> from sympy import S, multigamma
>>> from sympy import Symbol
>>> x = Symbol('x')
>>> p = Symbol('p', positive=True, integer=True)

>>> multigamma(x, p)
pi**(p*(p - 1)/4)*Product(gamma(-_k/2 + x + 1/2), (_k, 1, p))

Several special values are known:

>>> multigamma(1, 1)
1
>>> multigamma(4, 1)
6
>>> multigamma(S(3)/2, 1)
sqrt(pi)/2

Writing ``multigamma`` in terms of the ``gamma`` function:

>>> multigamma(x, 1)
gamma(x)

>>> multigamma(x, 2)
sqrt(pi)*gamma(x)*gamma(x - 1/2)

>>> multigamma(x, 3)
pi**(3/2)*gamma(x)*gamma(x - 1)*gamma(x - 1/2)

Parameters
==========

p : order or dimension of the multivariate gamma function

See Also
========

gamma, lowergamma, uppergamma, polygamma, loggamma, digamma, trigamma,
sympy.functions.special.beta_functions.beta

References
==========

.. [1] https://en.wikipedia.org/wiki/Multivariate_gamma_function

Tc           	          SSK Jn  US:X  aJ  U R                  u  p4[        S5      nU R	                  X45      U" [        SUSU-
  S-  -   5      USU45      -  $ [        X5      e)Nr   rQ  rB   rQ   r7   )rS  rR  r9   r   r8   r:   r
   )r<   r=   rR  rV   rK   rQ   s         r0   r>   multigamma.fdiff"  s^    1q=99DAc
A99Q?3yAQ	M'BQ1I#NNN$T44r@   c                     SSK Jn  UR                  SL d  UR                  SL a  [	        S5      e[        S5      n[        X"S-
  -  S-  -  U" [        USU-
  S-  -   5      USU45      -  R                  5       $ )	Nr   )ProductFz+Order parameter p must be positive integer.rQ   r7      rB   )	sympy.concrete.productsr  rF   ri   r-   r   r   r3   doit)rO   rV   rK   r  rQ   s        r0   rS   multigamma.eval+  sw    3==E!Q\\U%:JKK#JQAYq[!'%QUAI*>+,a)#5 56:df	=r@   c                 ^    U R                   u  pU R                  UR                  5       U5      $ rs   )r9   r8   rc   )r<   rV   rK   s      r0   re   multigamma._eval_conjugate4  s$    yyyy**r@   c                     U R                   u  pSU-  nUR                  (       a  X2S-
  :*  SL a  g[        U5      (       a	  X2S-
  ::  a  gX2S-
  :  d  UR                  (       a  gg )NrB   r7   TF)r9   ri   r1   rj   )r<   rV   rK   ys       r0   rl   multigamma._eval_is_real8  sX    yyaC<<Qq5\d21::1Q<A;!// *r@   r   Nr   )r   r   r   r   r   r   r>   r   rS   re   rl   r   r   r@   r0   r  r    s2    7p J5 = =+r@   r  N)Cmathr   
sympy.corer   r   r   r   sympy.core.exprr   sympy.core.functionr	   r
   r   sympy.core.logicr   r   sympy.core.numbersr   r   r   r   sympy.core.powerr   &sympy.functions.special.zeta_functionsr   r   r   r   r   $sympy.functions.elementary.complexesr   r   &sympy.functions.elementary.exponentialr   r   #sympy.functions.elementary.integersr   r   (sympy.functions.elementary.miscellaneousr   (sympy.functions.elementary.trigonometricr   r    r!   %sympy.functions.combinatorial.numbersr"   r#   (sympy.functions.combinatorial.factorialsr$   r%   r&   sympy.utilities.miscr'   mpmathr(   r)   mpmath.libmp.libmpfr*   r1   r3   r   r   r:   rt   r   r  r  r   r@   r0   <module>r     s     1 1   N N 1 2 2   7 A A ? ; > 9 B B E S S '  +uO uxm m`_% _%Lj, j,Z	@5 @5FZ2o Z2|]2 ]2JY Yr@   