o
    §ƒðj¿§  ã                   @   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)ÚDefinedFunctionÚArgumentIndexErrorÚ	PoleError)Ú	fuzzy_andÚ	fuzzy_not)ÚRationalÚpiÚooÚI©ÚPow©Úzeta)ÚerfÚerfcÚEi)ÚreÚ
unpolarify)ÚexpÚlog)ÚceilingÚfloor)Úsqrt)ÚsinÚcosÚcot)Ú	bernoulliÚharmonic)Ú	factorialÚrfÚRisingFactorial)Úas_int)ÚmpÚworkprec)Úprec_to_dpsc                 C   s(   z	t | dd� W dS  ty   Y dS w )NF)ÚstrictT)r'   Ú
ValueError©Ún© r/   ú©/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/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„ 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.
    sympy.functions.special.beta_functions.beta: Euler Beta function.

    References
    ==========

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

    Té   c                 C   s2   |dkr|   | jd ¡td| jd ƒ S t| |ƒ‚©Nr3   r   )ÚfuncÚargsÚ	polygammar	   ©ÚselfÚargindexr/   r/   r0   Úfdiffr   s    
zgamma.fdiffc                 C   sî   |j rq|tju rtjS |tu rtS t|ƒr!|jrt|d ƒS tjS |jrs|j	dkrut
|jƒ|j	 }|jr;|tj}}n|d  }}|d@ dkrKtj}ntj}|ttdd| dƒƒ9 }|jrg|ttƒ d|  S d| ttƒ | S d S d 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   s2   

ã
z
gamma.evalc           	      K   sÐ   | j d }|jr3t|jƒ|jkr3tdƒ}|j|j }|j||j  }|  || ¡ ¡  |t	||jƒ¡S |j
rb| ¡ \}}|rP|jdkrPt|ƒ}|| f| }|}|j|ddiŽ}|  |¡t||ƒ S | j| j Ž S )Nr   Úxr3   ÚreevalF)r6   rB   rD   rE   rC   r   r5   Ú_eval_expand_funcÚsubsr   Úis_AddÚas_coeff_addr   Ú_new_rawargsr&   )	r9   ÚhintsrJ   rN   r.   rE   rL   ÚtailÚintpartr/   r/   r0   rP   ™   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@   r^   r   Úis_evenr_   r/   r/   r0   Ú_eval_is_positive¹   s   

ÿzgamma._eval_is_positiveNc                 K   ó   t t|ƒƒS ©N)r   Úloggamma)r9   ÚzÚlimitvarÚkwargsr/   r/   r0   Ú_eval_rewrite_as_tractableÀ   ó   z gamma._eval_rewrite_as_tractablec                 K   s   t |d ƒS ©Nr3   ©r$   ©r9   rf   rh   r/   r/   r0   Ú_eval_rewrite_as_factorialÃ   rj   z gamma._eval_rewrite_as_factorialr   c                    sl   | j d  |d¡}|jr|dkstƒ  |||¡S | j d | }|  |d ¡t| j d | d ƒ  |||¡S ©Nr   r3   )r6   ÚlimitÚ
is_IntegerÚsuperÚ_eval_nseriesr5   r%   )r9   rN   r.   ÚlogxÚcdirÚx0Út©Ú	__class__r/   r0   rs   Æ   s
   .zgamma._eval_nseriesc                 C   sh   | j d }| |d¡}|jr)|jr)| }tj| |  |d ¡ }|||  |¡ S |js1|  |¡S t	ƒ ‚ro   )
r6   rQ   r]   r\   r   rG   r5   Úas_leading_termÚis_infiniter
   )r9   rN   rt   ru   rJ   rv   r.   Úresr/   r/   r0   Ú_eval_as_leading_termÍ   s   

