
    g                         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
          ZBd%S )&    )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                 L    	 t          | d           dS # t          $ r Y dS w xY w)NF)strictT)r(   
ValueErrorns    c/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/functions/special/gamma_functions.pyintliker2      sA    qt   uu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/

    T   c                     |dk    r<|                      | j        d                   t          d| j        d                   z  S t          | |          Nr5   r   )funcargs	polygammar
   selfargindexs     r1   fdiffzgamma.fdiffr   sH    q==99TYq\**9Q	!+E+EEE$T8444    c                 ~   |j         r0|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        }}n)|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 d S d S d S )Nr5      r      )	is_Numberr   NaNr   r2   is_positiver%   ComplexInfinityis_RationalqabspOneNegativeOner   ranger   r   )clsargr0   kcoeffs        r1   evalz
gamma.evalx   s@   = 	5ae||u	 5? -$S1W---,, 55A::CE

ce+A 2#$ae5 !AAq5A::$%EEE$%MET%1Q3"2"2333E 5$T"XX~144 !tDHH}u44;	5 	55 5:r?   c                 t   | j         d         }|j        rt          |j                  |j        k    rt          d          }|j        |j        z  }|j        ||j        z  z
  }|                     ||z                                                                 |t          ||j                            S |j
        rq|                                \  }}|r%|j        dk    rt          |          }||z
  f|z   }|} |j        |ddi}|                     |          t          ||          z  S  | j        | j          S )Nr   xr5   reevalF)r9   rG   rI   rJ   rH   r   r8   _eval_expand_funcsubsr   is_Addas_coeff_addr   _new_rawargsr'   )	r<   hintsrO   rT   r0   rJ   rQ   tailintparts	            r1   rV   zgamma._eval_expand_func   s0   il? 	X35zzCE!!#JJESUNEAceGOyyQ''99;;@@HQPSPUDVDVWWW: 	@**,,KE4  A,,)D0#3#T8%88D99T???4#?#???ty$)$$r?   c                 f    |                      | j        d                                                   S Nr   )r8   r9   	conjugater<   s    r1   _eval_conjugatezgamma._eval_conjugate   s&    yy1//11222r?   c                     | j         d         }|j        r	|j        rdS t          |          r|dk    rdS |j        s|j        rdS d S )Nr   FT)r9   is_nonpositive
