U
    /d                     @   s   d 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 dddZdd Zdd ZddddZ dS )zWThe adaptation of Trust Region Reflective algorithm for a linear
least-squares problem.    N)norm)qrsolve_triangular)lsmr)OptimizeResult   )givens_elimination)EPSstep_size_to_boundfind_active_constraints	in_boundsmake_strictly_feasiblebuild_quadratic_1devaluate_quadraticminimize_quadratic_1dCL_scaling_vectorreflective_transformationprint_header_linearprint_iteration_linearcompute_gradregularized_lsq_operatorright_multiplied_operatorTc                 C   s   |r|  }|  }t||||  tt|}tt| | t| }	t||	k\}
|t|
|
 }||
 }t	|}t
|||||
 < |S )a  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 ith 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.
    )copyr   npabsdiagr	   maxZnonzeroZix_zerosr   )mnRZQTbpermr   copy_RvZ
abs_diag_R	thresholdZnnsx r&   B/tmp/pip-unpacked-wheel-9gxwnfpp/scipy/optimize/_lsq/trf_linear.pyregularized_lsq_with_qr   s     
r(   c                 C   s   d}t |||  ||\}	}
|	| }t| || }|d| | krDqN|d9 }qt|	||}t|dkrt ||| |  ||\}	}
t|	||dd}	|	| }t| || }|||fS )z=Find an appropriate step size using backtracking line search.r   g      ?r   Zrstep)r   r   r   r   anyr   )Agr%   pthetap_dot_glbubalphaZx_new_stepcost_changeactiver&   r&   r'   backtrackingE   s    
r8   c
                 C   s  t | | ||r|S t| |||\}
}t|}||t  d9  < || }||
9 }||
9 }| | }t||||\}}d|	 | }||	9 }|dkrt|||||d\}}}t|||||d\}}|||  }|| }ntj}||	9 }||	9 }t	||||d}| }|| }t| |||\}}||	9 }t||||d\}}t||d|\}}||9 }||k rl||k rl|S ||k r||k r|S |S dS )zDSelect the best step according to Trust Region Reflective algorithm.r   r   )s0r   )c)r   N)
r   r
   r   r   Zastypeboolr   r   infr   )r%   A_hg_hZc_hr.   p_hdr1   r2   r/   Zp_stridehitsZr_hrZ
x_on_boundZ
r_stride_ur4   Z
r_stride_labr;   Zr_strideZr_valueZp_valueZag_hZagZag_stride_uZ	ag_strideZag_valuer&   r&   r'   select_stepZ   sN    
    

rF   )lsmr_maxiterc
          /      C   s^  | j \}}t|||\}}t|||dd}|dkrt| ddd\}}}|j}||k rpt|t|| |ff}t|}t||}n8|dkrt|| }d}|d krd	| }n|d
krd}| 	|| }t
| |}dt	|| }|}d }d }d }|d krd}|	dkrt  t|D ]}t||||\}}|| } t| tjd}!|!|k rXd}|	dkrrt|||||! |d k	r q$|| }"|"d }#|d }$|$| }%t| |$}&|dkr|	||d |< t||||$|  |||#dd }'n`|dkrJt|&|#}(||d |< |r2d	td|! })tttd|)|! }t|(||
||dd  }'|$|' }*t	|*|}+|+dkrld}dtd|! },t||&|%|"|*|'|$|||,
}-t| ||- }|dk rt| |||*|,|+||\}}-}nt||- ||dd}t|-}| 	|| }t
| |}||| k rd}dt	|| }q|d kr2d}t||||d}.t||||!|.|d ||dS )Ng?r*   exactZeconomicT)modeZpivotingr   Fg{Gz?autor)   d      )ordr   )r"   )maxiterZatolZbtolr   r9   g{Gzt?)Zrtol)r%   ZfuncostZ
optimalityactive_maskZnitstatusinitial_cost)shaper   r   r   Tr   Zvstackr   mindotr   r   ranger   r   r=   r   r   r(   r   r   r	   r   rF   r   r8   r   r   )/r,   rE   Zx_lsqr1   r2   ZtolZ
lsq_solverZlsmr_tolZmax_iterverboserG   r   r   r%   r4   ZQTr    r!   ZQTrkZr_augZauto_lsmr_tolrC   r-   rO   rR   Ztermination_statusZ	step_normr6   	iterationr#   ZdvZg_scaledZg_normZdiag_hZdiag_root_hrA   r?   r>   r@   Zlsmr_opetar.   r0   r/   r5   rP   r&   r&   r'   
trf_linear   s    







 


 


 

       

     r\   )T)!__doc__Znumpyr   Znumpy.linalgr   Zscipy.linalgr   r   Zscipy.sparse.linalgr   Zscipy.optimizer   r   commonr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r(   r8   rF   r\   r&   r&   r&   r'   <module>   s   D
35