o
    *ήc4                     @   s  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d Z:G dd de
Z;G dd de
Z<G dd de
Z=G dd de
Z>G dd de
Z?G d d! d!e
Z@G d"d# d#e
ZAG d$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                 C   s(   z	t | dd W dS  ty   Y dS w )NF)strictT)r'   
ValueErrorn r/   N/tmp/pip-target-vg8gfxp4/lib/python/sympy/functions/special/gamma_functions.pyintlike   s   r1   c                       s   e Zd ZdZdZejfZdddZe	dd Z
dd	 Zd
d Zdd Zdd ZdddZdd Zd fdd	ZdddZ  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 + polygamma(2, 1)/6 - 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.
    beta: Euler Beta function.

    References
    ==========

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

    T   c                 C   s2   |dkr|  | jd td| jd  S t| |Nr3   r   )funcargs	polygammar	   selfargindexr/   r/   r0   fdiffr   s    
zgamma.fdiffc                 C   s   |j ru|tju rtjS |tju rtjS t|r#|jr t|d S tjS |jrw|j	dkryt
|j|j	 }|jr=|tj}}n|d  }}|d@ dkrMtj}ntj}|ttdd| d9 }|jrj|ttj d|  S d| ttj | S d S d S d S )Nr3      r      )	is_Numberr   NaNInfinityr1   is_positiver$   ComplexInfinityis_RationalqabspOneNegativeOner   ranger   Pi)clsargr.   kcoeffr/   r/   r0   evalx   s2   



