U
    9%ed                     @   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k
r(   Y dS X d S )NF)strictT)r'   
ValueErrorn r/   f/var/www/html/Darija-Ai-API/env/lib/python3.8/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 - 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.
    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                 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krtjS |tkr"tS t|rD|jr<t|d S tjS n|jr|j	dkrt
|j|j	 }|jrx|tj }}n&|d  }}|d@ dkrtj}ntj}|ttdd| d9 }|jr|tt d|  S d| tt | S d S )Nr3      r      )	is_Numberr   NaNr   r1   is_positiver$   ComplexInfinityis_RationalqabspOneNegativeOner   ranger   r   )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   ZreevalF)r6   rB   rD   rE   rC   r   r5   _eval_expand_funcsubsr   is_AddZas_coeff_addr   Z_new_rawargsr&   )	r9   hintsrJ   rN   r.   rE   rL   tailZintpartr/   r/   r0   rO      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   r@   is_nonintegerr9   rN   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   r@   rZ   r   is_evenr[   r/   r/   r0   _eval_is_positive   s
    
zgamma._eval_is_positiveNc                 K   s   t t|S N)r   loggamma)r9   zZlimitvarkwargsr/   r/   r0   _eval_rewrite_as_tractable   s    z gamma._eval_rewrite_as_tractablec                 K   s   t |d S Nr3   r$   r9   ra   rb   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   rN   r.   logxcdirx0t	__class__r/   r0   rl      s
    zgamma._eval_nseriesc                 C   sl   | j d }||d}|jrR|jrR| }tj| | |d  }||| | S |jsb| |S t	 d S rh   )
r6   rP   rY   rX   r   rG   r5   as_leading_termis_infiniter
   )r9   rN   rm   rn   rJ   ro   r.   resr/   r/   r0   _eval_as_leading_term   s    

zgamma._eval_as_leading_term)r3   )N)r   )Nr   )__name__
__module____qualname____doc__
unbranchedr   rA   Z_singularitiesr;   classmethodrM   rO   rW   r\   r^   rc   rg   rl   rv   __classcell__r/   r/   rq   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] https://dlmf.nist.gov/8
    .. [4] https://functions.wolfram.com/GammaBetaErf/Gamma2/
    .. [5] https://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   ara   r/   r/   r0   r;     s    

 zlowergamma.fdiffc              	      s  t jk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krt jt  S  t jk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/   re   .0rK   rN   r/   r0   
<listcomp>K  s     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 r3   r2   r   r   rN   r/   r0   r   P  s     r=   )r   Zeroextract_branch_factorrY   r@   r   r~   rX   r   r   rG   r$   r   r>   rF   r   r   r   rj   r   rH   r2   r   is_zero)rI   r   rN   nxr.   br/   r   r0   rM   #  s6    
2"

4H^zlowergamma.evalc              	   C   sj   t dd | jD rb| jd |}| jd |}t| t|d|}W 5 Q R X t||S | S d S )Nc                 s   s   | ]}|j V  qd S r_   Z	is_numberr   rN   r/   r/   r0   	<genexpr>V  s     z)lowergamma._eval_evalf.<locals>.<genexpr>r   r3   )allr6   
_to_mpmathr)   r(   gammaincr   _from_mpmathr9   precr   ra   ru   r/   r/   r0   _eval_evalfU  s    
zlowergamma._eval_evalfc                 C   s8   | j d }|tjtjfkr4| | j d  | S d S r4   r6   r   r   NegativeInfinityr5   rU   r[   r/   r/   r0   rW   _  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 r_   )	r6   r   _eval_is_meromorphicrP   rY   r@   	is_finiter   r   )r9   rN   r   sra   Z
args_meromz0s0r/   r/   r0   r   d  s    

zlowergamma._eval_is_meromorphicc           	         s   ddl m} | j\ |d tk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   ra   r/   r0   r   z  s     z+lowergamma._eval_aseries.<locals>.<genexpr>r3   )
sympy.series.orderr   r6   r   hasr   sumrH   rk   _eval_aseries)	r9   r.   args0rN   rm   r   rL   Zsum_exprorq   r   r0   r   u  s    
 zlowergamma._eval_aseriesc                 K   s   t |t|| S r_   )r2   r   r9   r   rN   rb   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   rY   rX   rewriter   r9   r   rN   rb   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   r[   r/   r/   r0   _eval_is_zero  s    
zlowergamma._eval_is_zero)r<   )rw   rx   ry   rz   r;   r|   rM   r   rW   r   r   r   r   r   r}   r/   r/   rq   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] 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

    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   sh   t dd | jD rd| jd |}| jd |}t| t||tj}W 5 Q R X t||S | S )Nc                 s   s   | ]}|j V  qd S r_   r   r   r/   r/   r0   r     s     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rHtjkr"tjS tkr0tjS jrHt j	rHt
 S  \}} jr| j	r|t}|krzt |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kr:j	r:t  S  tjkrPt S  tjkrpt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rL|| 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/   re   r   ra   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   ra   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   r@   r2   r   rY   r   r   rX   r   r   rG   r$   r   r   rF   r   r   r   rj   r   rH   )rI   r   ra   r   r   r.   r   r/   r   r0   rM     sh    

