o
    §ƒðjé1  ã                   @   s’   d dl 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 ddd„ZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZdS )é    )ÚS)ÚDefinedFunctionÚArgumentIndexError)ÚDummyÚuniquely_named_symbol)ÚgammaÚdigamma)Úcatalan)Ú	conjugatec                 C   s0   ddl m}m} ||kr|dƒS || ||||ƒS )Nr   )ÚbetaincÚmpf)Úmpmathr   r   )ÚaÚbÚx1Úx2Úregr   r   © r   ú¨/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/functions/special/beta_functions.pyÚbetainc_mpmath_fix	   s   r   c                   @   s\   e Zd ZdZdZdd„ Zeddd„ƒZdd	„ Zd
d„ Z	dd„ Z
dd„ Zddd„Zdd„ ZdS )ÚbetaaÙ	  
    The beta integral is called the Eulerian integral of the first kind by
    Legendre:

    .. math::
        \mathrm{B}(x,y)  \int^{1}_{0} t^{x-1} (1-t)^{y-1} \mathrm{d}t.

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

    The Beta function or Euler's first integral is closely associated
    with the gamma function. The Beta function is often used in probability
    theory and mathematical statistics. It satisfies properties like:

    .. math::
        \mathrm{B}(a,1) = \frac{1}{a} \\
        \mathrm{B}(a,b) = \mathrm{B}(b,a)  \\
        \mathrm{B}(a,b) = \frac{\Gamma(a) \Gamma(b)}{\Gamma(a+b)}

    Therefore for integral values of $a$ and $b$:

    .. math::
        \mathrm{B} = \frac{(a-1)! (b-1)!}{(a+b-1)!}

    A special case of the Beta function when `x = y` is the
    Central Beta function. It satisfies properties like:

    .. math::
        \mathrm{B}(x) = 2^{1 - 2x}\mathrm{B}(x, \frac{1}{2})
        \mathrm{B}(x) = 2^{1 - 2x} cos(\pi x) \mathrm{B}(\frac{1}{2} - x, x)
        \mathrm{B}(x) = \int_{0}^{1} \frac{t^x}{(1 + t)^{2x}} dt
        \mathrm{B}(x) = \frac{2}{x} \prod_{n = 1}^{\infty} \frac{n(n + 2x)}{(n + x)^2}

    Examples
    ========

    >>> from sympy import I, pi
    >>> from sympy.abc import x, y

    The Beta function obeys the mirror symmetry:

    >>> from sympy import beta, conjugate
    >>> conjugate(beta(x, y))
    beta(conjugate(x), conjugate(y))

    Differentiation with respect to both $x$ and $y$ is supported:

    >>> from sympy import beta, diff
    >>> diff(beta(x, y), x)
    (polygamma(0, x) - polygamma(0, x + y))*beta(x, y)

    >>> diff(beta(x, y), y)
    (polygamma(0, y) - polygamma(0, x + y))*beta(x, y)

    >>> diff(beta(x), x)
    2*(polygamma(0, x) - polygamma(0, 2*x))*beta(x, x)

    We can numerically evaluate the Beta function to
    arbitrary precision for any complex numbers x and y:

    >>> from sympy import beta
    >>> beta(pi).evalf(40)
    0.02671848900111377452242355235388489324562

    >>> beta(1 + I).evalf(20)
    -0.2112723729365330143 - 0.7655283165378005676*I

    See Also
    ========

    gamma: Gamma function.
    uppergamma: Upper incomplete gamma function.
    lowergamma: Lower incomplete gamma function.
    polygamma: Polygamma function.
    loggamma: Log Gamma function.
    digamma: Digamma function.
    trigamma: Trigamma function.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Beta_function
    .. [2] https://mathworld.wolfram.com/BetaFunction.html
    .. [3] https://dlmf.nist.gov/5.12

    Tc                 C   s`   | j \}}|dkrt||ƒt|ƒt|| ƒ  S |dkr+t||ƒt|ƒt|| ƒ  S t| |ƒ‚)Né   é   )Úargsr   r   r   )ÚselfÚargindexÚxÚyr   r   r   Úfdiffm   s   

z
beta.fdiffNc                 C   s8   |d u r	t ||ƒS |jr|jrt ||dd� ¡ S d S d S )NF)Úevaluate)r   Ú	is_NumberÚdoit)Úclsr   r   r   r   r   Úevalx   s
   
ÿz	beta.evalc                 K   s
  | j d  }}t| j ƒdk}|r| j d n| j d  }}| dd¡r2|jdi |¤Ž}|jdi |¤Ž}|js8|jr;tjS |tju rDd| S |tju rMd| S ||d kr]d|| t|ƒ  S || }|j	rt|j
rt|j	du rt|j	du rttjS ||kr€||kr€|s€| S t||ƒS )Nr   r   ÚdeepTFr   )r   ÚlenÚgetr!   Úis_zeror   ÚComplexInfinityÚOner	   Ú
is_integerÚis_negativeÚZeror   )r   Úhintsr   ÚxoldÚsingle_argumentr   ÚyoldÚsr   r   r   r!      s*   



z	beta.doitc                 K   s&   | j \}}t|ƒt|ƒ t|| ƒ S ©N)r   r   )r   r-   r   r   r   r   r   Ú_eval_expand_func—   s   
zbeta._eval_expand_funcc                 C   s   | j d jo| j d jS ©Nr   r   )r   Úis_real©r   r   r   r   Ú_eval_is_real›   ó   zbeta._eval_is_realc                 C   s    |   | jd  ¡ | jd  ¡ ¡S r4   )Úfuncr   r
   r6   r   r   r   Ú_eval_conjugatež   s    zbeta._eval_conjugatec                 K   s   | j di |¤ŽS )Nr   )r3   )r   r   r   Ú	piecewiseÚkwargsr   r   r   Ú_eval_rewrite_as_gamma¡   s   zbeta._eval_rewrite_as_gammac                 K   sH   ddl m} ttd||gƒjƒ}|||d  d| |d   |ddfƒS ©Nr   )ÚIntegralÚtr   ©Úsympy.integrals.integralsr?   r   r   Úname)r   r   r   r<   r?   r@   r   r   r   Ú_eval_rewrite_as_Integral¤   s   (zbeta._eval_rewrite_as_Integralr2   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
unbranchedr   Úclassmethodr#   r!   r3   r7   r:   r=   rD   r   r   r   r   r      s    V
r   c                   @   sH   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ ZdS )r   a[  
    The Generalized Incomplete Beta function is defined as

    .. math::
        \mathrm{B}_{(x_1, x_2)}(a, b) = \int_{x_1}^{x_2} t^{a - 1} (1 - t)^{b - 1} dt

    The Incomplete Beta function is a special case
    of the Generalized Incomplete Beta function :

    .. math:: \mathrm{B}_z (a, b) = \mathrm{B}_{(0, z)}(a, b)

    The Incomplete Beta function satisfies :

    .. math:: \mathrm{B}_z (a, b) = (-1)^a \mathrm{B}_{\frac{z}{z - 1}} (a, 1 - a - b)

    The Beta function is a special case of the Incomplete Beta function :

    .. math:: \mathrm{B}(a, b) = \mathrm{B}_{1}(a, b)

    Examples
    ========

    >>> from sympy import betainc, symbols, conjugate
    >>> a, b, x, x1, x2 = symbols('a b x x1 x2')

    The Generalized Incomplete Beta function is given by:

    >>> betainc(a, b, x1, x2)
    betainc(a, b, x1, x2)

    The Incomplete Beta function can be obtained as follows:

    >>> betainc(a, b, 0, x)
    betainc(a, b, 0, x)

    The Incomplete Beta function obeys the mirror symmetry:

    >>> conjugate(betainc(a, b, x1, x2))
    betainc(conjugate(a), conjugate(b), conjugate(x1), conjugate(x2))

    We can numerically evaluate the Incomplete Beta function to
    arbitrary precision for any complex numbers a, b, x1 and x2:

    >>> from sympy import betainc, I
    >>> betainc(2, 3, 4, 5).evalf(10)
    56.08333333
    >>> betainc(0.75, 1 - 4*I, 0, 2 + 3*I).evalf(25)
    0.2241657956955709603655887 + 0.3619619242700451992411724*I

    The Generalized Incomplete Beta function can be expressed
    in terms of the Generalized Hypergeometric function.

    >>> from sympy import hyper
    >>> betainc(a, b, x1, x2).rewrite(hyper)
    (-x1**a*hyper((a, 1 - b), (a + 1,), x1) + x2**a*hyper((a, 1 - b), (a + 1,), x2))/a

    See Also
    ========

    beta: Beta function
    hyper: Generalized Hypergeometric function

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Beta_function#Incomplete_beta_function
    .. [2] https://dlmf.nist.gov/8.17
    .. [3] https://functions.wolfram.com/GammaBetaErf/Beta4/
    .. [4] https://functions.wolfram.com/GammaBetaErf/BetaRegularized4/02/

    é   Tc                 C   sb   | j \}}}}|dkrd| |d   ||d   S |dkr,d| |d  ||d   S t| |ƒ‚©Né   r   rK   )r   r   ©r   r   r   r   r   r   r   r   r   r   ø   s   
zbetainc.fdiffc                 C   s
   t | jfS r2   )r   r   r6   r   r   r   Ú_eval_mpmath  s   
zbetainc._eval_mpmathc                 C   ó   t dd„ | jD ƒƒrdS d S )Nc                 s   ó   � | ]}|j V  qd S r2   ©r5   ©Ú.0Úargr   r   r   Ú	<genexpr>  ó   € z(betainc._eval_is_real.<locals>.<genexpr>T©Úallr   r6   r   r   r   r7     ó   ÿzbetainc._eval_is_realc                 C   ó   | j tt| jƒŽ S r2   ©r9   Úmapr
   r   r6   r   r   r   r:   
  ó   zbetainc._eval_conjugatec                 K   sL   ddl m} ttd||||gƒjƒ}|||d  d| |d   |||fƒS r>   rA   )r   r   r   r   r   r<   r?   r@   r   r   r   rD     s   (z!betainc._eval_rewrite_as_Integralc                 K   sT   ddl m} || ||d| f|d f|ƒ || ||d| f|d f|ƒ  | S ©Nr   )Úhyperr   )Úsympy.functions.special.hyperr`   )r   r   r   r   r   r<   r`   r   r   r   Ú_eval_rewrite_as_hyper  s   Hzbetainc._eval_rewrite_as_hyperN)rE   rF   rG   rH   ÚnargsrI   r   rO   r7   r:   rD   rb   r   r   r   r   r   ­   s    Gr   c                       sX   e Zd ZdZdZdZ‡ fdd„Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Z‡  ZS )Úbetainc_regularizeda�  
    The Generalized Regularized Incomplete Beta function is given by

    .. math::
        \mathrm{I}_{(x_1, x_2)}(a, b) = \frac{\mathrm{B}_{(x_1, x_2)}(a, b)}{\mathrm{B}(a, b)}

    The Regularized Incomplete Beta function is a special case
    of the Generalized Regularized Incomplete Beta function :

    .. math:: \mathrm{I}_z (a, b) = \mathrm{I}_{(0, z)}(a, b)

    The Regularized Incomplete Beta function is the cumulative distribution
    function of the beta distribution.

    Examples
    ========

    >>> from sympy import betainc_regularized, symbols, conjugate
    >>> a, b, x, x1, x2 = symbols('a b x x1 x2')

    The Generalized Regularized Incomplete Beta
    function is given by:

    >>> betainc_regularized(a, b, x1, x2)
    betainc_regularized(a, b, x1, x2)

    The Regularized Incomplete Beta function
    can be obtained as follows:

    >>> betainc_regularized(a, b, 0, x)
    betainc_regularized(a, b, 0, x)

    The Regularized Incomplete Beta function
    obeys the mirror symmetry:

    >>> conjugate(betainc_regularized(a, b, x1, x2))
    betainc_regularized(conjugate(a), conjugate(b), conjugate(x1), conjugate(x2))

    We can numerically evaluate the Regularized Incomplete Beta function
    to arbitrary precision for any complex numbers a, b, x1 and x2:

    >>> from sympy import betainc_regularized, pi, E
    >>> betainc_regularized(1, 2, 0, 0.25).evalf(10)
    0.4375000000
    >>> betainc_regularized(pi, E, 0, 1).evalf(5)
    1.00000

    The Generalized Regularized Incomplete Beta function can be
    expressed in terms of the Generalized Hypergeometric function.

    >>> from sympy import hyper
    >>> betainc_regularized(a, b, x1, x2).rewrite(hyper)
    (-x1**a*hyper((a, 1 - b), (a + 1,), x1) + x2**a*hyper((a, 1 - b), (a + 1,), x2))/(a*beta(a, b))

    See Also
    ========

    beta: Beta function
    hyper: Generalized Hypergeometric function

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Beta_function#Incomplete_beta_function
    .. [2] https://dlmf.nist.gov/8.17
    .. [3] https://functions.wolfram.com/GammaBetaErf/Beta4/
    .. [4] https://functions.wolfram.com/GammaBetaErf/BetaRegularized4/02/

    rK   Tc                    s   t ƒ  | ||||¡S r2   )ÚsuperÚ__new__)r"   r   r   r   r   ©Ú	__class__r   r   rf   c  s   zbetainc_regularized.__new__c                 C   s   t g | j¢tdƒ‘R fS )Nr   )r   r   r   r6   r   r   r   rO   f  r8   z betainc_regularized._eval_mpmathc                 C   sv   | j \}}}}|dkrd| |d   ||d   t||ƒ S |dkr6d| |d  ||d   t||ƒ S t| |ƒ‚rL   )r   r   r   rN   r   r   r   r   i  s   (&
zbetainc_regularized.fdiffc                 C   rP   )Nc                 s   rQ   r2   rR   rS   r   r   r   rV   u  rW   z4betainc_regularized._eval_is_real.<locals>.<genexpr>TrX   r6   r   r   r   r7   t  rZ   z!betainc_regularized._eval_is_realc                 C   r[   r2   r\   r6   r   r   r   r:   x  r^   z#betainc_regularized._eval_conjugatec           
      K   sd   ddl m} ttd||||gƒjƒ}||d  d| |d   }|||||fƒ}	|	|||ddfƒ S r>   rA   )
r   r   r   r   r   r<   r?   r@   Ú	integrandÚexprr   r   r   rD   {  s
   z-betainc_regularized._eval_rewrite_as_Integralc                 K   sb   ddl m} || ||d| f|d f|ƒ || ||d| f|d f|ƒ  | }|t||ƒ S r_   )ra   r`   r   )r   r   r   r   r   r<   r`   rj   r   r   r   rb   ‚  s   Hz*betainc_regularized._eval_rewrite_as_hyper)rE   rF   rG   rH   rc   rI   rf   rO   r   r7   r:   rD   rb   Ú__classcell__r   r   rg   r   rd     s    Erd   N)r   )Ú
sympy.corer   Úsympy.core.functionr   r   Úsympy.core.symbolr   r   Ú'sympy.functions.special.gamma_functionsr   r   Ú%sympy.functions.combinatorial.numbersr	   Ú$sympy.functions.elementary.complexesr
   r   r   r   rd   r   r   r   r   Ú<module>   s    
 m