U
    »mœd13  ã                   @   sÀ   d Z ddlmZmZmZmZ ddlmZ ddlZ	ddlZ	zddl
mZ dZW n  ek
rl   ddlZdZY nX ddlZddlmZ d	d
gZdd	„ Zdd„ Zdd„ Zdd„ Zdd„ Zddd
„ZdS )z1Basic linear factorizations needed by the solver.é    )ÚbmatÚ
csc_matrixÚeyeÚissparse)ÚLinearOperatorN©Úcholesky_AAtTF)ÚwarnÚorthogonalityÚprojectionsc                 C   sn   t j |¡}t| ƒr(tjjj| dd�}nt jj| dd�}|dksH|dkrLdS t j |  |¡¡}|||  }|S )a�  Measure orthogonality between a vector and the null space of a matrix.

    Compute a measure of orthogonality between the null space
    of the (possibly sparse) matrix ``A`` and a given vector ``g``.

    The formula is a simplified (and cheaper) version of formula (3.13)
    from [1]_.
    ``orth =  norm(A g, ord=2)/(norm(A, ord='fro')*norm(g, ord=2))``.

    References
    ----------
    .. [1] Gould, Nicholas IM, Mary E. Hribar, and Jorge Nocedal.
           "On the solution of equality constrained quadratic
            programming problems arising in optimization."
            SIAM Journal on Scientific Computing 23.4 (2001): 1376-1395.
    Zfro)Úordr   )ÚnpÚlinalgÚnormr   ÚscipyÚsparseÚdot)ÚAÚgZnorm_gZnorm_AZnorm_A_gZorth© r   úg/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scipy/optimize/_trustregion_constr/projections.pyr
      s    c           	         s@   t ˆ ƒ‰‡ ‡‡‡fdd„}‡ ‡fdd„}‡ ‡fdd„}|||fS )zLReturn linear operators for matrix A using ``NormalEquation`` approach.
    c                    sf   ˆˆ   | ¡ƒ}| ˆ j  |¡ }d}tˆ |ƒˆkrb|ˆkr:qbˆˆ   |¡ƒ}|ˆ j  |¡ }|d7 }q"|S ©Nr   é   ©r   ÚTr
   )ÚxÚvÚzÚk©r   ÚfactorÚ	max_refinÚorth_tolr   r   Ú
null_space@   s    
z/normal_equation_projections.<locals>.null_spacec                    s   ˆˆ   | ¡ƒS ©N©r   ©r   ©r   r    r   r   Úleast_squaresR   s    z2normal_equation_projections.<locals>.least_squaresc                    s   ˆ j  ˆ| ƒ¡S r$   ©r   r   r&   r'   r   r   Ú	row_spaceV   s    z.normal_equation_projections.<locals>.row_spacer   ©	r   ÚmÚnr"   r!   Útolr#   r(   r*   r   r   r   Únormal_equation_projections9   s
    r/   c           	   
      s¤   t ttˆƒˆ jgˆ dggƒƒ‰ztjj ˆ¡‰W n2 tk
rb   t	dƒ t
ˆ  ¡ ˆˆˆˆ|ƒ Y S X ‡ ‡‡‡‡‡‡fdd„}‡‡‡fdd„}‡‡fdd„}|||fS )	z;Return linear operators for matrix A - ``AugmentedSystem``.NzVSingular Jacobian matrix. Using dense SVD decomposition to perform the factorizations.c                    s|   t  | t  ˆ¡g¡}ˆ|ƒ}|d ˆ… }d}tˆ |ƒˆkrx|ˆkrDqx|ˆ |¡ }ˆ|ƒ}||7 }|d ˆ… }|d7 }q,|S r   )r   ÚhstackÚzerosr
   r   )r   r   Úlu_solr   r   Únew_vZ	lu_update©r   ÚKr,   r!   r-   r"   Úsolver   r   r#   q   s    
