ó
V™xTc           @   sŠ   d  d l  m Z d d d d e e d „ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d	 „ Z e	 d
 k r† e d e ƒ Z
 d GHn  d S(   iÿÿÿÿ(   t   divisioni    i   c         C   sÄ  d d l  m } | j } | j |  j ƒ }	 d  |	 _ | rd | d  k	 rd | j d |  j d | ƒ }
 n0 | r | j |  j ƒ }
 n t ‚ | j	 | ƒ }
 yþd } t
 } | } xå| r¿ | d 7} n  |  d | d | ƒ \ } } } } | r"| rd	 Gt | ƒ G| Gt | ƒ GHq"d
 Gt | ƒ G| GHn  | rwd d l m } | d  k r\| j | j ƒ  ƒ } n  |	 j | | | | ƒ } n |	 j | | | ƒ } | r°|	 j ƒ  } |	 j ƒ  } q¬ n  |	 j ƒ  sÃt ‚ |	 j ƒ  sÖt ‚ | r|	 j ƒ  | k r|
 | | ƒ rPqn |
 | ƒ rPn  | d  k	 r6|	 j ƒ  | k r6Pn  | rW|	 j | | | | ƒ } n |	 j | | | ƒ } | svPn  |	 j ƒ  } |	 j ƒ  } q¬ Wn" t k
 r¶} t | ƒ |	 _ n X| |	 _ |	 S(   Niÿÿÿÿ(   t   lbfgst   nt   epsi    i   t   requests_f_and_gt   requests_diags   x,f,d:s   x,f:(   t   flex(   t   scitbxR   t   extt	   minimizerR   t   Nonet   errort   traditional_convergence_testt   RuntimeErrort   drop_convergence_testt   Truet   tuplet   scitbx.array_familyR   t   doublet   sizet   runR   R   t   AssertionErrort   itert   strt   n_calls(   t   target_evaluatort   min_iterationst   max_iterationsR   t    traditional_convergence_test_epst   use_curvaturest   verboset   scitbx_lbfgsR   R	   t   is_convergedt   icallR   R   t   xt   ft   gt   dR   t   have_requestt   e(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lbfgs/tst_curvatures.pyt	   lbfgs_run   sn    		  !   	t   lbfgs_with_curvatures_mix_inc           B   s,   e  Z d  Z d d d e d „ Z d „  Z RS(   s  Mix in this class with your application-specific minimizer class.
     The app-specific class is required to implement:
       1. In the constructor
            a) define the free parameters as self.x = flex.double()
            b) last line: call the lbfgs_with_curvatures_mix_in constructor
       2. compute_functional_and_gradients() returns (double functional, flex.double gradients)
       3. curvatures() returns flex.double curvatures == diagonal elements of Hessian matrix

     Exposed variable traditional_convergence_test_eps gives some flexibility
     to the application program to specify the epsilon value for hitting
     convergence.  Choosing the right value avoids unnecessary minimization
     iterations that optimize the target functional to the last decimal place.
  i    iè  c         C   s@   t  |  j ƒ |  _ t d |  d | d | d | d | ƒ |  _ d  S(   NR   R   R   R   R   (   t   lenR"   R   R(   R	   (   t   selfR   R   R   R   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lbfgs/tst_curvatures.pyt   __init__Y   s    c         C   sÏ   | r | r t  } t  } n  | rG |  j ƒ  \ |  _ |  _ d  |  _ n  | r³ |  j ƒ  |  _ |  j d k } |  j j | d ƒ |  j j | ƒ j	 d ƒ s  t
 ‚ d |  j |  _ n  |  j |  j |  j |  j f S(   Ni    iè  i   (   R   t    compute_functional_and_gradientsR#   R$   R
   R%   t
   curvaturest   set_selectedt   selectt   all_gtR   R"   (   R+   R   R   t   sel(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lbfgs/tst_curvatures.pyt   __call__d   s    	!N(   t   __name__t
   __module__t   __doc__R
   R   R,   R3   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lbfgs/tst_curvatures.pyR)   J   s
   	t   fit_xy_translationc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sy  Elementary test case.  Six groups of xy observations generated by
     random Gaussian with mean = 0 and sigma = 1.  Fit the population means with
     six xy model means.  However, since the sample sizes are
     extremely different, convergence is slow if using a global least-squares
     target function.  Convergence speeds up considerably if curvatures
     are added.
  c         C   sÌ   | |  _  |  j ƒ  d d l m } d |  _ | j d g d ƒ |  _ t j |  d d d d d	 | ƒ|  j  rÈ g  |  j d d  d
 … D] } d | ^ q„ GHg  |  j d d  d
 … D] } d | ^ q° GHn  d  S(   Niÿÿÿÿ(   R   i    g        i   R   R   iè  R   i   s   %8.5fi   (	   R   t   create_test_dataR   R   t   count_iterationsR   R"   R)   R,   (   R+   R   R   R   t   a(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lbfgs/tst_curvatures.pyR,   �   s    	
		,c         C   sŠ   d d l  m } | j d g d ƒ } x] t d ƒ D]O } |  j | k } d | j t ƒ | d | <d | j t ƒ | d | d <q3 W| S(	   Niÿÿÿÿ(   R   g        i   i   g       @i   i   (   R   R   R   t   xranget   master_groupst   countR   (   R+   R   t   curvsR"   t	   selection(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lbfgs/tst_curvatures.pyR.   �   s    #c   
      C   sÁ  d d l  m } d d  l } | j t |  j ƒ ƒ |  _ | j t |  j ƒ ƒ |  _ xc t d ƒ D]U } |  j	 | k } |  j j
 | |  j d | ƒ |  j j
 | |  j d | d ƒ q_ W|  j |  j } |  j |  j } | | | | } | j | ƒ } | j d g d ƒ }	 xo t d ƒ D]a } |  j	 | k } d | j | j | ƒ ƒ |	 d | <d | j | j | ƒ ƒ |	 d | d <qW|  j r¨d	 G| j | j | ƒ ƒ GHn  |  j d 7_ | |	 f S(
   Niÿÿÿÿ(   R   i   i   i   g        i   g       Às   Functional (   R   R   t   mathR   R*   t
   observed_xt   model_mean_xt   model_mean_yR;   R<   R/   R"   t
   observed_yt   sumR0   R   t   sqrtt   meanR9   (
   R+   R   R@   R"   R?   t   delxt   delyt   delrsqR#   t	   gradients(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lbfgs/tst_curvatures.pyR-   ™   s*    &$,	c         C   sÄ  d d l  m } | j ƒ  |  _ | j ƒ  |  _ | j ƒ  |  _ d } d d  l } | j d ƒ x‰ d d d d d	 d
 g D]o } x\ t	 | ƒ D]N } |  j j
 | j d d ƒ ƒ |  j j
 | j d d ƒ ƒ |  j j
 | ƒ qˆ W| d 7} qu W| j ƒ  |  _ | j ƒ  |  _ xm t	 d ƒ D]_ } |  j | k } |  j j
 | j |  j j | ƒ ƒ ƒ |  j j
 | j |  j j | ƒ ƒ ƒ qW|  j rÀg  |  j D] } d | ^ q‰GHg  |  j D] } d | ^ q¨GHn  d  S(   Niÿÿÿÿ(   R   i    g        i0u  i¨a  i N  i'  iÐ  i   g      ð?i   i   s   %8.5f(   R   R   R   RA   RD   t   intR<   t   randomt   seedR;   t   appendt   gausst   known_mean_xt   known_mean_yRG   R0   R   (   R+   R   t   igroupRM   t   group_NR"   R?   R:   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lbfgs/tst_curvatures.pyR8   µ   s,    %)	(   R4   R5   R6   R,   R.   R-   R8   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lbfgs/tst_curvatures.pyR7   x   s
   				c         C   sX   t  d t d |  ƒ } | j d k  s* t ‚ t  d t d |  ƒ } | j d k sT t ‚ d  S(   NR   R   i
   i(   (   R7   R   R9   R   t   FalseR
   (   R   t   C(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lbfgs/tst_curvatures.pyR   Ï   s
    t   __main__R   t   OKN(   t
   __future__R    R
   RU   R(   t   objectR)   R7   R   R4   t   result(    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lbfgs/tst_curvatures.pyt   <module>   s   :.W	