
    sg                        d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
mZmZ d dlmZmZ d dlmZmZmZmZ d dlmZ d dlmZ d d	lmZmZmZ d d
lmZmZ d dl m!Z!m"Z" d dl#m$Z$m%Z% d dl&m'Z' d dl(m)Z)m*Z*m+Z+ d dl,m-Z-m.Z. d dl/m0Z0m1Z1m2Z2 d dl3m4Z4 d dl5m6Z6m7Z7 d dl8m9Z9 d Z: G d de
      Z; G d de
      Z< G d de
      Z= G d de
      Z> G d de
      Z? G d d e
      Z@ G d! d"e
      ZA G d# d$e
      ZBy%)&    )prod)AddSDummyexpand_func)Expr)Function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                 >    	 t        | d       y# t        $ r Y yw xY w)NF)strictT)r'   
ValueErrorns    Z/var/www/html/venv/lib/python3.12/site-packages/sympy/functions/special/gamma_functions.pyintliker1      s&    q s    	c                        e Zd ZdZdZej                  fZddZe	d        Z
d Zd Zd Zd Zdd	Zd
 Zd fd	ZddZ xZS )gammaa  
    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                     |dk(  r8| j                  | j                  d         t        d| j                  d         z  S t        | |      N   r   )funcargs	polygammar
   selfargindexs     r0   fdiffzgamma.fdiffr   sA    q=99TYYq\*9Q		!+EEE$T844    c                    |j                   rK|t        j                  u rt        j                  S |t        u rt        S t	        |      r*|j
                  rt        |dz
        S t        j                  S |j                  r|j                  dk(  rt        |j                        |j                  z  }|j
                  r|t        j                  }}n0|dz   x}}|dz  dk(  rt        j                  }nt        j                  }|t        t        dd|z  d            z  }|j
                  r|t!        t"              z  d|z  z  S d|z  t!        t"              z  |z  S y y y )Nr6      r      )	is_Numberr   NaNr   r1   is_positiver$   ComplexInfinityis_RationalqabspOneNegativeOner   ranger   r   )clsargr/   kcoeffs        r0   evalz
gamma.evalx   s   ==aee|uu	??$S1W--,,,55A:CEE
cee+A#$aee5 !AAq5A:$%EEE$%MMET%1Q3"233E$T"X~144 !tDH}u44%  ! r>   c                    | j                   d   }|j                  rt        |j                        |j                  kD  rt        d      }|j                  |j                  z  }|j                  ||j                  z  z
  }| j                  ||z         j                         j                  |t        ||j                              S |j                  rj|j                         \  }}|r%|j                  dk7  rt        |      }||z
  f|z   }|} |j                  |ddi}| j                  |      t        ||      z  S  | j                  | j                    S )Nr   xr6   reevalF)r8   rF   rH   rI   rG   r   r7   _eval_expand_funcsubsr   is_Addas_coeff_addr   _new_rawargsr&   )	r;   hintsrN   rS   r/   rI   rP   tailintparts	            r0   rU   zgamma._eval_expand_func   s   iil??355zCEE!#JEESUUNEEAceeGOyyQ'99;@@HQPSPUPUDVWW::**,KE4A,)D0#3##T8%8D99T??4#???tyy$))$$r>   c                 Z    | j                  | j                  d   j                               S Nr   )r7   r8   	conjugater;   s    r0   _eval_conjugatezgamma._eval_conjugate   s"    yy1//122r>   c                     | j                   d   }|j                  r|j                  ryt        |      r|dk  ry|j                  s|j
                  ryy )Nr   FT)r8   is_nonpositive
is_integerr1   rD   is_nonintegerr;   rS   s     r0   _eval_is_realzgamma._eval_is_real   sF    IIaL1:!q&==AOO ,r>   c                 ~    | j                   d   }|j                  ry|j                  rt        |      j                  S y )Nr   T)r8   rD   re   r   is_evenrf   s     r0   _eval_is_positivezgamma._eval_is_positive   s5    IIaL==__8### r>   c                 *    t        t        |            S N)r   loggamma)r;   zlimitvarkwargss       r0   _eval_rewrite_as_tractablez gamma._eval_rewrite_as_tractable   s    8A;r>   c                     t        |dz
        S Nr6   )r$   r;   rn   rp   s      r0   _eval_rewrite_as_factorialz gamma._eval_rewrite_as_factorial   s    Qr>   c                 0   | j                   d   j                  |d      }|j                  r|dk  st        |   |||      S | j                   d   |z
  }| j                  |dz         t        | j                   d   | dz         z  j	                  |||      S Nr   r6   )r8   limit