is_integerr2   rE   is_nonintegerr<   rT   s     r1   _eval_is_realzgamma._eval_is_real   sc    IaL 	 	51:: 	!q&&5= 	AO 	4	 	r?   c                 h    | j         d         }|j        rdS |j        rt          |          j        S d S )Nr   T)r9   rE   rf   r   is_evenrg   s     r1   _eval_is_positivezgamma._eval_is_positive   s?    IaL= 	$4_ 	$88##	$ 	$r?   Nc                 :    t          t          |                    S N)r   loggamma)r<   zlimitvarkwargss       r1   _eval_rewrite_as_tractablez gamma._eval_rewrite_as_tractable   s    8A;;r?   c                 &    t          |dz
            S Nr5   r%   r<   ro   rq   s      r1   _eval_rewrite_as_factorialz gamma._eval_rewrite_as_factorial   s    Qr?   r   c                 `   | j         d                             |d          }|j        r|dk    s#t                                          |||          S | j         d         |z
  }|                     |dz             t          | j         d         | dz             z                      |||          S Nr   r5   )r9   limit
is_Integersuper_eval_nseriesr8   r&   )r<   rT   r0   logxcdirx0t	__class__s          r1   r}   zgamma._eval_nseries   s    Yq\1%% 	5"''77((At444IaL2		!a%  DIaL2#'!:!::II!QPTUUUr?   c                 D   | j         d         }|                    |d          }|j        rM|j        rF| }t          j        |z  |                     |dz             z  }|||z                       |          z  S |j        s|                     |          S t                      ry   )
r9   rW   re   rd   r   rL   r8   as_leading_termis_infiniter   )r<   rT   r~   r   rO   r   r0   ress           r1   _eval_as_leading_termzgamma._eval_as_leading_term   s    ilXXa^^= 	!R. 	!A-"499QU#3#33Ca003333 	!99R== kkr?   r5   rm   )r   r_   )__name__
__module____qualname____doc__
unbranchedr   rF   _singularitiesr>   classmethodrR   rV   rb   rh   rk   rr   rw   r}   r   __classcell__r   s   @r1   r4   r4   "   s       J JX J')N5 5 5 5 5 5 [5@% % %(3 3 3  $ $ $            V V V V V V
 
 
 
 
 
 
 
r?   r4   c                   d     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/

    rA   c                 d   ddl m} |dk    r0| j        \  }}t          t	          |                     ||dz
  z  z  S |dk    r_| j        \  }}t          |          t          |          z  t          |          t          ||          z  z
   |g ddgdd|gg |          z
  S t          | |          Nr   )meijergrA   r5   )
sympy.functions.special.hyperr   r9   r   r   r4   digammar   
uppergammar
   r<   r=   r   aro   s        r1   r>   zlowergamma.fdiff  s    999999q==9DAq
1~&&q1q5z11]]9DAq88GAJJ&Q
1a0@0@)@@'"q!fq!QiQ778 8 %T8444r?   c                 F   t           j        u rt           j        S                                 \  }}j        r-j        r&t                    }|k    rt          |          S nj        rVj        rO|dk    rHdt          z  t          z  |z  t           j
         z  z  t                     z  t          |          z   S n<|dk    r6t          dt          z  t          z  |z  z            t          |          z  S j        rt           j        u rt           j        t                     z
  S t           j        u r1t!          t                    t#          t!                              z  S j        sdz  j        rdz
  }|j        rۉj        rSt          |          t                     t          |          z  t'          fdt)                    D              z  z
  S t+                    t          t           j                  t!          t                    z  t                     t'          fdt)          dt           j        z             D              z  z
  z  S j        st           j
        t           j        z
  z  t          z  t#          t!                              z  t+          dz
            z  t                     t'          fdt)          dt-          dd          z
            D              z  z   S j        rt           j        S d S )Nr   rA   r5   c                 :    g | ]}|z  t          |          z  S  ru   .0rP   rT   s     r1   
<listcomp>z#lowergamma.eval.<locals>.<listcomp>K  s.    LuLuLughQRSVV_`aVbVbMbLuLuLur?   c                 n    g | ]1}|t           j        z
  z  t          t           j        |z             z  2S r   )r   Halfr4   r   s     r1   r   z#lowergamma.eval.<locals>.<listcomp>M  sZ      XY  XY  XY~XY\]`a`f\fXghmnontwxnxhyhyXy  XY  XY  XYr?   c                 l    g | ]0}|z   d z
  z  t                    z  t          |z             z  1S r   r4   )r   rP   r   rT   s     r1   r   z#lowergamma.eval.<locals>.<listcomp>P  s      dp  dp  dp  NOdehilmhmpqhqdrsxyzs{s{d{  }B  CD  GH  CH  }I  }I  eI  dp  dp  dpr?   rB   )r   Zeroextract_branch_factorre   rE   r   r   rd   r   r   rL   r%   r   rC   rK   r   r   r   r{   r   rM   r4   r   is_zero)rN   r   rT   nxr0   bs    ``   r1   rR   zlowergamma.eval#  s   " ;;6M''))A< 	5AM 	5ABQww!!R((( \ 	5a. 	5AvvtAvax 33IqbMMAJqRTDUDUUU !VVqtAvaxz??:a#4#444 ; 	qAEzzusA2ww&afBxxDGG,, 	q!A#!1 	qE= [| [(||c1"gg	!.DsLuLuLuLulqrsltltLuLuLuGv.vvv$QxxAFA)>)>tBxx)G#qb''RU  XY  XY  XY  XY  DI  JK  MN  QR  QW  MW  DX  DX  XY  XY  XY  SZ  KZ  *Z   [  [| q=16A:6r9#d1gg,,FuQQRU||SVY[\Z\V]V]^a  dp  dp  dp  dp  dp  SX  YZ  \d  ef  hi  \j  \j  mn  \n  So  So  dp  dp  dp  _q  Wq  q  q9 	6M	 	r?   c                 f   t          d | j        D                       r| j        d                             |          }| j        d                             |          }t          |          5  t	          j        |d|          }d d d            n# 1 swxY w Y   t          j        ||          S | S )Nc              3   $   K   | ]}|j         V  d S rm   	is_numberr   rT   s     r1   	<genexpr>z)lowergamma._eval_evalf.<locals>.<genexpr>V  $      ..qq{......r?   r   r5   )allr9   
_to_mpmathr*   r)   gammaincr   _from_mpmathr<   precr   ro   r   s        r1   _eval_evalfzlowergamma._eval_evalfU  s    ..DI..... 		!''--A	!''--A$ + +k!Q**+ + + + + + + + + + + + + + +$S$///Ks   .BBBc                     | j         d         }|t          j        t          j        fvrE|                     | j         d                                         |                                          S d S r7   r9   r   r   NegativeInfinityr8   r`   rg   s     r1   rb   zlowergamma._eval_conjugate_  X    IaLQVQ/00099TYq\3355q{{}}EEE 10r?   c                    | j         \  }}t          |                    ||          |                    ||          g          }|s|S |                    ||          }|j        rt          |j        |j        g          S |                    ||          }t          |j        |j        t          |j                  g          S rm   )	r9   r   _eval_is_meromorphicrW   re   rE   	is_finiter   r   )r<   rT   r   sro   
args_meromz0s0s           r1   r   zlowergamma._eval_is_meromorphicd  s     y1 6 6q! < <""1a(( * + +
 	VVAq\\< 	<amR\:;;;VVAq\\",i
6K6KLMMMr?   c                 v  	
 ddl m} | j        \  	
|d         t          u rt
                    |          s_
	z  t          
           z  }t          	