z
gamma.evalc           	      K   s   | j d }|jr3t|j|jkr3td}|j|j }|j||j  }| ||  |t	||jS |j
rb| \}}|rP|jdkrPt|}|| f| }|}|j|ddi}| |t|| S | j| j  S )Nr   xr3   reevalF)r6   rC   rE   rF   rD   r   r5   _eval_expand_funcsubsr   is_Addas_coeff_addr   _new_rawargsr&   )	r9   hintsrL   rP   r.   rF   rN   tailintpartr/   r/   r0   rR      s    
"zgamma._eval_expand_funcc                 C   s   |  | jd  S Nr   )r5   r6   	conjugater9   r/   r/   r0   _eval_conjugate   s   zgamma._eval_conjugatec                 C   sB   | 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)r6   is_nonpositive
is_integerr1   rA   is_nonintegerr9   rP   r/   r/   r0   _eval_is_real   s   
zgamma._eval_is_realc                 C   s(   | j d }|jr
dS |jrt|jS d S )Nr   T)r6   rA   r`   r   is_evenra   r/   r/   r0   _eval_is_positive   s   

zgamma._eval_is_positiveNc                 K      t t|S N)r   loggamma)r9   zlimitvarkwargsr/   r/   r0   _eval_rewrite_as_tractable      z gamma._eval_rewrite_as_tractablec                 K   s   t |d S Nr3   r$   r9   rh   rj   r/   r/   r0   _eval_rewrite_as_factorial   rl   z gamma._eval_rewrite_as_factorialr   c                    sl   | j d |d}|jr|dkst |||S | j d | }| |d t| j d | d  |||S Nr   r3   )r6   limit
is_Integersuper_eval_nseriesr5   r%   )r9   rP   r.   logxcdirx0t	__class__r/   r0   ru      s
   .zgamma._eval_nseriesc                 C   sh   | j d }||d}|jr)|jr)| }tj| | |d  }||| | S |js1| |S t	 rq   )
r6   rS   r_   r^   r   rH   r5   as_leading_termis_infiniter
   )r9   rP   rv   rw   rL   rx   r.   resr/   r/   r0   _eval_as_leading_term   s   

zgamma._eval_as_leading_termr3   rf   )r   rZ   )__name__
__module____qualname____doc__
unbranchedr   rB   _singularitiesr;   classmethodrO   rR   r]   rb   rd   rk   rp   ru   r   __classcell__r/   r/   rz   r0   r2   "   s    L

 	
r2   c                       sf   e Zd ZdZdddZedd Zdd Zd	d
 Zdd Z	 fddZ
dd Zdd Zdd Z  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.
    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] http://dlmf.nist.gov/8
    .. [4] http://functions.wolfram.com/GammaBetaErf/Gamma2/
    .. [5] http://functions.wolfram.com/GammaBetaErf/Gamma3/

    r<   c                 C   s   ddl m} |dkr| j\}}tt| ||d   S |dkrC| j\}}t|t| t|t||  |g ddgdd|gg | S t	| |Nr   )meijergr<   r3   )
sympy.functions.special.hyperr   r6   r   r   r2   digammar   
uppergammar	   r9   r:   r   arh   r/   r/   r0   r;     s   

 
zlowergamma.fdiffc              	      s  t ju rt jS  \}} jr" jr"t}|kr!t |S n8 jrE jrE|dkrDdt t	 | t j
    t   t | S n|dkrZtdt t	 |   t | S  jr t ju rjt jt  S  t ju rytttt S  jsd  jr d }|jrɈ jrt|t t| tfddt D    S t tt jtt t tfddtd t j D     S  jst j
t j   t tt td   t t fddtdtdd  D    S jrt jS d S )	Nr   r<   r3   c                       g | ]
} | t | qS r/   rn   .0rM   rP   r/   r0   
<listcomp>K      z#lowergamma.eval.<locals>.<listcomp>c                    s(   g | ]} |t j  tt j|  qS r/   )r   Halfr2   r   r   r/   r0   r   M  s   ( c                    s0   g | ]}|  d   t   t  |  qS r   r2   r   r   rP   r/   r0   r   P  s   0 r=   )r   Zeroextract_branch_factorr_   rA   r   r   r^   r   r   rH   r$   r   r>   rG   r   r   r   rs   r   rI   r2   r   is_zero)rK   r   rP   nxr.   br/   r   r0   rO   #  s<   

0"

4H^zlowergamma.evalc                 C   sz   t dd | jD r;| jd |}| jd |}t| t|d|}W d    n1 s0w   Y  t||S | S )Nc                 s       | ]}|j V  qd S rf   	is_numberr   rP   r/   r/   r0   	<genexpr>V      z)lowergamma._eval_evalf.<locals>.<genexpr>r   r3   )allr6   
_to_mpmathr)   r(   gammaincr   _from_mpmathr9   precr   rh   r~   r/   r/   r0   _eval_evalfU  s   
zlowergamma._eval_evalfc                 C   8   | j d }|tjtjfvr| | j d  | S d S r4   r6   r   r   NegativeInfinityr5   r[   ra   r/   r/   r0   r]   _     
zlowergamma._eval_conjugatec                 C   st   | j \}}t||||||g}|s|S |||}|jr(t|j|jgS |||}t|j|jt|jgS rf   )	r6   r   _eval_is_meromorphicrS   r_   rA   	is_finiter   r   )r9   rP   r   srh   
args_meromz0s0r/   r/   r0   r   d  s   

zlowergamma._eval_is_meromorphicc           	         s   ddl m} | j\ |d tju rA|sA  t  }t fddt|d D }|   |   }|| | S t	 
||||S )Nr   )Oc                 3   s&    | ]}| t  |d   V  qdS )r3   N)r%   r   r   rh   r/   r0   r   z  s   $ z+lowergamma._eval_aseries.<locals>.<genexpr>r3   )sympy.series.orderr   r6   r   r@   hasr   sumrI   rt   _eval_aseries)	r9   r.   args0rP   rv   r   rN   sum_exprorz   r   r0   r   u  s   
 zlowergamma._eval_aseriesc                 K      t |t|| S rf   )r2   r   r9   r   rP   rj   r/   r/   r0   _eval_rewrite_as_uppergamma     z&lowergamma._eval_rewrite_as_uppergammac                 K   s,   ddl m} |jr|jr| S | t|S )Nr   expint)'sympy.functions.special.error_functionsr   r_   r^   rewriter   r9   r   rP   rj   r   r/   r/   r0   _eval_rewrite_as_expint  s   z"lowergamma._eval_rewrite_as_expintc                 C   s   | j d }|jr
dS d S )Nr3   T)r6   r   ra   r/   r/   r0   _eval_is_zero  s   
zlowergamma._eval_is_zeror<   )r   r   r   r   r;   r   rO   r   r]   r   r   r   r   r   r   r/   r/   rz   r0   r      s    
7
1

r   c                   @   sV   e Zd ZdZdddZdd Zedd Zd	d
 Zdd Z	dd Z
dd Zd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.
    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] http://dlmf.nist.gov/8
    .. [4] http://functions.wolfram.com/GammaBetaErf/Gamma2/
    .. [5] http://functions.wolfram.com/GammaBetaErf/Gamma3/
    .. [6] https://en.wikipedia.org/wiki/Exponential_integral#Relation_with_other_functions

    r<   c                 C   s   ddl m} |dkr| j\}}tt|  ||d   S |dkr<| j\}}t||t| |g ddgdd|gg | S t| |r   )r   r   r6   r   r   r   r   r	   r   r/   r/   r0   r;     s   

,
zuppergamma.fdiffc                 C   s|   t dd | jD r<| jd |}| jd |}t| t||tj}W d    n1 s1w   Y  t||S | S )Nc                 s   r   rf   r   r   r/   r/   r0   r     r   z)uppergamma._eval_evalf.<locals>.<genexpr>r   r3   )	r   r6   r   r)   r(   r   infr   r   r   r/   r/   r0   r     s   
zuppergamma._eval_evalfc                    s  ddl m} jr%tju rtjS tju rtjS jr%t j	r%t
 S  \}} jr? j	r?t}|kr>t |S nJ jrb jrb|dkradt t | tj    t   t | S n'|dkrt
 dtdt t |     tdt t |   t |  S  jrS tju rj	rt  S  tju rt S  tju rtttt S  jsd  jrS d }|j	r jrt t| tfddt D   S t
 tt tj tdd   t  t t fd	dt tj D    S |jr|| t|d   S  jsStjtj   t tt t
d     t  t fd
dttj  D    S  jraj	rat  S jrot j	rqt
 S d S d S )Nr   r   r3   r<   c                    r   r/   rn   r   rh   r/   r0   r     s    z#uppergamma.eval.<locals>.<listcomp>r=   c                    s2   g | ]}t tj |  |  t d    qS r   )r2   r   r   r   r   rh   r/   r0   r     s    *c                    s,   g | ]}| t   t  | d   qS r   r   r   r   r/   r0   r     s    $)r   r   r>   r   r?   r@   r   r   r   rA   r2   r   r_   r   r   r^   r   r   rH   r$   r   r   rG   r   r   r   rs   r   rI   )rK   r   rh   r   r   r.   r   r/   r   r0   rO     sn   



0F



&*zuppergamma.evalc                 C   r   r4   r   r9   rh   r/   r/   r0   r]     r   zuppergamma._eval_conjugatec                 C   s   t | ||S rf   )r   r   )r9   rP   r   r/   r/   r0   r   "  s   zuppergamma._eval_is_meromorphicc                 K   r   rf   )r2   r   r   r/   r/   r0   _eval_rewrite_as_lowergamma%  r   z&uppergamma._eval_rewrite_as_lowergammac                 K   s   t t|t|| S rf   )r   rg   r   r   r/   r/   r0   rk   (  s   z%uppergamma._eval_rewrite_as_tractablec                 K   s"   ddl m} |d| |||  S )Nr   r   r3   )r   r   r   r/   r/   r0   r   +  s   z"uppergamma._eval_rewrite_as_expintNr   )r   r   r   r   r;   r   r   rO   r]   r   r   rk   r   r/   r/   r/   r0   r     s    
A	
8r   c                       s   e Zd ZdZ fddZdddZdd Zd	d
 Zdd Zdd Z	 fddZ
edd Zdd Zdd Zdd ZdddZ  ZS )r7   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).

    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.
    beta: Euler Beta function.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Polygamma_function
    .. [2] http://mathworld.wolfram.com/PolygammaFunction.html
    .. [3] http://functions.wolfram.com/GammaBetaErf/PolyGamma/
    .. [4] http://functions.wolfram.com/GammaBetaErf/PolyGamma2/

    c                    sJ   | j d }|jr|jr|js|t|kr#|jr!t |S d S d S d S d S rZ   )r6   r   is_realr_   intis_nonnegativert   r   )r9   r   r.   rz   r/   r0   r     s   
zpolygamma._eval_evalfr<   c                 C   s2   |dkr| j d d \}}t|d |S t| |Nr<   r3   )r6   r7   r	   )r9   r:   r.   rh   r/   r/   r0   r;     s   
zpolygamma.fdiffc                 C   s$   | j d jr| j d jrdS d S d S )Nr   r3   T)r6   rA   r\   r/   r/   r0   rb     s   zpolygamma._eval_is_realc                 C   s,   | j d }t|j|jg}t|jt|gS rm   )r6   r   is_negativer_   
is_complexr   )r9   rh   is_negative_integerr/   r/   r0   _eval_is_complex  s   
zpolygamma._eval_is_complexc                 C   ,   | j d jr| j d jr| j d jS d S d S rq   )r6   rA   is_oddr\   r/   r/   r0   rd        zpolygamma._eval_is_positivec                 C   r   rq   )r6   rA   rc   r\   r/   r/   r0   _eval_is_negative  r   zpolygamma._eval_is_negativec                    s  ddl m} |d tks| jd jr| jd js!t ||||S | jd  | jd }|dkrst dd    }d }|dk rG|d  |}n#t	|d d }	 fddt
d|	D }
|t|
 8 }|d |  |}||||| S t|}||| d    }t	|d d }	t
d|	D ].}|d| | d  d| | d  d| d| d   }|td| |  d|   7 }q|d d|	   |}|dkr|d  |}n|dkr|d d  |}| ||| }dd  |  | |||S )Nr   Orderr3   r<   c                    s,   g | ]}t d | d |  d |    qS r   r"   r   r   r/   r0   r     s   , z+polygamma._eval_aseries.<locals>.<listcomp>)r   r   r   r6   rs   r   rt   r   r   r   rI   r   ru   r2   r"   )r9   r.   r   rP   rv   r   Nrr   mlface0rM   rz   r   r0   r     s@   



8"zpolygamma._eval_aseriesc                 C   s  |j r|jrt|}||krt||S |jr4|tju r4tj|d  t| d|d  d  t	|d  S |tju r=t
|S |jr|tju rHtjS |tju r`|jrY|jrVtjS tjS |jr_tjS n+|jr|jritjS |jrvt|d dtj S |jrtj|d  t| t	|d | S |jr|tju rtjS |jr| \}}|dkrtttj|ddS d S |tjtjfv rtjS |tj}|tjtjfv rtjS d S d S )Nr3   r<      F)evaluate)r_   r   r   r7   rA   r   r   rH   r$   r   rg   r>   r?   r@   r   r   rs   r^   rB   r#   
EulerGammar   rC   as_numer_denomr   r   extract_multiplicativelyImaginaryUnit)rK   r.   rh   nzrF   rD   ry   r/   r/   r0   rO     sV   

