
    ^Mhq3                         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		 ddl
mZ dZn# e$ r	 ddlZdZY nw xY wddlZddlmZ d	d
gZd Zd Zd Zd Zd ZddZdS )z1Basic linear factorizations needed by the solver.    )bmat
csc_matrixeyeissparse)LinearOperatorNcholesky_AAtTF)warnorthogonalityprojectionsc                    t           j                            |          }t          |           r't          j        j                            | d          }n!t           j                            | d          }|dk    s|dk    rdS t           j                            |                     |                    }|||z  z  }|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orths         n/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/optimize/_trustregion_constr/projections.pyr   r      s    $ Y^^AF{{ .$))!)77u-- {{fkkqy~~aeeAhh''Hvf}%DK    c                 Z    	 t                     	 	fd} 	fd} 	fd}|||fS )zLReturn linear operators for matrix A using ``NormalEquation`` approach.
    c                 `                         |                     }| j                             |          z
  }d}t          |          k    r[|k    rnT                      |                    }|j                             |          z
  }|dz  }t          |          k    [|S Nr      r   Tr   )xvzkr   factor	max_refinorth_tols       r   
null_spacez/normal_equation_projections.<locals>.null_space@   s    F15588

N Aq!!H,,I~~quuQxx  AACGGAJJAFA Aq!!H,, r   c                 @                          |                     S Nr   r$   r   r(   s    r   least_squaresz2normal_equation_projections.<locals>.least_squaresR   s    vaeeAhhr   c                 J    j                              |                     S r-   r#   r   r/   s    r   	row_spacez.normal_equation_projections.<locals>.row_spaceV   s    swwvvayy!!!r   r   )
r   mnr*   r)   tolr+   r0   r3   r(   s
   `  ``    @r   normal_equation_projectionsr7   9   s     !__F       $           " " " " " " }i//r   c           	         	
 t          t          t                     j        g dgg                    		 t          j        j                            	          
nG# t          $ r: t          dd           t                                           |          cY S w xY w 	
fd}
fd}
fd}|||fS )z;Return linear operators for matrix A - ``AugmentedSystem``.NzVSingular Jacobian matrix. Using dense SVD decomposition to perform the factorizations.   
stacklevelc                 R   t          j        | t          j        	          g          } |          }|d          }d}t          |          k    rR|
k    rnK|                    |          z
  } |          }||z  }|d          }|dz  }t          |          k    R|S r    )r   hstackzerosr   r   )r$   r%   lu_solr&   r'   new_v	lu_updater   Kr4   r)   r5   r*   solves          r   r+   z0augmented_system_projections.<locals>.null_spacer   s     Iq"(1++&'' q2A2J Aq!!H,,I~~ f%E eI iFrr
