o
    hq3                     @   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   b/var/www/vscode/kcb/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modeNzPSingular 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rt| |||||\}}}	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