ó
Q™xTc           @   s	  d  d l  m Z d  d l m 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  d l m Z m Z d	 „  Z d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z e d k rd  d l Z e d e j d ƒ n  d S(   iÿÿÿÿ(   t   division(   t   bfgsN(   t   flex(   t   eigensystem(   t   matrix(   t   approx_equal(   t   cost   sinc         C   sq  t  j d d g ƒ } |  j | ƒ } t  j d d g d d g g ƒ } g  } t j | d | d | ƒ} t | | j | ƒ ƒ s„ t ‚ | d | } |  j | ƒ } t j d | d	 | | d
 | | ƒ } | j	 t j
 d | | d | | ƒ ƒ t j | d | d | ƒ}	 t |	 | j | ƒ ƒ s't ‚ | d |	 }
 |  j |
 ƒ } t j d | d	 |
 | d
 | | ƒ } | j	 t j
 d |
 | d | | ƒ ƒ t j | d | d | ƒ} t | | j | ƒ ƒ sÊt ‚ |
 d | } |  j | ƒ } t j d | d	 | |
 d
 | | ƒ } | j	 t j
 d | |
 d | | ƒ ƒ t j | d | d | ƒ} t | | j | ƒ ƒ smt ‚ d  S(   Ng      @g      Ài   i    t   hk0t   gki   t   hkt   skt   ykt   st   yi   i   gUUUUUUÕ?gš™™™™™É?g      Ø?(   R   t   doublet   gR   t   hg_two_loop_recursionR   t   matrix_multiplyt   AssertionErrort   h_updatet   appendt   memory_element(   t   fght   x0t   g0t   h0t   memoryt   hg0t   x1t   g1t   h1t   hg1t   x2t   g2t   h2t   hg2t   x3t   g3t   h3t   hg3(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyt   exercise_two_loop_recursion
   s0    !&'&'&'t   refineryc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   sS   | |  _  t j d d g ƒ |  _ |  j j ƒ  |  _ g  |  _ t j j	 d |  ƒ d  S(   Ni   iüÿÿÿt   target_evaluator(
   R   R   R   t   xt	   deep_copyt   prev_xR   t   scitbxt   lbfgst   run(   t   OR   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyt   __init__)   s
    		c         C   s(   |  j  j |  j ƒ |  j  j |  j ƒ f S(   N(   R   t   fR,   R   (   R2   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyt    compute_functional_and_gradients0   s    c            sÂ  |  j  ‰ |  j ‰ |  j j ˆ ƒ ‰  |  j j ˆ ƒ ‰ ‡  ‡ ‡ ‡ f d †  } t j d d g d d g g ƒ } | } | | | ƒ |  j j ˆ ƒ } t j | ƒ } | j	 ƒ  j
 d ƒ rÞ | j ƒ  } | j ƒ  | | | ƒ n  t j d d g d d g g ƒ } t j d |  j d	 | d
 ˆ ƒ } | } x5 |  j D]* }	 t j d | d |	 j d |	 j ƒ } q0W| j ˆ ƒ }
 t | |
 ƒ s‚t ‚ |  j j t j d ˆ ˆ d ˆ ˆ  ƒ ƒ |  j j ƒ  |  _  d  S(   Nc            s¸   t  j | ˆ ˆ ˆ ˆ  ƒ } t  j |  ˆ ˆ ˆ ˆ  ƒ } t t j | ƒ j ƒ  | ƒ s^ t ‚ t j	 | ƒ } | j
 ƒ  j d ƒ sˆ t ‚ t  j d ˆ ˆ d ˆ ˆ  ƒ d k s´ t ‚ d  S(   Ni    R   R   (   R   R   t   b_updateR   R   t   sqrt   inverseR   R   t   real_symmetrict   valuest   all_gtt
   h0_scaling(   t   bkR
   t   hlt   blt   es(   R	   t   glt   xkt   xl(    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyt   check8   s    $i   i    gÍÌÌÌÌÌì?gš™™™™™¹?gš™™™™™É¿gš™™™™™é?R   R   R	   R
   R   R   R   R   (   R.   R,   R   R   R   R   t   hR   R9   R:   R;   R-   t   matrix_inversion_in_placeR   R   R   R   R   R   R   R   R   R   R   (   R2   t	   minimizerRD   R=   R
   R@   R   t   hg_tlrt   h_updt   mt   hg_upd(    (   R	   RA   RB   RC   s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyt   callback_after_step3   s.    		!
!!(*(   t   __name__t
   __module__R3   R5   RL   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyR*   '   s   		t   fgh1c           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s<   | j  ƒ  d k s t ‚ | \ } } | d d | d d S(   Ni   i   (   t   sizeR   (   R2   R,   t   at   b(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyR4   X   s    c         C   s/   | \ } } t  j d | d d | d g ƒ S(   Ni   i   (   R   R   (   R2   R,   RQ   RR   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyR   ]   s    c         C   s   t  j d d g d d g g ƒ S(   Ni   i    (   R   R   (   R2   R,   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyRE   a   s    	(   RM   RN   R4   R   RE   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyRO   V   s   		t   fgh2c           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s>   | j  ƒ  d k s t ‚ | \ } } | d d t | d ƒ S(   Ni   i   (   RP   R   R   (   R2   R,   RQ   RR   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyR4   h   s    c         C   s1   | \ } } t  j d | d t | d ƒ g ƒ S(   Ni   i   (   R   R   R   (   R2   R,   RQ   RR   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyR   m   s    c         C   s6   | \ } } t  j d d g d t | d ƒ g g ƒ S(   Ni   i    (   R   R   R   (   R2   R,   RQ   RR   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyRE   q   s    	(   RM   RN   R4   R   RE   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyRS   f   s   		c         C   sX   t  |  ƒ d k s t ‚ x4 t t g D]& } t d | ƒ  ƒ t d | ƒ  ƒ q% Wd GHd  S(   Ni    R   t   OK(   t   lenR   RO   RS   R)   R*   (   t   argsR   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyR1   w   s
    t   __main__RV   i   (   t
   __future__R    t	   cctbx.omzR   t   scitbx.lbfgsR/   t   scitbx.array_familyR   t   scitbx.linalgR   R   t   libtbx.test_utilsR   t   mathR   R   R)   t   objectR*   RO   RS   R1   RM   t   syst   argv(    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/omz/tst_bfgs.pyt   <module>   s   	/	