fdt          |dz
            D                       } |
	z  	| z  z            }||z  |z   S t                      	                    ||||          S )Nr   )Oc              3   J   K   | ]}|z  t          |d z             z  V  dS )r5   N)r&   )r   rP   r   ro   s     r1   r   z+lowergamma._eval_aseries.<locals>.<genexpr>z  s8      CC1a41a!e,CCCCCCr?   r5   )
sympy.series.orderr   r9   r   hasr   sumrM   r|   _eval_aseries)r<   r0   args0rT   r~   r   rQ   sum_expror   ro   r   s            @@r1   r   zlowergamma._eval_aseriesu  s    ((((((y18r>>!%%((>qDaRLECCCCCeAEllCCCCCH!Q$qA2w,A>A%%ww$$Qq$777r?   c                 B    t          |          t          ||          z
  S rm   )r4   r   r<   r   rT   rq   s       r1   _eval_rewrite_as_uppergammaz&lowergamma._eval_rewrite_as_uppergamma      Qxx*Q****r?   c                     ddl m} |j        r	|j        r| S |                     t
                                        |          S )Nr   expint)'sympy.functions.special.error_functionsr   re   rd   rewriter   r<   r   rT   rq   r   s        r1   _eval_rewrite_as_expintz"lowergamma._eval_rewrite_as_expint  sM    BBBBBB< 	A, 	K||J''//777r?   c                 2    | j         d         }|j        rdS d S )Nr5   T)r9   r   rg   s     r1   _eval_is_zerozlowergamma._eval_is_zero  s&    IaL9 	4	 	r?   rA   )r   r   r   r   r>   r   rR   r   rb   r   r   r   r   r   r   r   s   @r1   r   r      s        4 4n5 5 5 5 / / [/b  F F F
N N N"8 8 8 8 8+ + +8 8 8      r?   r   c                   T    e Zd ZdZddZd Zed             Zd Zd Z	d Z
d	 Zd
 ZdS )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

    rA   c                 &   ddl m} |dk    r1| j        \  }}t          t	          |                      ||dz
  z  z  S |dk    r?| j        \  }}t          ||          t          |          z   |g ddgdd|gg |          z   S t          | |          r   )r   r   r9   r   r   r   r   r
   r   s        r1   r>   zuppergamma.fdiff  s    999999q==9DAqA'''AE
22]]9DAqa##CFF*WWR!Q!QBPQ-R-RRR$T8444r?   c                 z   t          d | j        D                       r| j        d                             |          }| j        d                             |          }t          |          5  t	          j        ||t          j                  }d d d            n# 1 swxY w Y   t          j        ||          S | S )Nc              3   $   K   | ]}|j         V  d S rm   r   r   s     r1   r   z)uppergamma._eval_evalf.<locals>.<genexpr>  r   r?   r   r5   )	r   r9   r   r*   r)   r   infr   r   r   s        r1   r   zuppergamma._eval_evalf  s    ..DI..... 	0	!''--A	!''--A$ 0 0k!Q//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0$S$///s   .!BB"Bc           	      h   ddl m} j        rYt          j        u rt          j        S t
          u rt          j        S j        r#t                    j	        rt                    S                                 \  }}j        r-j	        r&t                    }|k    rt          |          S n҉j        rVj        rO|dk    rHdt           z  t"          z  |z  t          j         z  z  t'                     z  t          |          z   S nu|dk    rot                    dt)          dt           z  t"          z  |z  z            z
  z  t)          dt           z  t"          z  |z  z            t          |          z  z   S j        rYt          j        u rj	        rt+                      S t          j        u rt)                     S t          j        u r1t1          t                     t3          t1                              z  S j        sdz  j        rĉdz
  }|j	        rj        rCt)                     t'          |          z  t7          fdt9                    D              z  S t                    t3          t1                              z  t          j        t          d          dz  z
  z  t)                     z  t1                    z  t7          fdt9          t          j        z
            D              z  z   S |j        r# ||           t                    |dz   z  z  S j        st          j        t          j        z
  z  t           z  t3          t1                              z  t          dz
            z  z  t)                     z  t7          fd	t9          t          j        z
            D              z  z
  S j        rj	        rt+                      S j        r#t                    j	        rt                    S d S d S )
Nr   r   r5   rA   c                 :    g | ]}|z  t          |          z  S r   ru   r   rP   ro   s     r1   r   z#uppergamma.eval.<locals>.<listcomp>  s?     >Q >Q >QBC ?@dYq\\>Q >Q >Q >Qr?   rB   c                 ~    g | ]9}t          t          j         |z
             |z  z  t          d z
            z  :S r   )r4   r   r   r   rP   r   ro   s     r1   r   z#uppergamma.eval.<locals>.<listcomp>  sY     (D (D (D,- ).qvgk(:(:qb1W(DuQqSzz(Q (D (D (Dr?   c                 f    g | ]-}|z  t                    z  t          |z   d z             z  .S r   r   r   s     r1   r   z#uppergamma.eval.<locals>.<listcomp>  sQ     5Q 5Q 5Q9: 67TE!HH_uQqSQRU||5S 5Q 5Q 5Qr?   )r   r   rC   r   rD   r   r   r   r   rE   r4   r   re   r   r   rd   r   r   rL   r%   r   r   rK   r   r   r   r{   r   rM   )rN   r   ro   r   r   r0   r   s    ``    r1   rR   zuppergamma.eval  sk   BBBBBB; 	$AEzzubv $a55$ $ 88O ''))A< 	VAM 	VABBww!!R((( \ 	Va. 	VAvv"uQwqy!!44Yr]]BZPQSUEVEVVV !VV88QQrT!VAXaZ01C"Qq
OOJqRTDUDU4UUU ; 	SAF{{q}{A2waeA2wwafBxxT!WW-- S!A#!1 SE= 
@| F"A2ww15 >Q >Q >Q >QGLQxx>Q >Q >Q 9R  R R !&a4Q== 8 !AaDDF
 ;c1"gg EQ O"% (D (D (D (D (D16q16z1B1B(D (D (D #E!E!E F \ @!61"a==AQ)???| SMAFQJ7"<T$q'']]J5QRSTQT::UdS!WWns 5Q 5Q 5Q 5Q 5Q>CAFQJ>O>O5Q 5Q 5Q 0R RR S 9 	 	rFF7N9 	A* 	88O	 	 	 	r?   c                     | j         d         }|t          j        t          j        fvrE|                     | j         d                                         |                                          S d S r7   r   r<   ro   s     r1   rb   zuppergamma._eval_conjugate  r   r?   c                 :    t                               | ||          S rm   )r   r   )r<   rT   r   s      r1   r   zuppergamma._eval_is_meromorphic"  s    ..tQ:::r?   c                 B    t          |          t          ||          z
  S rm   )r4   r   r   s       r1   _eval_rewrite_as_lowergammaz&uppergamma._eval_rewrite_as_lowergamma%  r   r?   c                 \    t          t          |                    t          ||          z
  S rm   )r   rn   r   r   s       r1   rr   z%uppergamma._eval_rewrite_as_tractable(  s%    8A;;*Q"2"222r?   c                 8    ddl m}  |d|z
  |          ||z  z  S )Nr   r   r5   )r   r   r   s        r1   r   z"uppergamma._eval_rewrite_as_expint+  s3    BBBBBBva!eQ1$$r?   Nr   )r   r   r   r   r>   r   r   rR   rb   r   r   rr   r   r   r?   r1   r   r     s        > >B	5 	5 	5 	5   6 6 [6pF F F
; ; ;+ + +3 3 3% % % % %r?   r   c                   x     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 )r:   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                 6   |t           j        u s|t           j        u rt           j        S |t          u r|j        rt          n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)|                    t                    t          t           fv rt          S |j        rt          |dz
            t           j        z
  S |j        rF|                                \  }}|dk    r+t%          t'          t           j        |d                    S d S d S |j        r|j        rt-          |          }||k    rt'          ||          S |j        r6t           j        |dz   z  t/          |          z  t1          |dz   |          z  S |t           j        u rEt           j        |dz   z  t/          |          z  d|dz   z  dz
  z  t1          |dz             z  S d S d S d S )NrA   r5      F)evaluate)r   rD   r   r   r   r{   rd   rF   rL   rn   r   r   extract_multiplicativelyr   r$   
EulerGammarG   as_numer_denomr   r:   re   is_nonnegativer   r%   r   r   )rN   r0   ro   rJ   rH   nzs         r1   rR   zpolygamma.eval  s   ::ae5L"WW.22.\ 	Va. 	V$$!-A;;QrTQ..Y 	VRCxx155a88R"IEE	 M!}}q|33 M''))166&yU'K'K'KLLLM M 6\ 	Va. 	VABBww B'''| V}qs+ill:T!A#q\\IIaf}qs+ill:a!A#hqjIDQRSTQTIIUU	V 	V 	V 	V r?   c                 V    | j         d         j        r| j         d         j        rdS d S d S )Nr   r5   T)r9   rE   ra   s    r1   rh   zpolygamma._eval_is_real  s<    9Q<# 		!(@ 	4	 	 	 	r?   c                     | j         d         }t          |j        |j        g          }t          |j        t          |          g          S rt   )r9   r   is_negativere   
is_complexr   )r<   ro   is_negative_integers      r1   _eval_is_complexzpolygamma._eval_is_complex  sB    IaL'(EFF!,	2E(F(FGHHHr?   c                 p    | j         \  }}|j        r |j        r	|j        rdS |j        r|j        rdS d S d S d S NTF)r9   rE   is_oddis_realrj   r<   r0   ro   s      r1   rk   zpolygamma._eval_is_positive  se    y1= 	x AI ty Q] u		 	   r?   c                 p    | j         \  }}|j        r |j        r	|j        rdS |j        r|j        rdS d S d S d S r   )r9   rE   rj   r   r  r  s      r1   _eval_is_negativezpolygamma._eval_is_negative  se    y1= 	y Q] tx AI u		 	   r?   c           	        	
 | j         \  j        r\j        rTj        rj         d         j        rdz    dk    r5t	          fdt          dt                    dz             D              }n2t	          fdt          t                               D               }t          z
            t          j	        z  t                    z  |z  z   S nj        r                                \  j        rej        r^fdt          t                              D             }dk    rt	          | z  t                    z   S t	          | dz   z  z  S z  dk    rj        r                                 \  	
