o
    tBh                     @   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 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RQTbpermr   copy_Rv
abs_diag_R	thresholdnnsx r+   u/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/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   rstep)r   r   r   r   anyr   )Agr*   pthetap_dot_glbubalphax_new_stepcost_changeactiver+   r+   r,   backtrackingF   s    
r?   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   astypeboolr   r   infr   )r*   A_hg_hc_hr4   p_hdr7   r8   r5   p_stridehitsr_hr
x_on_bound
r_stride_ur;   
r_stride_labrB   r_strider_valuep_valueag_hagag_stride_u	ag_strideag_valuer+   r+   r,   select_step[   sF   



r\   c
           .      C   sJ  | 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rt  t|D ]}t||||\}}|| }t|tjd} | |k rd}|	dkrt|||||  |d ur n|| }!|!d }"|d }#|#| }$t| |#}%|dkr|	||d |< t|
|||#|  |||"dd }&n/|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/   exacteconomicT)modepivotingr   Fg{Gz?autor.   d      )ordr   )r%   )atolbtolr   r@   g{Gzt?)rtol)r*   funcost
optimalityactive_masknitstatusinitial_cost)shaper   r   r   Tr   vstackr   mindotr   r   ranger   r   rE   r   r   r-   r   r   r	   r   r\   r   r?   r   r   ).r2   rS   x_lsqr7   r8   tol
lsq_solverlsmr_tolmax_iterverboser    r!   r*   r;   QTr"   r$   QTrkr_augauto_lsmr_tolrN   r3   ri   rn   termination_status	step_normr=   	iterationr&   dvg_scaledg_normdiag_hdiag_root_hrJ   rG   rF   rI   lsmr_opetar4   r6   r5   r<   rk   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?   r\   r   r+   r+   r+   r,   <module>   s    D
44