is_Integersuper_eval_nseriesr7   r%   )r;   rS   r/   logxcdirx0t	__class__s          r0   r{   zgamma._eval_nseries   s    YYq\1%"'7(At44IIaL2		!a% DIIaL2#'!::II!QPTUUr>   c                 F   | j                   d   }|j                  |d      }|j                  rN|j                  rB| }t        j
                  |z  | j                  |dz         z  }|||z   j                  |      z  S |j                  s| j                  |      S t               rw   )
r8   rV   rd   rc   r   rK   r7   as_leading_termis_infiniter   )r;   rS   r|   r}   rN   r~   r/   ress           r0   _eval_as_leading_termzgamma._eval_as_leading_term   s    iilXXa^==R..A--"499QU#33Ca0033399R= kr>   r6   rl   )r   r^   )__name__
__module____qualname____doc__
unbranchedr   rE   _singularitiesr=   classmethodrQ   rU   ra   rg   rj   rq   ru   r{   r   __classcell__r   s   @r0   r3   r3   "   sb    JX J'')N5 5 5@%(3$  V
r>   r3   c                   ^     e Zd ZdZddZed        Zd Zd Zd Z	 fdZ
d Zd	 Zd
 Z xZS )
lowergammaa  
    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                 6   ddl m} |dk(  r-| j                  \  }}t        t	        |             ||dz
  z  z  S |dk(  rQ| j                  \  }}t        |      t        |      z  t        |      t        ||      z  z
   |g ddgdd|gg |      z
  S t        | |      Nr   )meijergr@   r6   )
sympy.functions.special.hyperr   r8   r   r   r3   digammar   
uppergammar
   r;   r<   r   arn   s        r0   r=   zlowergamma.fdiff  s    9q=99DAq
1~&q1q5z11]99DAq8GAJ&Q
1a0@)@@"q!fq!QiQ78 8 %T844r>   c                 
   |t         j                  u rt         j                  S |j                         \  }}|j                  r(|j                  rt        |      }||k7  rt        ||      S |j                  rS|j                  rG|dk7  rsdt        z  t        z  |z  t         j                  | z  z  t        |       z  t        ||      z   S |dk7  r,t        dt        z  t        z  |z  |z        t        ||      z  S |j                  rR|t         j                  u rt         j                  t        |       z
  S |t         j                  u r$t!        t              t#        t!        |            z  S |j$                  sd|z  j$                  r|dz
  }|j                  r|j                  rSt        |      t        |       t        |      z  t'        t)        |      D cg c]  }||z  t        |      z   c} z  z
  S t+        |      t        t         j                  |      t!        t              z  t        |       t'        t)        d|t         j                  z         D cg c]5  }||t         j                  z
  z  t+        t         j                  |z         z  7 c} z  z
  z  S |j$                  st         j                  t         j                  |z
  z  t        z  t#        t!        |            z  t+        d|z
        z  t        |       t'        t)        dt-        dd      |z
        D cg c](  }|||z   dz
  z  t+        |      z  t+        ||z         z  * c} z  z   S |j.                  rt         j                  S y c c}w c c}w c c}w )Nr   r@   r6   rA   )r   Zeroextract_branch_factorrd   rD   r   r   rc   r   r   rK   r$   r   rB   rJ   Halfr   r   ry   r   rL   r3   r   is_zero)rM   r   rS   nxr/   brO   s          r0   rQ   zlowergamma.eval#  s~   " ;66M'')A<<AMMABQw!!R((\\a..AvtAvax 33IqbMAJqRTDUUU!VqtAvaxz?:a#444 ;;AEEzuusA2w&affBxDG,,!A#!1!1E==||(|c1"g	!.DslqrsltLughQRSVV_`aVbMbLuGv.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~XY\]`a`f`f\fXghmnontntwxnxhyXy  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  NOdehilmhmpqhqdrsxyzs{d{  }B  CD  GH  CH  }I  eI  dp  _q  Wq  q  q9966M  Mv XY dps   M6:M;$-N c                 P   t        d | j                  D              r}| j                  d   j                  |      }| j                  d   j                  |      }t        |      5  t	        j
                  |d|      }d d d        t        j                  |      S | S # 1 sw Y   !xY w)Nc              3   4   K   | ]  }|j                     y wrl   	is_number.0rS   s     r0   	<genexpr>z)lowergamma._eval_evalf.<locals>.<genexpr>V       .qq{{.   r   r6   )allr8   
_to_mpmathr)   r(   gammaincr   _from_mpmathr;   precr   rn   r   s        r0   _eval_evalfzlowergamma._eval_evalfU  s    .DII..		!''-A		!''-A$ +kk!Q*+$$S$//K	+ +s   $BB%c                     | j                   d   }|t        j                  t        j                  fvr;| j	                  | j                   d   j                         |j                               S y r5   r8   r   r   NegativeInfinityr7   r_   rf   s     r0   ra   zlowergamma._eval_conjugate_  S    IIaLQVVQ//0099TYYq\335q{{}EE 1r>   c                    | j                   \  }}t        |j                  ||      |j                  ||      g      }|s|S |j                  ||      }|j                  r!t        |j
                  |j                  g      S |j                  ||      }t        |j                  |j                  t        |j                        g      S rl   )	r8   r   _eval_is_meromorphicrV   rd   rD   	is_finiter   r   )r;   rS   r   srn   