t          j         t$          t'          	t$          z  
z            z  dz  z
  t          
          z
  t	          	
fdt          d
          D              z   }dk    r9t)                    z
  |t	          fdt                    D              z   S dk     r<t)          dz
            z   |t	          fd	t                    D              z
  S d
k    r*t+                    t          dt$          z            dz  z
  S j        du s	j        du rt/          d          }t1          |                              |                              |dz             }|t          j        t7                     z   t1          dz             z  z   t9                     z  S t                    S )Nr   r5   c                 6    g | ]}t          |z
            S r   r   r   iero   s     r1   r   z/polygamma._eval_expand_func.<locals>.<listcomp>  sB     %I %I %I*+ &)E1&& && %I %I %Ir?   c                 6    g | ]}t          |z             S r   r   r  s     r1   r   z/polygamma._eval_expand_func.<locals>.<listcomp>  sB     &C &C &C*+ '*E1'& '& &C &C &Cr?   c           
      R    g | ]#}t          t          |          z             $S r   )r:   r   )r   r  rQ   r0   ro   s     r1   r   z/polygamma._eval_expand_func.<locals>.<listcomp>  sM     ? ? ?'( &aX5." ." *" # # ? ? ?r?   rA   c           
          g | ]M}t          d |z  t          z  z  z            t          d t          |t          z  z            z            z  NS r   )r!   r   r   r    )r   rP   rJ   rH   s     r1   r   z/polygamma._eval_expand_func.<locals>.<listcomp>  sR    ZZZ#a!ebj1nq())CCB
