o
    oh                     @   s  d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
mZmZ d dlmZmZ d dlmZmZmZmZ d dlmZ d dlmZ d d	lmZmZmZ d d
lmZmZ d dl m!Z!m"Z" d dl#m$Z$m%Z% d dl&m'Z' d dl(m)Z)m*Z*m+Z+ d dl,m-Z-m.Z. d dl/m0Z0m1Z1m2Z2 d dl3m4Z4 d dl5m6Z6m7Z7 d dl8m9Z9 dd Z:G dd de
Z;G dd de
Z<G dd de
Z=G dd de
Z>G dd de
Z?G d d! d!e
Z@G d"d# d#e
ZAG d$d% d%e
ZBd&S )'    )prod)AddSDummyexpand_func)Expr)FunctionArgumentIndexError	PoleError)	fuzzy_and	fuzzy_not)RationalpiooIPowzeta)erferfcEi)re
unpolarify)explog)ceilingfloor)sqrt)sincoscot)	bernoulliharmonic)	factorialrfRisingFactorial)as_int)mpworkprec)prec_to_dpsc                 C   s(   z	t | dd W dS  ty   Y dS w )NF)strictT)r'   
ValueErrorn r/   {/var/www/html/construction_image-detection-poc/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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   rX   )__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sd  jr d }|jrɈ jrt|t t| tfddt D    S t tt jtt t tfddtd t j D     S  jst j
t j   t tt td   t t fddtdtdd  D    S jrt jS d S )	Nr   r<   r3   c                       g | ]
} | t | qS r/   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rt
 dtdt t |     tdt t |   t |  S  jrR tju rj	rt  S  tju rt S  tju rtttt S  jsd  jrR d }|j	r jrt t| tfddt D   S t
 tt tj tdd   t  t t fd	dt tj D    S |jr|| t|d   S  j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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rt|}||krt||S |jrt j|d  t| t|d | S |t ju rt 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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 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_harmonicNr   c                    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_evalfrX   r   )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                   @   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   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_polygammaNr   c                 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_termr~   rX   )r   r   r   r   r   r;   r`   rb   r   r   r   rM   rP   r   r"  r}   r/   r/   r/   r0   r   $  s    /

r   c                   @   sx   e Zd ZdZdd ZdddZdd Zd	d
 Zdd Zdd Z	e
dd Zdd Zdd Zdd Zdd ZdddZdS )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_harmonicNr   c                 C   s   | j d }td||S ro   r$  r%  r/   r/   r0   r}     r&  ztrigamma._eval_as_leading_termr~   rX   )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