a
    RG5d4                     @   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 0 d S )NF)strictT)r'   
ValueErrorn r/   c/var/www/html/django/DPS/env/lib/python3.9/site-packages/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   s6   |dkr(|  | jd td| jd  S t| |d S Nr3   r   )funcargs	polygammar	   selfargindexr/   r/   r0   fdiffr   s     zgamma.fdiffc                 C   s   |j r|tju rtjS |tju r&tjS t|rH|jr@t|d S tjS n|jr|j	dkrt
|j|j	 }|jr||tj }}n&|d  }}|d@ dkrtj}ntj}|ttdd| d9 }|jr|ttj d|  S d| ttj | 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   s,    


z
gamma.evalc           	      K   s   | j d }|jrft|j|jkrftd}|j|j }|j||j  }| ||  |t	||jS |j
r| \}}|r|jdkrt|}|| 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   s   t t|S N)r   loggamma)r9   zlimitvarkwargsr/   r/   r0   _eval_rewrite_as_tractable   s    z gamma._eval_rewrite_as_tractablec                 K   s   t |d S Nr3   r$   r9   rg   ri   r/   r/   r0   _eval_rewrite_as_factorial   s    z gamma._eval_rewrite_as_factorialr   c                    sl   | j d |d}|jr |dks0t |||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   rs      s
    zgamma._eval_nseriesc                 C   sl   | j d }||d}|jrR|jrR| }tj| | |d  }||| | S |jsb| |S t	 d S ro   )
r6   rS   r_   r^   r   rH   r5   as_leading_termis_infiniter
   )r9   rP   rt   ru   rL   rv   r.   resr/   r/   r0   _eval_as_leading_term   s    

zgamma._eval_as_leading_term)r3   )N)r   )Nr   )__name__
__module____qualname____doc__
unbranchedr   rB   _singularitiesr;   classmethodrO   rR   r]   rb   rd   rj   rn   rs   r}   __classcell__r/   r/   rx   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r8| j\}}tt| ||d   S |dkr| j\}}t|t| t|t||  |g ddgdd|gg | S t	| |d S Nr   )meijergr<   r3   )
sympy.functions.special.hyperr   r6   r   r   r2   digammar   
uppergammar	   r9   r:   r   arg   r/   r/   r0   r;     s    

 zlowergamma.fdiffc              	      s  t ju rt jS  \}} jrD jrDt}|krt |S np jr jr|dkrdt t	 | t j
    t   t | S n*|dkrtdt t	 |   t | S  jr t ju rt jt  S  t ju rtttt S  jsd  jr d }|jr jrTt|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st 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                    s   g | ]} | t | qS r/   rl   .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  r   c                    s0   g | ](}|  d   t   t  |  qS r3   r2   r   r   rP   r/   r0   r   P  r   r=   )r   Zeroextract_branch_factorr_   rA   r   r   r^   r   r   rH   r$   r   r>   rG   r   r   r   rq   r   rI   r2   r   is_zero)rK   r   rP   nxr.   br/   r   r0   rO   #  s6    
2"