OO0C,D,DDZZZr?   c                      g | ]
}d |z   z  S r   r   r   rP   r   s     r1   r   z/polygamma._eval_expand_func.<locals>.<listcomp>  s!    %E%E%Eqa26l%E%E%Er?   c                 &    g | ]}d d z
  |z
  z  S r   r   r  s     r1   r   z/polygamma._eval_expand_func.<locals>.<listcomp>   s&    %I%I%I1a26A:&6%I%I%Ir?   Fr   )r9   r{   r   rX   r   rM   intr:   r   rL   r%   is_Mulas_two_termsrE   r   rG   r   r   r   r"   r   rn   re   r   r   diffrW   r   r4   )r<   r[   r\   part_1r   dztrQ   r	  r0   rJ   rH   ro   r   s         @@@@@@@r1   rV   zpolygamma._eval_expand_func  s   y1< 	A, 	x q	# Xa%Aqyy" %I %I %I %I %I/4QE

Q/G/G%I %I %I  J !$ &C &C &C &C &C/4S%[[/A/A&C &C &C !D  D$QE	22Q]A5EiPQll5RSW5WWWX  	>>++q# 9(9 9? ? ? ? ? ?,1#e**,=,=? ? ?DAvv"Dz%/#e**<<"Dz%!a%.88U
66am6##%%DAq l]R#a"fqj//%9A%==AFZZZZZeTUWXkkZZZJ\ \F 1uu!HHU%E%E%E%EE!HH%E%E%E FFFQ!a%LLU%I%I%I%Ia%I%I%I JJJ77A;;QrTQ..<5  A$4$=$=c

Aq!**//!$$))!QqS11C1<'1"++5ac1EEPQrRRAr?   c                     |j         r=|j        r8t          j        |dz   z  t	          |          z  t          |dz   |          z  S d S d S rt   )re   rE   r   rL   r%   r   r<   r0   ro   rq   s       r1   _eval_rewrite_as_zetazpolygamma._eval_rewrite_as_zeta  s\    < 	FAM 	F=1q5))A,,6tAE1~~EE	F 	F 	F 	Fr?   c                     |j         rr|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 d S rt   )re   r   r$   r   r   rL   r%   r   r  s       r1   _eval_rewrite_as_harmonicz#polygamma._eval_rewrite_as_harmonic  s    < 	^y ^A55}qs+ill:d1Q3ii(STUVSVXYZ[X[J\J\>\]]		^ 	^r?   Nr   c                    ddl m} fd| j        D             \  }} ||          }|dk    rB|                    dz            r*|t	                    n|}|                                |z  S |                     ||          S )Nr   Orderc                 :    g | ]}|                               S r   )r   )r   r   rT   s     r1   r   z3polygamma._eval_as_leading_term.<locals>.<listcomp>  s'    888!!!$$888r?   r5   )r   r  r9   containsr   getnr8   )r<   rT   r~   r   r  r0   ro   r   s    `      r1   r   zpolygamma._eval_as_leading_term  s    ,,,,,,8888di8881E!QKK66ajj1oo6!\3q666tD6688d?"99Q??"r?   rA   c                 x    |dk    r%| j         d d         \  }}t          |dz   |          S t          | |          NrA   r5   )r9   r:   r
   )r<   r=   r0   ro   s       r1   r>   zpolygamma.fdiff   sB    q==9RaR=DAqQUA&&&$T8444r?   c                 T   ddl m} |d         t          k    s$| 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  |          }nOt          |dz   dz            }	fdt          d|	          D             }