2


$
zpolygamma.evalc                    s  | j \jrjrjrXj d   jrWd   dkr3tfddtdt d D  }ntfddtt  D   }t  tj	 t
 |  S n:jr \  jr jr fddtt D }dkrt|   t  S t|  d   S  9 dkrjr \tj ttt   d  t tfddtdD   }dkrt |tfd	dtD   S dk rtd  |tfd
dtD   S tS )Nr   r3   c                    s   g | ]	}t |  qS r/   r   r   ierh   r/   r0   r     
    z/polygamma._eval_expand_func.<locals>.<listcomp>c                    s   g | ]	}t |  qS r/   r   r   r   r/   r0   r   "  r   c                    s    g | ]}t t|  qS r/   )r7   r   r   )rN   r.   rh   r/   r0   r   )  s
    r<   c                    s<   g | ]}t d | t    td t|t    qS r   )r    r   r   r   r   )rF   rD   r/   r0   r   7     < c                    s   g | ]}d  |  qS r   r/   r   r   r/   r0   r   <  s    c                    s   g | ]
}d  d  |  qS r   r/   r   r   r/   r0   r   @  r   )r6   rs   r   rT   r   rI   r   r7   r   rH   r$   is_Mulas_two_termsrA   r   rC   r   r   r   r!   r   )r9   rW   rX   part_1r/   )rN   r   r.   rF   rD   rh   r   r0   rR     sN   




