
ʽXc           @` s  d  Z  d d l m Z m Z m Z d d l m Z d d l Z d d l	 m
 Z
 d d l m Z m Z m Z d d l m Z d d l m Z m Z e j e  j Z d	   Z d d
 d d  Z d   Z d   Z d d d  Z d d  Z d d  Z d   Z  d   Z! d d  Z" d d  Z# d   Z$ d   Z% d   Z& d   Z' d   Z( d   Z) d   Z* d d  Z+ d   Z, d    Z- d!   Z. e/ d"  Z0 e/ d#  Z1 d$   Z2 d%   Z3 d S(&   s+   Functions used by least-squares algorithms.i    (   t   divisiont   print_functiont   absolute_import(   t   copysignN(   t   norm(   t
   cho_factort	   cho_solvet   LinAlgError(   t   issparse(   t   LinearOperatort   aslinearoperatorc   
      C` s   t  j | |  } | d k r- t d   n  t  j |  |  } t  j |  |   | d } | d k rt t d   n  t  j | | | |  } | t | |  } | | } | | }	 | |	 k  r | |	 f S|	 | f Sd S(   s}  Find the intersection of a line with the boundary of a trust region.
    
    This function solves the quadratic equation with respect to t
    ||(x + s*t)||**2 = Delta**2.
    
    Returns
    -------
    t_neg, t_pos : tuple of float
        Negative and positive roots.
    
    Raises
    ------
    ValueError
        If `s` is zero or `x` is not within the trust region.
    i    s   `s` is zero.i   s#   `x` is not within the trust region.N(   t   npt   dott
   ValueErrort   sqrtR   (
   t   xt   st   Deltat   at   bt   ct   dt   qt   t1t   t2(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   intersect_trust_region   s    


g{Gz?i
   c	      	   C` s  d   }	 | | }
 | |  k rD t  | | d } | d | k } n t } | r | j | |  } t |  | k r | d d f Sn  t |
  | } | r |	 d |
 | |  \ } } | | } n d } | d	 k s | r| d k rt d | | | d  } n | } x t |  D] } | | k  s9| | k rWt d | | | d  } n  |	 | |
 | |  \ } } | d k  r| } n  | | } t | | |  } | | | | | 8} t j |  | | k  rPqqW| j |
 | d |  } | | t |  9} | | | d f S(
   s  Solve a trust-region problem arising in least-squares minimization.
    
    This function implements a method described by J. J. More [1]_ and used
    in MINPACK, but it relies on a single SVD of Jacobian instead of series
    of Cholesky decompositions. Before running this function, compute:
    ``U, s, VT = svd(J, full_matrices=False)``.
    
    Parameters
    ----------
    n : int
        Number of variables.
    m : int
        Number of residuals.
    uf : ndarray
        Computed as U.T.dot(f).
    s : ndarray
        Singular values of J.
    V : ndarray
        Transpose of VT.
    Delta : float
        Radius of a trust region.
    initial_alpha : float, optional
        Initial guess for alpha, which might be available from a previous
        iteration. If None, determined automatically.
    rtol : float, optional
        Stopping tolerance for the root-finding procedure. Namely, the
        solution ``p`` will satisfy ``abs(norm(p) - Delta) < rtol * Delta``.
    max_iter : int, optional
        Maximum allowed number of iterations for the root-finding procedure.
    
    Returns
    -------
    p : ndarray, shape (n,)
        Found solution of a trust-region problem.
    alpha : float
        Positive value such that (J.T*J + alpha*I)*p = -J.T*f.
        Sometimes called Levenberg-Marquardt parameter.
    n_iter : int
        Number of iterations made by root-finding procedure. Zero means
        that Gauss-Newton step was selected as the solution.
    
    References
    ----------
    .. [1] More, J. J., "The Levenberg-Marquardt Algorithm: Implementation
           and Theory," Numerical Analysis, ed. G. A. Watson, Lecture Notes
           in Mathematics 630, Springer Verlag, pp. 105-116, 1977.
    c         S` sR   | d |  } t  | |  } | | } t j | d | d  | } | | f S(   s   Function of which to find zero.
        
        It is defined as "norm of regularized (by alpha) least-squares
        solution minus `Delta`". Refer to [1]_.
        i   i   (   R   R   t   sum(   t   alphat   sufR   R   t   denomt   p_normt   phit	   phi_prime(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   phi_and_derivativel   s
    
 i    ig        gMbP?g      ?i   i   N(	   t   EPSt   FalseR   R   t   Nonet   maxt   rangeR   t   abs(   t   nt   mt   ufR   t   VR   t   initial_alphat   rtolt   max_iterR!   R   t	   thresholdt	   full_rankt   pt   alpha_upperR   R    t   alpha_lowerR   t   itt   ratio(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   solve_lsq_trust_region;   s@    1	
	
c         C` s  yR t  |   \ } } t | | f |  } t j | |  | d k rQ | t f SWn t k
 re n X|  d | d } |  d	 | d } |  d
 | d } | d | }	 | d | }
 t j | |	 d | | |
 d | d | | |
 | |	 g  } t j |  } t j | t j	 |   } | t j
 d | d | d d | d d | d f  } d t j | |  j |  d d t j | |  } t j |  } | d d  | f } | t f S(   s  Solve a general trust-region problem in 2 dimensions.
    
    The problem is reformulated as a 4-th order algebraic equation,
    the solution of which is found by numpy.roots.
    
    Parameters
    ----------
    B : ndarray, shape (2, 2)
        Symmetric matrix, defines a quadratic term of the function.
    g : ndarray, shape (2,)
        Defines a linear term of the function.
    Delta : float
        Radius of a trust region.
    
    Returns
    -------
    p : ndarray, shape (2,)
        Found solution.
    newton_step : bool
        Whether the returned solution is the Newton step which lies within
        the trust region.
    i   i    i   i   g      ?t   axisN(   i    i    (   i    i   (   i   i   (   R   R   R   R   t   TrueR   t   arrayt   rootst   realt   isrealt   vstackR   t   argminR#   (   t   Bt   gR   t   Rt   lowerR1   R   R   R   R   t   ft   coeffst   tt   valuet   i(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   solve_trust_region_2d   s*    ?=6c         C` sa   | d k r | | } n d } | d k  r8 d | }  n | d k rW | rW |  d 9}  n  |  | f S(   s   Update the radius of a trust region based on the cost reduction.

    Returns
    -------
    Delta : float
        New radius.
    ratio : float
        Ratio between actual and predicted reductions. Zero if predicted
        reduction is zero.
    i    g      ?g      ?g       @(    (   R   t   actual_reductiont   predicted_reductiont	   step_normt	   bound_hitR5   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   update_tr_radius   s    c   
      C` s  |  j  |  } t j  | |  } | d k	 rJ | t j  | | |  7} n  | d 9} t j  | |  } | d k	 r|  j  |  } | t j  | |  7} d t j  | |  t j  | |  }	 | d k	 r| t j  | | |  7} |	 d t j  | | |  7}	 n  | | |	 f S| | f Sd S(   s  Parameterize a multivariate quadratic function along a line.
    
    The resulting univariate quadratic function is given as follows:
    ::
        f(t) = 0.5 * (s0 + s*t).T * (J.T*J + diag) * (s0 + s*t) +
               g.T * (s0 + s*t)
    
    Parameters
    ----------
    J : ndarray, sparse matrix or LinearOperator shape (m, n)
        Jacobian matrix, affects the quadratic term.
    g : ndarray, shape (n,)
        Gradient, defines the linear term.
    s : ndarray, shape (n,)
        Direction vector of a line.
    diag : None or ndarray with shape (n,), optional
        Addition diagonal part, affects the quadratic term.
        If None, assumed to be 0.
    s0 : None or ndarray with shape (n,), optional
        Initial point. If None, assumed to be 0.
    
    Returns
    -------
    a : float
        Coefficient for t**2.
    b : float
        Coefficient for t.
    c : float
        Free term. Returned only if `s0` is provided.
    g      ?N(   R   R   R$   (
   t   JR@   R   t   diagt   s0t   vR   R   t   uR   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   build_quadratic_1d   s    
&!c   	      C` s   | | g } |  d k rU d | |  } | | k  o= | k  n rU | j  |  qU n  t j |  } |  | d | | | } t j |  } | | | | f S(   s   Minimize a 1-d quadratic function subject to bounds.
    
    The free term `c` is 0 by default. Bounds must be finite.
    
    Returns
    -------
    t : float
        Minimum point.
    y : float
        Minimum value.
    i    g      i   (   t   appendR   t   asarrayR>   (	   R   R   t   lbt   ubR   RE   t   extremumt   yt	   min_index(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   minimize_quadratic_1d/  s    c         C` s   | j  d k r\ |  j |  } t j | |  } | d k	 r | t j | | |  7} q n[ |  j | j  } t j | d d d } | d k	 r | t j | | d d d 7} n  t j | |  } d | | S(   s   Compute values of a quadratic function arising in least squares.
    
    The function is 0.5 * s.T * (J.T * J + diag) * s + g.T * s.
    
    Parameters
    ----------
    J : ndarray, sparse matrix or LinearOperator, shape (m, n)
        Jacobian matrix, affects the quadratic term.
    g : ndarray, shape (n,)
        Gradient, defines the linear term.
    s : ndarray, shape (k, n) or (n,)
        Array containing steps as rows.
    diag : ndarray, shape (n,), optional
        Addition diagonal part, affects the quadratic term.
        If None, assumed to be 0.
    
    Returns
    -------
    values : ndarray with shape (k,) or float
        Values of the function. If `s` was 2-dimensional then ndarray is
        returned, otherwise float is returned.
    i   i   R7   i    g      ?N(   t   ndimR   R   R$   t   TR   (   RN   R@   R   RO   t   JsR   t   l(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   evaluate_quadraticF  s     $c         C` s   t  j |  | k |  | k @ S(   s$   Check if a point lies within bounds.(   R   t   all(   R   RV   RW   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt	   in_boundsp  s    c         C` s   t  j |  } | | } t  j |   } | j t  j  t  j d d  3 t  j | |  | | | |  | |  | | <Wd QXt  j |  } | t  j | |  t  j	 |  j
 t  f S(   s  Compute a min_step size required to reach a bound.
    
    The function computes a positive scalar t, such that x + s * t is on
    the bound.
    
    Returns
    -------
    step : float
        Computed step. Non-negative value.
    hits : ndarray of int with shape of x
        Each element indicates whether a corresponding variable reaches the
        bound:
             
             *  0 - the bound was not hit.
             * -1 - the lower bound was hit.
             *  1 - the upper bound was hit.
    t   overt   ignoreN(   R   t   nonzerot
   empty_liket   fillt   inft   errstatet   maximumt   mint   equalt   signt   astypet   int(   R   R   RV   RW   t   non_zerot
   s_non_zerot   stepst   min_step(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   step_size_to_boundu  s    
g|=c         C` s   t  j |  d t } | d k rE d | |  | k <d | |  | k <| S|  | } | |  } | t  j d t  j |   } | t  j d t  j |   } t  j |  | t  j | |  k @}	 d | |	 <t  j |  | t  j | |  k @}
 d | |
 <| S(   s  Determine which constraints are active in a given point.
    
    The threshold is computed using `rtol` and the absolute value of the
    closest bound.
    
    Returns
    -------
    active : ndarray of int with shape of x
        Each component shows whether the corresponding constraint is active:
             
             *  0 - a constraint is not active.
             * -1 - a lower bound is active.
             *  1 - a upper bound is active.
    t   dtypei    ii   (   R   t
   zeros_likeRo   Rj   R'   t   isfinitet   minimum(   R   RV   RW   R-   t   activet
   lower_distt
   upper_distt   lower_thresholdt   upper_thresholdt   lower_activet   upper_active(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   find_active_constraints  s     



c   	      C` s"  |  j    } t |  | | |  } t j | d  } t j | d  } | d k r t j | | | |  | | <t j | | | |  | | <n^ | | | t j d t j | |   | | <| | | t j d t j | |   | | <| | k  | | k B} d | | | | | | <| S(   s   Shift a point to the interior of a feasible region.
    
    Each element of the returned vector is at least at a relative distance
    `rstep` from the closest bound. If ``rstep=0`` then `np.nextafter` is used.
    ii   i    g      ?(   t   copyR   R   Rl   t	   nextafterRj   R'   (	   R   RV   RW   t   rstept   x_newRy   t
   lower_maskt
   upper_maskt   tight_bounds(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   make_strictly_feasible  s    !((c         C` s   t  j |   } t  j |   } | d k  t  j |  @} | | |  | | | <d | | <| d k t  j |  @} |  | | | | | <d | | <| | f S(   sI  Compute Coleman-Li scaling vector and its derivatives.
    
    Components of a vector v are defined as follows:
    ::
               | ub[i] - x[i], if g[i] < 0 and ub[i] < np.inf
        v[i] = | x[i] - lb[i], if g[i] > 0 and lb[i] > -np.inf
               | 1,           otherwise
    
    According to this definition v[i] >= 0 for all i. It differs from the
    definition in paper [1]_ (eq. (2.2)), where the absolute value of v is
    used. Both definitions are equivalent down the line.
    Derivatives of v with respect to x take value 1, -1 or 0 depending on a
    case.
    
    Returns
    -------
    v : ndarray with shape of x
        Scaling vector.
    dv : ndarray with shape of x
        Derivatives of v[i] with respect to x[i], diagonal elements of v's
        Jacobian.
    
    References
    ----------
    .. [1] M.A. Branch, T.F. Coleman, and Y. Li, "A Subspace, Interior,
           and Conjugate Gradient Method for Large-Scale Bound-Constrained
           Minimization Problems," SIAM Journal on Scientific Computing,
           Vol. 21, Number 1, pp 1-23, 1999.
    i    ii   (   R   t	   ones_likeRv   Rw   (   R   R@   RV   RW   RQ   t   dvt   mask(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   CL_scaling_vector  s    

c         C` s  t  |  | |  r% |  t j |   f St j |  } t j |  } |  j   } t j |  d t } | | @} t j |  | d | | |  |  | | <|  | | | k  | | <| | @} t j |  | d | | |  |  | | <|  | | | k | | <| | @} | | } t j	 |  | | | d | |  }	 | | t j |	 d | | |	  | | <|	 | | k | | <t j |   }
 d |
 | <| |
 f S(   s3   Compute reflective transformation and its gradient.Ru   i   i(
   Rb   R   R   Rw   R   Rv   t   boolRj   Rx   t	   remainder(   RY   RV   RW   t	   lb_finitet	   ub_finiteR   t
   g_negativeR   R   RE   R@   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   reflective_transformation   s(    **

&*
c           C` s&   t  d j d d d d d d   d  S(   Ns*   {0:^15}{1:^15}{2:^15}{3:^15}{4:^15}{5:^15}t	   Iterations
   Total nfevt   Costs   Cost reductions	   Step normt
   Optimality(   t   printt   format(    (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   print_header_nonlinear"  s    	c         C` sn   | d  k r d } n d j |  } | d  k r9 d } n d j |  } t d j |  | | | | |   d  S(   Nt    i   s
   {0:^15.2e}s(   {0:^15}{1:^15}{2:^15.4e}{3}{4}{5:^15.2e}s                  s                  (   R$   R   R   (   t	   iterationt   nfevt   costt   cost_reductionRK   t
   optimality(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   print_iteration_nonlinear(  s    			c           C` s#   t  d j d d d d d   d  S(   Ns#   {0:^15}{1:^15}{2:^15}{3:^15}{4:^15}R   R   s   Cost reductions	   Step normR   (   R   R   (    (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   print_header_linear9  s    	c         C` sk   | d  k r d } n d j |  } | d  k r9 d } n d j |  } t d j |  | | | |   d  S(   NR   i   s
   {0:^15.2e}s!   {0:^15}{1:^15.4e}{2}{3}{4:^15.2e}s                  s                  (   R$   R   R   (   R   R   R   RK   R   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   print_iteration_linear?  s    			c         C` s0   t  |  t  r |  j |  S|  j j |  Sd S(   s4   Compute gradient of the least-squares cost function.N(   t
   isinstanceR	   t   rmatvecR]   R   (   RN   RC   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   compute_gradR  s    c         C` s   t  |   r= t j |  j d  j d d   j   d } n t j |  d d d d } | d k ry d | | d k <n t j | |  } d | | f S(   s5   Compute variables scale based on the Jacobian matrix.i   R7   i    g      ?i   N(   R   R   RU   t   powerR   t   ravelR$   Rj   (   RN   t   scale_inv_oldt	   scale_inv(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   compute_jac_scaleZ  s    1c         ` sa   t          f d   }    f d   }    f d   } t   j d | d | d | S(   s#   Return diag(d) J as LinearOperator.c         ` s      j  |   S(   N(   t   matvec(   R   (   RN   R   (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyR   m  s    c         ` s      j  |   S(   N(   t   matmat(   t   X(   RN   R   (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyR   p  s    c         ` s     j  |  j     S(   N(   R   R   (   R   (   RN   R   (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyR   s  s    R   R   R   (   R
   R	   t   shape(   RN   R   R   R   R   (    (   RN   R   s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   left_multiplied_operatori  s    c         ` sa   t          f d   }    f d   }    f d   } t   j d | d | d | S(   s#   Return J diag(d) as LinearOperator.c         ` s     j  t j |     S(   N(   R   R   R   (   R   (   RN   R   (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyR   ~  s    c         ` s$     j  |   d  d   t j f  S(   N(   R   R   t   newaxis(   R   (   RN   R   (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyR     s    c         ` s      j  |   S(   N(   R   (   R   (   RN   R   (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyR     s    R   R   R   (   R
   R	   R   (   RN   R   R   R   R   (    (   RN   R   s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   right_multiplied_operatorz  s    c         ` sb   t         j \  }    f d   }     f d   } t  | | f d | d | S(   s   Return a matrix arising in regularized least squares as LinearOperator.
    
    The matrix is
        [ J ]
        [ D ]
    where D is diagonal matrix with elements from `diag`.
    c         ` s    t  j   j |    |  f  S(   N(   R   t   hstackR   (   R   (   RN   RO   (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyR     s    c         ` s)   |    } |   }   j  |   | S(   N(   R   (   R   t   x1t   x2(   RN   RO   R)   (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyR     s    

R   R   (   R
   R   R	   (   RN   RO   R(   R   R   (    (   RN   RO   R)   s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   regularized_lsq_operator  s
    c         C` s   | r% t  |  t  r% |  j   }  n  t |   rU |  j | j |  j d d 9_ n+ t  |  t  rv t |  |  }  n
 |  | 9}  |  S(   sl   Compute J diag(d).
    
    If `copy` is False, `J` is modified in place (unless being LinearOperator).
    t   modet   clip(   R   R	   R   R   t   datat   taket   indicesR   (   RN   R   R   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   right_multiply  s    $
c         C` s   | r% t  |  t  r% |  j   }  n  t |   r[ |  j t j | t j |  j   9_ n> t  |  t  r| t	 |  |  }  n |  | d d  t j
 f 9}  |  S(   sl   Compute diag(d) J.
    
    If `copy` is False, `J` is modified in place (unless being LinearOperator).
    N(   R   R	   R   R   R   R   t   repeatt   difft   indptrR   R   (   RN   R   R   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   left_multiply  s    *c   	      C` s\   |  | | k  o | d k } | | | | k  } | r@ | r@ d S| rJ d S| rT d Sd Sd S(   s8   Check termination condition for nonlinear least squares.g      ?i   i   i   N(   R$   (	   t   dFt   Ft   dx_normt   x_normR5   t   ftolt   xtolt   ftol_satisfiedt   xtol_satisfied(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   check_termination  s    c         C` sc   | d d | d | d } t  | | t  k  <| d C} | | d | 9} t |  | d t | f S(   sd   Scale Jacobian and residuals for a robust loss function.
    
    Arrays are modified in place.
    i   i   g      ?R   (   R"   R   R#   (   RN   RC   t   rhot   J_scale(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   scale_for_robust_loss_function  s
    
(4   t   __doc__t
   __future__R    R   R   t   mathR   t   numpyR   t   numpy.linalgR   t   scipy.linalgR   R   R   t   scipy.sparseR   t   scipy.sparse.linalgR	   R
   t   finfot   floatt   epsR"   R   R$   R6   RH   RM   RS   R[   R`   Rb   Rt   R   R   R   R   R   R   R   R   R   R   R   R   R   R8   R   R   R   R   (    (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/common.pyt   <module>   sF   	'q	3	3*		'	,	"									