
    (phq3                         S r SSKJrJrJrJr  SSKJr  SSKr	SSKr	 SSK
Jr  SrSSKrSSKJr  S	S
/rS rS rS rS rS rSS jrg! \ a	    SSKrSr N1f = f)z1Basic linear factorizations needed by the solver.    )bmat
csc_matrixeyeissparse)LinearOperatorNcholesky_AAtTF)warnorthogonalityprojectionsc                 v   [         R                  R                  U5      n[        U 5      (       a)  [        R
                  R                  R                  U SS9nO[         R                  R                  U SS9nUS:X  d  US:X  a  g[         R                  R                  U R                  U5      5      nXCU-  -  nU$ )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         a/var/www/html/venv/lib/python3.13/site-packages/scipy/optimize/_trustregion_constr/projections.pyr   r      s    $ YY^^AF{{$$))!)7u- {fkyy~~aeeAh'Hf}%DK    c                 V   ^ ^^^	 [        T 5      m	U U	UU4S jnU U	4S jnU U	4S jnXgU4$ )zLReturn linear operators for matrix A using ``NormalEquation`` approach.
    c                 @  > T" TR                  U 5      5      nU TR                  R                  U5      -
  nSn[        TU5      T:  aU  UT:  a   U$ T" TR                  U5      5      nUTR                  R                  U5      -
  nUS-  n[        TU5      T:  a  MU  U$ Nr      r   Tr   )xvzkr   factor	max_refinorth_tols       r   
null_space/normal_equation_projections.<locals>.null_space@   s    1558
N Aq!H,I~ 	 quuQx AACCGGAJAFA Aq!H, r   c                 2   > T" TR                  U 5      5      $ Nr   r$   r   r(   s    r   least_squares2normal_equation_projections.<locals>.least_squaresR   s    aeeAhr   c                 F   > TR                   R                  T" U 5      5      $ r.   r#   r   r0   s    r   	row_space.normal_equation_projections.<locals>.row_spaceV   s    sswwvay!!r   r   )
r   mnr*   r)   tolr+   r1   r5   r(   s
   `  ``    @r   normal_equation_projectionsr:   9   s/     !_F $ " i//r   c           	      l  ^ ^^^^^	^
 [        [        [        T5      T R                  /T S//5      5      m	 [        R
                  R                  R                  T	5      m
U U	UUUUU
4S jnUUU
4S jnUU
4S jnXgU4$ ! [         a+    [        SSS9  [        T R                  5       TTTTU5      s $ f = f)z;Return linear operators for matrix A - ``AugmentedSystem``.NzVSingular Jacobian matrix. Using dense SVD decomposition to perform the factorizations.   
stacklevelc                 (  > [         R                  " U [         R                  " T	5      /5      nT" U5      nUS T nSn[        TU5      T:  aE  UT
:  a   U$ UTR	                  U5      -
  nT" U5      nX&-  nUS T nUS-  n[        TU5      T:  a  ME  U$ r    )r   hstackzerosr   r   )r$   r%   lu_solr&   r'   new_v	lu_updater   Kr7   r)   r8   r*   solves          r   r+   0augmented_system_projections.<locals>.null_spacer   s     IIq"((1+&' q2AJ Aq!H,I~  f%E eI Fr
AFA Aq!H,  r   c                 |   > [         R                  " U [         R                  " T5      /5      nT" U5      nUTTT-    $ r.   r   r@   rA   )r$   r%   rB   r7   r8   rF   s      r   r1   3augmented_system_projections.<locals>.least_squares   s;     IIq"((1+&' qa!}r   c                 v   > [         R                  " [         R                  " T5      U /5      nT" U5      nUS T $ r.   rI   )r$   r%   rB   r8   rF   s      r   r5   /augmented_system_projections.<locals>.row_space   s7     IIrxx{A&' qbqzr   )r   r   r   r#   r   r   r   
factorizedRuntimeErrorr
   svd_factorization_projectionstoarray)r   r7   r8   r*   r)   r9   r+   r1   r5   rE   rF   s   `````    @@r   augmented_system_projectionsrQ   \   s     	4#a&!##D	234A
=##..q1 D i//M  = +	 -QYY[-.8-6= 	=	=s   )A> >2B32B3c                 R  ^ ^^^^	^
^ [         R                  R                  T R                  SSS9u  m
mm	[        R                  R                  TSSS24   [        R                  5      U:  a  [        SSS9  [        T TUTTU5      $ U U	U
UUUU4S	 jnU	U
UU4S
 jnU	U
U4S jnXgU4$ )zMReturn linear operators for matrix A using ``QRFactorization`` approach.
    Teconomic)pivotingmodeNzPSingular Jacobian matrix. Using SVD decomposition to perform the factorizations.r<   r=   c                   > TR                   R                  U 5      n[        R                  R	                  T	USS9n[
        R                  " T
5      nX#T'   U TR                   R                  U5      -
  nSn[        TU5      T:  a|  UT:  a   U$ TR                   R                  U5      n[        R                  R	                  T	USS9nX#T'   UTR                   R                  U5      -
  nUS-  n[        TU5      T:  a  M|  U$ )NFlowerr   r!   )r#   r   r   r   solve_triangularr   rA   r   )r$   aux1aux2r%   r&   r'   r   PQRr7   r)   r*   s         r   r+   0qr_factorization_projections.<locals>.null_space   s    sswwqz||,,QE,BHHQK!
N Aq!H,I~  33771:D<<00D0FDaDACCGGAJAFA Aq!H, r   c                    > TR                   R                  U 5      n[        R                  R	                  TUSS9n[
        R                  " T5      nX#T'   U$ )NFrX   )r#   r   r   r   rZ   r   rA   )r$   r[   r\   r&   r]   r^   r_   r7   s       r   r1   3qr_factorization_projections.<locals>.least_squares   sH    sswwqz||,,QE,BHHQK!r   c                 t   > U T   n[         R                  R                  TUSSS9nTR                  U5      nU$ )NFr#   )rY   trans)r   r   rZ   r   )r$   r[   r\   r&   r]   r^   r_   s       r   r5   /qr_factorization_projections.<locals>.row_space   sC    t||,,Q3836 - 8 EE$Kr   )	r   r   qrr#   r   r   infr
   rO   )r   r7   r8   r*   r)   r9   r+   r1   r5   r]   r^   r_   s   `` ``    @@@r   qr_factorization_projectionsrh      s     llooaccDzoBGAq!	yy~~aAh'#- +	 -Q1-5-6-02 	2 2  i//r   c                    ^ ^^^	^
^ [         R                  R                  T SS9u  m	mm
T	SS2TU:  4   m	T
TU:  SS24   m
TTU:     mU U	U
UUU4S jnU	U
U4S jnU	U
U4S jnXgU4$ )zNReturn linear operators for matrix A using ``SVDFactorization`` approach.
    F)full_matricesNc                   > TR                  U 5      nST-  U-  nTR                  U5      nU TR                  R                  U5      -
  nSn[        TU5      T
:  ah  UT	:  a   U$ TR                  U5      nST-  U-  nTR                  U5      nUTR                  R                  U5      -
  nUS-  n[        TU5      T
:  a  Mh  U$ )Nr!   r   r"   )r$   r[   r\   r%   r&   r'   r   UVtr)   r*   ss         r   r+   1svd_factorization_projections.<locals>.null_space   s    vvays4xEE$K
N Aq!H,I~  66!9DQ3t8DdAACCGGAJAFA Aq!H, r   c                 \   > TR                  U 5      nST-  U-  nTR                  U5      nU$ Nr!   r/   r$   r[   r\   r&   rl   rm   rn   s       r   r1   4svd_factorization_projections.<locals>.least_squares  s/    vvays4xEE$Kr   c                    > TR                   R                  U 5      nST-  U-  nTR                   R                  U5      nU$ rq   r4   rr   s       r   r5   0svd_factorization_projections.<locals>.row_space  s7    sswwqzs4xDDHHTNr   )r   r   svd)r   r7   r8   r*   r)   r9   r+   r1   r5   rl   rm   rn   s   `  ``    @@@r   rO   rO      sv     ||7HAq" 	
!QW*A	AGQJB	!c'
A 0 i//r   c                 8   [         R                  " U 5      u  pVXV-  S:X  a  [        U 5      n [        U 5      (       aD  Uc  SnUS;  a  [	        S5      eUS:X  a'  [
        (       d  [        R                  " S[        SS9  SnOUc  S	nUS
;  a  [	        S5      eUS:X  a  [        XXbX45      u  pxn	ODUS:X  a  [        XXbX45      u  pxn	O-US	:X  a  [        XXbX45      u  pxn	OUS:X  a  [        XXbX45      u  pxn	[        Xf4W5      n
[        XV4W5      n[        Xe4W	5      nXU4$ )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   AugmentedSystem)NormalEquationrx   z%Method not allowed for sparse matrix.ry   zmOnly accepts 'NormalEquation' option when scikit-sparse is available. Using 'AugmentedSystem' option instead.r<   r=   QRFactorization)rz   SVDFactorizationz#Method not allowed for dense array.r{   )r   shaper   r   
ValueErrorsksparse_availablewarningsr
   ImportWarningr:   rQ   rh   rO   r   )r   methodr*   r)   r9   r7   r8   r+   r1   r5   ZLSYs                r   r   r   #  sM   T 88A;DA 	saxqM {{>&F>>DEE%%.@.@MM > (A7 'F>&F@@BCC!!)!YL 	-
9	$	$*1iM 	-
9	$	$*1iM 	-
9	%	%+A!yN 	-
9 	vz*A		.Bvy)A!8Or   )Ng-q=r<   gV瞯<)__doc__scipy.sparser   r   r   r   scipy.sparse.linalgr   scipy.linalgr   sksparse.cholmodr	   r~   ImportErrorr   numpyr   r
   __all__r   r:   rQ   rh   rO   r    r   r   <module>r      s|    7 : : .  -    F 0FP0f;0|30lts  s   A	 	AA