
:>"^Ô  ã               @   sL   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}.
é    NgH¯¼šò×z>g      0Cg      °<c             C   s~   |  d k r d S|  d k r  d S| d k r8 t  d ƒ ‚ |  d k  sP |  | k  ri d t d | d |  ƒ St 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)ÚxÚdf© r   ú9/tmp/pip-build-ww9dw3qa/biopython/Bio/codonalign/chisq.pyÚ	chisqprob   s    r	   c             C   sk  t  j |  t  j | ƒ | t  j |  ƒ ƒ } d |  } | | d } d } d } | } | d } | | }	 | |	 }
 xï | d 7} | d 7} | d 7} | | } | | | | } |	 | | | } | d k r÷ | | } t |
 | | ƒ } | }
 n d } | } | } |	 } | }	 t | ƒ t k rO| t 9} | t 9} | t 9} |	 t 9}	 | t k rx |
 | Sqx Wd 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ycÚpkZqkÚrÚtr   r   r   r   -   s@    !-








	



r   c             C   sˆ   t  j |  t  j | ƒ | t  j |  ƒ ƒ } |  } d } d } xB | d 7} | | | 9} | | 7} | | t k rB | | |  SqB Wd S)zéLeft 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