ó
]xTc           @   s|  d  d l  m Z d  d l Td  d l Z d  d l Z d  d l m Z d  d l m Z d  d l	 m
 Z
 d  d l m Z d e _ d	   Z d
 e f d     YZ d   Z d   Z d   Z d d d  Z e d  Z d e f d     YZ d e f d     YZ d d d$ d  Z d d d d  Z d d d  Z d d d d  Z d e f d      YZ d!   Z  d d d"  Z! d#   Z" d S(%   i’’’’(   t   division(   t   *N(   t   matrix(   t   flex(   t   rational(   t   maths  
Fits a gaussian function to a list of points.

Parameters
----------
x : scitbx.array_family.flex.double
y : scitbx.array_family.flex.double

Returns
-------
scitbx.math.gaussian_fit_1d_analytical

Examples
--------
>>> from scitbx.array_family import flex
>>> from scitbx.math import gaussian_fit_1d_analytical
>>> x, y = flex.double(), flex.double()
>>> x.append(-1) ; y.append(.1)
>>> x.append(0) ; y.append(1)
>>> x.append(1) ; y.append(.1)
>>> fit = gaussian_fit_1d_analytical(x, y)
>>> print fit.a, fit.b
1.0 2.30258509299
c         C   s   t  j j |   S(   N(   R   t   mediant
   dispersion(   t   data(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   median_statistics#   s    t   line_given_pointsc           B   s#   e  Z d    Z d   Z d   Z RS(   c         C   s\   g  | D] } t  j |  ^ q |  _ |  j d |  j d |  _ t |  j j    |  _ d  S(   Ni   i    (   R   t   colt   pointst   deltat   floatt   norm_sqt   delta_norm_sq(   t   selfR   t   point(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   __init__(   s    %c         C   sL   |  j  d k r$ | |  j d j   S|  j j | |  j d  j   |  j  S(   sA   http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.htmli    (   R   R   R   R   t   cross(   R   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   distance_sq-   s     c         C   sA   |  j  d | j |  j  |  j j   } |  j  d |  j | S(   Ni    (   R   t   dotR   R   (   R   R   t   t(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   perp_xyz4   s    +(   t   __name__t
   __module__R   R   R   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyR
   &   s   		c         C   s2   t  t |  d |  d |  d d d d  d  S(   Ni    i   i   g      š?g       @g      šæ(   t   maxt   min(   t   r(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt*   r3_rotation_cos_rotation_angle_from_matrix8   s    (c         C   sY   t  |   } | d k  r= t j |  } | t j |  } n d } d | |  |  j   S(   s   
  Chirikjian, G. Stochastic models, information theory, and Lie groups.
  Volume 2, pp 39-40.
  Applied and Numerical Harmonic Analysis, Birkhauser
  g      š?g      ą?(   R   R   t   acost   sint	   transpose(   R   t	   cos_thetat   thetat   a1(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   r3_rotation_matrix_logarithm?   s    c          G   sq   |  s t  d  n  d d l } d d l } t | j g  |  D] } | j j t |   ^ q:  } t | j	    S(   sż   
  Calculates an approximation of the Riemannian (geometric) mean through
  quaternion averaging

  Arithmetic and geometric solutions for average rigid-body rotation (2010).
  I Sharf, A Wolf & M.B. Rubin, Mechanism and Machine Theory, 45, 1239-1251
  s   Average of empty sequencei’’’’N(
   t	   TypeErrort   operatort   scitbx.matrixt   reducet   addR   R   t%   r3_rotation_matrix_as_unit_quaterniont%   r3_rotation_unit_quaternion_as_matrixt	   normalize(   t   matricesR'   t   scitbxt   et   average(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt1   r3_rotation_average_rotation_matrix_from_matricesQ   s    	+id   g-Cėā6?c      
   C   s  |  s t  d  n  | d k  s* | d k r6 t d  n  d d l m } d d l } | | } d t |   } |  d } x t |  D]t } | j   }	 | t | j	 g  |  D] }
 | j
 j d |	 |
  ^ qØ  } | j   | k  rē | S| | j   9} q Wt d	  d S(
   s,  
  Calculates an approximation of the Riemannian (geometric) mean by iteratively
  averaging the rotation matrix logarithms

  Nonparametric Second-order Theory of Error Propagation on Motion Groups (2008).
  Y. Wang & G. S. Chirikjian, The International Journal of Robotics Research 27, 1258-1273
  s   Average of empty sequencei    s   Invalid iteration parametersi’’’’(   t   so3_lie_algebraNg      š?R   s   Iteration limit exceeded(   R&   t
   ValueErrort   scitbx.mathR3   R'   t   lent   ranget   inverseR)   R*   t   elementt   from_rotation_matrixR   t   exponentialt   RuntimeError(   R.   t   maxitert   convergenceR3   R'   t   conv_sqt   normt   currentt   iR8   t   mt   log_diff(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt,   r3_rotation_average_rotation_via_lie_algebrah   s&    	

-c         C   s`  | r- g  |  D] } | t  j d ^ q }  n  g  |  D] } t  j |  ^ q4 } g  |  D] } t  j |  ^ qV } t j | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d f	  S(   Ni“   i    i   i   (   R   t   pit   cosR    R   t   sqr(   t   anglest   degt   at   ct   s(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   euler_angles_as_matrix   s    '""'('(t   erf_verificationc           B   s   e  Z d  d  Z d   Z RS(   g»½×Łß|Ū=c         C   s   | |  _  d |  _ d  S(   Ni    (   t	   tolerancet	   max_delta(   R   RP   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyR   ”   s    	c         C   s  | |  } t  |  t  |  k rt  |  j   j d  } t  |  j   j d  } t |  t |  k sx t  t |  d k r§ | d | d k s§ t  n  t t | d  t | d   } |  j | k  r| |  _ | |  j k r	| G| G| G| GHq	qn  d  S(   NR0   i   i   i    (	   t   strt   lowert   splitR6   t   AssertionErrort   absR   RQ   RP   (   R   t   ft   xt   expected_resultt   resultt   met   mexR   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   __call__„   s    $	(   R   R   R   R]   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyRO      s   t   minimum_covering_sphere_ndc           B   s5   e  Z d d  d d  Z d   Z d   Z d   Z RS(   i   i    c         C   ss  t  |  d k s$ | d k s$ t  | d  k r9 d } n  d |  _ t  |  d k rj | |  _ | |  _ d  St  |  d k r t | d  |  _ | |  _ d  St  | d j  } d t  |  } t j	 g  t
 t  |   D] } | ^ qŲ  } xjt j d g |  }	 x. t | j |  D] \ } }
 |	 | |
 7}	 qWt j g  | D] }
 t |	 |
  ^ qD } d } x5 t | j | j  D] \ } } | | | d 7} qWt j |  } | j   } | | | | k  rŌPn  g  } x4 t | j | j  D] \ } } | j | |  qšWt j |  } | j   } | d k s>t  | | } |  j d 7_ qš |	 j |  _ | |  _ d  S(   Ni    gķµ ÷Ę°>i   g      š?i   (   R6   RU   t   Nonet   _n_iterationst   _centert   _radiust   tuplet   elemsR   t   rowt   xrangeR   t   zipRV   R   t   sqrtR   t   appendt   sum(   R   R   t   epsilont   radius_if_one_or_no_pointst   center_if_no_pointst   n_dimt   wRB   t   weightsRX   R   t   radiit   sigmaR   t   taut   w_rt   sum_w_r(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyR   µ   sJ    $ 					.,""
c         C   s   |  j  S(   N(   R`   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   n_iterationsß   s    c         C   s   |  j  S(   N(   Ra   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   centerā   s    c         C   s   |  j  S(   N(   Rb   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   radiuså   s    N(   i    i    i    (   R   R   R_   R   Rv   Rw   Rx   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyR^   ³   s   &		i   i    c      	   C   sU   | d  k r d } n  t |  t j  r0 t } n t } | d |  d | d | d |  S(   Ngķµ ÷Ę°>R   Rk   Rl   Rm   (   R_   t
   isinstanceR   t   vec3_doublet   minimum_covering_sphere_3dR^   (   R   Rk   Rl   Rm   t   impl(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   minimum_covering_sphereč   s     		c         C   s=   | d  k	 r' | j   d k s' t  n  t j |  | | |  S(   Ni   (   R_   t   ndRU   t   extt!   row_echelon_back_substitution_int(   t   row_echelon_formt   vt   solutiont   independent_flags(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyR   ų   s    c         C   sL   | d  k	 s t  | d  k	 r9 | j   d k s9 t  n  t j |  | |  S(   Ni   (   R_   RU   R~   R   t#   row_echelon_back_substitution_float(   R   R   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyR     s
    gź-q=i
   c         C   s  t  |  t j  r! |  j   }  n  t  | t j  r] | j   d k sK t  t j |  } n  | d	 k rr d } n, t	 |  j
    t j	 t j |    | } t	 | |  } | | } |  j   } | j   } t d | d | d |  }	 |	 j d k r |	 j d t j |	 j  d |  }
 nš |  j   s2t  |  j   } t j j j |  } | j   } | j   } | j |  j |  } | j |  } |	 j } t d | d | d |  }	 |	 j | k sĖt  |	 j d t j |	 j d  d |  }
 |
 d	 k	 r| j |
  }
 n  |
 S(
   s  Assumes a is symmetric, without checking to avoid overhead.

Special case of
  generalized_inverse(a) * b
taking advantage of the fact that a is real and symmetric.

Opportunistic algorithm: first assumes that a has full rank. If this
is true, solves a*x=b for x using simple back-substitution.

Only if a is rank-deficient:
  To obtain the x with minimum norm, transforms a to a basis formed
  by its eigenvectors, solves a*x=b in this basis, then transforms
  x back to the original basis system.

Returns None if a*x=b has no solution.
i   i    t   a_workt   b_workt   min_abs_pivott   free_valuesRk   t   max_rankN(   Ry   R   t   rect   as_flex_double_matrixt	   n_columnsRU   R   t   doubleR_   R   t   allRV   t	   deep_copyt   row_echelon_full_pivotingt   nullityt   back_substitutiont   is_square_matrixR/   t   linalgt   eigensystemt   real_symmetrict   vectorst   matrix_transposet   matrix_multiplyt   rank(   RK   t   bt   relative_min_abs_pivott   absolute_min_abs_pivott    back_substitution_epsilon_factorR   Rk   t   awt   bwt   efRX   t   esRL   t   ctR   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt)   solve_a_x_eq_b_min_norm_given_a_sym_b_col  s@    	,
			t   finite_difference_computationc           B   s    e  Z d  Z d   Z d   Z RS(   s°   Computing the derivative of a scalar function f(x, ...) wrt x,
  where x may be a vector, involves computing f(x + h, ...)
  where h is suitably chosen a delta. So as to keep numerical accuracy, it
  is best to follow a well known recipe to choose h knowing its
  direction. When using that value, a finite difference computation typically
  reaches a precision of self.precision

  Reference: chap 5.7 of Numerical Recipes in C
  c         C   s.   t  j t j  j j  } t  j |  |  _ d  S(   N(   R   Rh   R/   t   double_numeric_limitsRk   t	   precision(   R   R0   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyR   S  s    c         C   s<   | |  j  } } | t |  } | | } | | | } | S(   sĀ    Compute the value h in the given direction that is most suitable
    for use in finite difference computation around the given point x.
    The vector 'direction' may not be a unit-vector.
    (   RØ   RV   (   R   R   t	   directionRX   RM   t   ut   h(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt
   best_deltaW  s
    
(   R   R   t   __doc__R   R¬   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyR¦   H  s   		c         C   s   t  j } t  j } | t  j d } | t  j d } | d |  | |  | |  } | d |  | |  | |  }	 | d |  | |  }
 | |	 |
 f S(   sj   
  Returns a point on sphere of radius r: see definition of polar (sperical)
  coordinate system in 3D.
  i“   i    i   i   (   R   R    RG   RF   (   R   t   s_degt   t_degRw   R    RG   RM   R   RX   t   yt   z(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   point_on_sphereb  s    		""c   
      C   sA  | | g j  d   d k s! t  | d |  d | d |  d | d |  d g } t j | g  } | d  k	 r | d k sÜ t  nU | d k s t  t j | d d | d d | d d  } t | |  d } | d t | d  } t j   } x- t	 | d  D] }	 | j
 | |	 |   qW| j |  | S(   Ni   i    i   (   t   countR_   RU   R   Rz   R   Rh   t   intR   Rf   t   extendRi   (
   t   startt   endt   nt   stept   vecR   t
   vec_lengtht   drR   RB   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   equally_spaced_points_on_vectorp  s    !3/c         C   s%  t  |   } t |  } | d rS | | d } | | d d  } | | d } n> | | d | | d d d } | | d  } | | d } t |  } | d rĘ | | d } | | d } nD | | d | | d d d } | | d | | d d d } | d | | | | d f S(   sŗ   
  Returns the Tukey five number summary (min, lower hinge, median, upper hinge,
  max) for a sequence of observations. This function gives the same results
  as R's fivenum function.
  i   i   i    i’’’’(   t   sortedR6   (   R   t   sortsRø   t   medRS   t   uppert   lhinget   uhinge(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   five_number_summary  s     
"
""(   i    i    i    (#   t
   __future__R    t   scitbx.math.extt   scitbx.linalg.eigensystemR/   t   scitbx.math.gaussianR   t   scitbx.array_familyR   t   boostR   t   stdlibR   t   gaussian_fit_1d_analyticalR­   R	   t   objectR
   R   R%   R2   RE   t   FalseRN   RO   R^   R_   R}   R   R   R„   R¦   R²   R½   RÄ   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/__init__.pyt   <module>   s@   
					%7	8	