zgamma._eval_as_leading_term©r3   rd   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
unbranchedr   rA   Ú_singularitiesr;   ÚclassmethodrM   rP   r[   r`   rb   ri   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.
    sympy.functions.special.beta_functions.beta: Euler Beta function.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Incomplete_gamma_function#Lower_incomplete_gamma_function
    .. [2] Abramowitz, Milton; Stegun, Irene A., eds. (1965), Chapter 6,
           Section 5, Handbook of Mathematical Functions with Formulas, Graphs,
           and Mathematical Tables
    .. [3] https://dlmf.nist.gov/8
    .. [4] https://functions.wolfram.com/GammaBetaErf/Gamma2/
    .. [5] https://functions.wolfram.com/GammaBetaErf/Gamma3/

    r<   c                 C   s�   ddl m} |dkr| j\}}tt|ƒ ƒ||d   S |dkrC| j\}}t|ƒt|ƒ t|ƒt||ƒ  |g ddgdd|gg |ƒ S t	| |ƒ‚©Nr   )Úmeijergr<   r3   )
Úsympy.functions.special.hyperr‰   r6   r   r   r2   Údigammar   Ú
uppergammar	   ©r9   r:   r‰   Úarf   r/   r/   r0   r;     s   

 ÿ
zlowergamma.fdiffc              	      s  ˆt ju rt jS ˆ ¡ \}}ˆ jr"ˆ jr"tˆƒ}|ˆkr!tˆ |ƒS n8ˆ jrEˆ jrE|dkrDdt t	 | t j
ˆ    tˆ  ƒ tˆ |ƒ S n|dkrZtdt t	 | ˆ  ƒtˆ |ƒ S ˆ jrûˆ t ju rjt jtˆ ƒ S ˆ t ju ryttƒttˆƒƒ S ˆ js�dˆ  jrûˆ d }|jrÉˆ jr¥t|ƒ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                    ó   g | ]
}ˆ | t |ƒ ‘qS r/   rl   ©Ú.0rK   ©rN   r/   r0   Ú
<listcomp>K  ó    z#lowergamma.eval.<locals>.<listcomp>c                    s(   g | ]}ˆ |t j  tt j| ƒ ‘qS r/   )r   ÚHalfr2   r�   r’   r/   r0   r“   M  s   ( c                    s0   g | ]}ˆ|ˆ  d   t ˆ ƒ t ˆ | ƒ ‘qS r~   ©r2   r�   ©rŽ   rN   r/   r0   r“   P  s   0 r=   )r   ÚZeroÚextract_branch_factorr]   r@   r   r‡   r\   r   r   rG   r$   r   r>   rF   r•   r   r   rq   r   rH   r2   r   Úis_zero)rI   rŽ   rN   Únxr.   Úbr/   r—   r0   rM   #  s<   

ÿ0ÿ"

4H^ÿzlowergamma.evalc                 C   sz   t dd„ | jD ƒƒr;| jd  |¡}| jd  |¡}t|ƒ� t |d|¡}W d   ƒ n1 s0w   Y  t ||¡S | S )Nc                 s   ó   � | ]}|j V  qd S rd   ©Ú	is_number©r‘   rN   r/   r/   r0   Ú	<genexpr>V  ó   € z)lowergamma._eval_evalf.<locals>.<genexpr>r   r3   )Úallr6   Ú
_to_mpmathr)   r(   Úgammaincr   Ú_from_mpmath©r9   ÚprecrŽ   rf   r|   r/   r/   r0   Ú_eval_evalfU  s   
ÿzlowergamma._eval_evalfc                 C   ó8   | j d }|tjtjfvr|  | j d  ¡ | ¡ ¡S d S r4   ©r6   r   r˜   ÚNegativeInfinityr5   rY   r_   r/   r/   r0   r[   _  ó   
ÿzlowergamma._eval_conjugatec                 C   st   | j \}}t| ||¡| ||¡gƒ}|s|S | ||¡}|jr(t|j|jgƒS | ||¡}t|j|jt|jƒgƒS rd   )	r6   r   Ú_eval_is_meromorphicrQ   r]   r@   Ú	is_finiter   rš   )r9   rN   rŽ   Úsrf   Ú
args_meromÚz0Ús0r/   r/   r0   r®   d  s   

ÿzlowergamma._eval_is_meromorphicc           	         s’   ddl m} | j\‰ ‰|d t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°   rf   r/   r0   r¡   z  s   €$ z+lowergamma._eval_aseries.<locals>.<genexpr>r3   )
Úsympy.series.orderr´   r6   r   Úhasr   ÚsumrH   rr   Ú_eval_aseries)	r9   r.   Úargs0rN   rt   r´   rL   Úsum_exprÚorx   rµ   r0   r¹   u  s   
 zlowergamma._eval_aseriesc                 K   ó   t |ƒt||ƒ S rd   )r2   rŒ   ©r9   r°   rN   rh   r/   r/   r0   Ú_eval_rewrite_as_uppergamma  ó   z&lowergamma._eval_rewrite_as_uppergammac                 K   s,   ddl m} |jr|jr| S |  t¡ |¡S )Nr   ©Úexpint)Ú'sympy.functions.special.error_functionsrÂ   r]   r\   ÚrewriterŒ   ©r9   r°   rN   rh   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<   )r   r€   r�   r‚   r;   r…   rM   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.
    sympy.functions.special.beta_functions.beta: Euler Beta function.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Incomplete_gamma_function#Upper_incomplete_gamma_function
    .. [2] Abramowitz, Milton; Stegun, Irene A., eds. (1965), Chapter 6,
           Section 5, Handbook of Mathematical Functions with Formulas, Graphs,
           and Mathematical Tables
    .. [3] https://dlmf.nist.gov/8
    .. [4] https://functions.wolfram.com/GammaBetaErf/Gamma2/
    .. [5] https://functions.wolfram.com/GammaBetaErf/Gamma3/
    .. [6] https://en.wikipedia.org/wiki/Exponential_integral#Relation_with_other_functions

    r<   c                 C   s‚   ddl m} |dkr| j\}}tt|ƒ ƒ ||d   S |dkr<| j\}}t||ƒt|ƒ |g ddgdd|gg |ƒ S t| |ƒ‚rˆ   )rŠ   r‰   r6   r   r   rŒ   r   r	   r�   r/   r/   r0   r;   Ð  s   

,
zuppergamma.fdiffc                 C   s|   t dd„ | jD ƒƒr<| jd  |¡}| jd  |¡}t|ƒ� t ||tj¡}W d   ƒ n1 s1w   Y  t ||¡S | S )Nc                 s   r�   rd   rž   r    r/   r/   r0   r¡   Ü  r¢   z)uppergamma._eval_evalf.<locals>.<genexpr>r   r3   )	r£   r6   r¤   r)   r(   r¥   Úinfr   r¦   r§   r/   r/   r0   r©   Û  s   
ÿzuppergamma._eval_evalfc                    sä  ddl m} ˆjr$ˆtju rtjS ˆtu rtjS ˆjr$tˆ ƒj	r$t
ˆ ƒS ˆ ¡ \}}ˆ jr>ˆ j	r>tˆƒ}ˆ|kr=tˆ |ƒS nJˆ jraˆ jra|dkr`dt t | tjˆ    tˆ  ƒ tˆ |ƒ S n'|dkrˆt
ˆ ƒdtdt t | ˆ  ƒ  tdt t | ˆ  ƒtˆ |ƒ  S ˆ j�rRˆ tju ršˆj	rštˆ ƒ S ˆ tju r¤tˆ ƒS ˆ tju r³ttƒttˆƒƒ S ˆ js¼dˆ  j�rRˆ d }|j	�rˆ jrÝtˆ ƒt|ƒ t‡fdd„tˆ ƒD ƒŽ  S t
ˆ ƒttˆƒƒ tjˆ tdƒd   tˆ ƒ tˆƒ t‡ ‡fd	d„tˆ tj ƒD ƒŽ   S |j�r|| ˆƒtˆƒ|d   S ˆ j�sRtjtjˆ   t ttˆƒƒ t
dˆ  ƒ ˆˆ  tˆ ƒ t‡ ‡fd
d„ttjˆ  ƒD ƒŽ   S ˆ j�r`ˆj	�r`tˆ ƒ S ˆj�rntˆ ƒj	�rpt
ˆ ƒS d S d S )Nr   rÁ   éþÿÿÿr3   r<   c                    r�   r/   rl   r�   ©rf   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Ž   rf   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™   r]   r   rŒ   r\   r   r   rG   r$   r   r   rF   r•   r   r   rq   r   rH   )rI   rŽ   rf   rÂ   r›   r.   rœ   r/   rÌ   r0   rM   ä  sn   