z0augmented_system_projections.<locals>.null_spacec                    s,   t  | t  ˆ ¡g¡}ˆ|ƒ}|ˆˆ ˆ … S r$   ©r   r0   r1   ©r   r   r2   )r,   r-   r6   r   r   r(   “   s    z3augmented_system_projections.<locals>.least_squaresc                    s(   t  t  ˆ ¡| g¡}ˆ|ƒ}|d ˆ … S r$   r7   r8   )r-   r6   r   r   r*   ¡   s    z/augmented_system_projections.<locals>.row_space)r   r   r   r   r   r   r   Z
factorizedÚRuntimeErrorr	   Úsvd_factorization_projectionsZtoarrayr+   r   r4   r   Úaugmented_system_projections\   s        þ
"
r;   c           	         s˜   t jjˆ jddd�\‰‰‰tj ˆddd…f tj¡|k rTtdƒ tˆ ˆ|ˆˆ|ƒS ‡ ‡‡‡‡‡‡fdd„}‡‡‡‡fd	d
„}‡‡‡fdd„}|||fS )zMReturn linear operators for matrix A using ``QRFactorization`` approach.
    TZeconomic)ZpivotingÚmodeéÿÿÿÿNzPSingular Jacobian matrix. Using SVD decomposition to perform the factorizations.c                    s    ˆj  | ¡}tjjˆ|dd�}t ˆ¡}||ˆ< | ˆ j  |¡ }d}tˆ |ƒˆkrœ|ˆkr\qœˆj  |¡}tjjˆ|dd�}||ˆ< |ˆ j  |¡ }|d7 }qD|S )NF©Úlowerr   r   )r   r   r   r   Úsolve_triangularr   r1   r
   ©r   Úaux1Úaux2r   r   r   ©r   ÚPÚQÚRr,   r!   r"   r   r   r#   ½   s    

z0qr_factorization_projections.<locals>.null_spacec                    s4   ˆj  | ¡}tjjˆ|dd�}t ˆ¡}||ˆ < |S )NFr>   )r   r   r   r   r@   r   r1   ©r   rB   rC   r   )rE   rF   rG   r,   r   r   r(   Ö   s
    
z3qr_factorization_projections.<locals>.least_squaresc                    s*   | ˆ  }t jjˆ|ddd�}ˆ |¡}|S )NFr   )r?   Ztrans)r   r   r@   r   rH   )rE   rF   rG   r   r   r*   ß   s    
þ
z/qr_factorization_projections.<locals>.row_space)	r   r   Zqrr   r   r   Úinfr	   r:   r+   r   rD   r   Úqr_factorization_projections®   s     ý		rJ   c           	         sŠ   t jjˆ dd�\‰‰‰ˆdd…ˆ|kf ‰ˆˆ|kdd…f ‰ˆˆ|k ‰‡ ‡‡‡‡‡fdd„}‡‡‡fdd„}‡‡‡fdd	„}|||fS )
zNReturn linear operators for matrix A using ``SVDFactorization`` approach.
    F)Zfull_matricesNc                    sŠ   ˆ  | ¡}dˆ | }ˆ  |¡}| ˆ j  |¡ }d}tˆ |ƒˆkr†|ˆkrLq†ˆ  |¡}dˆ | }ˆ  |¡}|ˆ j  |¡ }|d7 }q4|S )Nr   r   r   rA   ©r   ÚUÚVtr!   r"   Úsr   r   r#   ÷   s    