|t          |
 z  } |d|z  z  |          }|                    |||          |z   S t          |          }|||z  dz  z  z   }t          |dz   dz            }	t          d|	          D ]L}|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  }M |dd|	z  z  z  |          }|dk    r |dz  |          }n|dk    r |ddz  z  |          }|                    ||          |z   }ddz  |z  z  |z                      |||          S )Nr   r  r5   rA   c                 R    g | ]#}t          d |z            d |z  d |z  z  z  z  $S r   r#   r   s     r1   r   z+polygamma._eval_aseries.<locals>.<listcomp>8  s8    JJJYqs^^qs1qs8|4JJJr?   r  )r   r  r   r9   r{   r   r|   r   r   r   rM   r   r}   r4   r#   )r<   r0   r   rT   r~   r  Nrr   mlface0rP   ro   r   s                 @r1   r   zpolygamma._eval_aseries'  s   ,,,,,,8r>>1( -1Yq\-H 77((E1d;;;IaLIaL66 AAaC AA1uuE!A#qMMQUQJ''JJJJeAqkkJJJS!WE!AqD&!$$??1a..22 ((Cquac{"BQ
##A1a[[ 2 21Q37Q;'1q15!A#!aIi!nnS(QqS11aAaCj!$$AAvvE!A#qMMaE!AqD&!$$  At,,q0A"Q$NQ&55aDAAAr?   c                    t          d | j        D                       sd S | j        d                             |dz             }| j        d                             |dz             }t          j        |          r|dk    rt
          j        S t          |dz             5  t          j        |          r|dk    rt          j        ||          }not          j	        |dz   |          }t          j	        |dz   |d          }|t          j
        t          j        |           z   |z  z   t          j        |           z  }d d d            n# 1 swxY w Y   t          j        ||          S )Nc              3   $   K   | ]}|j         V  d S rm   r   )r   r  s     r1   r   z(polygamma._eval_evalf.<locals>.<genexpr>Q  s$      2211;222222r?   r      r5   )r   r9   r   r)   isintr   rF   r*   r:   r   eulerr   rgammar   r   )r<   r   r   ro   r   ztr  s          r1   r   zpolygamma._eval_evalfP  s   22	22222 	FIaL##DG,,IaL##DG,,8A;; 	%166$$d2g 	O 	Ox{{ OqAvvl1a((WQqS!__gac1a((bhQB72==A2N	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O  d+++s   B EEEr_   r   )r   r   r   r   r   rR   rh   r   rk   r  rV   r  r  r   r>   r   r   r   r   s   @r1   r:   r:   4  s       h hT V V [V:  I I I
    3 3 3jF F F^ ^ ^# # # #5 5 5 5'B 'B 'B 'B 'BR, , , , , , ,r?   r:   c                   d     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 )rn   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        rt	          t          |                    S n|j        r~|                                \  }}|j        r`|dk    rZt	          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 d S r#  )re   rd   r   rE   r   r4   is_rationalr   r   r   r   r   rI   rF   rD   )rN   ro   rJ   rH   s       r1   rR   zloggamma.eval  s    < 		U %	 %588}}$%] 	U##%%DAq} Ua488a!a%j0588;eQUAFN>S>SSTTT77IVVr\\$$::5L :r?   c                 f   ddl m} | j        d         }|j        r|                                \  }}||z  }|||z  z
  }|j        r|j        r||k     rt          d          }|j        rKt          ||z            |t          |          z  z
   |t          |dz
  |z  |z             |d|f          z   S |j	        r\t          ||z            |t          |          z  z
  t          t          z  |z  z    |t          ||z  |z
            |d| f          z
  S |j        rt          ||z            S | S )Nr   SumrP   r5   )sympy.concrete.summationsr9  r9   rG   r   rE   r   rn   r   r   r   r   r   )r<   r[   r9  ro   rJ   rH   r0   rP   s           r1   rV   zloggamma._eval_expand_func  sU   111111IaL= 	+##%%DAq QAAaCA} + +1q55#JJ= +#AE??Qs1vvX5CQ	A<N<NQRTUWXPY8Z8ZZZ] +#AE??Qs1vvX51Q>S1q\\TUWX[\Z\S]A^A^^^Y +#AE??*r?   Nr   c                     | j         d                             |d          }|j        r& | j        | j          }|                    |||          S t                                          |||          S r_   )r9   rz   r   _eval_rewrite_as_intractabler}   r|   )r<   rT   r0   r~   r   r   fr   s          r1   r}   zloggamma._eval_nseries  sk    Yq\1%%: 	/1149=A??1a...ww$$Q4000r?   c                   	 ddl m} |d         t          k    r$t                                          ||||          S | j        d         	t          	          	t          j        z
  z  	z
  t          dt          z            dz  z   }	fdt          d|          D             }d }|dk    r |d|          }n |d	|z  z  |          }|t          | z                       |||          |z   S )Nr   r  rA   c                 j    g | ]/}t          d |z            d |z  d |z  dz
  z  d |z  dz
  z  z  z  0S )rA   r5   r&  r   s     r1   r   z*loggamma._eval_aseries.<locals>.<listcomp>  sI    PPPqYqs^^qsAaC!G}Q1q\9:PPPr?   r5   )r   r  r   r|   r   r9   r   r   r   r   rM   r   r}   )r<   r0   r   rT   r~   r  r(  r*  r   ro   r   s            @r1   r   zloggamma._eval_aseries  s    ,,,,,,8r>>77((E1d;;;IaLFFAJ!#c!B$iik1PPPPE!QKKPPP66aAAa1fa  ACG**1a66::r?   c                 :    t          t          |                    S rm   )r   r4   rv   s      r1   r<  z%loggamma._eval_rewrite_as_intractable  s    588}}r?   c                 D    | j         d         }|j        rdS |j        rdS d S )Nr   TF)r9   rE   rd   r   s     r1   rh   zloggamma._eval_is_real  s6    IaL= 	4 	5	 	r?   c                     | j         d         }|t          j        t          j        fvr'|                     |                                          S d S r_   r   r   s     r1   rb   zloggamma._eval_conjugate  sD    IaLQVQ/00099Q[[]]+++ 10r?   r5   c                 d    |dk    rt          d| j        d                   S t          | |          r7   )r:   r9   r
   r;   s     r1   r>   zloggamma.fdiff  s1    q==Q	!---$T8444r?   r_   r   )r   r   r   r   r   rR   rV   r}   r   r<  rh   rb   r>   r   r   s   @r1   rn   rn   a  s        l lZ   [&  *1 1 1 1 1 1; ; ; ; ;    , , ,
5 5 5 5 5 5 5 5r?   rn   c                   h    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dS )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                     | j         d         }t          |          }t          d|                              |          S )Nr   r/   r9   r+   r:   evalfr<   r   ro   nprecs       r1   r   zdigamma._eval_evalfT  9    IaLD!!A$$u$---r?   r5   c                 `    | j         d         }t          d|                                          S r_   r9   r:   r>   r<   r=   ro   s      r1   r>   zdigamma.fdiffY  '    IaLA$$&&&r?   c                 F    | j         d         }t          d|          j        S r_   r9   r:   r  r   s     r1   rh   zdigamma._eval_is_real]      IaLA&&r?   c                 F    | j         d         }t          d|          j        S r_   r9   r:   rE   r   s     r1   rk   zdigamma._eval_is_positivea      IaLA**r?   c                 F    | j         d         }t          d|          j        S r_   r9   r:   r   r   s     r1   r  zdigamma._eval_is_negativee  rT  r?   c                     |                      t                    }t          j        g|z   }|                    ||||          S rm   )r   r:   r   r   r   r<   r0   r   rT   r~   as_polygammas         r1   r   zdigamma._eval_aseriesi  s;    ||I..	E!))!UAt<<<r?   c                 "    t          d|          S r_   r:   rN   ro   s     r1   rR   zdigamma.evaln      Ar?   c                 d    | j         d         }t          d|                              d          S )Nr   Tr8   r9   r:   expandr<   r[   ro   s      r1   rV   zdigamma._eval_expand_funcr  ,    IaLA%%4%000r?   c                 @    t          |dz
            t          j        z
  S rt   )r$   r   r   rv   s      r1   r  z!digamma._eval_rewrite_as_harmonicv  s    A--r?   c                 "    t          d|          S r_   r[  rv   s      r1   _eval_rewrite_as_polygammaz"digamma._eval_rewrite_as_polygammay      Ar?   Nr   c                 b    | j         d         }t          d|                              |          S r_   r9   r:   r   r<   rT   r~   r   ro   s        r1   r   zdigamma._eval_as_leading_term|  )    IaLA..q111r?   r   r_   )r   r   r   r   r   r>   rh   rk   r  r   r   rR   rV   r  rf  r   r   r?   r1   r   r   $  s        . .^. . .
' ' ' '' ' '+ + ++ + += = =
   [1 1 1. . .  2 2 2 2 2 2r?   r   c                   n    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dS )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                     | j         d         }t          |          }t          d|                              |          S )Nr   r5   r/   rF  rH  s       r1   r   ztrigamma._eval_evalf  rJ  r?   r5   c                 `    | j         d         }t          d|                                          S ry   rL  rM  s      r1   r>   ztrigamma.fdiff  rN  r?   c                 F    | j         d         }t          d|          j        S ry   rP  r   s     r1   rh   ztrigamma._eval_is_real  rQ  r?   c                 F    | j         d         }t          d|          j        S ry   rS  r   s     r1   rk   ztrigamma._eval_is_positive  rT  r?   c                 F    | j         d         }t          d|          j        S ry   rV  r   s     r1   r  ztrigamma._eval_is_negative  rT  r?   c                     |                      t                    }t          j        g|z   }|                    ||||          S rm   )r   r:   r   rK   r   rX  s         r1   r   ztrigamma._eval_aseries  s;    ||I..5 ))!UAt<<<r?   c                 "    t          d|          S rt   r[  r\  s     r1   rR   ztrigamma.eval  r]  r?   c                 d    | j         d         }t          d|                              d          S )Nr   r5   Tr_  r`  rb  s      r1   rV   ztrigamma._eval_expand_func  rc  r?   c                 "    t          d|          S )NrA   r   rv   s      r1   r  ztrigamma._eval_rewrite_as_zeta  s    Aqzzr?   c                 "    t          d|          S rt   r[  rv   s      r1   rf  z#trigamma._eval_rewrite_as_polygamma  rg  r?   c                 F    t          |dz
  d           t          dz  dz  z   S )Nr5   rA   r   )r$   r   rv   s      r1   r  z"trigamma._eval_rewrite_as_harmonic  s&    Q"""RUQY..r?   Nr   c                 b    | j         d         }t          d|                              |          S ry   ri  rj  s        r1   r   ztrigamma._eval_as_leading_term  rk  r?   r   r_   )r   r   r   r   r   r>   rh   rk   r  r   r   rR   rV   r  rf  r  r   r   r?   r1   rm  rm    s        . .^. . .