$

(
zpolygamma._eval_expand_funcc                 K   s>   |j r|tj jrtj|d  t| t|d | S d S d S rm   )r_   r   rG   r   rH   r$   r   r9   r.   rh   rj   r/   r/   r0   _eval_rewrite_as_zetaD  s
   $zpolygamma._eval_rewrite_as_zetac                 K   sV   |j r)|jrt|d tj S tj|d  t| t|d t|d |d   S d S rm   )r_   r   r#   r   r   rH   r$   r   r   r/   r/   r0   _eval_rewrite_as_harmonicI  s
   4z#polygamma._eval_rewrite_as_harmonicNr   c                    s`   ddl m}  fdd| jD \}}|| }|dkr*|d  r*| t  S | ||S )Nr   r   c                    s   g | ]}|  qS r/   )r|   )r   r   r   r/   r0   r   R  s    z3polygamma._eval_as_leading_term.<locals>.<listcomp>r3   )r   r   r6   containsgetnr   r5   )r9   rP   rv   rw   r   r.   rh   r   r/   r   r0   r   P  s   
zpolygamma._eval_as_leading_termr   rZ   )r   r   r   r   r   r;   rb   r   rd   r   r   r   rO   rR   r   r   r   r   r/   r/   rz   r0   r7   4  s    b
)
6.r7   c                       sd   e Zd ZdZedd Zdd Zd fdd		Z fd
dZdd Z	dd Z
dd ZdddZ  ZS )rg   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*polygamma(2, 1)/6 + O(x**4)

    We can numerically evaluate the ``gamma`` 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.
    beta: Euler Beta function.

    References
    ==========

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

    c                 C   s   |j r|jr	tjS |jrtt|S n*|jr=| \}}|jr=|dkr=tt	tj