args_meromz0s0s           r0   r   zlowergamma._eval_is_meromorphicd  s     yy1 6 6q! <""1a( * +
VVAq\<<ammR\\:;;VVAq\",,i

6KLMMr>   c                 (  	
 ddl m} | j                  \  	
|d   t        u r^
j	                  |      sM
	z  t        
       z  }t        	
fdt        |dz
        D              } |
	z  	| z  z        }||z  |z   S t        | %  ||||      S )Nr   )Oc              3   H   K   | ]  }|z  t        |d z         z    yw)r6   N)r%   )r   rO   r   rn   s     r0   r   z+lowergamma._eval_aseries.<locals>.<genexpr>z  s$     C1a41a!e,Cs   "r6   )
sympy.series.orderr   r8   r   hasr   sumrL   rz   _eval_aseries)r;   r/   args0rS   r|   r   rP   sum_expror   rn   r   s            @@r0   r   zlowergamma._eval_aseriesu  s    (yy18r>!%%(qDaRLECeAElCCH!Q$qA2w,A>A%%w$Qq$77r>   c                 2    t        |      t        ||      z
  S rl   )r3   r   r;   r   rS   rp   s       r0   _eval_rewrite_as_uppergammaz&lowergamma._eval_rewrite_as_uppergamma      Qx*Q***r>   c                     ddl m} |j                  r|j                  r| S | j	                  t
              j	                  |      S )Nr   expint)'sympy.functions.special.error_functionsr   rd   rc   rewriter   r;   r   rS   rp   r   s        r0   _eval_rewrite_as_expintz"lowergamma._eval_rewrite_as_expint  s3    B<<A,,K||J'//77r>   c                 <    | j                   d   }|j                  ryy )Nr6   T)r8   r   rf   s     r0   _eval_is_zerozlowergamma._eval_is_zero  s    IIaL99 r>   r@   )r   r   r   r   r=   r   rQ   r   ra   r   r   r   r   r   r   r   s   @r0   r   r      sH    4n5 / /bF
N"8+8r>   r   c                   L    e Zd ZdZddZd Zed        Zd Zd Z	d Z
d Zd	 Zy
)r   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                    ddl m} |dk(  r.| j                  \  }}t        t	        |              ||dz
  z  z  S |dk(  r9| j                  \  }}t        ||      t        |      z   |g ddgdd|gg |      z   S t        | |      r   )r   r   r8   r   r   r   r   r
   r   s        r0   r=   zuppergamma.fdiff  s    9q=99DAqA''AE