AFA Aq!!H,,  r   c                     t          j        | t          j                  g          } |          }|z            S r-   r   r=   r>   )r$   r%   r?   r4   r5   rC   s      r   r0   z3augmented_system_projections.<locals>.least_squares   sB     Iq"(1++&'' qa!e}r   c                 ~    t          j        t          j                  | g          } |          }|d          S r-   rE   )r$   r%   r?   r5   rC   s      r   r3   z/augmented_system_projections.<locals>.row_space   s>     Irx{{A&'' qbqbzr   )r   r   r   r#   r   r   r   
factorizedRuntimeErrorr
   svd_factorization_projectionstoarray)r   r4   r5   r*   r)   r6   r+   r0   r3   rB   rC   s   `````    @@r   augmented_system_projectionsrK   \   sJ    	4#a&&!#D	23344A
=#..q11 = = = +	 	 	 	 -QYY[[-.8-6= = 	= 	= 	=	=          D            }i//s   $A" "AB&%B&c                 ^   	
 t           j                             j        dd          \  
	t          j                            dddf         t          j                  |k     r%t          dd           t           ||          S  	
fd	}	
fd
}	
f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.r9   r:   c                    j                             |           }t          j                            	|d          }t          j        
          }||<   | j                             |          z
  }d}t          |          k    r~|k    rnwj                             |          }t          j                            	|d          }||<   |j                             |          z
  }|dz  }t          |          k    ~|S )NFlowerr   r!   )r#   r   r   r   solve_triangularr   r>   r   )r$   aux1aux2r%   r&   r'   r   PQRr4   r)   r*   s         r   r+   z0qr_factorization_projections.<locals>.null_space   s    swwqzz|,,QE,BBHQKK!

N Aq!!H,,I~~3771::D<00D0FFDAaDACGGAJJAFA Aq!!H,, r   c                     j                             |           }t          j                            |d          }t          j                  }||<   |S )NFrR   )r#   r   r   r   rT   r   r>   )r$   rU   rV   r&   rW   rX   rY   r4   s       r   r0   z3qr_factorization_projections.<locals>.least_squares   sK    swwqzz|,,QE,BBHQKK!r   c                     |          }t           j                            |dd          }                    |          }|S )NFr#   )rS   trans)r   r   rT   r   )r$   rU   rV   r&   rW   rX   rY   s       r   r3   z/qr_factorization_projections.<locals>.row_space   sH    t|,,Q3836 - 8 8 EE$KKr   )	r   r   qrr#   r   r   infr
   rI   )r   r4   r5   r*   r)   r6   r+   r0   r3   rW   rX   rY   s   `` ``    @@@r   qr_factorization_projectionsr_      s(    looacDzoBBGAq!	y~~aAAAh''#-- +	 	 	 	 -Q1-5-6-02 2 	2          2              }i//r   c                     	
 t           j                             d          \  	
	dd|k    f         	
|k    ddf         
|k              	
fd}	
fd}	
fd}|||fS )zNReturn linear operators for matrix A using ``SVDFactorization`` approach.
    F)full_matricesNc                                         |           }dz  |z  }                     |          }| j                             |          z
  }d}t          |          
k    ro|	k    rnh                     |          }dz  |z  }                     |          }|j                             |          z
  }|dz  }t          |          
k    o|S )Nr!   r   r"   )r$   rU   rV   r%   r&   r'   r   UVtr)   r*   ss         r   r+   z1svd_factorization_projections.<locals>.null_space   s    vvayys4xEE$KK

N Aq!!H,,I~~66!99DQ3t8DdAACGGAJJAFA Aq!!H,, r   c                 l                         |           }dz  |z  }                     |          }|S Nr!   r.   r$   rU   rV   r&   rc   rd   re   s       r   r0   z4svd_factorization_projections.<locals>.least_squares  s3    vvayys4xEE$KKr   c                     j                             |           }dz  |z  }j                             |          }|S rg   r2   rh   s       r   r3   z0svd_factorization_projections.<locals>.row_space  s7    swwqzzs4xDHHTNNr   )r   r   svd)r   r4   r5   r*   r)   r6   r+   r0   r3   rc   rd   re   s   `  ``    @@@r   rI   rI      s     |77HAq" 	
!!!QW*A	AGQQQJB	!c'
A         0             }i//r   -q=r9   V瞯<c                    t          j        |           \  }}||z  dk    rt          |           } t          |           rC|d}|dvrt	          d          |dk    r%t
          st          j        dt          d	           d}n|d
}|dvrt	          d          |dk    rt          | |||||          \  }}}	n\|dk    rt          | |||||          \  }}}	n=|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)NormalEquationrn   z%Method not allowed for sparse matrix.ro   zmOnly accepts 'NormalEquation' option when scikit-sparse is available. Using 'AugmentedSystem' option instead.r9   r:   QRFactorization)rp   SVDFactorizationz#Method not allowed for dense array.rq   )r   shaper   r   
ValueErrorsksparse_availablewarningsr
   ImportWarningr7   rK   r_   rI   r   )r   methodr*   r)   r6   r4   r5   r+   r0   r3   ZLSYs                r   r   r   #  s   T 8A;;DAq 	saxxqMM {{ D>&F>>>DEEE%%%.@%M > (A7 7 7 7 'F>&F@@@BCCC!!!)!Q8YLL 	-
M99	$	$	$*1aHiMM 	-
M99	$	$	$*1aHiMM 	-
M99	%	%	%+Aq!Xy#NN 	-
M9 	1vz**A	A	.	.B1vy))Ab!8Or   )Nrk   r9   rl   )__doc__scipy.sparser   r   r   r   scipy.sparse.linalgr   scipy.linalgr   sksparse.cholmodr	   rt   ImportErrorru   numpyr   r
   __all__r   r7   rK   r_   rI   r    r   r   <module>r      sO   7 7 : : : : : : : : : : : : . . . . . .        ------   OOO                F 0  0  0FP0 P0 P0f;0 ;0 ;0|30 30 30lt t t t t ts   ' 55