ÿ0ÿF



ÿ&ÿÿÿ*ÿÿÿzuppergamma.evalc                 C   rª   r4   r«   ©r9   rf   r/   r/   r0   r[     r­   zuppergamma._eval_conjugatec                 C   s   t  | ||¡S rd   )r‡   r®   )r9   rN   rŽ   r/   r/   r0   r®   "  s   zuppergamma._eval_is_meromorphicc                 K   r½   rd   )r2   r‡   r¾   r/   r/   r0   Ú_eval_rewrite_as_lowergamma%  rÀ   z&uppergamma._eval_rewrite_as_lowergammac                 K   s   t t|ƒƒt||ƒ S rd   )r   re   r‡   r¾   r/   r/   r0   ri   (  s   z%uppergamma._eval_rewrite_as_tractablec                 K   s"   ddl m} |d| |ƒ||  S )Nr   rÁ   r3   )rÃ   rÂ   rÅ   r/   r/   r0   rÆ   +  s   z"uppergamma._eval_rewrite_as_expintNrÈ   )r   r€   r�   r‚   r;   r©   r…   rM   r[   r®   rÎ   ri   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„ 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.
    sympy.functions.special.beta_functions.beta: Euler Beta function.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Polygamma_function
    .. [2] https://mathworld.wolfram.com/PolygammaFunction.html
    .. [3] https://functions.wolfram.com/GammaBetaErf/PolyGamma/
    .. [4] https://functions.wolfram.com/GammaBetaErf/PolyGamma2/
    .. [5] O. Espinosa and V. Moll, "A generalized polygamma function",
           *Integral Transforms and Special Functions* (2004), 101-115.

    c                 C   st  |t ju s
