B
    b                 @   s4   d Z ddlZdZdZdZdd Zdd	 Zd
d ZdS )zwPython implementation of chisqprob, to avoid SciPy dependency.

Adapted from SciPy: scipy/special/cephes/{chdtr,igam}.
    NgHz>g      0Cg      <c             C   s`   | dkrdS | dkrdS |dkr(t d| dk s8| |k rNdtd| d|   S td| d|  S )a  Probability value (1-tail) for the Chi^2 probability distribution.

    Broadcasting rules apply.

    Parameters
    ----------
    x : array_like or float > 0

    df : array_like or float, probably int >= 1

    Returns
    -------
    chisqprob : ndarray
        The area from ``chisq`` to infinity under the Chi^2 probability
        distribution with degrees of freedom ``df``.

    r   g      ?g        zDomain error.g      ?)
ValueError_igam_igamc)xZdf r   3lib/python3.7/site-packages/Bio/codonalign/chisq.py	chisqprob   s    r   c             C   s  t | t | | t |  }d|  }|| d }d}d}|}|d }|| }	||	 }
x|d7 }|d7 }|d7 }|| }|| ||  }|	| ||  }|dkr|| }t|
| | }|}
nd}|}|}|	}|}	t|tkr|t9 }|t9 }|t9 }|	t9 }	|tkr\|
| S q\W dS )a  Complemented incomplete Gamma integral (PRIVATE).

    Parameters
    ----------
    a: float
    x: float

    Returns
    -------
    float

    Notes
    -----
    The function is defined by::

        igamc(a,x)   =   1 - igam(a,x)

                                inf.
                                   -
                          1       | |  -t  a-1
                    =   -----     |   e   t   dt.
                         -      | |
                        | (a)    -
                                    x

    In this implementation both arguments must be positive.
    The integral is evaluated by either a power series or
    continued fraction expansion, depending on the relative
    values of a and x.

    g      ?g        g       @r   N)mathexploglgammaabsBIGBIGINVMACHEP)ar   axyzcZpkm2Zqkm2Zpkm1Zqkm1ansZycZpkZqkrtr   r   r   r   -   s@    !"r   c             C   sl   t | t | | t |  }| }d}d}x8|d7 }||| 9 }||7 }|| tkr0|| |  S q0W dS )zLeft tail of incomplete Gamma function (PRIVATE).

    Computes this formula::

                 inf.      k
          a  -x   -       x
         x  e     >   ----------
                  -     -
                k=0   | (a+k+1)

    g      ?N)r	   r
   r   r   r   )r   r   r   r   r   r   r   r   r   r   s   s    "r   )__doc__r	   r   r   r   r   r   r   r   r   r   r   <module>   s   F