U
    ¦»|eÚ  ã                   @   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	   ÚmaxÚnonzeroÚix_Úzerosr   )ÚmÚnÚRZQTbÚpermr   Úcopy_RÚvZ
abs_diag_RÚ	thresholdZnnsÚx© r(   ú[/var/www/website-v5/atlas_env/lib/python3.8/site-packages/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¡rªt ||| |  ||ƒ\}	}
t|	||dd�}	|	| }t| ||ƒ }|||fS )z=Find an appropriate step size using backtracking line search.r   gš™™™™™¹¿ç      à?r   ©Úrstep)r   r   r   r   Úanyr   )ÚAÚgr'   ÚpÚthetaÚp_dot_gÚlbÚubÚalphaÚx_newÚ_ÚstepÚcost_changeÚactiver(   r(   r)   ÚbacktrackingE   s    
r<   c
                 C   sŒ  t | | ||ƒr|S t| |||ƒ\}
}t |¡}|| t¡  d9  < || }||
9 }||
9 }| | }t||||ƒ\}}d|	 | }||	9 }|dkrØt|||||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   ÚastypeÚboolr   r   Úinfr   )r'   ÚA_hÚg_hZc_hr1   Úp_hÚdr4   r5   r2   Úp_strideÚhitsÚr_hÚrÚ
x_on_boundÚ
r_stride_ur8   Ú
r_stride_lÚaÚbr?   Úr_strideÚr_valueÚp_valueÚag_hÚagZag_stride_uÚ	ag_strideÚag_valuer(   r(   r)   Úselect_stepZ   sN    
    ÿ

rW   )Úlsmr_maxiterc
          /      C   s^  | j \}}t|||ƒ\}}t|||dd�}|dkr†t| ddd�\}}}|j}||k rpt |t || |f¡f¡}t |¡}t||ƒ}n8|dkr¾t || ¡}d}|d kr²d	| }n|d
kr¾d}|  	|¡| }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 �rÊt| |||*|,|+||ƒ\}}-}nt||- ||dd�}t|-ƒ}|  	|¡| }t
| |ƒ}||| k �rd}dt 	||¡ }�q|d k�r2d}t||||d�}.t||||!|.|d ||d�S )Ngš™™™™™¹?r,   ÚexactÚeconomicT)ÚmodeÚpivotingr   Fg{®Gáz„?Úautor+   éd   é   )Úordr   )r$   )ÚmaxiterÚatolÚbtolr   r=   g{®Gázt?)Úrtol)r'   ÚfunÚcostÚ
optimalityÚactive_maskÚnitÚstatusÚinitial_cost)Úshaper   r   r   ÚTr   Úvstackr   ÚminÚdotr   r   Úranger   r   rB   r   r   r*   r   r   r	   r   rW   r   r<   r   r   )/r/   rO   Úx_lsqr4   r5   ÚtolÚ
lsq_solverÚlsmr_tolÚmax_iterÚverboserX   r    r!   r'   r8   ZQTr"   r#   ZQTrÚkZr_augZauto_lsmr_tolrJ   r0   rf   rk   Útermination_statusÚ	step_normr:   Ú	iterationr%   ÚdvZg_scaledÚg_normÚdiag_hZdiag_root_hrF   rD   rC   rE   Úlsmr_opÚetar1   r3   r2   r9   rh   r(   r(   r)   Ú
trf_linearŽ   sÌ    







 ÿ


 ÿ


 ÿÿ

       ÿ

     ýr�   )T)!Ú__doc__Únumpyr   Únumpy.linalgr   Úscipy.linalgr   r   Úscipy.sparse.linalgr   Úscipy.optimizer   r   Úcommonr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r*   r<   rW   r�   r(   r(   r(   r)   Ú<module>   s   D
35ÿ