o
    8Va                     @   s  d dl mZmZmZmZmZmZmZ d dl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 d dlmZ d dlmZmZmZ d d	l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+m,Z,m-Z-m.Z. dd Z/G dd deZ0G dd deZ1G dd deZ2G dd deZ3G dd deZ4G dd deZ5G dd deZ6G d d! d!eZ7d"S )#    )AddSsympifyoopiDummyexpand_func)as_int)FunctionArgumentIndexError)	fuzzy_and	fuzzy_not)RationalPowzeta)erferfcEi)re)explog)ceilingfloor)sqrt)sincoscot)	bernoulliharmonic)	factorialrfRisingFactorialc                 C   s(   z	t | dd W dS  ty   Y dS w )NF)strictT)r	   
ValueError)n r'   I/usr/lib/python3/dist-packages/sympy/functions/special/gamma_functions.pyintlike   s   r)   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| |Nr+   r   )funcargs	polygammar   selfargindexr'   r'   r(   fdiffl   s    
zgamma.fdiffc                 C   s   |j rx|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rz|j	dkr|t
|j|j	 }|jr=|tj}}n|d  }}|d@ dkrMtj}ntj}tdd| dD ]}||9 }qX|jrm|ttj d|  S d| ttj | S d S d S d S )Nr+      r      )	is_Numberr   NaNInfinityr)   is_positiver!   ComplexInfinityis_RationalqabspOneNegativeOneranger   Pi)clsargr&   kcoeffir'   r'   r(   evalr   s4   




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   xr+   ZreevalF)r.   r;   r=   r>   r<   r   r-   _eval_expand_funcsubsr   is_AddZas_coeff_addr   Z_new_rawargsr#   )	r1   hintsrD   rI   r&   r>   rF   tailZintpartr'   r'   r(   rJ      s    
"zgamma._eval_expand_funcc                 C   s   |  | jd  S Nr   )r-   r.   	conjugater1   r'   r'   r(   _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)r.   is_nonpositive
is_integerr)   r9   is_nonintegerr1   rI   r'   r'   r(   _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)r.   r9   rU   r   is_evenrV   r'   r'   r(   _eval_is_positive   s   

zgamma._eval_is_positiveNc                 K      t t|S N)r   loggamma)r1   zZlimitvarkwargsr'   r'   r(   _eval_rewrite_as_tractable      z gamma._eval_rewrite_as_tractablec                 K   s   t |d S Nr+   r!   r1   r]   r^   r'   r'   r(   _eval_rewrite_as_factorial   r`   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   r+   )r.   limit
is_Integersuper_eval_nseriesr-   r"   )r1   rI   r&   logxcdirx0t	__class__r'   r(   ri      s
   .zgamma._eval_nseriesc           	      C   sr   ddl m} | jd }||d}|jr.|jr.| }d| | |d  }||| | S |js6| |S | )Nr   )	PoleErrorr+   )	sympyrp   r.   rK   rT   rS   r-   as_leading_termZis_infinite)	r1   rI   rj   rk   rp   rD   rl   r&   resr'   r'   r(   _eval_as_leading_term   s   

zgamma._eval_as_leading_termr+   r[   )r   rO   )__name__
__module____qualname____doc__
unbranchedr   r:   Z_singularitiesr3   classmethodrH   rJ   rR   rW   rY   r_   rd   ri   ru   __classcell__r'   r'   rn   r(   r*      s    L

!	
r*   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/

    r4   c                 C   s   ddl m}m} |dkr| j\}}t|| ||d   S |dkrE| j\}}t|t| t|t||  |g ddgdd|gg | S t	| |Nr   )meijerg
unpolarifyr4   r+   )
rr   r   r   r.   r   r*   digammar   
uppergammar   r1   r2   r   r   ar]   r'   r'   r(   r3     s   

 
zlowergamma.fdiffc              	      s  ddl m}m} tju rtjS  \}} jr* jr*|}|kr)t |S n7 jrL j	rL|dkrKdt
 | | d    t   t | S n|dkratdt
 | |   t | S  jr tju rrtjt  S  tju rt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sd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   Ir4   rq   r+   c                       g | ]
} | t | qS r'   rb   .0rE   rI   r'   r(   
<listcomp>H      z#lowergamma.eval.<locals>.<listcomp>c                    s(   g | ]} |t j  tt j|  qS r'   )r   Halfr*   r   r   r'   r(   r   J  s   ( c                    s0   g | ]}|  d   t   t  |  qS rv   r*   r   r   rI   r'   r(   r   M  s   0 r5   )rr   r   r   r   Zeroextract_branch_factorrT   r9   r~   rS   r   r!   r   r6   r?   r   r   r   rg   r   rA   r*   r   is_zero)rC   r   rI   r   r   nxr&   br'   r   r(   rH     s>   

."

4H\zlowergamma.evalc                 C   s   ddl m}m} ddlm} tdd | jD rI| jd |}| jd |}|| ||d|}W d    n1 s>w   Y  |	||S | S )Nr   mpworkprecExprc                 s       | ]}|j V  qd S r[   	is_numberr   rI   r'   r'   r(   	<genexpr>U      z)lowergamma._eval_evalf.<locals>.<genexpr>r+   )
mpmathr   r   rr   r   allr.   
_to_mpmathgammainc_from_mpmathr1   precr   r   r   r   r]   rt   r'   r'   r(   _eval_evalfR  s   
zlowergamma._eval_evalfc                 C   8   | j d }|tjtjfvr| | j d  | S d S r,   r.   r   r   NegativeInfinityr-   rP   rV   r'   r'   r(   rR   ^     
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 r[   )	r.   r   _eval_is_meromorphicrK   rT   r9   Z	is_finiter   r   )r1   rI   r   sr]   Z
args_meromz0Zs0r'   r'   r(   r   c  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 )r+   N)r"   r   r   r]   r'   r(   r   y  s   $ z+lowergamma._eval_aseries.<locals>.<genexpr>r+   )rr   r   r.   r   r8   Zhasr   sumrA   rh   _eval_aseries)	r1   r&   args0rI   rj   r   rF   Zsum_exprorn   r   r(   r   t  s   
 zlowergamma._eval_aseriesc                 K      t |t|| S r[   )r*   r   r1   r   rI   r^   r'   r'   r(   _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)rr   r   rT   rS   rewriter   r1   r   rI   r^   r   r'   r'   r(   _eval_rewrite_as_expint  s   z"lowergamma._eval_rewrite_as_expintc                 C   s   | j d }|jr
dS d S )Nr+   T)r.   r   rV   r'   r'   r(   _eval_is_zero  s   
zlowergamma._eval_is_zeror4   )rw   rx   ry   rz   r3   r|   rH   r   rR   r   r   r   r   r   r}   r'   r'   rn   r(   r~      s    
7
2
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

    r4   c                 C   s   ddl m}m} |dkr| j\}}t||  ||d   S |dkr>| j\}}t||t| |g ddgdd|gg | S t| |r   )rr   r   r   r.   r   r   r   r   r   r'   r'   r(   r3     s   

,
zuppergamma.fdiffc                 C   s   ddl m}m} ddlm} tdd | jD rJ| jd |}| jd |}|| ||||j	}W d    n1 s?w   Y  |
||S | S )Nr   r   r   c                 s   r   r[   r   r   r'   r'   r(   r     r   z)uppergamma._eval_evalf.<locals>.<genexpr>r+   )r   r   r   rr   r   r   r.   r   r   infr   r   r'   r'   r(   r     s   
zuppergamma._eval_evalfc           	         s  ddl m}m}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rC jrC|}|krBt |S nI jre jre|dkrddt | | d    t   t | S n'|dkrt dtdt | |     tdt | |   t |  S  jrT tju rjrt  S  tju rt S  tju rtttt S  jsd  jrT d }|jr jrt t| tfddt D   S t tt d td	d   t  t t fd
dt tj D    S |jr!|| ||d   S  jsTdtj   t tt td     t  t fddttj  D    S  j	rbjrbt  S j	rpt
 jrrt S d S d S )Nr   )r   r   r   rq   r+   r4   c                    r   r'   rb   r   r]   r'   r(   r   	  r   z#uppergamma.eval.<locals>.<listcomp>r5   c                    s2   g | ]}t tj |  |  t d    qS rv   )r*   r   r   r   r   r]   r'   r(   r     s   2 c                    s,   g | ]}| t   t  | d   qS rv   r   r   r   r'   r(   r        , )rr   r   r   r   r6   r   r7   r8   r   r   r   r9   r*   r   rT   r   rS   r   r!   r   r   r?   r   r   r   rg   r   rA   )	rC   r   r]   r   r   r   r   r&   r   r'   r   r(   rH     sV   



.F


,\^zuppergamma.evalc                 C   r   r,   r   r1   r]   r'   r'   r(   rR     r   zuppergamma._eval_conjugatec                 C   s   t | ||S r[   )r~   r   )r1   rI   r   r'   r'   r(   r     s   zuppergamma._eval_is_meromorphicc                 K   r   r[   )r*   r~   r   r'   r'   r(   _eval_rewrite_as_lowergamma   r   z&uppergamma._eval_rewrite_as_lowergammac                 K   s   t t|t|| S r[   )r   r\   r~   r   r'   r'   r(   r_   #  s   z%uppergamma._eval_rewrite_as_tractablec                 K   s"   ddl m} |d| |||  S )Nr   r   r+   )rr   r   r   r'   r'   r(   r   &  s   z"uppergamma._eval_rewrite_as_expintNr   )rw   rx   ry   rz   r3   r   r|   rH   rR   r   r   r_   r   r'   r'   r'   r(   r     s    
A
2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 )r/   a*
  
    The function ``polygamma(n, z)`` returns ``log(gamma(z)).diff(n + 1)``.

    Explanation
    ===========

    It is a meromorphic function on $\mathbb{C}$ and defined as the $(n+1)$-th
    derivative of the logarithm of the gamma function:

    .. math::
        \psi^{(n)} (z) := \frac{\mathrm{d}^{n+1}}{\mathrm{d} z^{n+1}} \log\Gamma(z).

    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 rO   )r.   r   is_realrT   intis_nonnegativerh   r   )r1   r   r&   rn   r'   r(   r     s   
zpolygamma._eval_evalfr4   c                 C   s2   |dkr| j d d \}}t|d |S t| |Nr4   r+   )r.   r/   r   )r1   r2   r&   r]   r'   r'   r(   r3     s   
zpolygamma.fdiffc                 C   s$   | j d jr| j d jrdS d S d S )Nr   r+   T)r.   r9   rQ   r'   r'   r(   rW     s   zpolygamma._eval_is_realc                 C   s,   | j d }t|j|jg}t|jt|gS ra   )r.   r   is_negativerT   Z
is_complexr   )r1   r]   Zis_negative_integerr'   r'   r(   _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 re   )r.   r9   is_oddrQ   r'   r'   r(   rY        zpolygamma._eval_is_positivec                 C   r   re   )r.   r9   rX   rQ   r'   r'   r(   _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   Orderr+   r4   c                    s,   g | ]}t d | d |  d |    qS r   r   r   r   r'   r(   r     r   z+polygamma._eval_aseries.<locals>.<listcomp>rq   )rr   r   r   r.   rg   r   rh   r   r   r   rA   r   ri   r*   r   )r1   r&   r   rI   rj   r   Nrr   mlZfacZe0rE   rn   r   r(   r     s@   



8"zpolygamma._eval_aseriesc                 C   s  t t||f\}}ddlm} |jr|jr"||}||kr"t||S |jrB|tj	u rBd|d  t
| d|d  d  t|d  S |tju rKt|S |jr|tju rVtjS |tju rn|jrg|jrdtjS tjS |jrmtjS n+|jr|jrwtjS |jrtj t|d d S |jrd|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 )	Nr   )r   rq   r+   r4      F)Zevaluate)mapr   rr   r   rT   r   r/   r9   r   r   r!   r   r@   r\   r6   r7   r8   r   r   rg   rS   r:   
EulerGammar    r   r;   as_numer_denomr   r   Zextract_multiplicativelyImaginaryUnit)rC   r&   r]   r   Znzr>   r<   rm   r'   r'   r(   rH     sZ   

0


"
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dt  D   }t  d t |  S n;j	r
 \  jr jr fddtd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   r+   c                    s   g | ]	}t |  qS r'   r   r   rG   er]   r'   r(   r     
    z/polygamma._eval_expand_func.<locals>.<listcomp>c                    s   g | ]	}t |  qS r'   r   r   r   r'   r(   r      r   rq   c                    s    g | ]}t t|  qS r'   )r/   r   r   )rF   r&   r]   r'   r(   r   '  s
    r4   c                    s<   g | ]}t d | t    td t|t    qS r   )r   r   r   r   r   )r>   r<   r'   r(   r   5     < c                    s   g | ]}d  |  qS rv   r'   r   r   r'   r(   r   :  s    c                    s   g | ]
}d  d  |  qS rv   r'   r   r   r'   r(   r   >  r   )r.   rg   r   rL   r   rA   r   r/   r!   Zis_MulZas_two_termsr9   r   r;   r   r   r   r   r   r   )r1   rM   rN   Zpart_1r'   )rF   r   r&   r>   r<   r]   r   r(   rJ     sN   




"
(
zpolygamma._eval_expand_funcc                 K   s<   |j r|tj jrd|d  t| t|d | S d S d S )Nrq   r+   )rT   r   r?   r   r!   r   r1   r&   r]   r^   r'   r'   r(   _eval_rewrite_as_zetaB  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 ra   )rT   r   r    r   r   r@   r!   r   r   r'   r'   r(   _eval_rewrite_as_harmonicG  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'   )rs   )r   r   r   r'   r(   r   P  s    z3polygamma._eval_as_leading_term.<locals>.<listcomp>r+   )rr   r   r.   containsZgetnr   r-   )r1   rI   rj   rk   r   r&   r]   r   r'   r   r(   ru   N  s   
zpolygamma._eval_as_leading_termr   rO   )rw   rx   ry   rz   r   r3   rW   r   rY   r   r   r|   rH   rJ   r   r   ru   r}   r'   r'   rn   r(   r/   /  s    b
)
9.r/   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*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   t |}|jr|jrtjS |jrtt|S n*|jrA|	 \}}|jrA|dkrAtt
tjdd|   t| t|d tj  S |tju rItjS t|tju rStjS |tju r[tjS d S r   )r   rT   rS   r   r8   r9   r   r*   Zis_rationalr   r   rB   r   r=   r:   r7   )rC   r]   r>   r<   r'   r'   r(   rH     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   SumrE   r+   )rr   r   r.   r;   r   r9   r   r\   r   r   r   rB   r   r   )r1   rM   r   r]   r>   r<   r&   rE   r'   r'   r(   rJ     s   
8Fzloggamma._eval_expand_funcNr   c                    sB   | j d |d}|jr| j| j  }||||S t |||S rO   )r.   rf   r   _eval_rewrite_as_intractableri   rh   )r1   rI   r&   rj   rk   rl   frn   r'   r(   ri     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   r4   c                    s<   g | ]}t d | d | d | d   d | d    qS )r4   r+   r   r   r   r'   r(   r     r   z*loggamma._eval_aseries.<locals>.<listcomp>r+   )rr   r   r   rh   r   r.   r   r   r   r   rA   r   ri   )	r1   r&   r   rI   rj   r   r   r   r   rn   r   r(   r     s   
&zloggamma._eval_aseriesc                 K   rZ   r[   )r   r*   rc   r'   r'   r(   r     r`   z%loggamma._eval_rewrite_as_intractablec                 C   s"   | j d }|jr
dS |jrdS d S )Nr   TF)r.   r9   rS   r   r'   r'   r(   rW   
  s   
zloggamma._eval_is_realc                 C   s,   | j d }|tjtjfvr| | S d S rO   r   r   r'   r'   r(   rR     s   
zloggamma._eval_conjugater+   c                 C   s"   |dkrt d| jd S t| |r,   )r/   r.   r   r0   r'   r'   r(   r3     s   
zloggamma.fdiffrO   rv   )rw   rx   ry   rz   r|   rH   rJ   ri   r   r   rW   rR   r3   r}   r'   r'   rn   r(   r\   X  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   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d||S rO   r.   r/   Zevalfr1   r   r]   r'   r'   r(   r   M     
zdigamma._eval_evalfr+   c                 C   s   | j d }td| S rO   r.   r/   r3   r1   r2   r]   r'   r'   r(   r3   Q     
zdigamma.fdiffc                 C      | j d }td|jS rO   r.   r/   r   r   r'   r'   r(   rW   U     
zdigamma._eval_is_realc                 C   r   rO   r.   r/   r9   r   r'   r'   r(   rY   Y  r   zdigamma._eval_is_positivec                 C   r   rO   r.   r/   r   r   r'   r'   r(   r   ]  r   zdigamma._eval_is_negativec                 C   &   |  t}tjg| }|||||S r[   )r   r/   r   r   r   r1   r&   r   rI   rj   Zas_polygammar'   r'   r(   r   a     
zdigamma._eval_aseriesc                 C   
   t d|S rO   r/   rC   r]   r'   r'   r(   rH   f     
zdigamma.evalc                 K   s   | j d }td|jddS )Nr   Tr-   r.   r/   expandr1   rM   r]   r'   r'   r(   rJ   j     
zdigamma._eval_expand_funcc                 K   s   t |d tj S ra   )r    r   r   rc   r'   r'   r(   r   n  r   z!digamma._eval_rewrite_as_harmonicc                 K   r   rO   r   rc   r'   r'   r(   _eval_rewrite_as_polygammaq     
z"digamma._eval_rewrite_as_polygammaNr   c                 C   s   | j d }td||S rO   r.   r/   rs   r1   rI   rj   rk   r]   r'   r'   r(   ru   t  r   zdigamma._eval_as_leading_termrv   rO   )rw   rx   ry   rz   r   r3   rW   rY   r   r   r|   rH   rJ   r   r  ru   r'   r'   r'   r(   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d||S re   r   r   r'   r'   r(   r     r   ztrigamma._eval_evalfr+   c                 C   s   | j d }td| S re   r   r   r'   r'   r(   r3     r   ztrigamma.fdiffc                 C      | j d }td|jS re   r   r   r'   r'   r(   rW     r   ztrigamma._eval_is_realc                 C   r  re   r   r   r'   r'   r(   rY     r   ztrigamma._eval_is_positivec                 C   r  re   r   r   r'   r'   r(   r     r   ztrigamma._eval_is_negativec                 C   r   r[   )r   r/   r   r?   r   r   r'   r'   r(   r     r   ztrigamma._eval_aseriesc                 C   r   ra   r   r   r'   r'   r(   rH     r   ztrigamma.evalc                 K   s   | j d }td|jddS )Nr   r+   Tr   r   r   r'   r'   r(   rJ     r  ztrigamma._eval_expand_funcc                 K   r   )Nr4   r   rc   r'   r'   r(   r     r  ztrigamma._eval_rewrite_as_zetac                 K   r   ra   r   rc   r'   r'   r(   r    r  z#trigamma._eval_rewrite_as_polygammac                 K   s   t |d d tjd d  S )Nr+   r4      )r    r   rB   rc   r'   r'   r(   r     s   z"trigamma._eval_rewrite_as_harmonicNr   c                 C   s   | j d }td||S re   r  r  r'   r'   r(   ru     r   ztrigamma._eval_as_leading_termrv   rO   )rw   rx   ry   rz   r   r3   rW   rY   r   r   r|   rH   rJ   r   r  r   ru   r'   r'   r'   r(   r  z  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

    Tr4   c                 C   s^   ddl m} |dkr*| j\}}td}| |||td|d| d  |d|f S t| |)Nr   r   r4   rE   r+   )rr   r   r.   r   r-   r/   r   )r1   r2   r   rI   r>   rE   r'   r'   r(   r3     s   
.
zmultigamma.fdiffc                 C   sz   ddl m} tt||f\}}|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.rE   r+      r4   )rr   r
  r   r   r9   rT   r%   r   r   r*   Zdoit)rC   rI   r>   r
  rE   r'   r'   r(   rH   "  s   &zmultigamma.evalc                 C   s   | j \}}| | |S r[   )r.   r-   rP   )r1   rI   r>   r'   r'   r(   rR   ,  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 )Nr4   r+   TF)r.   rT   r)   rU   )r1   rI   r>   yr'   r'   r(   rW   0  s   
zmultigamma._eval_is_realNr   )
rw   rx   ry   rz   r{   r3   r|   rH   rR   rW   r'   r'   r'   r(   r	    s    8
	
	r	  N)8Z
sympy.corer   r   r   r   r   r   r   Zsympy.core.compatibilityr	   Zsympy.core.functionr
   r   Zsympy.core.logicr   r   Zsympy.core.numbersr   Zsympy.core.powerr   Z&sympy.functions.special.zeta_functionsr   Z'sympy.functions.special.error_functionsr   r   r   Z$sympy.functions.elementary.complexesr   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#   r)   r*   r~   r   r/   r\   r   r  r	  r'   r'   r'   r(   <module>   s<   $  ? 4 #  + F]d