z1svd_factorization_projections.<locals>.null_spacec                    s$   ˆ  | ¡}dˆ | }ˆ   |¡}|S ©Nr   r%   rH   ©rL   rM   rN   r   r   r(     s    

z4svd_factorization_projections.<locals>.least_squaresc                    s(   ˆ j  | ¡}dˆ | }ˆj  |¡}|S rO   r)   rH   rP   r   r   r*     s    z0svd_factorization_projections.<locals>.row_space)r   r   Zsvdr+   r   rK   r   r:   ë   s    r:   çê-�™—q=é   çVçž¯Ò<c                 C   s<  t  | ¡\}}|| dkr"t| ƒ} t| ƒrd|dkr6d}|dkrFtdƒ‚|dkr€ts€t dt¡ d}n|dkrpd}|d	kr€td
ƒ‚|dkr¢t	| |||||ƒ\}}}	nf|dkrÄt
| |||||ƒ\}}}	nD|dkræt| |||||ƒ\}}}	n"|dk�rt| |||||ƒ\}}}	t||f|ƒ}
t||f|ƒ}t||f|	ƒ}|
||fS )a  Return three linear operators related with a given matrix A.

    Parameters
    ----------
    A : sparse matrix (or ndarray), shape (m, n)
        Matrix ``A`` used in the projection.
    method : string, optional
        Method used for compute the given linear
        operators. Should be one of:

            - 'NormalEquation': The operators
               will be computed using the
               so-called normal equation approach
               explained in [1]_. In order to do
               so the Cholesky factorization of
               ``(A A.T)`` is computed. Exclusive
               for sparse matrices.
            - 'AugmentedSystem': The operators
               will be computed using the
               so-called augmented system approach
               explained in [1]_. Exclusive
               for sparse matrices.
            - 'QRFactorization': Compute projections
               using QR factorization. Exclusive for
               dense matrices.
            - 'SVDFactorization': Compute projections
               using SVD factorization. Exclusive for
               dense matrices.

    orth_tol : float, optional
        Tolerance for iterative refinements.
    max_refin : int, optional
        Maximum number of iterative refinements.
    tol : float, optional
        Tolerance for singular values.

    Returns
    -------
    Z : LinearOperator, shape (n, n)
        Null-space operator. For a given vector ``x``,
        the null space operator is equivalent to apply
        a projection matrix ``P = I - A.T inv(A A.T) A``
        to the vector. It can be shown that this is
        equivalent to project ``x`` into the null space
        of A.
    LS : LinearOperator, shape (m, n)
        Least-squares operator. For a given vector ``x``,
        the least-squares operator is equivalent to apply a
        pseudoinverse matrix ``pinv(A.T) = inv(A A.T) A``
        to the vector. It can be shown that this vector
        ``pinv(A.T) x`` is the least_square solution to
        ``A.T y = x``.
    Y : LinearOperator, shape (n, m)
        Row-space operator. For a given vector ``x``,
        the row-space operator is equivalent to apply a
        projection matrix ``Q = A.T inv(A A.T)``
        to the vector.  It can be shown that this
        vector ``y = Q x``  the minimum norm solution
        of ``A y = x``.

    Notes
    -----
    Uses iterative refinements described in [1]
    during the computation of ``Z`` in order to
    cope with the possibility of large roundoff errors.

    References
    ----------
    .. [1] Gould, Nicholas IM, Mary E. Hribar, and Jorge Nocedal.
        "On the solution of equality constrained quadratic
        programming problems arising in optimization."
        SIAM Journal on Scientific Computing 23.4 (2001): 1376-1395.
    r   NÚAugmentedSystem)ÚNormalEquationrT   z%Method not allowed for sparse matrix.rU   zmOnly accepts 'NormalEquation' option when scikit-sparse is available. Using 'AugmentedSystem' option instead.ÚQRFactorization)rV   ÚSVDFactorizationz#Method not allowed for dense array.rW   )r   Úshaper   r   Ú
ValueErrorÚsksparse_availableÚwarningsr	   ÚImportWarningr/   r;   rJ   r:   r   )r   Úmethodr"   r!   r.   r,   r-   r#   r(   r*   ÚZZLSÚYr   r   r   r   !  sB    Jýÿ
ÿ
ÿ

ÿ)NrQ   rR   rS   )Ú__doc__Zscipy.sparser   r   r   r   Zscipy.sparse.linalgr   Zscipy.linalgr   Zsksparse.cholmodr   rZ   ÚImportErrorr[   Únumpyr   r	   Ú__all__r
   r/   r;   rJ   r:   r   r   r   r   r   Ú<module>   s*   
þ##R=6