2
F

&*zuppergamma.evalc                 C   s8   | j d }|tjtjfkr4| | j d  | S d S r4   r   r9   ra   r/   r/   r0   rW     s    
zuppergamma._eval_conjugatec                 C   s   t | ||S r_   )r~   r   )r9   rN   r   r/   r/   r0   r   "  s    zuppergamma._eval_is_meromorphicc                 K   s   t |t|| S r_   )r2   r~   r   r/   r/   r0   _eval_rewrite_as_lowergamma%  s    z&uppergamma._eval_rewrite_as_lowergammac                 K   s   t t|t|| S r_   )r   r`   r~   r   r/   r/   r0   rc   (  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<   )rw   rx   ry   rz   r;   r   r|   rM   rW   r   r   rc   r   r/   r/   r/   r0   r     s   A
	
8r   c                       s   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdddZdddZ fddZ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).

    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.
    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                 C   sp  |t jks|t jkrt jS |tkr2|jr,tS t jS |jrD|jrDt jS |t jkrft	|t
dt d  S |jr|t ks|ttt fkrtS |jrt|d t j S |jr| \}}|dkrttt j|ddS n|jrl|jrlt|}||krt||S |jr.t j|d  t| t|d | S |t jkrlt j|d  t| d|d  d  t|d  S d S )Nr<   r3      F)evaluate)r   r?   r   r   r   rj   rX   rA   rG   r`   r   r   Zextract_multiplicativelyr   r#   
EulerGammarB   as_numer_denomr   r7   rY   is_nonnegativer   r$   r   r   )rI   r.   ra   rE   rC   Znzr/   r/   r0   rM     s2    


$zpolygamma.evalc                 C   s    | j d jr| j d jrdS d S )Nr   r3   T)r6   r@   rV   r/   r/   r0   r\     s    zpolygamma._eval_is_realc                 C   s,   | j d }t|j|jg}t|jt|gS rd   )r6   r   is_negativerY   Z
is_complexr   )r9   ra   Zis_negative_integerr/   r/   r0   _eval_is_complex  s    
zpolygamma._eval_is_complexc                 C   s4   | j \}}|jr0|jr |jr dS |jr0|jr0dS d S NTF)r6   r@   is_oddis_realr]   r9   r.   ra   r/   r/   r0   r^     s    
zpolygamma._eval_is_positivec                 C   s4   | j \}}|jr0|jr |jr dS |jr0|jr0dS d S r   )r6   r@   r]   r   r   r   r/   r/   r0   _eval_is_negative  s    
zpolygamma._eval_is_negativec                    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 dkr*ttdt d  S jdksBjdkrtd}t|||d }|tjt  td   t  S tS )Nr   r3   c                    s   g | ]}t |  qS r/   r   r   iera   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   )rL   r.   ra   r/   r0   r     s
    r<   c                    s<   g | ]4}t d | t    td t|t    qS r<   )r    r   r   r   r   )rE   rC   r/   r0   r     s     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      s     Fr   )r6   rj   r   rQ   r   rH   intr7   r   rG   r$   Zis_MulZas_two_termsr@   r   rB   r   r   r   r!   r   r`   rY   r   r   diffrP   r   r2   )r9   rR   rS   Zpart_1r   dztr/   )rL   r   r.   rE   rC   ra   r   r0   rO     sX    




&

(


,zpolygamma._eval_expand_funcc                 K   s4   |j r0|jr0tj|d  t| t|d | S d S rd   )rY   r@   r   rG   r$   r   r9   r.   ra   rb   r/   r/   r0   _eval_rewrite_as_zeta  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 rd   )rY   r   r#   r   r   rG   r$   r   r   r/   r/   r0   _eval_rewrite_as_harmonic  s    z#polygamma._eval_rewrite_as_harmonicNr   c                    st   ddl m}  fdd| jD \}}|| }|dkrd|d  rd|d krTt n|}| | S | ||S d S )Nr   Orderc                    s   g | ]}|  qS r/   )rs   )r   r   r   r/   r0   r     s     z3polygamma._eval_as_leading_term.<locals>.<listcomp>r3   )r   r   r6   containsr   Zgetnr5   )r9   rN   rm   rn   r   r.   ra   r   r/   r   r0   rv     s    
zpolygamma._eval_as_leading_termr<   c                 C   s6   |dkr(| j d d \}}t|d |S t| |d S Nr<   r3   )r6   r7   r	   )r9   r:   r.   ra   r/   r/   r0   r;      s    zpolygamma.fdiffc                    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   r   r3   r<   c                    s,   g | ]$}t d | d |  d |    qS r   r"   r   r   r/   r0   r   8  s     z+polygamma._eval_aseries.<locals>.<listcomp>r   )r   r   r   r6   rj   r   rk   r   r   r   rH   r   rl   r2   r"   )r9   r.   r   rN   rm   r   Nrr   mlZfacZe0rK   rq   r   r0   r   '  s@    