22]99DAqa#CF*WR!Q!QBPQ-RRR$T844r>   c                 l   t        d | j                  D              r| j                  d   j                  |      }| j                  d   j                  |      }t        |      5  t	        j
                  ||t        j                        }d d d        t        j                  |      S | S # 1 sw Y   !xY w)Nc              3   4   K   | ]  }|j                     y wrl   r   r   s     r0   r   z)uppergamma._eval_evalf.<locals>.<genexpr>  r   r   r   r6   )	r   r8   r   r)   r(   r   infr   r   r   s        r0   r   zuppergamma._eval_evalf  s    .DII..		!''-A		!''-A$ 0kk!Q/0$$S$//0 0s   $&B**B3c                 (	   ddl m} |j                  rf|t        j                  u rt        j                  S |t
        u rt        j                  S |j                  r t        |      j                  rt        |      S |j                         \  }}|j                  r(|j                  rt        |      }||k7  rt        ||      S |j                  rS|j                  rG|dk7  rdt         z  t"        z  |z  t        j$                  | z  z  t'        |       z  t        ||      z   S |dk7  r[t        |      dt)        dt         z  t"        z  |z  |z        z
  z  t)        dt         z  t"        z  |z  |z        t        ||      z  z   S |j                  r|t        j                  u r|j                  rt+        |        S |t        j,                  u rt)        |       S |t        j.                  u r$t1        t               t3        t1        |            z  S |j4                  sd|z  j4                  r|dz
  }|j                  r|j                  rGt)        |       t'        |      z  t7        t9        |      D cg c]  }||z  t'        |      z   c} z  S t        |      t3        t1        |            z  t        j$                  |t        d      dz  z
  z  t)        |       z  t1        |      z  t7        t9        |t        j.                  z
        D cg c]5  }t        t        j.                   |z
        | |z  z  t        d|z
        z  7 c} z  z   S |j4                  r || |      t        |      |dz   z  z  S |j4                  st        j$                  t        j.                  |z
  z  t         z  t3        t1        |            z  t        d|z
        z  ||z  t)        |       z  t7        t9        t        j.                  |z
        D cg c]%  }||z  t        |      z  t        ||z   dz         z  ' c} z  z
  S |j                  r|j                  rt+        |        S |j                  r!t        |      j                  rt        |      S y y c c}w c c}w c c}w )Nr   r   r6   r@   rA   )r   r   rB   r   rC   r   r   r   r   rD   r3   r   rd   r   r   rc   r   r   rK   r$   r   r   rJ   r   r   r   ry   r   rL   )rM   r   rn   r   r   r/   r   rO   s           r0   rQ   zuppergamma.eval  s   B;;AEEzuubvva5$$ 8O '')A<<AMMABBw!!R((\\a..Av"uQwqy!!44Yr]BZPQSUEVVV!V8QQrT!VAXaZ01C"Qq
OJqRTDU4UUU ;;AFF{q}}A2waeeA2waffBxT!W--!A#!1!1E==||"A2w15GLQx>QBC ?@dYq\>Q >Q 9R  R R !&a4Q= 8 !AaDF
 ;c1"g EQ O"%16q166z1B(D,- ).qvvgk(:qb1W(DuQqSz(Q (D #E!E!E F \\!1"a=AQ)???||MMAFFQJ7"<T$q']J5QRSTQT:UdS!Wns>CAFFQJ>O5Q9: 67TE!H_uQqSQRU|5S 5Q 0R RR S 99rF7N99A**8O +9%>Q
(D5Qs   .R
:R
 *Rc                     | j                   d   }|t        j                  t        j                  fvr;| j	                  | j                   d   j                         |j                               S y r5   r   r;   rn   s     r0   ra   zuppergamma._eval_conjugate  r   r>   c                 0    t         j                  | ||      S rl   )r   r   )r;   rS   r   s      r0   r   zuppergamma._eval_is_meromorphic"  s    ..tQ::r>   c                 2    t        |      t        ||      z
  S rl   )r3   r   r   s       r0   _eval_rewrite_as_lowergammaz&uppergamma._eval_rewrite_as_lowergamma%  r   r>   c                 D    t        t        |            t        ||      z
  S rl   )r   rm   r   r   s       r0   rq   z%uppergamma._eval_rewrite_as_tractable(  s    8A;*Q"222r>   c                 2    ddl m}  |d|z
  |      ||z  z  S )Nr   r   r6   )r   r   r   s        r0   r   z"uppergamma._eval_rewrite_as_expint+  s    Ba!eQ1$$r>   Nr   )r   r   r   r   r=   r   r   rQ   ra   r   r   rq   r    r>   r0   r   r     sA    >B	5 6 6pF
;+3%r>   r   c                   r     e Zd ZdZed        Zd Zd Zd Zd Z	d Z
d Zd	 Zdd
ZddZ fdZd Z xZS )r9   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                 b   |t         j                  u s|t         j                  u rt         j                  S |t        u r"|j                  rt        S t         j                  S |j
                  r|j                  rt         j                  S |t         j                  u r!t        |      t        dt        z        dz  z
  S |j                  r|t         u s"|j                  t              t        t         fv rt        S |j
                  rt        |dz
        t         j                  z
  S |j                   r>|j#                         \  }}|dk  r%t%        t'        t         j                  |d            S y y |j(                  r|j*                  rt-        |      }||k7  rt'        ||      S |j
                  r2t         j                  |dz   z  t/        |      z  t1        |dz   |      z  S |t         j2                  u r=t         j                  |dz   z  t/        |      z  d|dz   z  dz
  z  t1        |dz         z  S y y y )Nr@   r6      F)evaluate)r   rC   r   r   r   ry   rc   rE   rK   rm   r   r   extract_multiplicativelyr   r#   
