ó
Ê½÷Xc           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l	 m
 Z
 m Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m  Z  e! d „ Z" d „  Z# d „  Z$ d „  Z% d S(   sW   The adaptation of Trust Region Reflective algorithm for a linear
least-squares problem.i    (   t   divisiont   print_functiont   absolute_importN(   t   norm(   t   qrt   solve_triangular(   t   lsmr(   t   OptimizeResulti   (   t   givens_elimination(   t   EPSt   step_size_to_boundt   find_active_constraintst	   in_boundst   make_strictly_feasiblet   build_quadratic_1dt   evaluate_quadratict   minimize_quadratic_1dt   CL_scaling_vectort   reflective_transformationt   print_header_lineart   print_iteration_lineart   compute_gradt   regularized_lsq_operatort   right_multiplied_operatorc         C` sÏ   | r | j  ƒ  } n  | j  ƒ  } t | | | | ƒ t j t j | ƒ ƒ } t t |  | ƒ t j | ƒ }	 t j | |	 k ƒ \ }
 | t j |
 |
 ƒ } | |
 } t j	 | ƒ } t
 | | ƒ | | |
 <| S(   sÉ  Solve regularized least squares using information from QR-decomposition.

    The initial problem is to solve the following system in a least-squares
    sense:
    ::

        A x = b
        D x = 0

    Where D is diagonal matrix. The method is based on QR decomposition
    of the form A P = Q R, where P is a column permutation matrix, Q is an
    orthogonal matrix and R is an upper triangular matrix.

    Parameters
    ----------
    m, n : int
        Initial shape of A.
    R : ndarray, shape (n, n)
        Upper triangular matrix from QR decomposition of A.
    QTb : ndarray, shape (n,)
        First n components of Q^T b.
    perm : ndarray, shape (n,)
        Array defining column permutation of A, such that i-th column of
        P is perm[i]-th column of identity matrix.
    diag : ndarray, shape (n,)
        Array containing diagonal elements of D.

    Returns
    -------
    x : ndarray, shape (n,)
        Found least-squares solution.
    (   t   copyR   t   npt   abst   diagR	   t   maxt   nonzerot   ix_t   zerosR   (   t   mt   nt   Rt   QTbt   permR   t   copy_Rt   vt
   abs_diag_Rt	   thresholdt   nnst   x(    (    s=   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/trf_linear.pyt   regularized_lsq_with_qr   s    ! 
c         C` sø   d } x_ t  rg t | | | | | ƒ \ }	 }
 |	 | } t |  | | ƒ } | d | | k r	 Pq	 q	 Wt |	 | | ƒ } t j | d k ƒ rë t | | | | | | ƒ \ }	 }
 t |	 | | d d ƒ}	 |	 | } t |  | | ƒ } n  | | | f S(   s=   Find an appropriate step size using backtracking line search.i   gš™™™™™¹¿i    t   rstep(   t   TrueR   R   R   R   t   anyR   (   t   At   gR*   t   pt   thetat   p_dot_gt   lbt   ubt   alphat   x_newt   _t   stept   cost_changet   active(    (    s=   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/trf_linear.pyt   backtrackingH   s    	 
$
c
         C` s  t  |  | | | ƒ r | St |  | | | ƒ \ }
 } t j | ƒ } | | j t ƒ c d 9<| | } | |
 9} | |
 9} |  | } t | | | | ƒ \ } } d |	 | } | |	 9} | d k r't | | | d | d | ƒ\ } } } t | | | | d | ƒ\ } } | | | } | | } n	 t j } | |	 9} | |	 9} t	 | | | d | ƒ} | } | | } t |  | | | ƒ \ } } | |	 9} t | | | d | ƒ\ } } t | | d | ƒ \ } } | | 9} | | k  rñ| | k  rñ| S| | k  r| | k  r| S| Sd S(   sD   Select the best step according to Trust Region Reflective algorithm.iÿÿÿÿi   i    t   s0R   t   cN(
   R   R
   R   R   t   astypet   boolR   R   t   infR   (   R*   t   A_ht   g_ht   c_hR1   t   p_ht   dR4   R5   R2   t   p_stridet   hitst   r_ht   rt
   x_on_boundt
   r_stride_uR8   t
   r_stride_lt   at   bR>   t   r_stridet   r_valuet   p_valuet   ag_ht   agt   ag_stride_ut	   ag_stridet   ag_value(    (    s=   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/trf_linear.pyt   select_step\   sD    




'	




c
   .      C` sÔ  |  j  \ }
 } t | | | ƒ \ } } t | | | d d ƒ} | d k rÍ t |  d d d t ƒ\ } } } | j } |
 | k  r¬ t j | t j | |
 | f ƒ f ƒ } n  t j | ƒ } t	 |
 | ƒ } nV | d k r#t j |
 | ƒ } t
 } | d  k rd | } q#| d	 k r#t } q#n  |  j | ƒ | } t |  | ƒ } d
 t j | | ƒ } | } d  } d  } d  } | d  k rˆd } n  |	 d k ržt ƒ  n  xËt | ƒ D]½} t | | | | ƒ \ } } | | } t | d t j ƒ}  |  | k  r d } n  |	 d k r%t | | | | |  ƒ n  | d  k	 r5Pn  | | }! |! d
 }" | d
 }# |# | }$ t |  |# ƒ }% | d k r¸| j | ƒ | | *t |
 | | |# | | | |" d t
 ƒ}& n€ | d k r8t |% |" ƒ }' | | |
 *| rd t	 d
 |  ƒ }( t t t	 d |( |  ƒ ƒ } n  t |' | d | d | ƒd }& n  |# |& }) t j |) | ƒ }* |* d k rid } n  d t	 d |  ƒ }+ t | |% |$ |! |) |& |# | | |+ ƒ
 }, t |  | |, ƒ } | d k  rït |  | | |) |+ |* | | ƒ \ } }, } n t | |, | | d d ƒ} t |, ƒ } |  j | ƒ | } t |  | ƒ } | | | k  rRd } n  d
 t j | | ƒ } q«W| d  k rd } n  t | | | d | ƒ}- t d | d | d | d |  d |- d | d d | d | ƒ S(   NR,   gš™™™™™¹?t   exactt   modet   economict   pivotingR   g{®Gáz„?t   autog      à?id   i   t   ordi   R%   t   atolt   btoli    iÿÿÿÿg{®Gázt?t   rtolR*   t   funt   costt
   optimalityt   active_maskt   nitt   statust   initial_cost(   t   shapeR   R   R   R-   t   TR   t   vstackR   t   mint   Falset   Nonet   dotR   R   t   rangeR   R   RA   R   R   R+   R   R   R	   R   RX   R   R<   R   R   (.   R/   RO   t   x_lsqR4   R5   t   tolt
   lsq_solvert   lsmr_tolt   max_itert   verboseR    R!   R*   R8   t   QTR"   R$   t   QTrt   kt   r_augt   auto_lsmr_tolRJ   R0   Rc   Rh   t   termination_statust	   step_normR:   t	   iterationR&   t   dvt   g_scaledt   g_normt   diag_ht   diag_root_hRF   RC   RB   RE   t   lsmr_opt   etaR1   R3   R2   R9   Re   (    (    s=   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/trf_linear.pyt
   trf_linear   sž    !	+	

	




#
	'*		(&   t   __doc__t
   __future__R    R   R   t   numpyR   t   numpy.linalgR   t   scipy.linalgR   R   t   scipy.sparse.linalgR   t   scipy.optimizeR   R   t   commonR	   R
   R   R   R   R   R   R   R   R   R   R   R   R   R   R-   R+   R<   RX   R†   (    (    (    s=   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/_lsq/trf_linear.pyt   <module>   s   d4		4