|t ju rt jS |tu r|jrtS t jS |jr"|jr"t jS |t ju r3t	|ƒt
dt ƒd  S |jrn|t u sE| t¡tt fv rGtS |jrSt|d ƒt j S |jrj| ¡ \}}|dkrlttt j|dd�ƒS d S d S |jr´|jr¶t|ƒ}||kr�t||ƒS |jr–t j|d  t|ƒ t|d |ƒ S |t ju r¸t j|d  t|ƒ d|d  d  t|d ƒ S d S d S d S )Nr<   r3   é   F)Úevaluate)r   r?   r   rš   r˜   rq   r\   rA   rG   re   r   r   Úextract_multiplicativelyr   r#   Ú
EulerGammarB   Úas_numer_denomr   r7   r]   Úis_nonnegativer   r$   r   r•   )rI   r.   rf   rE   rC   Únzr/   r/   r0   rM   Ÿ  s:   
û
$
2ùzpolygamma.evalc                 C   s$   | j d jr| j d jrdS d S d S )Nr   r3   T)r6   r@   rZ   r/   r/   r0   r`   ½  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   rf   Úis_negative_integerr/   r/   r0   Ú_eval_is_complexÁ  s   
zpolygamma._eval_is_complexc                 C   s<   | j \}}|jr|jr|jrdS |jr|jrdS d S d S d S ©NTF)r6   r@   Úis_oddÚis_realra   ©r9   r.   rf   r/   r/   r0   rb   Æ  ó   
üzpolygamma._eval_is_positivec                 C   s<   | j \}}|jr|jr|jrdS |jr|jrdS d S d S d S rÚ   )r6   r@   ra   rÛ   rÜ   rÝ   r/   r/   r0   Ú_eval_is_negativeÎ  rÞ   zpolygamma._eval_is_negativec                    sˆ  | j \‰‰ˆjr’ˆjr’ˆjrXˆj d ‰ ˆ jrWˆd  ‰ˆ dkr3t‡‡fdd„tdtˆ ƒd ƒD ƒŽ }nt‡‡fdd„ttˆ  ƒƒD ƒŽ  }tˆˆˆ  ƒtj	ˆ t
ˆƒ |  S n:ˆjr’ˆ ¡ \‰ ‰ˆ jrŽˆ jrŽ‡ ‡‡fdd„ttˆ ƒƒD ƒ}ˆdk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rÝtˆƒ‰ˆˆ ‰|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u �sˆjdu �r?tdƒ}t|ˆƒ |¡ |ˆd ¡}|tjtˆ ƒ tˆd ˆƒ  tˆ ƒ S tˆˆƒS )Nr   r3   c                    s   g | ]	}t ˆ| ˆ ƒ‘qS r/   r   ©r‘   Úi©Úerf   r/   r0   r“   ß  ó
    ÿÿz/polygamma._eval_expand_func.<locals>.<listcomp>c                    s   g | ]	}t ˆ| ˆ ƒ‘qS r/   r   rà   râ   r/   r0   r“   â  rä   c                    s    g | ]}t ˆˆt|ˆ ƒ ƒ‘qS r/   )r7   r   rà   )rL   r.   rf   r/   r0   r“   é  s
    ÿÿr<   c                    s<   g | ]}t d | t ˆ  ˆ ƒtd t|t ˆ ƒ ƒ ‘qS rÈ   )r    r   r   r   r�   )rE   rC   r/   r0   r“   ÷  ó   < c                    s   g | ]}d ˆ |  ‘qS r~   r/   r�   ©r²   r/   r0   r“   ü  s    c                    s   g | ]
}d ˆ d  |  ‘qS r~   r/   r�   ræ   r/   r0   r“      r”   éÿÿÿÿFr°   )r6   rq   rÔ   rR   r   rH   Úintr7   r   rG   r$   Úis_MulÚas_two_termsr@   r   rB   rÓ   rÒ   r   r!   r   re   r]   r   r   ÚdiffrQ   r‹   r2   )r9   rU   rV   Úpart_1r°   Údztr/   )rL   rã   r.   rE   rC   rf   r²   r0   rP   Ö  sZ   



ÿ
ÿ$ø

ÿ(ÿ
,
zpolygamma._eval_expand_funcc                 K   s8   |j r|jrtj|d  t|ƒ t|d |ƒ S d S d S rk   )r]   r@   r   rG   r$   r   ©r9   r.   rf   rh   r/   r/   r0   Ú_eval_rewrite_as_zeta  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 rk   )r]   rš   r#   r   rÒ   rG   r$   r   rî   r/   r/   r0   Ú_eval_rewrite_as_harmonic  s
   4üz#polygamma._eval_rewrite_as_harmonicc                    sp   ddl m} ‡ fdd„| jD ƒ\}}||ˆ ƒ}|dkr2| dˆ  ¡r2|d u r*tˆ ƒn|}| ¡ | S |  ||¡S )Nr   ©ÚOrderc                    s   g | ]}|  ˆ ¡‘qS r/   )rz   )r‘   rŽ   r’   r/   r0   r“     s    z3polygamma._eval_as_leading_term.<locals>.<listcomp>r3   )r¶   rò   r6   Úcontainsr   Úgetnr5   )r9   rN   rt   ru   rò   r.   rf   r¼   r/   r’   r0   r}     s   
zpolygamma._eval_as_leading_termr<   c                 C   s2   |dkr| j d d… \}}t|d |ƒS t| |ƒ‚©Nr<   r3   )r6   r7   r	   )r9   r:   r.   rf   r/   r/   r0   r;      s   
zpolygamma.fdiffc                    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   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   rq   rÔ   rr   r¹   r   r   rH   r   rs   r2   r"   )r9   r.   rº   rN   rt   rò   ÚNÚrr¼   ÚmÚlÚfacÚe0rK   rx   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 |¡r,|dkr,tjS t|d ƒ�< t |¡rC|dkrCt ||¡}n$t 	|d |¡}t 	|d |d¡}|tj
t | ¡ |  t | ¡ }W d   ƒ n1 sqw   Y  t ||¡S )Nc                 s   r�   rd   rž   rà   r/   r/   r0   r¡   Q  r¢   z(polygamma._eval_evalf.<locals>.<genexpr>r   é   r3   )r£   r6   r¤   r(   Úisintr   rA   r)   r7   r   Úeulerr‹   Úrgammar   r¦   )r9   r¨   r°   rf   r|   Úztrí   r/   r/   r0   r©   P  s   &€úzpolygamma._eval_evalfrÈ   )r   r€   r�   r‚   r…   rM   r`   rÙ   rb   rß   rP   rï   rð   r}   r;   r¹   r©   r†   r/   r/   rx   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 )re   aõ
  
    The ``loggamma`` function implements the logarithm of the
    gamma function (i.e., $\log\Gamma(x)$).

    Examples
    ========

    Several special values are known. For numerical integral
    arguments we have:

    >>> from sympy import loggamma
    >>> loggamma(-2)
    oo
    >>> loggamma(0)
    oo
    >>> loggamma(1)
    0
    >>> loggamma(2)
    0
    >>> loggamma(3)
    log(2)

    And for symbolic values:

    >>> from sympy import Symbol
    >>> n = Symbol("n", integer=True, positive=True)
    >>> loggamma(n)
    log(gamma(n))
    >>> loggamma(-n)
    oo

    For half-integral values:

    >>> from sympy import S
    >>> loggamma(S(5)/2)
    log(3*sqrt(pi)/4)
    >>> loggamma(n/2)
    log(2**(1 - n)*sqrt(pi)*gamma(n)/gamma(n/2 + 1/2))

    And general rational arguments:

    >>> from sympy import expand_func
    >>> L = loggamma(S(16)/3)
    >>> expand_func(L).doit()
    -5*log(3) + loggamma(1/3) + log(4) + log(7) + log(10) + log(13)
    >>> L = loggamma(S(19)/4)
    >>> expand_func(L).doit()
    -4*log(4) + loggamma(3/4) + log(3) + log(7) + log(11) + log(15)
    >>> L = loggamma(S(23)/7)
    >>> expand_func(L).doit()
    -3*log(7) + log(2) + loggamma(2/7) + log(9) + log(16)

    The ``loggamma`` function has the following limits towards infinity:

    >>> from sympy import oo
    >>> loggamma(oo)
    oo
    >>> loggamma(-oo)
    zoo

    The ``loggamma`` function obeys the mirror symmetry
    if $x \in \mathbb{C} \setminus \{-\infty, 0\}$:

    >>> from sympy.abc import x
    >>> from sympy import conjugate
    >>> conjugate(loggamma(x))
    loggamma(conjugate(x))

    Differentiation with respect to $x$ is supported:

    >>> from sympy import diff
    >>> diff(loggamma(x), x)
    polygamma(0, x)

    Series expansion is also supported:

    >>> from sympy import series
    >>> series(loggamma(x), x, 0, 4).cancel()
    -log(x) - EulerGamma*x + pi**2*x**2/12 - x**3*zeta(3)/3 + O(x**4)

    We can numerically evaluate the ``loggamma`` function
    to arbitrary precision on the whole complex plane:

    >>> from sympy import I
    >>> loggamma(5).evalf(30)
    3.17805383034794561964694160130
    >>> loggamma(I).evalf(20)
    -0.65092319930185633889 - 1.8724366472624298171*I

    See Also
    ========

    gamma: Gamma function.
    lowergamma: Lower incomplete gamma function.
    uppergamma: Upper incomplete gamma function.
    polygamma: Polygamma function.
    digamma: Digamma function.
    trigamma: Trigamma function.
    sympy.functions.special.beta_functions.beta: Euler Beta function.

    References
    ==========

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

    c                 C   s¨   |j r|jrtS |jrtt|ƒƒS n)|jr;| ¡ \}}|jr;|dkr;ttt	ƒdd|   t|ƒ t|d t