EulerGammarF   as_numer_denomr   r9   rd   is_nonnegativer   r$   r   r   )rM   r/   rn   rI   rG   nzs         r0   rQ   zpolygamma.eval  s   :aee55L"W2..\\a..$$$!--A;QrTQ..YYRCx155a8R"IE	!}q||33'')16&yU'KLL 	  \\a..ABBw B''||}}qs+il:T!A#q\IIaff}}qs+il:a!A#hqjIDQRSTQTIUU  /\r>   c                 l    | j                   d   j                  r| j                   d   j                  ryy y )Nr   r6   T)r8   rD   r`   s    r0   rg   zpolygamma._eval_is_real  s/    99Q<##		!(@(@ )A#r>   c                     | j                   d   }t        |j                  |j                  g      }t        |j                  t        |      g      S rs   )r8   r   is_negativerd   
is_complexr   )r;   rn   is_negative_integers      r0   _eval_is_complexzpolygamma._eval_is_complex  sA    IIaL'(EF!,,	2E(FGHHr>   c                     | j                   \  }}|j                  r4|j                  r|j                  ry|j                  r|j                  ryy y y NTF)r8   rD   is_oddis_realri   r;   r/   rn   s      r0   rj   zpolygamma._eval_is_positive  sB    yy1==xxAIIyyQ]] +y r>   c                     | j                   \  }}|j                  r4|j                  r|j                  ry|j                  r|j                  ryy y y r   )r8   rD   ri   r   r   r   s      r0   _eval_is_negativezpolygamma._eval_is_negative  sB    yy1==yyQ]]xxAII &x r>   c                    | j                   \  }}|j                  r|j                  rw|j                  r|j                   d   }|j                  rO|dz    }|dkD  r:t	        t        dt        |      dz         D cg c]  }t        ||z
  |       c} }n7t	        t        t        |             D cg c]  }t        ||z   |       c}  }t        |||z
        t        j                  |z  t        |      z  |z  z   S |j                  r|j                         \  }}|j                  rq|j                  ret        t        |            D cg c]  }t        ||t        ||      z          }}|dk(  rt	        | |z  t!        |      z   S t	        | ||dz   z  z  S ||z  }|dk(  r9|j"                  r,|j%                         \  }}	t        j&                   t(        t+        |t(        z  |	z        z  dz  z
  t!        |	      z
  t	        t        d|	      D 
cg c]?  }
t-        d|
z  t(        z  |z  |	z        t!        dt/        |
t(        z  |	z        z        z  A c}
 z   }|dkD  r8t1        |      }||z
  }|t	        t        |      D 
cg c]
  }
d||
z   z   c}
 z   S |dk  r>t1        d|z
        }||z   }|t	        t        |      D 
cg c]  }
d|dz
  |
z
  z   c}
 z
  S |dk(  r!t3        |      t!        dt(        z        dz  z
  S |j4                  du s|j                  du rvt7        d      }t9        ||      j;                  |      j=                  ||dz         }|t        j&                  t?        |       z   t9        |dz   |      z  z   tA        |       z  S t        ||      S c c}w c c}w c c}w c c}
w c c}
w c c}
w )Nr   r6   r@   Fr   )!r8   ry   r   rW   r   rL   intr   r9   r   rK   r$   is_Mulas_two_termsrD   r   r   rF   r   r   r   r!   r    r   r   rm   rd   r   r   diffrV   r   r3   )r;   rZ   r/   rn   rP   eir[   rI   rG   rO   part_1r   r   dzts                  r0   rU   zpolygamma._eval_expand_func  s   yy1<<A,,xxq	##a%Aqy"/4QE
Q/G%I*+ &)E1&& %I  J !$/4S%[/A&C*+ '*E1'& &C !D  D$QE	2Q]]A5EiPQl5RSW5WWW>>+q##(9(9,1#e*,=?'( &aX5." *" # ?D ?Av"Dz%/#e*<<"Dz%!a%.88U
6amm##%DAq ll]R#a"fqj/%9A%==AFNSTUWXkZ#a!ebj1nq()CCB
O0C,DDZJ\ \F 1u!HUE!H%Eqa26l%E FFFQ!a%LUa%I1a26A:&6%I JJJ7A;QrTQ..<<5 A$4$4$=c
Aq!*//!$))!QqS1C1<<'1"+5ac1EEPQrRRAU%I&C? [
 &F &Js%   <N$3N) N.AN3
N8
N=
c                     |j                   r?|j                  r2t        j                  |dz   z  t	        |      z  t        |dz   |      z  S y y rs   )rd   rD   r   rK   r$   r   r;   r/   rn   rp   s       r0   _eval_rewrite_as_zetazpolygamma._eval_rewrite_as_zeta  sA    <<AMM==1q5))A,6tAE1~EE *<r>   c                     |j                   ro|j                  rt        |dz
        t        j                  z
  S t        j
                  |dz   z  t        |      z  t        |dz         t        |dz
  |dz         z
  z  S y rs   )rd   r   r#   r   r   rK   r$   r   r  s       r0   _eval_rewrite_as_harmonicz#polygamma._eval_rewrite_as_harmonic  so    <<yyA55}}qs+il:d1Q3i(STUVSVXYZ[X[J\>\]]	 r>   c                    ddl m} | j                  D cg c]  }|j                  |       c}\  }} |||      }|dk(  r6|j	                  d|z        r"|t        |      n|}|j                         |z  S | j                  ||      S c c}w )Nr   Orderr6   )r   r
  r8   r   containsr   getnr7   )	r;   rS   r|   r}   r
  r   r/   rn   r   s	            r0   r   zpolygamma._eval_as_leading_term  s|    ,.2ii8!!!$81!QK6ajj1o!\3q6tD668d?"99Q?" 9s   B	c                 f    |dk(  r!| j                   d d \  }}t        |dz   |      S t        | |      Nr@   r6   )r8   r9   r
   )r;   r<   r/   rn   s       r0   r=   zpolygamma.fdiff   s:    q=99Ra=DAqQUA&&$T844r>   c                    ddl m} |d   t        k7  s2| j                  d   j                  r| j                  d   j
                  st        |   ||||      S | j                  d   }| j                  d   }|dk(  rt        |      dd|z  z  z
  }d }	|dk  r |d|z  |      }	n_t        |dz   dz        }