8$

zpolygamma._eval_aseriesc              	   C   s   t dd | jD sd S | jd |d }| jd |d }t|rX|dkrXtjS t|d n t|r|dkrt||}nHt	|d |}t	|d |d}|tj
t|  |  t|  }W 5 Q R X t||S )Nc                 s   s   | ]}|j V  qd S r_   r   r   r/   r/   r0   r   Q  s     z(polygamma._eval_evalf.<locals>.<genexpr>r      r3   )r   r6   r   r(   Zisintr   rA   r)   r7   r   Zeulerr   Zrgammar   r   )r9   r   r   ra   ru   Zztr   r/   r/   r0   r   P  s    0zpolygamma._eval_evalf)Nr   )r<   )rw   rx   ry   rz   r|   rM   r\   r   r^   r   rO   r   r   rv   r;   r   r   r}   r/   r/   rq   r0   r7   4  s   j
5


)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 )r`   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.
    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                 C   s   |j r$|jrtS |jrvtt|S nR|jrv| \}}|jrv|dkrvttt	dd|   t| t|d t
j  S |tkrtS t|tkrt
jS |t
jkrt
jS d S r   )rY   rX   r   r@   r   r2   Zis_rationalr   r   r   r   r   rD   rA   r?   )rI   ra   rE   rC   r/   r/   r0   rM     s    2
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
t |  |t|| | |d| f S |jrt|| S | S )Nr   SumrK   r3   )sympy.concrete.summationsr   r6   rB   r   r@   r   r`   r   r   r   r   r   )r9   rR   r   ra   rE   rC   r.   rK   r/   r/   r0   rO     s    
8Bzloggamma._eval_expand_funcNr   c                    sB   | j d |d}|jr2| j| j  }||||S t |||S rT   )r6   ri   r   _eval_rewrite_as_intractablerl   rk   )r9   rN   r.   rm   rn   ro   frq   r/   r0   rl     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     s     z*loggamma._eval_aseries.<locals>.<listcomp>r3   )r   r   r   rk   r   r6   r   r   r   r   rH   r   rl   )	r9   r.   r   rN   rm   r   r   r   r   rq   r   r0   r     s    
&zloggamma._eval_aseriesc                 K   s   t t|S r_   )r   r2   rf   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   r@   rX   r   r/   r/   r0   r\     s
    
zloggamma._eval_is_realc                 C   s,   | j d }|tjtjfkr(| | S d S rT   r   r   r/   r/   r0   rW     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   )rw   rx   ry   rz   r|   rM   rO   rl   r   r   r\   rW   r;   r}   r/   r/   rq   r0   r`   a  s   m
r`   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   aM  
    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] https://mathworld.wolfram.com/DigammaFunction.html
    .. [3] https://functions.wolfram.com/GammaBetaErf/PolyGamma2/

    c                 C   s$   | j d }t|}td|j|dS )Nr   r-   r6   r*   r7   Zevalfr9   r   ra   Znprecr/   r/   r0   r   T  s    
zdigamma._eval_evalfr3   c                 C   s   | j d }td| S rT   r6   r7   r;   r9   r:   ra   r/   r/   r0   r;   Y  s    
zdigamma.fdiffc                 C   s   | j d }td|jS rT   r6   r7   r   r   r/   r/   r0   r\   ]  s    
zdigamma._eval_is_realc                 C   s   | j d }td|jS rT   r6   r7   r@   r   r/   r/   r0   r^   a  s    
zdigamma._eval_is_positivec                 C   s   | j d }td|jS rT   r6   r7   r   r   r/   r/   r0   r   e  s    
zdigamma._eval_is_negativec                 C   s&   |  t}tjg| }|||||S r_   )r   r7   r   r   r   r9   r.   r   rN   rm   Zas_polygammar/   r/   r0   r   i  s    
zdigamma._eval_aseriesc                 C   s
   t d|S rT   r7   rI   ra   r/   r/   r0   rM   n  s    zdigamma.evalc                 K   s   | j d }td|jddS )Nr   Tr5   r6   r7   expandr9   rR   ra   r/   r/   r0   rO   r  s    