j ƒ ƒS |tu rAtS t|ƒtu rJt
jS |t
ju rRt
jS d S rõ   )r]   r\   r   r@   r   r2   Úis_rationalrÓ   r   r   r   r•   rD   rA   r?   )rI   rf   rE   rC   r/   r/   r0   rM   Ï  s"   ÿ2
ÿzloggamma.evalc                 K   sô   ddl m} | jd }|jrx| ¡ \}}|| }|||  }|jrx|jrx||k rxtdƒ}|jrKt|| ƒ|t|ƒ  |t|d | | ƒ|d|fƒ S |j	rot|| ƒ|t|ƒ  t
t |  |t|| | ƒ|d| fƒ S |jrxt|| ƒS | S )Nr   ©ÚSumrK   r3   )Úsympy.concrete.summationsr  r6   rB   rÓ   r@   r   re   r   rÖ   r   r   rš   )r9   rU   r  rf   rE   rC   r.   rK   r/   r/   r0   rP   ã  s   
8Bzloggamma._eval_expand_funcNr   c                    sB   | j d  |d¡}|jr| j| j Ž }| |||¡S tƒ  |||¡S rX   )r6   rp   rš   Ú_eval_rewrite_as_intractablers   rr   )r9   rN   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rE|d|ƒ}n	|dˆ |  |ƒ}|t|Ž   |||¡| S )Nr   rñ   r<   c                    s<   g | ]}t d | ƒd | d | d  ˆ d | d    ‘qS )r<   r3   rö   r�   rË   r/   r0   r“     rå   z*loggamma._eval_aseries.<locals>.<listcomp>r3   )r¶   rò   r   rr   r¹   r6   r   r   r•   r   rH   r   rs   )	r9   r.   rº   rN   rt   rò   rø   rú   r¼   rx   rË   r0   r¹   ÿ  s   
&zloggamma._eval_aseriesc                 K   rc   rd   )r   r2   rm   r/   r/   r0   r    rj   z%loggamma._eval_rewrite_as_intractablec                 C   s"   | j d }|jr
dS |jrdS d S )Nr   TF)r6   r@   r\   rÍ   r/   r/   r0   r`     s   
ÿzloggamma._eval_is_realc                 C   s,   | j d }|tjtjfvr|  | ¡ ¡S d S rX   r«   rÍ   r/   r/   r0   r[     s   
ÿzloggamma._eval_conjugater3   c                 C   s"   |dkrt d| jd ƒS t| |ƒ‚r4   )r7   r6   r	   r8   r/   r/   r0   r;     s   
zloggamma.fdiffrX   r~   )r   r€   r�   r‚   r…   rM   rP   rs   r¹   r  r`   r[   r;   r†   r/   r/   rx   r0   re   a  s    m
re   c                   @   sn   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S )r‹   at  
    The ``digamma`` function is the first derivative of the ``loggamma``
    function

    .. math::
        \psi(x) := \frac{\mathrm{d}}{\mathrm{d} z} \log\Gamma(z)
                = \frac{\Gamma'(z)}{\Gamma(z) }.

    In this case, ``digamma(z) = polygamma(0, z)``.

    Examples
    ========

    >>> from sympy import digamma
    >>> digamma(0)
    zoo
    >>> from sympy import Symbol
    >>> z = Symbol('z')
    >>> digamma(z)
    polygamma(0, z)

    To retain ``digamma`` as it is:

    >>> digamma(0, evaluate=False)
    digamma(0)
    >>> digamma(z, evaluate=False)
    digamma(z)

    See Also
    ========

    gamma: Gamma function.
    lowergamma: Lower incomplete gamma function.
    uppergamma: Upper incomplete gamma function.
    polygamma: Polygamma function.
    loggamma: Log Gamma function.
    trigamma: Trigamma function.
    sympy.functions.special.beta_functions.beta: Euler Beta function.

    References
    ==========

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

    c                 C   s$   | j d }t|ƒ}td|ƒj|d�S )Nr   r-   ©r6   r*   r7   Úevalf©r9   r¨   rf   Únprecr/   r/   r0   r©   T  ó   
