ó
]™xTc           @   sÁ   d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l m Z d „  Z d „  Z	 d d e
 d	 „ Z e d
 k r½ e j d ƒ x- e d ƒ D] Z e d d d e d e
 ƒ q’ Wd GHn  d S(   iÿÿÿÿ(   t   divisionN(   t   matrix(   t   flexc         C   sa   | t  j t  j | ƒ |  ƒ t t  j |  | ƒ t  j |  | ƒ ƒ } | | j ƒ  t  j | ƒ S(   N(   t   matht   powt   sint   complext   cost	   conjugate(   t   lmt   ct   tt   pt   y(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/tests/tst_uniform_rotation_matrix.pyt   ylm	   s    Fc         C   si   t  j d d d g ƒ } | | } t j | d ƒ } t j | d | d ƒ t j } t |  | | | ƒ S(   Ni    i   i   (   R   t   colR   t   acost   atan2t   piR   (   R	   R
   t   Rt   xt   new_xt   thetat   phi(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/tests/tst_uniform_rotation_matrix.pyt	   add_point   s
    
!i'  i   c   
   
   C   sË  | d k r/ d } d t  j d t  j ƒ } nO | d k r^ d } d t  j d t  j ƒ } n  d	 } d t  j d t  j ƒ } d d g } x{ t |  ƒ D]m } t j t j ƒ  ƒ t j t j ƒ  ƒ g } x: t	 t
 | ƒ ƒ D]& } | | c t | | | | ƒ 7<qÚ Wq— Wd t  j |  }	 x= t	 t
 | ƒ ƒ D]) } |	 | | | | <| r,| | Gq,q,W| rcHn  | d j d k s|t ‚ | d j d k  s•t ‚ | d j d k s®t ‚ | d j d k  sÇt ‚ d S(   sÿ  
  The surface integral of a spherical harmonic function with its conjugate
  should be 1. (http://mathworld.wolfram.com/SphericalHarmonic.html, Eq 7)

  From Mathematica,

  l = 10;
  m = 10;
  y = SphericalHarmonicY[l, m, \[Theta], \[Phi]];
  Integrate[y*Conjugate[y]*Sin[\[Theta]], {\[Theta], 0, Pi}, {\[Phi], 0, 2*Pi}]

  should yield 1.

  By picking uniformly random points on a sphere, the surface integral can be
  numerically approximated.

  The results in the comments below are for N = 1 000 000.
  i    i   g      à¿g      ø?i   i   i    iM   i
   i   iñÌ g        g      @g333333ë?gffffffò?Ng      ¸?g      ¸¿g      P?(   R   t   sqrtR   t   rangeR   t   sqrR   t    random_double_r3_rotation_matrixt*   random_double_r3_rotation_matrix_arvo_1992t   xranget   lenR   t   realt   AssertionError(
   t   Nt   choicet   verboseR	   R
   t   resultt   iR   t   jt
   point_area(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/tests/tst_uniform_rotation_matrix.pyt   test_uniform_rotation_matrix   s2    (t   __main__i    i   R"   iè  R#   R$   t   OK(   t
   __future__R    R   t   randomt   scitbxR   t   scitbx.array_familyR   R   R   t   FalseR)   t   __name__t   set_random_seedR   R&   (    (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/tests/tst_uniform_rotation_matrix.pyt   <module>   s   		8