ó
î&]\c           @` s»   d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z	 d d l
 m Z m Z m Z m Z g  Z d e f d „  ƒ  YZ d d d d d d d	 d
 d d e e d e d „ Z d S(   s   Trust-region optimization.i    (   t   divisiont   print_functiont   absolute_importNi   (   t   _check_unknown_optionst   wrap_functiont   _status_messaget   OptimizeResultt   BaseQuadraticSubproblemc           B` s}   e  Z d  Z d
 d
 d „ Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 d „  Z
 e d „  ƒ Z d „  Z d	 „  Z RS(   sQ  
    Base/abstract class defining the quadratic model for trust-region
    minimization. Child classes must implement the ``solve`` method.

    Values of the objective function, jacobian and hessian (if provided) at
    the current iterate ``x`` are evaluated on demand and then stored as
    attributes ``fun``, ``jac``, ``hess``.
    c         C` sg   | |  _  d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ | |  _ | |  _	 | |  _
 | |  _ d  S(   N(   t   _xt   Nonet   _ft   _gt   _ht   _g_magt   _cauchy_pointt   _newton_pointt   _funt   _jact   _hesst   _hessp(   t   selft   xt   funt   jact   hesst   hessp(    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyt   __init__   s    										c         C` s7   |  j  t j |  j | ƒ d t j | |  j | ƒ ƒ S(   Ng      à?(   R   t   npt   dotR   R   (   R   t   p(    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyt   __call__%   s    c         C` s.   |  j  d k r' |  j |  j ƒ |  _  n  |  j  S(   s1   Value of objective function at current iteration.N(   R
   R	   R   R   (   R   (    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyR   (   s    c         C` s.   |  j  d k r' |  j |  j ƒ |  _  n  |  j  S(   s=   Value of jacobian of objective function at current iteration.N(   R   R	   R   R   (   R   (    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyR   /   s    c         C` s.   |  j  d k r' |  j |  j ƒ |  _  n  |  j  S(   s<   Value of hessian of objective function at current iteration.N(   R   R	   R   R   (   R   (    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyR   6   s    c         C` s9   |  j  d  k	 r" |  j  |  j | ƒ St j |  j | ƒ Sd  S(   N(   R   R	   R   R   R   R   (   R   R   (    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyR   =   s    c         C` s1   |  j  d k r* t j j |  j ƒ |  _  n  |  j  S(   sA   Magniture of jacobian of objective function at current iteration.N(   R   R	   t   scipyt   linalgt   normR   (   R   (    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyt   jac_magC   s    c         C` s¤   t  j | | ƒ } d t  j | | ƒ } t  j | | ƒ | d } t j | | d | | ƒ } | t j | | ƒ } | d | }	 d | | }
 t |	 |
 g ƒ S(   sÀ   
        Solve the scalar quadratic equation ||z + t d|| == trust_radius.
        This is like a line-sphere intersection.
        Return the two values of t, sorted from low to high.
        i   i   iþÿÿÿ(   R   R   t   matht   sqrtt   copysignt   sorted(   R   t   zt   dt   trust_radiust   at   bt   ct   sqrt_discriminantt   auxt   tat   tb(    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyt   get_boundaries_intersectionsJ   s    	c         C` s   t  d ƒ ‚ d  S(   Ns9   The solve method should be implemented by the child class(   t   NotImplementedError(   R   R)   (    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyt   solvea   s    N(   t   __name__t
   __module__t   __doc__R	   R   R   t   propertyR   R   R   R   R"   R1   R3   (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyR      s   			g      ð?g     @@g333333Ã?g-Cëâ6?c   &      K` s~  t  | ƒ | d$ k r% t d ƒ ‚ n  | d$ k rL | d$ k rL t d ƒ ‚ n  | d$ k rg t d ƒ ‚ n  d |	 k o~ d k  n s’ t d ƒ ‚ n  | d k r­ t d ƒ ‚ n  | d k rÈ t d ƒ ‚ n  | | k rã t d	 ƒ ‚ n  t j | ƒ j ƒ  } t |  | ƒ \ } }  t | | ƒ \ } } t | | ƒ \ } } t | | ƒ \ } } | d$ k rkt | ƒ d
 } n  d } | } | } | r| g } n  | | |  | | | ƒ } d } x“| j	 |
 k rBy | j
 | ƒ \ } } Wn# t j j j k
 rý} d } Pn X| | ƒ } | | } | | |  | | | ƒ }  | j |  j }! | j | }" |" d k r_d } Pn  |! |" }# |# d k  r‚| d 9} n( |# d k rª| rªt d | | ƒ } n  |# |	 k rÅ| } |  } n  | rä| j t j | ƒ ƒ n  | d$ k	 r| t j | ƒ ƒ n  | d 7} | j	 |
 k  r)d } Pn  | | k r°d } Pq°q°Wt d t d d d f }$ | rê| d k r€t |$ | ƒ n t d |$ | ƒ t d | j ƒ t d | ƒ t d | d ƒ t d | d ƒ t d | d ƒ n  t d | d | d k d | d | j d | j d | d d | d d | d d  | d! |$ | ƒ 
}% | d$ k	 rg| j |% d" <n  | rz| |% d# <n  |% S(%   sÐ  
    Minimization of scalar function of one or more variables using a
    trust-region algorithm.

    Options for the trust-region algorithm are:
        initial_trust_radius : float
            Initial trust radius.
        max_trust_radius : float
            Never propose steps that are longer than this value.
        eta : float
            Trust region related acceptance stringency for proposed steps.
        gtol : float
            Gradient norm must be less than `gtol`
            before successful termination.
        maxiter : int
            Maximum number of iterations to perform.
        disp : bool
            If True, print convergence message.
        inexact : bool
            Accuracy to solve subproblems. If True requires less nonlinear
            iterations, but more vector products. Only effective for method
            trust-krylov.

    This function is called by the `minimize` function.
    It is not supposed to be called directly.
    s7   Jacobian is currently required for trust-region methodss_   Either the Hessian or the Hessian-vector product is currently required for trust-region methodssB   A subproblem solving strategy is required for trust-region methodsi    g      Ð?s   invalid acceptance stringencys%   the max trust radius must be positives)   the initial trust radius must be positives?   the initial trust radius must be less than the max trust radiusiÈ   i   i   g      è?i   t   successt   maxiters:   A bad approximation caused failure to predict improvement.s3   A linalg error occurred, such as a non-psd Hessian.s	   Warning: s#            Current function value: %fs            Iterations: %ds!            Function evaluations: %ds!            Gradient evaluations: %ds             Hessian evaluations: %dR   t   statusR   R   t   nfevt   njevt   nhevt   nitt   messageR   t   allvecsN(   R   R	   t
   ValueErrort	   ExceptionR   t   asarrayt   flattenR   t   lenR"   R3   R    t   LinAlgErrorR   t   mint   appendt   copyR   t   printR   R   R   (&   R   t   x0t   argsR   R   R   t
   subproblemt   initial_trust_radiust   max_trust_radiust   etat   gtolR9   t   dispt
   return_allt   callbackt   inexactt   unknown_optionst   nfunt   njact   nhesst   nhesspt   warnflagR)   R   R@   t   mt   kR   t   hits_boundaryt   et   predicted_valuet
   x_proposedt
   m_proposedt   actual_reductiont   predicted_reductiont   rhot   status_messagest   result(    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyt   _minimize_trust_regionf   s¨    


	
	&(    (   R6   t
   __future__R    R   R   R#   t   numpyR   t   scipy.linalgR   t   optimizeR   R   R   R   t   __all__t   objectR   R	   t   Falset   TrueRh   (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_trustregion.pyt   <module>   s   "X		