zdigamma._eval_expand_funcc                 K   s   t |d tj S rd   )r#   r   r   rf   r/   r/   r0   r   v  s    z!digamma._eval_rewrite_as_harmonicc                 K   s
   t d|S rT   r   rf   r/   r/   r0   _eval_rewrite_as_polygammay  s    z"digamma._eval_rewrite_as_polygammaNr   c                 C   s   | j d }td||S rT   r6   r7   rs   r9   rN   rm   rn   ra   r/   r/   r0   rv   |  s    
zdigamma._eval_as_leading_term)r3   )Nr   )rw   rx   ry   rz   r   r;   r\   r^   r   r   r|   rM   rO   r   r   rv   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 )trigammaa7  
    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] https://mathworld.wolfram.com/TrigammaFunction.html
    .. [3] https://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 rh   r   r   r/   r/   r0   r;     s    
ztrigamma.fdiffc                 C   s   | j d }td|jS rh   r   r   r/   r/   r0   r\     s    
ztrigamma._eval_is_realc                 C   s   | j d }td|jS rh   r   r   r/   r/   r0   r^     s    
ztrigamma._eval_is_positivec                 C   s   | j d }td|jS rh   r   r   r/   r/   r0   r     s    
ztrigamma._eval_is_negativec                 C   s&   |  t}tjg| }|||||S r_   )r   r7   r   rF   r   r   r/   r/   r0   r     s    
ztrigamma._eval_aseriesc                 C   s
   t d|S rd   r   r   r/   r/   r0   rM     s    ztrigamma.evalc                 K   s   | j d }td|jddS )Nr   r3   Tr   r   r   r/   r/   r0   rO     s    
ztrigamma._eval_expand_funcc                 K   s
   t d|S )Nr<   r   rf   r/   r/   r0   r     s    ztrigamma._eval_rewrite_as_zetac                 K   s
   t d|S rd   r   rf   r/   r/   r0   r     s    z#trigamma._eval_rewrite_as_polygammac                 K   s   t |d d td d  S )Nr3   r<   r   )r#   r   rf   r/   r/   r0   r     s    z"trigamma._eval_rewrite_as_harmonicNr   c                 C   s   | j d }td||S rh   r   r   r/   r/   r0   rv     s    
ztrigamma._eval_as_leading_term)r3   )Nr   )rw   rx   ry   rz   r   r;   r\   r^   r   r   r|   rM   rO   r   r   r   rv   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<   rK   r3   )r   r   r6   r   r5   r7   r	   )r9   r:   r   rN   rE   rK   r/   r/   r0   r;   "  s    
.zmultigamma.fdiffc                 C   sh   ddl m} |jdks |jdkr(tdtd}t||d  d  |t|d| d  |d|f  S )	Nr   )ProductFz+Order parameter p must be positive integer.rK   r3      r<   )	Zsympy.concrete.productsr   r@   rY   r,   r   r   r2   Zdoit)rI   rN   rE   r   rK   r/   r/   r0   rM   +  s    &zmultigamma.evalc                 C   s   | j \}}| | |S r_   )r6   r5   rU   )r9   rN   rE   r/   r/   r0   rW   4  s    
zmultigamma._eval_conjugatec                 C   s^   | j \}}d| }|jr,||d kdkr,dS t|rD||d krDdS ||d ksV|jrZdS d S )Nr<   r3   TF)r6   rY   r1   rZ   )r9   rN   rE   yr/   r/   r0   r\   8  s    
zmultigamma._eval_is_realN)r<   )
rw   rx   ry   rz   r{   r;   r|   rM   rW   r\   r/   r/   r/   r0   r     s   8
	
r   N)Cmathr   Z
sympy.corer   r   r   r   Zsympy.core.exprr   Zsympy.core.functionr   r	   r
   Zsympy.core.logicr   r   Zsympy.core.numbersr   r   r   r   Zsympy.core.powerr   Z&sympy.functions.special.zeta_functionsr   r   r   r   r   Z$sympy.functions.elementary.complexesr   r   Z&sympy.functions.elementary.exponentialr   r   Z#sympy.functions.elementary.integersr   r   Z(sympy.functions.elementary.miscellaneousr   Z(sympy.functions.elementary.trigonometricr   r    r!   Z%sympy.functions.combinatorial.numbersr"   r#   Z(sympy.functions.combinatorial.factorialsr$   r%   r&   Zsympy.utilities.miscr'   Zmpmathr(   r)   Zmpmath.libmp.libmpfr*   r1   r2   r~   r   r7   r`   r   r   r   r/   r/   r/   r0   <module>   sB    = 1 '  / D^e