ó
[™xTc           @   s¶   d  d l  m Z d  d l m Z d  d l m Z d  d l m Z m Z d  d l	 m
 Z
 m Z d  d l m Z d „  Z d „  Z d	 „  Z e d
 „ Z d „  Z e d k r² e ƒ  n  d S(   iÿÿÿÿ(   t   division(   t   flex(   t   sparse(   t   normal_eqnst   normal_eqns_solving(   t   approx_equalt   Exception_expected(   t   test_problemsc          C   s  d d d f d d d f d d d f d d d f g }  t  j d ƒ } x< |  D]4 \ } } } | j d | d t j | ƒ d	 | ƒ qL Wt  j d ƒ } t j ƒ  } t j ƒ  } t j t |  ƒ d ƒ } xu t |  ƒ D]g \ } \ } } }	 | j	 | ƒ | j	 |	 ƒ x5 t
 d ƒ D]' }
 | |
 r| |
 | | |
 f <qqWqÐ W| j d | d
 | d | ƒ t | j ƒ  | j ƒ  d d ƒs~t ‚ t | j ƒ  | j ƒ  d d ƒs¥t ‚ t t | j ƒ  ƒ d d d d d d g d d ƒsÞt ‚ t t | j ƒ  ƒ d d d g d d ƒst ‚ d  S(   Ni   iÿÿÿÿi    i   i   iþÿÿÿt   right_hand_sidet   design_matrix_rowt   weightt   design_matrixt   weightst   epsgVçž¯Ò<i   iúÿÿÿi	   i   i   (   iÿÿÿÿi    i    (   i   iÿÿÿÿi    (   i    i   i   (   i    i   i    (   R   t	   linear_lst   add_equationR   t   doubleR   t   matrixt   lent	   enumeratet   appendt   xranget   add_equationsR   t   normal_matrix_packed_ut   AssertionErrorR   t   list(   t   py_eqst   eqs_0t   bt   at   wt   eqs_1t   it   b_t   a_t   w_t   j(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lstbx/tests/tst_normal_equations.pyt    exercise_linear_normal_equations	   s8    "
 $$6c          C   sà  t  j d t ƒ }  |  j ƒ  |  j |  j k s4 t ‚ d } t |  j ƒ  d | ƒ sX t ‚ t |  j	 ƒ  d | ƒ sv t ‚ t |  j
 ƒ  d | ƒ s” t ‚ |  j ƒ  j sª t ‚ y |  j ƒ  j ƒ  t ‚ Wn t k
 r× n Xy |  j ƒ  j ƒ  t ‚ Wn t k
 rn Xt t |  j ƒ  j ƒ  ƒ d d d d	 d
 d g | ƒ sBt ‚ t t |  j ƒ  j ƒ  ƒ d d d g | ƒ sut ‚ |  j ƒ  j ƒ  |  j ƒ  j sšt ‚ y |  j ƒ  j ƒ  t ‚ Wn t k
 rÇn Xy |  j ƒ  j ƒ  t ‚ Wn t k
 rõn Xt t |  j ƒ  j ƒ  ƒ d d d d d d g | ƒ s2t ‚ t t |  j ƒ  j ƒ  ƒ d d d g d d ƒsht ‚ t  j d t ƒ } | j ƒ  t | j	 ƒ  |  j	 ƒ  |  j ƒ  d d ƒsµt ‚ t | j
 ƒ  |  j
 ƒ  d d ƒsÜt ‚ d  S(   Nt
   normalisedg‚vIhÂ%,=gA!Ù<­ã?g‰ÅˆL¤?gæš¼wW2‡?g÷—ñïÍ×?g–O¾© Ù?gEwNr¤»?gø»E68ÊÚ?gL¨VÁ­´?g "3¡VMÉ?gÒ½oÞ‘¿?g&VÃ@7«Á?gØ—}O“Ë™¿ghjMÝ„ã?g©š{;‹ä?g9úO©Æ?gÝ²ÜàsE·?g{—NùÖ¿gBô8OÇÈ?gŽgR›ô?gd ãœºè¿gèöÒÃÊ{à¿R   gê-�™—q=gVçž¯Ò<(   R   t   polynomial_fitt   Falset   build_upt   n_equationst   n_dataR   R   t   optimal_scale_factort	   objectivet   chi_sqt   step_equationst   solvedt   cholesky_factor_packed_uR   t   RuntimeErrort   solutionR   R   R   t   solvet   Truet   sum_w_yo_sq(   t   testR   t   test_bis(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lstbx/tests/tst_normal_equations.pyt2   exercise_non_linear_ls_with_separable_scale_factor,   sp    


	

	
c          C   sÓ  t  j d t ƒ }  |  j ƒ  |  j |  j d k s8 t ‚ d } t |  j ƒ  d | ƒ s\ t ‚ |  j	 ƒ  } |  j
 ƒ  | j
 ƒ  k s† t ‚ |  j ƒ  j ƒ  j | j ƒ  j ƒ  ƒ s³ t ‚ |  j ƒ  j ƒ  j | j ƒ  j ƒ  ƒ sà t ‚ t |  j
 ƒ  d | ƒ sþ t ‚ t |  j ƒ  d | ƒ st ‚ t |  j ƒ  j ƒ  d | ƒ s@t ‚ t |  j ƒ  j ƒ  d | ƒ sdt ‚ t  j d t ƒ } | j ƒ  t | j ƒ  |  j ƒ  d d ƒs§t ‚ |  j } |  j ƒ  |  j | k sÏt ‚ d  S(   NR&   i   g‚vIhÂ%,=gA!Ù<­ã?g¸<ô«Aõ?g¸<ô«AÕ?gw•€·B‹û?gÕ.�±2d÷¿g:£[Lk2ù?gþe<Á{óõ?g4|Ø «ã¿gtç$×Aºñ?gþn‘Ž²ö?g~ö*ÕGjí¿g@d&Ôª)ó?R   gVçž¯Ò<(   gw•€·B‹û?gÕ.�±2d÷¿g:£[Lk2ù?(   gþe<Á{óõ?g4|Ø «ã¿gtç$×Aºñ?gþn‘Ž²ö?g~ö*ÕGjí¿g@d&Ôª)ó?(   R   t   polynomial_fit_with_penaltyR(   R)   R*   R+   R   R   R,   t   reduced_problemR-   R/   R   t   all_eqR   R.   R5   (   R7   R   t   reduR8   R*   (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lstbx/tests/tst_normal_equations.pyt?   exercise_non_linear_ls_with_separable_scale_factor_plus_penaltyq   s:    
  
'	
c         C   sÚ   |  j  ƒ  t j |  d t d d d d d d d d ƒ} |  j |  j k sO t ‚ t |  j |  j	 d	 d
 ƒsp t ‚ d | j
 GH| rÖ t d d ƒ } | d | j j ƒ  IJ| d | j j ƒ  IJ| d IJ| j ƒ  n  d  S(   Nt	   track_allt   gradient_thresholdg:Œ0âŽyE>t   step_thresholdt   taug-Cëâ6?t   n_max_iterationsiÈ   R   gü©ñÒMb@?s   L-M: %i iterationss   plot.nbR   s   g=%s;s	   \[Mu]=%s;s#   ListLogPlot[{g,\[Mu]},Joined->True](   t   restartR   t   levenberg_marquardt_iterationsR5   R*   R+   R   R   t   xt   arg_mint   n_iterationst   opent   gradient_norm_historyt   mathematica_formt
   mu_historyt   close(   t   non_linear_lst   plott
   iterationst   f(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lstbx/tests/tst_normal_equations.pyt   exercise_levenberg_marquardt–   s"    
	!
c          C   sV   d d  l  }  d |  j d k } t j ƒ  } t | | ƒ t ƒ  t ƒ  t ƒ  d GHd  S(   Niÿÿÿÿs   --ploti   t   OK(   t   syst   argvR   t   exponential_fitRR   R%   R9   R>   (   RT   RO   t   t(    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lstbx/tests/tst_normal_equations.pyt   run©   s    t   __main__N(   t
   __future__R    t   scitbx.array_familyR   t   scitbxR   t   scitbx.lstbxR   R   t   libtbx.test_utilsR   R   t   scitbx.lstbx.testsR   R%   R9   R>   R(   RR   RX   t   __name__(    (    (    sU   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/lstbx/tests/tst_normal_equations.pyt   <module>   s   	#	E	%	