t        d|
      D cg c]  }t        d|z        d|z  |d|z  z  z  z  ! }}|t        | z  } |d||z  z  |      }	|j                  |||      |	z   S t        |      }|||z  d|z  z  z   }t        |dz   dz        }
t        d|
      D ]H  }|d|z  |z   dz
  z  d|z  |z   dz
  z  d|z  d|z  dz
  z  z  }|t        d|z        |z  |d|z  z  z  z  }J  |d|d|
z  z  z  |      }	|dk(  r |d|z  |      }	n|dk(  r |d|dz  z  |      }	|j                  |||      |	z   }dd|z  |z  z  |z  j                  |||      S c c}w )Nr   r	  r6   r@   r   )r   r
  r   r8   ry   r   rz   r   r   r   rL   r"   r   r{   r3   )r;   r/   r   rS   r|   r
  rn   Nrr   mrO   lface0r   s                  r0   r   zpolygamma._eval_aseries'  sU   ,8r>1((TYYq\-H-H7(E1d;;IIaLIIaL6 AAaC AA1u!A#qMQUQJ'>CAqkJYqs^qs1qs8|4JJS!W!AqD&!$??1a.22 (Cquac{"BQ
#A1a[ 21Q37Q;'1q15!A#!aIi!nS(QqS112 aAaCj!$AAv!A#qMa!AqD&!$  At,q0A"Q$NQ&55aDAA- Ks   $Hc                    t        d | j                  D              sy | j                  d   j                  |dz         }| j                  d   j                  |dz         }t        j                  |      r|dk  rt
        j                  S t        |dz         5  t        j                  |      r|dk\  rt        j                  ||      }nwt        j                  |dz   |      }t        j                  |dz   |d      }|t        j                  t        j                  |       z   |z  z   t        j                  |       z  }d d d        t        j                  |      S # 1 sw Y   xY w)Nc              3   4   K   | ]  }|j                     y wrl   r   )r   r   s     r0   r   z(polygamma._eval_evalf.<locals>.<genexpr>Q  s     211;;2r   r      r6   )r   r8   r   r(   isintr   rE   r)   r9   r   eulerr   rgammar   r   )r;   r   r   rn   r   ztr  s          r0   r   zpolygamma._eval_evalfP  s!   2		22IIaL##DG,IIaL##DG,88A;16$$$d2g 	Oxx{qAvll1a(WWQqS!_ggac1a(bhhQB72==A2N	O   d++	O 	Os   B)EE(r^   r   )r   r   r   r   r   rQ   rg   r   rj   r   rU   r  r  r   r=   r   r   r   r   s   @r0   r9   r9   4  s]    hT V V:I
3jF^#5'BR,r>   r9   c                   ^     e Zd ZdZed        Zd Zd
 fd	Z fdZd Z	d Z
d Zdd	Z xZS )rm   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                    |j                   r2|j                  rt        S |j                  rt	        t        |            S |j                  rq|j                         \  }}|j                  rR|dk(  rMt	        t        t              dd|z
  z  z  t        |      z  t        |dz   t        j                  z        z        S |t        u rt        S t        |      t        u rt        j                  S |t        j                  u rt        j                  S y r  )rd   rc   r   rD   r   r3   is_rationalr   r   r   r   r   rH   rE   rC   )rM   rn   rI   rG   s       r0   rQ   zloggamma.eval  s    <<	58}$]]##%DAq}}a48a!a%j058;eQUAFFN>SSTT7IVr\$$$:55L r>   c                 L   ddl m} | j                  d   }|j                  r|j	                         \  }}||z  }|||z  z
  }|j
                  r|j
                  r||k  rt        d      }|j
                  r<t        ||z        |t        |      z  z
   |t        |dz
  |z  |z         |d|f      z   S |j                  rKt        ||z        |t        |      z  z
  t        t        z  |z  z    |t        ||z  |z
        |d| f      z
  S |j                  rt        ||z        S | S )Nr   SumrO   r6   )sympy.concrete.summationsr"  r8   rF   r   rD   r   rm   r   r   r   r   r   )r;   rZ   r"  rn   rI   rG   r/   rO   s           r0   rU   zloggamma._eval_expand_func  s   1IIaL==##%DAq QAAaCA}}1q5#J==#AE?Qs1vX5CQ	A<NQRTUWXPY8ZZZ]]#AE?Qs1vX51Q>S1q\TUWX[\Z\S]A^^^YY#AE?*r>   c                     | j                   d   j                  |d      }|j                  r, | j                  | j                    }|j	                  |||      S t
        |   |||      S r^   )r8   rx   r   _eval_rewrite_as_intractabler{   rz   )r;   rS   r/   r|   r}   r~   fr   s          r0   r{   zloggamma._eval_nseries  sa    YYq\1%::111499=A??1a..w$Q400r>   c                    ddl m} |d   t        k7  rt        |   ||||      S | j
                  d   }t        |      |t        j                  z
  z  |z
  t        dt        z        dz  z   }t        d|      D cg c]+  }t        d|z        d|z  d|z  dz
  z  |d|z  dz
  z  z  z  - }	}d }