zdigamma._eval_evalfr3   c                 C   s   | j d }td|ƒ ¡ S rX   ©r6   r7   r;   ©r9   r:   rf   r/   r/   r0   r;   Y  ó   
zdigamma.fdiffc                 C   ó   | j d }td|ƒjS rX   ©r6   r7   rÜ   rÍ   r/   r/   r0   r`   ]  ó   
zdigamma._eval_is_realc                 C   r  rX   ©r6   r7   r@   rÍ   r/   r/   r0   rb   a  r  zdigamma._eval_is_positivec                 C   r  rX   ©r6   r7   rÖ   rÍ   r/   r/   r0   rß   e  r  zdigamma._eval_is_negativec                 C   ó&   |   t¡}tjg| }| ||||¡S rd   )rÄ   r7   r   r˜   r¹   ©r9   r.   rº   rN   rt   Úas_polygammar/   r/   r0   r¹   i  ó   
zdigamma._eval_aseriesc                 C   ó
   t d|ƒS rX   ©r7   ©rI   rf   r/   r/   r0   rM   n  ó   
zdigamma.evalc                 K   s   | j d }td|ƒjdd�S )Nr   T©r5   ©r6   r7   Úexpand©r9   rU   rf   r/   r/   r0   rP   r  ó   
zdigamma._eval_expand_funcc                 K   s   t |d ƒtj S rk   )r#   r   rÒ   rm   r/   r/   r0   rð   v  rÀ   z!digamma._eval_rewrite_as_harmonicc                 K   r  rX   r  rm   r/   r/   r0   Ú_eval_rewrite_as_polygammay  ó   
z"digamma._eval_rewrite_as_polygammac                 C   s   | j d }td|ƒ |¡S rX   ©r6   r7   rz   ©r9   rN   rt   ru   rf   r/   r/   r0   r}   |  ó   
zdigamma._eval_as_leading_termNr~   )r   r€   r�   r‚   r©   r;   r`   rb   rß   r¹   r…   rM   rP   rð   r"  r}   r/   r/   r/   r0   r‹   $  s    /

