o
    'Æ&iÓ  ã                   @   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&   úIC:\wamp64\www\opt\env\Lib\site-packages\scipy/optimize/_lsq/trf_linear.pyÚregularized_lsq_with_qr   s    
r(   c                 C   s¶   d}	 t |||  ||ƒ\}	}
|	| }t| ||ƒ }|d| | kr#n|d9 }qt|	||ƒ}t |dk¡rVt ||| |  ||ƒ\}	}
t|	||dd}	|	| }t| ||ƒ }|||fS )z=Find an appropriate step size using backtracking line search.r   Tgš™™™™™¹¿ç      à?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rlt|||||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 |S )zDSelect the best step according to Trust Region Reflective algorithm.éÿÿÿÿr   r   )Ús0r   )Úc)r   )
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_hÚagZag_stride_uZ	ag_strideZag_valuer&   r&   r'   Úselect_stepZ   sF   


ÿ
rG   )Úlsmr_maxiterc
          /      C   sL  | j \}}t|||ƒ\}}t|||dd}|dkrCt| ddd\}}}|j}||k r8t |t || |f¡f¡}t |¡}t||ƒ}n|dkr_t || ¡}d}|d u rYd	| }n|d
kr_d}|  	|¡| }t
| |ƒ}dt 	||¡ }|}d }d }d }|d u rd}|	dkrˆtƒ  t|ƒD ]ü}t||||ƒ\}}|| } t| tjd}!|!|k r¨d}|	dkr´t|||||!ƒ |d urº nÏ|| }"|"d }#|d }$|$| }%t| |$ƒ}&|dkrí| 	|¡|d |…< t||||$|  |||#dd }'n0|dkrt|&|#ƒ}(||d |…< |rd	td|!ƒ })tttd|)|! ƒƒ}t|(||
||dd  }'|$|' }*t 	|*|¡}+|+dkr.d}dtd|!ƒ },t||&|%|"|*|'|$|||,ƒ
}-t| ||-ƒ }|dk r]t| |||*|,|+||ƒ\}}-}n
t||- ||dd}t|-ƒ}|  	|¡| }t
| |ƒ}||| k r€d}dt 	||¡ }qŒ|d u rd}t||||d}.t||||!|.|d ||dS )Ngš™™™™™¹?r*   ÚexactZeconomicT)ÚmodeZpivotingr   Fg{®Gáz„?Úautor)   éd   é   )Úordr   )r"   )ÚmaxiterZatolZbtolr   r9   g{®Gázt?)Zrtol)r%   ZfunÚcostZ
optimalityÚactive_maskÚnitÚstatusÚinitial_cost)Úshaper   r   r   ÚTr   Zvstackr   ÚminÚdotr   r   Úranger   r   r=   r   r   r(   r   r   r	   r   rG   r   r8   r   r   )/r,   rE   Zx_lsqr1   r2   ZtolZ
lsq_solverZlsmr_tolZmax_iterÚverboserH   r   r   r%   r4   ZQTr    r!   ZQTrÚkZr_augZauto_lsmr_tolrC   r-   rP   rT   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   rQ   r&   r&   r'   Ú
trf_linearŽ   s®   



ÿ

ÿ

ÿÿ

ÿ


ýr^   )T)!Ú__doc__Ú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   rG   r^   r&   r&   r&   r'   Ú<module>   s    D
35ÿ