ó
ã™xTc           @   s¢   d  d l  m Z d  d l Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 e	 f d
 „  ƒ  YZ
 d „  Z d „  Z d „  Z e e d „ Z d d „ Z d S(   iÿÿÿÿ(   t   divisionNc         C   s(   |  d k  r t  |  d ƒ St  |  d ƒ S(   Ni    g      à?(   t   int(   t   x(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   iround   s     c         C   s   t  t j |  ƒ ƒ S(   N(   R   t   matht   ceil(   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   iceil   s    c         C   s   t  t j |  ƒ ƒ S(   N(   R   R   t   floor(   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   ifloor   s    c         C   s   t  |  d ƒ S(   Ng      à?(   R   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   nearest_integer   s    c         C   s   |  p
 | S(   s2    does p => q in the sense of logical implication? (    (   t   pt   q(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt
   does_imply   s    c         C   s   t  |  | ƒ o t  | |  ƒ S(   s3    does p <=> q in the sense of logical equivalence? (   R   (   R
   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   are_equivalent   s    t   nested_loopc           B   s)   e  Z d e d  „ Z d „  Z d „  Z RS(   c         C   s&  | d  k r" d g t | ƒ } n t | ƒ t | ƒ k s@ t ‚ | s‚ t | ƒ } x- t t | ƒ ƒ D] } | | c d 7<qe Wn  x4 t t | ƒ ƒ D]  } | | | | k s• t ‚ q• W| |  _ | |  _ t | ƒ |  _ xE t t | ƒ ƒ D]1 } | | | | k rí |  j d c d 8<Pqí qí Wd  S(   Ni    i   iÿÿÿÿ(   t   Nonet   lent   AssertionErrort   listt   xranget   begint   endt   current(   t   OR   R   t
   open_ranget   i(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   __init__   s     		c         C   s   |  S(   N(    (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   __iter__.   s    c         C   s„   |  j  } |  j } |  j } t | ƒ } xP | d k ry | d 8} | | c d 7<| | | | k  rh | S| | | | <q* Wt ‚ d  S(   Ni    i   (   R   R   R   R   t   StopIteration(   R   t   bt   et   cR   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   next1   s    			
 N(   t   __name__t
   __module__R   t   TrueR   R   R    (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyR      s   	c         C   sù   t  |  ƒ d k r t St  |  ƒ d } xÌ t rô | } | d 8} |  | |  | k  r× t  |  ƒ } x, t r� | d 8} |  | |  | k  rb Pqb qb W|  | |  | |  | <|  | <|  | } |  | 3| j ƒ  |  j | ƒ t S| d k r) |  j ƒ  t Sq) Wd S(   sF  Emulation of C++ std::next_permutation:
  Treats all permutations of seq as a set of "dictionary" sorted
  sequences. Permutes the current sequence into the next one of this set.
  Returns true if there are more sequences to generate. If the sequence
  is the largest of the set, the smallest is generated and false returned.
i   i    N(   R   t   FalseR#   t   reverset   extend(   t   seqR   t   iit   jt   tail(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   next_permutation=   s*     	
	



c         C   s_   d d  l  } t |  ƒ } x@ t | ƒ D]2 } | j | ƒ } |  | |  | |  | <|  | <q% Wd  S(   Niÿÿÿÿ(   t   randomR   R   t	   randrange(   R   R,   t   nR   R)   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   random_permutation_in_placeY   s
    c         C   s[   g  } d } xH |  d k rV x+ |  | d k rH | j  | ƒ |  | }  q W| d 7} q W| S(   s<   http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKatai   i   i    (   t   append(   R.   t   resultt	   candidate(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   prime_factors_of`   s    c         C   sn   | r d } n d t  j } t  j |  | ƒ }  |  d k  rG |  | 7}  n  | rj |  | d k rj |  | 8}  n  |  S(   Nih  i   i    (   R   t   pit   fmod(   t   phit   degt   zero_centeredt   period(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   normalize_anglek   s     	 gÛù~j¼tã?c         C   sß   t  |  ƒ }  t |  ƒ } | d k r( d S| d k r< |  d St t | | ƒ | d ƒ } t | | ƒ } | | k r} |  | S|  | } |  | } | | } | | } | | k s· t ‚ | | | | }	 | |	 | | }
 |
 S(   s±   
  See Pozharski (2010) Acta. Cryst. D66, 970-978.  The default value of the
  pbs_fraction parameter is for 3D geometries, and should be adjusted as
  circumstances dictate.
  i    i   (   t   sortedR   t   minR   R   R   (   t   valuest   pbs_fractionR.   t   i_hight   i_lowt   x_hight   x_lowt	   frac_hight   frac_lowt
   frac_deltat   x_frac(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   percentile_based_spreadt   s$      



(   t
   __future__R    R   R   R   R   R	   R   R   t   objectR   R+   R/   R3   R$   R:   RG   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/math_utils.pyt   <module>   s   						$				