
Y_Sc        !   @   s  d  Z  d d l m Z da Z d d l Td d l Z d d l m Z d d l	 m
 Z
 d Z d Z d Z d Z d  Z db Z dc Z dd Z dR   Z dS   Z dT   Z dU   Z dV   Z dW   Z dX   Z dY   Z dZ   Z d[   Z d\   Z d]   Z e d  d^  Z  d_   Z! d`   Z" d S(e   s    Various bits of useful math not in the standard python library.

Constants :

- euler_gamma  = 0.577215...
- catalan      = 0.915965...
- golden_ratio = 1.618033...
- bits_per_nat = log2(e) = 1/log(2) 
- sqrt_2pi     = 2.50662...
    
Special Functions :



- gamma()                       -- Gamma function.
- lngamma()                     -- Logarithm of the gamma function
- factorial()                   -- Factorial function.
- digamma()                     -- Digamma function (logarithmic derivative of gamma).
- trigamma()                    -- Trigamma function (derivative of digamma).

- cgamma()                       -- Complex math version of counterparts above.
- clngamma()                                   
- cdigamma()                     
- ctrigamma()                    

- entropy()                     -- The entropy of a probability vector
- incomplete_gamma()            -- The 'upper' incomplete gamma function.
- normalized_incomplete_gamma() -- 
- log2()                          -- Base 2 logarithms.
- argmin()
- argmax()


i(   t   divisiont   euler_gammat   catalant   golden_ratiot   bits_per_natt   sqrt_2pit   gammat   lngammat	   factorialt   digammat   trigammat   cgammat   clngammat   cdigammat	   ctrigammat   entropyt   log2t   incomplete_gammat   normalized_incomplete_gammat   argmaxt   argmin(   t   *N(   t   counti   (   t   zipgox?g]O?gw?g+eG?g'@g     @g      `@g?gάL@g0Mg"=E,@g%{߿g:G?gŷKd?gtgmԞ$?g6*M+g&,?g@&ə%gU=w6!?gѵvg
>g      ?g       @g      @g      8@g      ^@g     @g     @g     @g     &Ag    KAg    Ag    Ag   2Ag   (;L4Bg  uwsBg  uwBg  7Bg  s6Cg h0{Cg ZACg Ƶ;(DglYaRwNDgΤ5Dg{zhRlDg!a?@)Eg~tEg凗Eggi=-FgQ[Fg-n'Fg3
VFg3
VIGc         C   s   t  |   j S(   s  The gamma function. Returns exact results for small integers. Will
    overflow for modest sized arguments. Use lngamma(z) instead. 
    
    See: Eric W. Weisstein. "Gamma Function." From MathWorld, A Wolfram Web Resource.
         http://mathworld.wolfram.com/GammaFunction.html

    (   R   t   real(   t   x(    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR      s    c         C   sj  t  |   }  t |  j  } | |  k rn | d k rA t  d d  S| t t  k rn t  t t |  d  Sn  |  } |  j d k  r d |  } n  t } t } | d } | d } | | } | | d } d } x: t t |  d d d  D] }	 | | |	 | |	 7} q Wt	 | d | | t
 j |  | }
 |  j d k  r`t t
 j t |   |
 }
 n  t  |
  S(   s2   Gamma function with complex arguments and results.i    g      ?g        i   g      ?i(   t   complext   floorR   t   lent   __factorialt   intt   __lanczos_gammat   __lanczos_coefficientst   rangeR   t   cmt   expt   pit   sin(   t   zt   nt   zzt   gt   ct   zht   zght   zpt   sst   kt   f(    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR      s.    


#(c         C   s   t  |   j S(   s*   The logarithm of the gamma function. 
    (   R   R   (   R   (    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR      s    c         C   sm  t  |   }  t |  j  } | |  k rn | d k rA t  d d  S| t t  k rn t  t t |  d  Sn  |  } |  j d k  r d |  } n  t } t } | d } | d } | | } | | d } d } x: t t |  d d d  D] }	 | | |	 | |	 7} q Wt	 | d | | t
 j |  | }
 |  j d k  r`t t
 j t |   |
 }
 n  t
 j |
  S(   s?   The logarithm of the gamma function for a complex argument
    i    g      ?g        i   g      ?i(   R   R   R   R   R   R   R   R    R!   R   R"   R#   R$   R%   t   log(   R&   R'   R(   R)   R*   R+   R,   R-   R.   R/   R0   (    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR      s.    


#(c         C   s   t  |  d  S(   s=    The factorial function. 
    factorial(z) == gamma(z+1)
    i   (   R   (   R&   (    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR     s    c         C   s   t  |   j S(   s  The digamma function, the logarithmic derivative of the gamma function.
             digamma(z) = d/dz ln( gamma(z) )


     See: Eric W. Weisstein. "Digamma Function." From MathWorld--
     A Wolfram Web Resource. http://mathworld.wolfram.com/DigammaFunction.html
    (   R   R   (   R   (    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR	   	  s    c         C   s  t  |   }  t } t } |  } |  j d k  r: d |  } n  d } d } x` t t |  d d d  D]B } d | | d d } | | | } | | } | | | } qc W| | d } | | d }	 t j |	  | | | |	 }
 |  j d k  r|
 t t j	 t |   8}
 n  |
 S(   s'   Digamma function with complex argumentsg      ?i   g        i    ig      ?i   (
   R   R   R    R   R!   R   R"   R1   R$   t   tan(   R&   R)   R*   R(   R'   t   dR/   t   dzt   ddt   ggR0   (    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR     s&    #
c         C   s   t  |   j S(   s  The trigamma function, the derivative of the digamma function.
            trigamma(z) = d/dz digamma(z) = d/dz d/dz ln( gamma(z) )
    
    See: Eric W. Weisstein. "Trigamma Function." From MathWorld--
    A Wolfram Web Resource. http://mathworld.wolfram.com/TrigammaFunction.html
    (   R   R   (   R   (    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR
   1  s    c         C   s  t  |   }  t } t } d } d } d } xl t t |  d d d  D]N } d |  | } | | | } | | 7} | | }	 | |	 7} | |	 | 7} qG W| | d 7} | | | | d | | } d |  |  }
 |  | d } |
 |  d | | 7}
 |
 d | 7}
 |
 | 7}
 |
 S(	   s>   The trigamma function with complex arguments and return value.g        i   i    ig      ?i   g      ?g       @(   R   R   R    R!   R   (   R&   R)   R*   t   t1t   t2t   t3R/   R4   t   dd1t   dd2t   resultR6   (    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR   :  s*    #



c         C   s   t  |  |  t |   S(   s0  The 'upper' incomplete gamma function:

                            oo
                             -
                            |    -t  a-1
    incomplete_gamma(a,x) = |   e   t   dt.
                            |
                           -                         
                            x

    In Mathematica, Gamma[a,x].

    Note that, very confusingly, the phrase 'incomplete gamma fucntion'
    can also refer to the same integral between 0 and x, (the 'lower'
    incomplete gamma function) or to the normalized versions,
    normalized_incomplete_gamma() )


    See: Eric W. Weisstein. "Gamma Function." From MathWorld, A Wolfram Web Resource.
         http://mathworld.wolfram.com/IncompleteGammaFunction.html

    Bugs :
        This implementation is not very accurate for some arguments. 
    (   R   R   (   t   aR   (    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR   W  s    c      	   C   s  d } d } d } |  d k s* | d k  r9 t  d   n  | d k rI d S| |  d k r d |  } | } xt t d |  D]c } | | |  | 9} | | 7} t | |  | k  ry d | t | |  t |  t |    Sqy Wt d	 |   nd } | d |  } d | }	 d | }
 |
 } x t d |  D] } | | |  } | d
 } | |
 | }
 t |
  | k  ry| }
 n  | | |	 }	 t |	  | k  r| }	 n  d |
 }
 |
 |	 } | | } t | d  | k  r1| t | |  t |  t |    Sq1Wt d	 |   d S(   s  The upper, incomplete gamma function normalized so that the limiting
    values are zero and one.
    
     Q(a,x) = incomplete_gamma(a,x) / gamma(a) 

    See: 
        incomplete_gamma()
    Bugs :
        This implementation is not very accurate for some arguments. 
    id   g`sbO>gKH9i    s   Invalid argumentsg        g      ?i   s'   Failed to converge after %d iterations.g       @N(   t
   ValueErrorR!   t   absR#   R1   R   t   RuntimeError(   R=   R   t   maxitert   epsilont   smallt   termt   totalR'   t   bR*   R3   t   ht   it   an(    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR   s  sL     

/


 	 	


+c         C   s   t  |  d  S(   s"    Return the base 2 logarithm of x i   (   R1   (   R   (    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR     s    c         C   s   t  |   d k r! t d   n  d } d } x] |  D]U } | d k rn | | 7} | t t |   | 7} q4 | d k  r4 t d   q4 q4 W| | t |  } | t |  } | S(   sh    The entropy S = -Sum_i p_i ln p_i
        pvec is a frequency vector, not necessarily normalized. 
    i    s   Zero length vectorg        s   Negative probability(   R   R>   R1   t   float(   t   pvect   baseRE   t   entt   p(    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR     s    
c         C   s   t  t |  t     d S(   sI   Return the index of the last occurrence of the maximum value in the list.i   (   t   maxR   R   (   t   alist(    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR     s    c         C   s   t  t |  t     d S(   sJ   Return the index of the first occurrence of the minimum value in the list.i   (   t   minR   R   (   RP   (    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyR     s    (   s   euler_gammas   catalans   golden_ratios   bits_per_nats   sqrt_2pis   gammas   lngammas	   factorials   digammas   trigammas   cgammas   clngammas   cdigammas	   ctrigammas   entropys   log2s   incomplete_gammas   normalized_incomplete_gammas   argmaxs   argming     @(   g?gάL@g0Mg"=E,@g%{߿g:G?gŷKd?gtgmԞ$?g6*M+g&,?g@&ə%gU=w6!?gѵvg
>(!   g      ?g      ?g       @g      @g      8@g      ^@g     @g     @g     @g     &Ag    KAg    Ag    Ag   2Ag   (;L4Bg  uwsBg  uwBg  7Bg  s6Cg h0{Cg ZACg Ƶ;(DglYaRwNDgΤ5Dg{zhRlDg!a?@)Eg~tEg凗Eggi=-FgQ[Fg-n'Fg3
VFg3
VIG(#   t   __doc__t
   __future__R    t   __all__t   matht   cmathR"   t	   itertoolsR   t   _py3kR   R   R   R   R   R   R   R    R   R   R   R   R   R   R	   R   R
   R   R   R   R   R#   R   R   R   (    (    (    s:   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/moremath.pyt   <module>=   s        
                                              		#		&								7		