§
    fŠtjÍ4  ã                   ó¾   — 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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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.é    )Úblock_arrayÚ	csc_arrayÚ	eye_arrayÚissparse)ÚLinearOperatorN)Úcholesky_AAtÚCholmodTypeConversionWarningTF)ÚwarnÚcatch_warningsÚ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         úl/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/optimize/_trustregion_constr/projections.pyr   r      s£   € õ$ ŒY�^Š^˜AÑÔ€Få��{„{ð .Ý”Ô$×)Ò)¨!°Ð)Ñ7Ô7ˆˆå”—’  u�Ñ-Ô-ˆð �‚{€{�f ’k�kØˆqåŒy�~Š~˜aŸeše A™hœhÑ'Ô'€Hà�v˜f‘}Ñ%€DØ€Kó    c                 ó¶   ‡ ‡‡‡	— t          dt          ¬¦  «        5  t          ‰ ¦  «        Š	ddd¦  «         n# 1 swxY w Y   ˆ ˆ	ˆˆfd„}ˆ ˆ	fd„}ˆ ˆ	fd„}|||fS )zLReturn linear operators for matrix A using ``NormalEquation`` approach.
    Úignore)ÚactionÚcategoryNc                 ó`  •—  ‰‰                      | ¦  «        ¦  «        }| ‰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_spaceD   s®   ø€ ØˆF�1—5’5˜‘8”8ÑÔˆØ�”—’˜‘
”
‰Nˆð ˆÝ˜A˜qÑ!Ô! HÒ,Ð,Ø�IŠ~ˆ~Øà��q—u’u˜Q‘x”xÑ Ô ˆAØ�A”C—G’G˜A‘J”J‘ˆAØ�‰FˆAõ ˜A˜qÑ!Ô! HÒ,Ð,ð ˆr   c                 ó@   •—  ‰‰                      | ¦  «        ¦  «        S ©N©r   ©r(   r   r,   s    €€r   Úleast_squaresz2normal_equation_projections.<locals>.least_squaresV   s   ø€ Øˆv�a—e’e˜A‘h”hÑÔÐr   c                 óJ   •— ‰j                               ‰| ¦  «        ¦  «        S r1   ©r'   r   r3   s    €€r   Ú	row_spacez.normal_equation_projections.<locals>.row_spaceZ   s   ø€ ØŒs�wŠw�v�v˜a‘y”yÑ!Ô!Ð!r   )r   r	   r   )
r   ÚmÚnr.   r-   Útolr/   r4   r7   r,   s
   `  ``    @r   Únormal_equation_projectionsr;   9   sï   øøøø€ õ 
˜xÕ2NÐ	OÑ	OÔ	Oð !ð !Ý˜a‘”ˆð!ð !ð !ñ !ô !ð !ð !ð !ð !ð !ð !øøøð !ð !ð !ð !ðð ð ð ð ð ð ð ð$ ð  ð  ð  ð  ð  ð"ð "ð "ð "ð "ð "ð �} iÐ/Ð/s   ›7·;¾;c           	      óv  ‡ ‡‡‡‡‡	‡
— t          t          ‰¦  «        ‰ j        g‰ dggd¬¦  «        Š		 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``.NÚcsc)ÚformatzVSingular 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   ÚKr8   r-   r9   r.   Úsolves          €€€€€€€r   r/   z0augmented_system_projections.<locals>.null_spacev   sÊ   ø€ õ ŒI�q�"œ( 1™+œ+Ð&Ñ'Ô'ˆð ��q‘”ˆØ�2�A�2ŒJˆð ˆÝ˜A˜qÑ!Ô! HÒ,Ð,Ø�IŠ~ˆ~Øð ˜Ÿš˜f™œÑ%ˆEð ˜˜e™œˆIð �iÑˆFØ�r˜�r”
ˆAØ�‰FˆAõ ˜A˜qÑ!Ô! HÒ,Ð,ð  ˆr   c                 ó„   •— t          j        | t          j        ‰¦  «        g¦  «        } ‰|¦  «        }|‰‰‰z   …         S r1   ©r   rC   rD   )r(   r)   rE   r8   r9   rI   s      €€€r   r4   z3augmented_system_projections.<locals>.least_squares˜   sB   ø€ õ ŒI�q�"œ( 1™+œ+Ð&Ñ'Ô'ˆð ��q‘”ˆà�a˜˜!™�eŒ}Ðr   c                 ó~   •— t          j        t          j        ‰¦  «        | g¦  «        } ‰|¦  «        }|d ‰…         S r1   rK   )r(   r)   rE   r9   rI   s      €€r   r7   z/augmented_system_projections.<locals>.row_space¦   s>   ø€ õ ŒI•r”x ‘{”{ AÐ&Ñ'Ô'ˆð ��q‘”ˆà�b�q�bŒzÐr   )r   r   r'   r   r   r   Ú
factorizedÚRuntimeErrorr
   Úsvd_factorization_projectionsÚtoarray)r   r8   r9   r.   r-   r:   r/   r4   r7   rH   rI   s   `````    @@r   Úaugmented_system_projectionsrQ   `   sH  øøøøøøø€ õ 	•i ‘l”l A¤CÐ(¨1¨d¨)Ð4¸UÐCÑCÔC€Að