dd|   t| t|d tj  S |tju rEtjS t|tju rOtjS |tju rWtjS d S r   )r_   r^   r   r@   rA   r   r2   is_rationalr   r   rJ   r   rE   rB   r?   )rK   rh   rF   rD   r/   r/   r0   rO     s"   4

zloggamma.evalc                 K   s   ddl m} | jd }|jrz| \}}|| }|||  }|jrz|jrz||k rztd}|jrKt|| |t|  |t|d | | |d|f S |j	rqt|| |t|  t
jt
j |  |t|| | |d| f S |jrzt|| S | S )Nr   SumrM   r3   )sympy.concrete.summationsr   r6   rC   r   rA   r   rg   r   r   r   rJ   r   r   )r9   rW   r   rh   rF   rD   r.   rM   r/   r/   r0   rR     s   
8Fzloggamma._eval_expand_funcNr   c                    sB   | j d |d}|jr| j| j  }||||S t |||S rZ   )r6   rr   r   _eval_rewrite_as_intractableru   rt   )r9   rP   r.   rv   rw   rx   frz   r/   r0   ru     s
   zloggamma._eval_nseriesc           	         s   ddl m} |d tkrt ||||S | jd  t  tj    tdt	 d  } fddt
d|D }d }|dkrE|d|}n	|d |  |}|t|  |||| S )Nr   r   r<   c                    s<   g | ]}t d | d | d | d   d | d    qS )r<   r3   r   r   r   r/   r0   r     r   z*loggamma._eval_aseries.<locals>.<listcomp>r3   )r   r   r   rt   r   r6   r   r   r   r   rI   r   ru   )	r9   r.   r   rP   rv   r   r   r   r   rz   r   r0   r     s   
&zloggamma._eval_aseriesc                 K   re   rf   )r   r2   ro   r/   r/   r0   r    rl   z%loggamma._eval_rewrite_as_intractablec                 C   s"   | j d }|jr
dS |jrdS d S )Nr   TF)r6   rA   r^   r   r/   r/   r0   rb   
  s   