r‹   c                   @   sv   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„ ZdS )Útrigammaa^  
    The ``trigamma`` function is the second derivative of the ``loggamma``
    function

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

    In this case, ``trigamma(z) = polygamma(1, z)``.

    Examples
    ========

    >>> from sympy import trigamma
    >>> trigamma(0)
    zoo
    >>> from sympy import Symbol
    >>> z = Symbol('z')
    >>> trigamma(z)
    polygamma(1, z)

    To retain ``trigamma`` as it is:

    >>> trigamma(0, evaluate=False)
    trigamma(0)
    >>> trigamma(z, evaluate=False)
    trigamma(z)


    See Also
    ========

    gamma: Gamma function.
    lowergamma: Lower incomplete gamma function.
    uppergamma: Upper incomplete gamma function.
    polygamma: Polygamma function.
    loggamma: Log Gamma function.
    digamma: Digamma function.
    sympy.functions.special.beta_functions.beta: Euler Beta function.

    References
    ==========

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

    c                 C   s$   | j d }t|ƒ}td|ƒj|d�S )Nr   r3   r-   r  r
  r/   r/   r0   r©   ²  r  ztrigamma._eval_evalfr3   c                 C   s   | j d }td|ƒ ¡ S ro   r  r  r/   r/   r0   r;   ·  r  ztrigamma.fdiffc                 C   ó   | j d }td|ƒjS ro   r  rÍ   r/   r/   r0   r`   »  r  ztrigamma._eval_is_realc                 C   r(  ro   r  rÍ   r/   r/   r0   rb   ¿  r  ztrigamma._eval_is_positivec                 C   r(  ro   r  rÍ   r/   r/   r0   rß   Ã  r  ztrigamma._eval_is_negativec                 C   r  rd   )rÄ   r7   r   rF   r¹   r  r/   r/   r0   r¹   Ç  r  ztrigamma._eval_aseriesc                 C   r  rk   r  r  r/   r/   r0   rM   Ì  r  ztrigamma.evalc                 K   s   | j d }td|ƒjdd�S )Nr   r3   Tr  r  r   r/   r/   r0   rP   Ð  r!  ztrigamma._eval_expand_funcc                 K   r  )Nr<   r   rm   r/   r/   r0   rï   Ô  r#  ztrigamma._eval_rewrite_as_zetac                 K   r  rk   r  rm   r/   r/   r0   r"  ×  r#  z#trigamma._eval_rewrite_as_polygammac                 K   s   t |d dƒ td d  S )Nr3   r<   rÏ   )r#   r   rm   r/   r/   r0   rð   Ú  s   z"trigamma._eval_rewrite_as_harmonicc                 C   s   | j d }td|ƒ |¡S ro   r$  r%  r/   r/   r0   r}   Ý  r&  ztrigamma._eval_as_leading_termNr~   )r   r€   r�   r‚   r©   r;   r`   rb   rß   r¹   r…   rM   rP   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,
    sympy.functions.special.beta_functions.beta

    References
    ==========

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

    Tr<   c                 C   s^   ddl m} |dkr*| j\}}tdƒ}|  ||¡|td|d| d  ƒ|d|fƒ S t| |ƒ‚)Nr   r  r<   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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.rK   r3   é   r<   )	Úsympy.concrete.productsr*  r@   r]   r,   r   r   r2   Údoit)rI   rN   rE   r*  rK   r/   r/   r0   rM   +  s   &ÿÿzmultigamma.evalc                 C   s   | j \}}|  | ¡ |¡S rd   )r6   r5   rY   )r9   rN   rE   r/   r/   r0   r[   4  r&  zmultigamma._eval_conjugatec                 C   s^   | j \}}d| }|jr||d kdu rdS t|ƒr"||d kr"dS ||d ks+|jr-dS d S )Nr<   r3   TF)r6   r]   r1   r^   )r9   rN   rE   Úyr/   r/   r0   r`   8  s   
ÿzmultigamma._eval_is_realNrÈ   )
r   r€   r�   r‚   rƒ   r;   r…   rM   r[   r`   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   re   r‹   r'  r)  r/   r/   r/   r0   Ú<module>   sD     = 1 '  / D^e