4H^zlowergamma.evalc                 C   s~   t dd | jD rv| jd |}| jd |}t| t|d|}W d    n1 s`0    Y  t||S | S d S )Nc                 s   s   | ]}|j V  qd S re   	is_numberr   rP   r/   r/   r0   	<genexpr>V  r   z)lowergamma._eval_evalf.<locals>.<genexpr>r   r3   )allr6   
_to_mpmathr)   r(   gammaincr   _from_mpmathr9   precr   rg   r|   r/   r/   r0   _eval_evalfU  s    
,zlowergamma._eval_evalfc                 C   s8   | j d }|tjtjfvr4| | j d  | S d S r4   r6   r   r   NegativeInfinityr5   r[   ra   r/   r/   r0   r]   _  s    
zlowergamma._eval_conjugatec                 C   st   | j \}}t||||||g}|s.|S |||}|jrPt|j|jgS |||}t|j|jt|jgS re   )	r6   r   _eval_is_meromorphicrS   r_   rA   	is_finiter   r   )r9   rP   r   srg   Z
args_meromz0s0r/   r/   r0   r   d  s    

zlowergamma._eval_is_meromorphicc           	         s   ddl m} | j\ |d tju r|s  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   rg   r/   r0   r   z  r   z+lowergamma._eval_aseries.<locals>.<genexpr>r3   )sympy.series.orderr   r6   r   r@   hasr   sumrI   rr   _eval_aseries)	r9   r.   args0rP   rt   r   rN   Zsum_exprorx   r   r0   r   u  s    
 zlowergamma._eval_aseriesc                 K   s   t |t|| S re   )r2   r   r9   r   rP   ri   r/   r/   r0   _eval_rewrite_as_uppergamma  s    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   ri   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/   rx   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rx| j\}}t||t| |g ddgdd|gg | S t| |d S 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 rx| jd |}| jd |}t|  t||tj}W d    n1 sb0    Y  t||S | S )Nc                 s   s   | ]}|j V  qd S re   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rJtju r"tjS tju r2tjS jrJt j	rJt
 S  \}} jr~ j	r~t}|kr|t |S n jrĈ jr|dkrdt t | tj    t   t | S nP|dkrt
 dtdt t |     tdt t |   t |  S  jr tju r<j	r<t  S  tju rRt S  tju rrtttt S  jsd  jr 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 n$|jrN|| t|d   S  jstjtj   t tt t
d     t  t fd
dttj  D    S  jr҈j	rt  S jrt j	rt
 S d S )Nr   r   r3   r<   c                    s   g | ]} | t | qS r/   rl   r   rg   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   rg   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   rq   r   rI   )rK   r   rg   r   r   r.   r   r/   r   r0   rO     sh    


2
F

&*zuppergamma.evalc                 C   s8   | j d }|tjtjfvr4| | j d  | S d S r4   r   r9   rg   r/   r/   r0   r]     s    
zuppergamma._eval_conjugatec                 C   s   t | ||S re   )r   r   )r9   rP   r   r/   r/   r0   r   "  s    zuppergamma._eval_is_meromorphicc                 K   s   t |t|| S re   )r2   r   r   r/   r/   r0   _eval_rewrite_as_lowergamma%  s    z&uppergamma._eval_rewrite_as_lowergammac                 K   s   t t|t|| S re   )r   rf   r   r   r/   r/   r0   rj   (  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_expintN)r<   )r~   r   r   r   r;   r   r   rO   r]   r   r   rj   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                    s>   | j d }|jr:|jr:|js(|t|kr:|jr:t |S d S rZ   )r6   r   is_realr_   intis_nonnegativerr   r   )r9   r   r.   rx   r/   r0   r     s    
zpolygamma._eval_evalfr<   c                 C   s6   |dkr(| j d d \}}t|d |S t| |d S Nr<   r3   )r6   r7   r	   )r9   r:   r.   rg   r/   r/   r0   r;     s    zpolygamma.fdiffc                 C   s    | j d jr| j d jr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 rk   )r6   r   is_negativer_   
is_complexr   )r9   rg   Zis_negative_integerr/   r/   r0   _eval_is_complex  s    
zpolygamma._eval_is_complexc                 C   s(   | j d jr$| j d jr$| j d jS d S ro   )r6   rA   is_oddr\   r/   r/   r0   rd     s    zpolygamma._eval_is_positivec                 C   s(   | j d jr$| j d jr$| j d jS d S ro   )r6   rA   rc   r\   r/   r/   r0   _eval_is_negative  s    zpolygamma._eval_is_negativec                    s  ddl m} |d tks0| jd jr0| jd jsBt ||||S | jd  | jd }|dkrt dd    }d }|dk r|d  |}nFt	|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 d S )Nr   Orderr3   r<   c                    s,   g | ]$}t d | d |  d |    qS r<   r"   r   r   r/   r0   r     r   z+polygamma._eval_aseries.<locals>.<listcomp>)r   r   r   r6   rq   r   rr   r   r   r   rI   r   rs   r2   r"   )r9   r.   r   rP   rt   r   Nrr   mlface0rM   rx   r   r0   r     s@    



8$

zpolygamma._eval_aseriesc                 C   s  |j r|jr(t|}||kr(t||S |jrj|tju rjtj|d  t| d|d  d  t	|d  S |tju r|t
|S |jr|tju rtjS |tju r|jr|jrtjS tjS |jrtjS nZ|jr|jrtjS |jrt|d dtj S |jrtj|d  t| t	|d | S |jr|tju r8tjS |jrl| \}}|dkrtttj|ddS n<|tjtjfv rtjS |tj}|tjtjfv rtjS d S )Nr3   r<      F)evaluate)r_   r   r   r7   rA   r   r   rH   r$   r   rf   r>   r?   r@   r   r   rq   r^   rB   r#   
EulerGammar   rC   as_numer_denomr   r   extract_multiplicativelyImaginaryUnit)rK   r.   rg   nzrF   rD   rw   r/   r/   r0   rO     sN    

2


$
zpolygamma.evalc                    s  | j \jr0jr0jrj d   jrd   dkrjtfddtdt d D  }n$tfddtt  D   }t  tj	 t
 |  S n|jr0 \  jr( jr( fddtt D }dkrt|   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 rtd  |tfd
dtD   S tS )Nr   r3   c                    s   g | ]}t |  qS r/   r   r   ierg   r/   r0   r     s   z/polygamma._eval_expand_func.<locals>.<listcomp>c                    s   g | ]}t |  qS r/   r   r   r   r/   r0   r   "  s   c                    s    g | ]}t t|  qS r/   )r7   r   r   )rN   r.   rg   r/   r0   r   )  s   r<   c                    s<   g | ]4}t d | t    td t|t    qS r   )r    r   r   r   r   )rF   rD   r/   r0   r   7  r   c                    s   g | ]}d  |  qS r   r/   r   r   r/   r0   r   <  r   c                    s   g | ]}d  d  |  qS r   r/   r   r   r/   r0   r   @  r   )r6   rq   r   rT   r   rI   r   r7   r   rH   r$   is_Mulas_two_termsrA   r   rC   r   r   r   r!   r   )r9   rW   rX   Zpart_1r/   )rN   r   r.   rF   rD   rg   r   r0   rR     sL    




&

(

zpolygamma._eval_expand_funcc                 K   s:   |j r6|tj jr6tj|d  t| t|d | S d S rk   )r_   r   rG   r   rH   r$   r   r9   r.   rg   ri   r/   r/   r0   _eval_rewrite_as_zetaD  s    zpolygamma._eval_rewrite_as_zetac                 K   sV   |j rR|jrt|d tj S tj|d  t| t|d t|d |d   S d S rk   )r_   r   r#   r   r   rH   r$   r   r   r/   r/   r0   _eval_rewrite_as_harmonicI  s    z#polygamma._eval_rewrite_as_harmonicNr   c                    sd   ddl m}  fdd| jD \}}|| }|dkrT|d  rT| t  S | ||S d S )Nr   r   c                    s   g | ]}|  qS r/   )rz   )r   r   r   r/   r0   r   R  r   z3polygamma._eval_as_leading_term.<locals>.<listcomp>r3   )r   r   r6   containsgetnr   r5   )r9   rP   rt   ru   r   r.   rg   r   r/   r   r0   r}   P  s    
zpolygamma._eval_as_leading_term)r<   )Nr   )r~   r   r   r   r   r;   rb   r   rd   r   r   r   rO   rR   r   r   r}   r   r/   r/   rx   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 )rf   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rztt|S nT|jrz| \}}|jrz|dkrztt	tj
dd|   t| t|d tj  S |tju rtjS t|tju rtjS |tju rtjS d S r   )r_   r^   r   r@   rA   r   r2   is_rationalr   r   rJ   r   rE   rB   r?   )rK   rg   rF   rD   r/   r/   r0   rO     s    4

zloggamma.evalc                 K   s   ddl m} | jd }|jr| \}}|| }|||  }|jr|jr||k rtd}|jrt|| |t|  |t|d | | |d|f S |j	rt|| |t|  t
jt
j |  |t|| | |d| f S |jrt|| S | S )Nr   SumrM   r3   )sympy.concrete.summationsr   r6   rC   r   rA   r   rf   r   r   r   rJ   r   r   )r9   rW   r   rg   rF   rD   r.   rM   r/   r/   r0   rR     s    
8Fzloggamma._eval_expand_funcNr   c                    sB   | j d |d}|jr2| j| j  }||||S t |||S rZ   )r6   rp   r   _eval_rewrite_as_intractablers   rr   )r9   rP   r.   rt   ru   rv   frx   r/   r0   rs     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r|d|}n|d |  |}|t|  |||| S )Nr   r   r<   c                    s<   g | ]4}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   rr   r   r6   r   r   r   r   rI   r   rs   )	r9   r.   r   rP   rt   r   r   r   r   rx   r   r0   r     s    
&zloggamma._eval_aseriesc                 K   s   t t|S re   )r   r2   rm   r/   r/   r0   r     s    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| |d S r4   )r7   r6   r	   r8   r/   r/   r0   r;     s    zloggamma.fdiff)Nr   )r3   )r~   r   r   r   r   rO   rR   rs   r   r   rb   r]   r;   r   r/   r/   rx   r0   rf   Z  s   m
rf   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   rg   Znprecr/   r/   r0   r   M  s    
zdigamma._eval_evalfr3   c                 C   s   | j d }td| S rZ   r6   r7   r;   r9   r:   rg   r/   r/   r0   r;   R  s    
zdigamma.fdiffc                 C   s   | j d }td|jS rZ   r6   r7   r   r   r/   r/   r0   rb   V  s    
zdigamma._eval_is_realc                 C   s   | j d }td|jS rZ   r6   r7   rA   r   r/   r/   r0   rd   Z  s    
zdigamma._eval_is_positivec                 C   s   | j d }td|jS rZ   r6   r7   r   r   r/   r/   r0   r   ^  s    
zdigamma._eval_is_negativec                 C   s&   |  t}tjg| }|||||S re   )r   r7   r   r   r   r9   r.   r   rP   rt   Zas_polygammar/   r/   r0   r   b  s    
zdigamma._eval_aseriesc                 C   s
   t d|S rZ   r7   rK   rg   r/   r/   r0   rO   g  s    zdigamma.evalc                 K   s   | j d }td|jddS )Nr   Tr5   r6   r7   expandr9   rW   rg   r/   r/   r0   rR   k  s    
zdigamma._eval_expand_funcc                 K   s   t |d tj S rk   )r#   r   r   rm   r/   r/   r0   r   o  s    z!digamma._eval_rewrite_as_harmonicc                 K   s
   t d|S rZ   r   rm   r/   r/   r0   _eval_rewrite_as_polygammar  s    z"digamma._eval_rewrite_as_polygammaNr   c                 C   s   | j d }td||S rZ   r6   r7   rz   r9   rP   rt   ru   rg   r/   r/   r0   r}   u  s    
zdigamma._eval_as_leading_term)r3   )Nr   )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     s    
ztrigamma._eval_evalfr3   c                 C   s   | j d }td| S ro   r   r   r/   r/   r0   r;     s    
ztrigamma.fdiffc                 C   s   | j d }td|jS ro   r   r   r/   r/   r0   rb     s    
ztrigamma._eval_is_realc                 C   s   | j d }td|jS ro   r   r   r/   r/   r0   rd     s    
ztrigamma._eval_is_positivec                 C   s   | j d }td|jS ro   r   r   r/   r/   r0   r     s    
ztrigamma._eval_is_negativec                 C   s&   |  t}tjg| }|||||S re   )r   r7   r   rG   r   r   r/   r/   r0   r     s    
ztrigamma._eval_aseriesc                 C   s
   t d|S rk   r   r   r/   r/   r0   rO     s    ztrigamma.evalc                 K   s   | j d }td|jddS )Nr   r3   Tr   r   r   r/   r/   r0   rR     s    
ztrigamma._eval_expand_funcc                 K   s
   t d|S )Nr<   r   rm   r/   r/   r0   r     s    ztrigamma._eval_rewrite_as_zetac                 K   s
   t d|S rk   r   rm   r/   r/   r0   r    s    z#trigamma._eval_rewrite_as_polygammac                 K   s   t |d d tjd d  S )Nr3   r<      )r#   r   rJ   rm   r/   r/   r0   r     s    z"trigamma._eval_rewrite_as_harmonicNr   c                 C   s   | j d }td||S ro   r  r  r/   r/   r0   r}     s    
ztrigamma._eval_as_leading_term)r3   )Nr   )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   sb   ddl m} |dkrT| j\}}td}| |||td|d| d  |d|f S t| |d S )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 re   )r6   r5   r[   )r9   rP   rF   r/   r/   r0   r]   -  s    
zmultigamma._eval_conjugatec                 C   s^   | j \}}d| }|jr,||d kdu r,dS t|rD||d krDdS ||d ksV|jrZdS d S )Nr<   r3   TF)r6   r_   r1   r`   )r9   rP   rF   yr/   r/   r0   rb   1  s    
zmultigamma._eval_is_realN)r<   )
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   rf   r   r  r  r/   r/   r/   r0   <module>   sB    = 1 '  ( D^e