|dk(  r
 |d|      }
n |d||z  z  |      }
|t        |	 z   j                  |||      |
z   S c c}w )Nr   r	  r@   r6   )r   r
  r   rz   r   r8   r   r   r   r   rL   r"   r   r{   )r;   r/   r   rS   r|   r
  rn   r  rO   r  r   r   s              r0   r   zloggamma._eval_aseries  s    ,8r>7(E1d;;IIaLFAJ!#c!B$ik1DI!QKPqYqs^qsAaC!G}Q1q\9:PP6aAa1fa ACG**1a6:: Qs   ;0C,c                 *    t        t        |            S rl   )r   r3   rt   s      r0   r%  z%loggamma._eval_rewrite_as_intractable  s    58}r>   c                 V    | j                   d   }|j                  ry|j                  ryy )Nr   TF)r8   rD   rc   r   s     r0   rg   zloggamma._eval_is_real  s*    IIaL== r>   c                     | j                   d   }|t        j                  t        j                  fvr| j	                  |j                               S y r^   r   r   s     r0   ra   zloggamma._eval_conjugate  s@    IIaLQVVQ//0099Q[[]++ 1r>   c                 V    |dk(  rt        d| j                  d         S t        | |      r5   )r9   r8   r
   r:   s     r0   r=   zloggamma.fdiff  s+    q=Q		!--$T844r>   r^   r   )r   r   r   r   r   rQ   rU   r{   r   r%  rg   ra   r=   r   r   s   @r0   rm   rm   a  sA    lZ  &*1;,
5r>   rm   c                   `    e Zd ZdZd ZddZd Zd Zd Zd Z	e
d        Zd	 Zd
 Zd ZddZy)r   at  
    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                 n    | j                   d   }t        |      }t        d|      j                  |      S )Nr   r.   r8   r*   r9   evalfr;   r   rn   nprecs       r0   r   zdigamma._eval_evalfT  3    IIaLD!A$$u$--r>   c                 T    | j                   d   }t        d|      j                         S r^   r8   r9   r=   r;   r<   rn   s      r0   r=   zdigamma.fdiffY  $    IIaLA$$&&r>   c                 L    | j                   d   }t        d|      j                  S r^   r8   r9   r   r   s     r0   rg   zdigamma._eval_is_real]  !    IIaLA&&&r>   c                 L    | j                   d   }t        d|      j                  S r^   r8   r9   rD   r   s     r0   rj   zdigamma._eval_is_positivea  !    IIaLA***r>   c                 L    | j                   d   }t        d|      j                  S r^   r8   r9   r   r   s     r0   r   zdigamma._eval_is_negativee  r<  r>   c                 |    | j                  t              }t        j                  g|z   }|j	                  ||||      S rl   )r   r9   r   r   r   r;   r/   r   rS   r|   as_polygammas         r0   r   zdigamma._eval_aseriesi  s7    ||I.	E!))!UAt<<r>   c                     t        d|      S r^   r9   rM   rn   s     r0   rQ   zdigamma.evaln      Ar>   c                 X    | j                   d   }t        d|      j                  d      S )Nr   Tr7   r8   r9   expandr;   rZ   rn   s      r0   rU   zdigamma._eval_expand_funcr  )    IIaLA%%4%00r>   c                 @    t        |dz
        t        j                  z
  S rs   )r#   r   r   rt   s      r0   r  z!digamma._eval_rewrite_as_harmonicv  s    A--r>   c                     t        d|      S r^   rC  rt   s      r0   _eval_rewrite_as_polygammaz"digamma._eval_rewrite_as_polygammay      Ar>   Nc                 V    | j                   d   }t        d|      j                  |      S r^   r8   r9   r   r;   rS   r|   r}   rn   s        r0   r   zdigamma._eval_as_leading_term|  &    IIaLA..q11r>   r   r^   )r   r   r   r   r   r=   rg   rj   r   r   r   rQ   rU   r  rN  r   r   r>   r0   r   r   $  sN    .^.
''++=
  1.2r>   r   c                   f    e Zd ZdZd ZddZd Zd Zd Zd Z	e
d        Zd	 Zd
 Zd Zd ZddZy)trigammaa^  
    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                 n    | j                   d   }t        |      }t        d|      j                  |      S )Nr   r6   r.   r.  r0  s       r0   r   ztrigamma._eval_evalf  r2  r>   c                 T    | j                   d   }t        d|      j                         S rw   r4  r5  s      r0   r=   ztrigamma.fdiff  r6  r>   c                 L    | j                   d   }t        d|      j                  S rw   r8  r   s     r0   rg   ztrigamma._eval_is_real  r9  r>   c                 L    | j                   d   }t        d|      j                  S rw   r;  r   s     r0   rj   ztrigamma._eval_is_positive  r<  r>   c                 L    | j                   d   }t        d|      j                  S rw   r>  r   s     r0   r   ztrigamma._eval_is_negative  r<  r>   c                 |    | j                  t              }t        j                  g|z   }|j	                  ||||      S rl   )r   r9   r   rJ   r   r@  s         r0   r   ztrigamma._eval_aseries  s7    ||I.5 ))!UAt<<r>   c                     t        d|      S rs   rC  rD  s     r0   rQ   ztrigamma.eval  rE  r>   c                 X    | j                   d   }t        d|      j                  d      S )Nr   r6   TrG  rH  rJ  s      r0   rU   ztrigamma._eval_expand_func  rK  r>   c                     t        d|      S )Nr@   r   rt   s      r0   r  ztrigamma._eval_rewrite_as_zeta  s    Aqzr>   c                     t        d|      S rs   rC  rt   s      r0   rN  z#trigamma._eval_rewrite_as_polygamma  rO  r>   c                 <    t        |dz
  d       t        dz  dz  z   S )Nr6   r@   r   )r#   r   rt   s      r0   r  z"trigamma._eval_rewrite_as_harmonic  s#    Q""RUQY..r>   Nc                 V    | j                   d   }t        d|      j                  |      S rw   rQ  rR  s        r0   r   ztrigamma._eval_as_leading_term  rS  r>   r   r^   )r   r   r   r   r   r=   rg   rj   r   r   r   rQ   rU   r  rN  r  r   r   r>   r0   rU  rU    sS    .^.
''++=
  1/2r>   rU  c                   8    e Zd ZdZdZddZed        Zd Zd Z	y)	
multigammaa  
    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           	          ddl m} |dk(  rL| j                  \  }}t        d      }| j	                  ||       |t        d|d|z
  dz  z         |d|f      z  S t        | |      )Nr   r!  r@   rO   r6   )r#  r"  r8   r   r7   r9   r
   )r;   r<   r"  rS   rI   rO   s         r0   r=   zmultigamma.fdiff"  sd    1q=99DAqc
A99Q?3yAQ	M'BQ1I#NNN$T844r>   c                     ddl m} |j                  du s|j                  du rt	        d      t        d      }t        ||dz
  z  dz  z   |t        |d|z
  dz  z         |d|f      z  j                         S )	Nr   )ProductFz+Order parameter p must be positive integer.rO   r6      r@   )	sympy.concrete.productsrf  rD   rd   r-   r   r   r3   doit)rM   rS   rI   rf  rO   s        r0   rQ   zmultigamma.eval+  sy    3==E!Q\\U%:JKK#JQAYq[!'%QUAI*>+,a)#5 56:df	=r>   c                 `    | j                   \  }}| j                  |j                         |      S rl   )r8   r7   r_   )r;   rS   rI   s      r0   ra   zmultigamma._eval_conjugate4  s&    yy1yy**r>   c                     | j                   \  }}d|z  }|j                  r||dz
  k  du ryt        |      r	||dz
  k  ry||dz
  kD  s|j                  ryy )Nr@   r6   TF)r8   rd   r1   re   )r;   rS   rI   ys       r0   rg   zmultigamma._eval_is_real8  s^    yy1aC<<Q1q5\d21:1Q<A;!// *r>   Nr   )
r   r   r   r   r   r=   r   rQ   ra   rg   r   r>   r0   rc  rc    s2    7p J5 = =+r>   rc  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   r9   rm   r   rU  rc  r   r>   r0   <module>r     s     1 1   G G 1 2 2   7 A A ? ; > 9 B B E S S '  +uH uxm m`_% _%Lj, j,Z	@5x @5FZ2h Z2|]2x ]2JY Yr>   