o
    Ö­jq3  ã                   @   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y5   ddlZdZY nw 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s$|dkr&d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.
    Úfro)Úordr   )ÚnpÚlinalgÚnormr   ÚscipyÚsparseÚdot)ÚAÚgÚnorm_gÚnorm_AÚnorm_A_gÚorth© r   úk/var/www/html/CropPilot/venv/lib/python3.10/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                    sv   ˆˆ   | ¡ƒ}| ˆ j  |¡ }d}tˆ |ƒˆkr9|ˆkr	 |S ˆˆ   |¡ƒ}|ˆ j  |¡ }|d7 }tˆ |ƒˆks|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
   
r4   c           	   
      s¦   t ttˆƒˆ jgˆ dggƒƒ‰z	tjj ˆ¡‰W n ty2   t	ddd� t
ˆ  ¡ ˆˆˆˆ|ƒ Y S w ‡ ‡‡‡‡‡‡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.é   ©Ú
stacklevelc                    sŒ   t  | t  ˆ¡g¡}ˆ|ƒ}|d ˆ… }d}tˆ |ƒˆkrD|ˆkr$	 |S |ˆ |¡ }ˆ|ƒ}||7 }|d ˆ… }|d7 }tˆ |ƒˆks|S r   )r   ÚhstackÚzerosr
   r   )r    r!   Úlu_solr"   r#   Únew_vÚ	lu_update©r   ÚKr1   r&   r2   r'   Úsolver   r   r(   r   s   õóz0augmented_system_projections.<locals>.null_spacec                    s,   t  | t  ˆ ¡g¡}ˆ|ƒ}|ˆˆ ˆ … S r)   ©r   r8   r9   ©r    r!   r:   )r1   r2   r?   r   r   r-   ”   s   z3augmented_system_projections.<locals>.least_squaresc                    s(   t  t  ˆ ¡| g¡}ˆ|ƒ}|d ˆ … S r)   r@   rA   )r2   r?   r   r   r/   ¢   s   z/augmented_system_projections.<locals>.row_space)r   r   r   r   r   r   r   Ú
factorizedÚRuntimeErrorr	   Úsvd_factorization_projectionsÚtoarrayr0   r   r=   r   Úaugmented_system_projections\   s    þþü"

rF   c           	         sœ   t jjˆ jddd�\‰‰‰tj ˆddd…f tj¡|k r,tddd� tˆ ˆ|ˆˆ|ƒS ‡ ‡‡‡‡‡‡fd	d
„}‡‡‡‡fdd„}‡‡‡fdd„}|||fS )zMReturn linear operators for matrix A using ``QRFactorization`` approach.
    TÚeconomic)ÚpivotingÚmodeéÿÿÿÿNzPSingular Jacobian matrix. Using SVD decomposition to perform the factorizations.r5   r6   c                    s°   ˆj  | ¡}tjjˆ|dd�}t ˆ¡}||ˆ< | ˆ j  |¡ }d}tˆ |ƒˆkrV|ˆkr0	 |S ˆj  |¡}tjjˆ|dd�}||ˆ< |ˆ j  |¡ }|d7 }tˆ |ƒˆks)|S )NF©Úlowerr   r   )r   r   r   r   Úsolve_triangularr   r9   r
   ©r    Úaux1Úaux2r!   r"   r#   ©r   ÚPÚQÚRr1   r&   r'   r   r   r(   ¿   s"   
	ù÷z0qr_factorization_projections.<locals>.null_spacec                    s4   ˆj  | ¡}tjjˆ|dd�}t ˆ¡}||ˆ < |S )NFrK   )r   r   r   r   rM   r   r9   ©r    rO   rP   r"   )rR   rS   rT   r1   r   r   r-   Ø   s
   
z3qr_factorization_projections.<locals>.least_squaresc                    s*   | ˆ  }t jjˆ|ddd�}ˆ |¡}|S )NFr   )rL   Útrans)r   r   rM   r   rU   )rR   rS   rT   r   r   r/   á   s   
þ
z/qr_factorization_projections.<locals>.row_space)	r   r   Úqrr   r   r   Úinfr	   rD   r0   r   rQ   r   Úqr_factorization_projections¯   s    þý	
	rY   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)Úfull_matricesNc                    sš   ˆ  | ¡}dˆ | }ˆ  |¡}| ˆ j  |¡ }d}tˆ |ƒˆkrK|ˆkr(	 |S ˆ  |¡}dˆ | }ˆ  |¡}|ˆ j  |¡ }|d7 }tˆ |ƒˆks!|S )Nr   r   r   rN   ©r   ÚUÚVtr&   r'   Úsr   r   r(   ù   s    

	
ù
÷z1svd_factorization_projections.<locals>.null_spacec                    s$   ˆ  | ¡}dˆ | }ˆ   |¡}|S ©Nr   r*   rU   ©r\   r]   r^   r   r   r-     s   

z4svd_factorization_projections.<locals>.least_squaresc                    s(   ˆ j  | ¡}dˆ | }ˆj  |¡}|S r_   r.   rU   r`   r   r   r/     s   z0svd_factorization_projections.<locals>.row_space)r   r   Úsvdr0   r   r[   r   rD   í   s   
rD   çê-�™—q=r5   çVçž¯Ò<c                 C   s>  t  | ¡\}}|| dkrt| ƒ} t| ƒr4|du rd}|dvr#tdƒ‚|dkr3ts3tjdtdd	� d}n|du r:d
}|dvrBtdƒ‚|dkrSt	| |||||ƒ\}}}	n2|dkrdt
| |||||ƒ\}}}	n!|d
krut| |||||ƒ\}}}	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)ÚNormalEquationrd   z%Method not allowed for sparse matrix.re   zmOnly accepts 'NormalEquation' option when scikit-sparse is available. Using 'AugmentedSystem' option instead.r5   r6   ÚQRFactorization)rf   ÚSVDFactorizationz#Method not allowed for dense array.rg   )r   Úshaper   r   Ú
ValueErrorÚsksparse_availableÚwarningsr	   ÚImportWarningr4   rF   rY   rD   r   )r   Úmethodr'   r&   r3   r1   r2   r(   r-   r/   ÚZÚLSÚYr   r   r   r   #  sD   Jý€
ÿ
ÿ
ÿÿ
)Nrb   r5   rc   )Ú__doc__Úscipy.sparser   r   r   r   Úscipy.sparse.linalgr   Úscipy.linalgr   Úsksparse.cholmodr   rj   ÚImportErrorrk   Únumpyr   r	   Ú__all__r
   r4   rF   rY   rD   r   r   r   r   r   Ú<module>   s.    þþ##S>6