zloggamma._eval_is_realc                 C   s,   | j d }|tjtjfvr| | S d S rZ   r   r   r/   r/   r0   r]     s   
zloggamma._eval_conjugater3   c                 C   s"   |dkrt d| jd S t| |r4   )r7   r6   r	   r8   r/   r/   r0   r;     s   
zloggamma.fdiffrZ   r   )r   r   r   r   r   rO   rR   ru   r   r  rb   r]   r;   r   r/   r/   rz   r0   rg   Z  s    m
rg   c                   @   sp   e Zd ZdZdd ZdddZdd Zd	d
 Zdd Zdd Z	e
dd Zdd Zdd Zdd ZdddZdS )r   aK  
    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.
    beta: Euler Beta function.

    References
    ==========

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

    c                 C   s$   | j d }t|}td|j|dS )Nr   r-   r6   r*   r7   evalfr9   r   rh   nprecr/   r/   r0   r   M     
zdigamma._eval_evalfr3   c                 C   s   | j d }td| S rZ   r6   r7   r;   r9   r:   rh   r/   r/   r0   r;   R     
zdigamma.fdiffc                 C      | j d }td|jS rZ   r6   r7   r   r   r/   r/   r0   rb   V     
zdigamma._eval_is_realc                 C   r  rZ   r6   r7   rA   r   r/   r/   r0   rd   Z  r  zdigamma._eval_is_positivec                 C   r  rZ   r6   r7   r   r   r/   r/   r0   r   ^  r  zdigamma._eval_is_negativec                 C   &   |  t}tjg| }|||||S rf   )r   r7   r   r   r   r9   r.   r   rP   rv   as_polygammar/   r/   r0   r   b     
zdigamma._eval_aseriesc                 C   
   t d|S rZ   r7   rK   rh   r/   r/   r0   rO   g     
zdigamma.evalc                 K   s   | j d }td|jddS )Nr   Tr5   r6   r7   expandr9   rW   rh   r/   r/   r0   rR   k     
zdigamma._eval_expand_funcc                 K   s   t |d tj S rm   )r#   r   r   ro   r/   r/   r0   r   o  r   z!digamma._eval_rewrite_as_harmonicc                 K   r  rZ   r  ro   r/   r/   r0   _eval_rewrite_as_polygammar     
z"digamma._eval_rewrite_as_polygammaNr   c                 C   s   | j d }td||S rZ   r6   r7   r|   r9   rP   rv   rw   rh   r/   r/   r0   r   u     
zdigamma._eval_as_leading_termr   rZ   )r   r   r   r   r   r;   rb   rd   r   r   r   rO   rR   r   r  r   r/   r/   r/   r0   r     s    /

r   c                   @   sx   e Zd ZdZdd ZdddZdd Zd	d
 Zdd Zdd Z	e
