ó
5™xTc           @   s÷   d  d l  m Z d  d l m Z d  d l m Z d  d l m Z d  d l m	 Z	 d  d l
 Z
 d  d l m Z d  d l Z e j d ƒ d	 e
 j d
 Z d d d „  ƒ  YZ d d „ Z d d „ Z d „  Z d „  Z e d k ró e e j d ƒ n  d S(   iÿÿÿÿ(   t   division(   t   matrix(   t   flex(   t'   tensor_rank_2_gradient_transform_matrix(   t   approx_equalN(   t   StringIOi    iþÿÿÿi   t   debye_wallerc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   ut   opst   hkl(   t   selfR   R   R	   (    (    si   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/structure_factor_calculus/u_star_derivatives.pyt   __init__   s    		c         C   s•   d } xˆ |  j  D]} } | t j d |  j ƒ | j ƒ  j ƒ  } t j |  j ƒ t j d | ƒ j t j	 |  j ƒ ƒ } | t
 j t | ƒ 7} q W| S(   Ni    t   sym_mat3(   R   R   t   symR   t	   transposet   as_sym_mat3t   rowR	   t   dott   colt   matht   expt   mtps(   R
   t   resultt   opt   op_ut   huh(    (    si   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/structure_factor_calculus/u_star_derivatives.pyt   f   s    )(c         C   s*  t  j d d ƒ } |  j \ } } } t j | d | d | d d | | d | | d | | g ƒ } xº |  j D]¯ } | t j d |  j ƒ | j ƒ  j	 ƒ  } t j
 |  j ƒ t j d | ƒ j t j |  j ƒ ƒ } t j t | ƒ t | }	 t | ƒ }
 |
 j t  j |	 ƒ ƒ } | | 7} qs W| S(   Ni   i    i   R   (   R   t   doubleR	   R   R   R   R   R   R   R   R   R   R   R   R   R   t   matrix_multiply(   R
   R   t   ht   kt   lt   d_exp_huh_d_uR   R   R   t   d_op_ut   gtmxt   d_u(    (    si   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/structure_factor_calculus/u_star_derivatives.pyR#      s    E)(c         C   sO  t  j t  j d d ƒ d ƒ } |  j \ } } } t  j | d | d | d d | | d | | d | | g ƒ } | j | ƒ } xÄ |  j D]¹ } | t j d |  j ƒ | j	 ƒ  j
 ƒ  } t j |  j ƒ t j d | ƒ j t j |  j ƒ ƒ }	 t j t |	 ƒ t d | }
 t | ƒ } | j |
 ƒ j | j ƒ  ƒ } | | 7} qŽ W| S(   Ni   i    i   R   (   R   R   t   gridR	   t   matrix_outer_productR   R   R   R   R   R   R   R   R   R   R   R   R   R   t   matrix_transpose(   R
   R   R   R   R   R    t   d2_exp_huh_d_uuR   R   R   t   d2_op_uR"   t   d2_u(    (    si   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/structure_factor_calculus/u_star_derivatives.pyR)   ,   s    E)((   t   __name__t
   __module__R   R   R#   R)   (    (    (    si   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/structure_factor_calculus/u_star_derivatives.pyR      s   				g:Œ0âŽyE>c   
   	   C   sÉ   t  j ƒ  } t |  ƒ } xª t d ƒ D]œ } g  } x^ | | g D]O } |  | | | | <t d t j | ƒ d | d | ƒ }	 | j |	 j ƒ  ƒ q? W|  | | | <| j | d | d d | ƒ q% W| S(   Ni   R   R   R	   i    i   i   (	   R   R   t   listt   xrangeR   R   R   t   appendR   (
   R   R   R	   t   epsR   t   u_epst   ipt   vst
   signed_epst   a(    (    si   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/structure_factor_calculus/u_star_derivatives.pyt   d_debye_waller_d_u_finite<   s    $%c   
   	   C   sÉ   t  j ƒ  } t |  ƒ } xª t d ƒ D]œ } g  } x^ | | g D]O } |  | | | | <t d t j | ƒ d | d | ƒ }	 | j |	 j ƒ  ƒ q? W|  | | | <| j	 | d | d d | ƒ q% W| S(   Ni   R   R   R	   i    i   i   (
   R   R   R,   R-   R   R   R   R.   R#   t   extend(
   R   R   R	   R/   R   R0   R1   R2   R3   R4   (    (    si   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/structure_factor_calculus/u_star_derivatives.pyt   d2_debye_waller_d_u_finiteI   s    $%c         C   sB   t  d t j  t j |  ƒ ƒ ƒ } t |  | | | ƒ s> t ‚ d  S(   Ni   (   t   maxR   t   absR   t   AssertionError(   t   anat   fint   s(    (    si   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/structure_factor_calculus/u_star_derivatives.pyt   compare_derivativesV   s    !c      	   C   s½  d |  k } | s t  ƒ  } n	 t j } xŠt d ƒ D]|} g  } x@ t d ƒ D]2 } | j t j t j d d d d ƒ d ƒ ƒ qM Wt j	 t j d d	 d d ƒ d
 d ƒ } t j
 t j d d d d ƒ d ƒ } t d | d | d | ƒ } t d | d | d | ƒ }	 | d It |	 ƒ IJ| j ƒ  }
 | d It |
 ƒ IJt |
 |	 ƒ t d | d | d | ƒ } | d It | ƒ IJ| j ƒ  } | d It | ƒ IJt | | ƒ | Jq4 Wd GHd  S(   Ns	   --verboseid   i   t   sizei	   t   factori   i   i   i   gü©ñÒMbP?R   R   R	   s
   grads_fin:s
   grads_ana:s
   curvs_fin:s
   curvs_ana:t   OK(   R   t   syst   stdoutR-   R.   R   t   sqrR   t   random_doubleR   R   R   R5   R,   R#   R>   R7   R)   (   t   argst   verboset   outt   i_trialR   t   iR   R	   t   dwt	   grads_fint	   grads_anat	   curvs_fint	   curvs_ana(    (    si   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/structure_factor_calculus/u_star_derivatives.pyt   exerciseZ   s.    	0)%t   __main__i   (    (   t
   __future__R    t   scitbxR   t   scitbx.array_familyR   t   scitbx.mathR   t   libtbx.test_utilsR   R   t	   cStringIOR   RB   t   set_random_seedt   piR   R   R5   R7   R>   RP   R*   t   argv(    (    (    si   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/structure_factor_calculus/u_star_derivatives.pyt   <module>   s    .		