' ' ' '' ' '+ + ++ + += = =
   [1 1 1    / / /2 2 2 2 2 2r?   rm  c                   @    e Zd ZdZdZd	dZed             Zd Zd Z	dS )

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

    TrA   c           	          ddl m} |dk    rV| j        \  }}t          d          }|                     ||           |t          d|d|z
  dz  z             |d|f          z  S t          | |          )Nr   r8  rA   rP   r5   )r:  r9  r9   r   r8   r:   r
   )r<   r=   r9  rT   rJ   rP   s         r1   r>   zmultigamma.fdiff"  s    111111q==9DAqc

A99Q??33yAQ	M'B'BQ1I#N#NNN$T84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                                  S )	Nr   )ProductFz+Order parameter p must be positive integer.rP   r5      rA   )	sympy.concrete.productsr~  rE   re   r.   r   r   r4   doit)rN   rT   rJ   r~  rP   s        r1   rR   zmultigamma.eval+  s    333333=E!!Q\U%:%:JKKK#JJQAYq[!''%QUAI*>*>+,a)#5 #5 56:dff	=r?   c                 f    | j         \  }}|                     |                                |          S rm   )r9   r8   r`   )r<   rT   rJ   s      r1   rb   zmultigamma._eval_conjugate4  s)    y1yy***r?   c                     | j         \  }}d|z  }|j        r||dz
  k    du rdS t          |          r||dz
  k    rdS ||dz
  k    s|j        rdS d S )NrA   r5   TF)r9   re   r2   rf   )r<   rT   rJ   ys       r1   rh   zmultigamma._eval_is_real8  su    y1aC< 	Q1q5\d2251:: 	1Q<<5A;;!/;4 ;r?   Nr   )
r   r   r   r   r   r>   r   rR   rb   rh   r   r?   r1   r{  r{    ss        7 7p J5 5 5 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+   r2   r4   r   r   r:   rn   r   rm  r{  r   r?   r1   <module>r     s         1 1 1 1 1 1 1 1 1 1 1 1             G G G G G G G G G G 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2             7 7 7 7 7 7 A A A A A A A A A A ? ? ? ? ? ? ? ? ; ; ; ; ; ; ; ; > > > > > > > > 9 9 9 9 9 9 B B B B B B B B B B E E E E E E E E S S S S S S S S S S ' ' ' ' ' '         + + + + + +  u u u u uH u u uxm m m m m m m m`_% _% _% _% _% _% _% _%Lj, j, j, j, j, j, j, j,Z	@5 @5 @5 @5 @5x @5 @5 @5FZ2 Z2 Z2 Z2 Z2h Z2 Z2 Z2|]2 ]2 ]2 ]2 ]2x ]2 ]2 ]2JY Y Y Y Y Y Y Y Y Yr?   