dd Zdd Zdd Zdd Zdd ZdddZdS )trigammaa5  
    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.
    beta: Euler Beta function.

    References
    ==========

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

    c                 C   s$   | j d }t|}td|j|dS )Nr   r3   r-   r  r  r/   r/   r0   r     r  ztrigamma._eval_evalfr3   c                 C   s   | j d }td| S rq   r  r	  r/   r/   r0   r;     r
  ztrigamma.fdiffc                 C      | j d }td|jS rq   r  r   r/   r/   r0   rb     r  ztrigamma._eval_is_realc                 C   r#  rq   r  r   r/   r/   r0   rd     r  ztrigamma._eval_is_positivec                 C   r#  rq   r  r   r/   r/   r0   r     r  ztrigamma._eval_is_negativec                 C   r  rf   )r   r7   r   rG   r   r  r/   r/   r0   r     r  ztrigamma._eval_aseriesc                 C   r  rm   r  r  r/   r/   r0   rO     r  ztrigamma.evalc                 K   s   | j d }td|jddS )Nr   r3   Tr  r  r  r/   r/   r0   rR     r  ztrigamma._eval_expand_funcc                 K   r  )Nr<   r   ro   r/   r/   r0   r     r  ztrigamma._eval_rewrite_as_zetac                 K   r  rm   r  ro   r/   r/   r0   r    r  z#trigamma._eval_rewrite_as_polygammac                 K   s   t |d d tjd d  S )Nr3   r<      )r#   r   rJ   ro   r/   r/   r0   r     s   z"trigamma._eval_rewrite_as_harmonicNr   c                 C   s   | j d }td||S rq   r  r   r/   r/   r0   r     r!  ztrigamma._eval_as_leading_termr   rZ   )r   r   r   r   r   r;   rb   rd   r   r   r   rO   rR   r   r  r   r   r/   r/   r/   r0   r"  {  s    /

r"  c                   @   s:   e Zd ZdZdZdddZedd Zdd	 Zd
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,
    beta

    References
    ==========

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

    Tr<   c                 C   s^   ddl m} |dkr*| j\}}td}| |||td|d| d  |d|f S t| |)Nr   r   r<   rM   r3   )r   r   r6   r   r5   r7   r	   )r9   r:   r   rP   rF   rM   r/   r/   r0   r;     s   
.
zmultigamma.fdiffc                 C   sh   ddl m} |jdu s|jdu rtdtd}t||d  d  |t|d| d  |d|f  S )	Nr   )ProductFz+Order parameter p must be positive integer.rM   r3      r<   )	sympy.concrete.productsr&  rA   r_   r,   r   r   r2   doit)rK   rP   rF   r&  rM   r/   r/   r0   rO   $  s   &zmultigamma.evalc                 C   s   | j \}}| | |S rf   )r6   r5   r[   )r9   rP   rF   r/   r/   r0   r]   -  r!  zmultigamma._eval_conjugatec                 C   s^   | j \}}d| }|jr||d kdu rdS t|r"||d kr"dS ||d ks+|jr-dS d S )Nr<   r3   TF)r6   r_   r1   r`   )r9   rP   rF   yr/   r/   r0   rb   1  s   
zmultigamma._eval_is_realNr   )
r   r   r   r   r   r;   r   rO   r]   rb   r/   r/   r/   r0   r%    s    8
	
r%  N)Cmathr   
sympy.corer   r   r   r   sympy.core.exprr   sympy.core.functionr   r	   r
   sympy.core.logicr   r   sympy.core.numbersr   r   r   r   sympy.core.powerr   &sympy.functions.special.zeta_functionsr   r   r   r   r   $sympy.functions.elementary.complexesr   r   &sympy.functions.elementary.exponentialr   r   #sympy.functions.elementary.integersr   r   (sympy.functions.elementary.miscellaneousr   (sympy.functions.elementary.trigonometricr   r    r!   %sympy.functions.combinatorial.numbersr"   r#   (sympy.functions.combinatorial.factorialsr$   r%   r&   sympy.utilities.miscr'   mpmathr(   r)   mpmath.libmp.libmpfr*   r1   r2   r   r   r7   rg   r   r"  r%  r/   r/   r/   r0   <module>   sD     = 1 '  ( D^e