=Ý”Ô#×.Ò.¨qÑ1Ô1ˆˆøÝð =ð =ð =Ýð +àð	ñ 	ô 	ð 	õ -¨Q¯YªY©[¬[Ø-.°°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ÚmodeéÿÿÿÿNzPSingular Jacobian matrix. Using SVD decomposition to perform the factorizations.r?   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   rD   r   )r(   Úaux1Úaux2r)   r*   r+   r   ÚPÚQÚRr8   r-   r.   s         €€€€€€€r   r/   z0qr_factorization_projections.<locals>.null_spaceÃ   sõ   ø€ àŒs�wŠw�q‰zŒzˆÝŒ|×,Ò,¨Q°¸EÐ,ÑBÔBˆÝŒH�Q‰KŒKˆØˆˆ!‰Ø�”—’˜‘
”
‰Nˆð ˆÝ˜A˜qÑ!Ô! HÒ,Ð,Ø�IŠ~ˆ~Øà”3—7’7˜1‘:”:ˆDÝ”<×0Ò0°°DÀÐ0ÑFÔFˆDØˆAˆa‰Dà�A”C—G’G˜A‘J”J‘ˆAØ�‰FˆAõ ˜A˜qÑ!Ô! HÒ,Ð,ð ˆr   c                 ó²   •— ‰j                              | ¦  «        }t          j                             ‰|d¬¦  «        }t          j        ‰¦  «        }||‰<   |S )NFrX   )r'   r   r   r   rZ   r   rD   )r(   r[   r\   r*   r]   r^   r_   r8   s       €€€€r   r4   z3qr_factorization_projections.<locals>.least_squaresÜ   sK   ø€ àŒs�wŠw�q‰zŒzˆÝŒ|×,Ò,¨Q°¸EÐ,ÑBÔBˆÝŒH�Q‰KŒKˆØˆˆ!‰Øˆr   c                 óˆ   •— | ‰         }t           j                             ‰|dd¬¦  «        }‰                     |¦  «        }|S )NFr'   )rY   Útrans)r   r   rZ   r   )r(   r[   r\   r*   r]   r^   r_   s       €€€r   r7   z/qr_factorization_projections.<locals>.row_spaceå   sH   ø€ à�ŒtˆÝŒ|×,Ò,¨Q°Ø38Ø36ð -ñ 8ô 8ˆð �EŠE�$‰KŒKˆØˆr   )	r   r   Úqrr'   r   r   Úinfr
   rO   )r   r8   r9   r.   r-   r:   r/   r4   r7   r]   r^   r_   s   `` ``    @@@r   Úqr_factorization_projectionsre   ³   s(  øøøøøøø€ õ Œl�oŠo˜aœc¨D°zˆoÑBÔB�G€A€qˆ!å	„y‡~‚~�a˜˜A˜A˜A˜”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(   r[   r\   r)   r*   r+   r   ÚUÚVtr-   r.   Úss         €€€€€€r   r/   z1svd_factorization_projections.<locals>.null_spaceý   sÒ   ø€ à�vŠv�a‰yŒyˆØ�‰s�4‰xˆØ�EŠE�$‰KŒKˆØ�”—’˜‘
”
‰Nˆð ˆÝ˜A˜qÑ!Ô! HÒ,Ð,Ø�IŠ~ˆ~Øà—6’6˜!‘9”9ˆDØ�Q‘3�t‘8ˆDØ—’�d‘”ˆAà�A”C—G’G˜A‘J”J‘ˆAØ�‰FˆAõ ˜A˜qÑ!Ô! HÒ,Ð,ð ˆr   c                 ól   •— ‰                      | ¦  «        }d‰z  |z  }‰                      |¦  «        }|S ©Nr%   r2   ©r(   r[   r\   r*   ri   rj   rk   s       €€€r   r4   z4svd_factorization_projections.<locals>.least_squares  s3   ø€ à�vŠv�a‰yŒyˆØ�‰s�4‰xˆØ�EŠE�$‰KŒKˆØˆr   c                 ó€   •— ‰j                              | ¦  «        }d‰z  |z  }‰j                              |¦  «        }|S rm   r6   rn   s       €€€r   r7   z0svd_factorization_projections.<locals>.row_space  s7   ø€ àŒs�wŠw�q‰zŒzˆØ�‰s�4‰xˆØŒD�HŠH�T‰NŒNˆØˆr   )r   r   Úsvd)r   r8   r9   r.   r-   r:   r/   r4   r7   ri   rj   rk   s   `  ``    @@@r   rO   rO   ñ   sê   øøøøøø€ õ Œ|×Ò °ÐÑ7Ô7�H€A€qˆ"ð 	
ˆ!ˆ!ˆ!ˆQ�ŠWˆ*Œ€AØ	ˆA�ŠG�Q�Q�QˆJŒ€BØ	ˆ!ˆcŠ'Œ
€Aðð ð ð ð ð ð ð ð ð ð0ð ð ð ð ð ð ðð ð ð ð ð ð ð �} iÐ/Ð/r   çê-�™—q=r?   ç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 array (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 array.ru   zmOnly accepts 'NormalEquation' option when scikit-sparse is available. Using 'AugmentedSystem' option instead.r?   r@   ÚQRFactorization)rv   ÚSVDFactorizationz#Method not allowed for dense array.rw   )r   Úshaper   r   Ú
ValueErrorÚsksparse_availableÚwarningsr
   ÚImportWarningr;   rQ   re   rO   r   )r   Úmethodr.   r-   r:   r8   r9   r/   r4   r7   ÚZÚLSÚYs                r   r   r   '  sÆ  € õT Œ8�A‰;Œ;�D€A€qð 	ˆ�sˆa‚x€xÝ�a‰LŒLˆõ ��{„{ð DØˆ>Ø&ˆFØÐ>Ð>Ð>ÝÐCÑDÔDÐDØÐ%Ò%Ð%Õ.@Ð%ÝŒMð >õ (°Að7ñ 7ô 7ð 7ð 'ˆFøàˆ>Ø&ˆFØÐ@Ð@Ð@ÝÐBÑCÔCÐCàÐ!Ò!Ð!å)¨!¨Q°°8¸YÈÑLÔLñ 	-ˆ
�M 9 9à	Ð$Ò	$Ð	$å*¨1¨a°°H¸iÈÑMÔMñ 	-ˆ
�M 9 9à	Ð$Ò	$Ð	$å*¨1¨a°°H¸iÈÑMÔMñ 	-ˆ
�M 9 9à	Ð%Ò	%Ð	%å+¨A¨q°!°X¸yÈ#ÑNÔNñ 	-ˆ
�M 9õ 	˜˜1�v˜zÑ*Ô*€AÝ	˜˜A˜ Ñ	.Ô	.€BÝ˜˜1�v˜yÑ)Ô)€Aàˆb�!ˆ8€Or   )Nrq   r?   rr   )Ú__doc__Úscipy.sparser   r   r   r   Úscipy.sparse.linalgr   Úscipy.linalgr   Úsksparse.cholmodr   r	   rz   ÚImportErrorr{   Únumpyr   r
   r   Ú__all__r   r;   rQ   re   rO   r   © r   r   ú<module>rŠ      s[  ðØ 7Ð 7à DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DØ .Ð .Ð .Ð .Ð .Ð .Ø Ð Ð Ð Ø Ð Ð Ð ðØKÐKÐKÐKÐKÐKÐKÐKØÐÐøØð ð ð Ø€O€O€OØÐÐÐðøøøð Ð Ð Ð Ø )Ð )Ð )Ð )Ð )Ð )Ð )Ð )ð Øð€ð ð  ð  ðF$0ð $0ð $0ðNP0ð P0ð P0ðf;0ð ;0ð ;0ð|30ð 30ð 30ðltð tð tð tð tð ts   ž
) ©7¶7