ó
n™xTc           @   sP   d  Z  d d l m Z d d l m Z d d l m Z d e f d „  ƒ  YZ d S(   s/   
Handling for normalized target and gradients.
iÿÿÿÿ(   t   division(   t   math(   t   Sorryt   energiesc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sé   
  Convenient wrapper class for encapsulating the output of any arbitrary
  restraining target function, such as the geometry restraints used elsewhere
  in CCTBX.  Tracks the number of restraints to facilitate normalized targets.
  c         C   sš   d |  _  d |  _ | s$ d  |  _ n` | d  k rT | d  k	 sB t ‚ | | ƒ |  _ n0 | d  k	 r{ | j ƒ  | k s{ t ‚ n  | |  _ | |  _ d  |  _ d  S(   Ni    (   t   number_of_restraintst   residual_sumt   Nonet	   gradientst   AssertionErrort   sizet   normalizationt   normalization_factor(   t   Ot   compute_gradientsR   t   gradients_sizet   gradients_factoryR
   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/restraints.pyt   __init__   s    				c         C   s‚   |  j  | j  7_  |  j | j 7_ |  j d  k	 r~ | j d  k	 sH t ‚ |  j j ƒ  | j j ƒ  k r~ |  j | j 7_ q~ n  |  S(   N(   R   R   R   R   R   t   id(   R   t   rhs(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/restraints.pyt   __iadd__%   s    c         C   sD   | d k r- t  j | ƒ r- t d ƒ ‚ q- n  t j |  | | ƒ d  S(   Nt   targets   target value (energy) is "nan"(   R   t   isnanR   t   objectt   __setattr__(   t   selft   attrt   value(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/restraints.pyt   __setattr1__.   s    c         C   sk   |  j  |  _ |  j rg d t d |  j ƒ |  _ |  j |  j 9_ |  j d  k	 rg |  j |  j 9_ qg n  d  S(   Ng      ð?i   (   R   R   R
   t   maxR   R   R   R   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/restraints.pyt   finalize_target_and_gradients4   s    	(   t   __name__t
   __module__t   __doc__R   R   R   R   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/restraints.pyR   
   s
   				N(	   R    t
   __future__R    t   stdlibR   t   libtbx.utilsR   R   R   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/restraints.pyt   <module>   s   