
    Џkh4                         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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y# e$ r	 ddlZdZY 1w xY w)z1Basic linear factorizations needed by the solver.    )block_array	csc_array	eye_arrayissparse)LinearOperatorN)cholesky_AAtCholmodTypeConversionWarningTF)warncatch_warningsorthogonalityprojectionsc                 v   t         j                  j                  |      }t        |       r,t        j
                  j                  j                  | d      }n!t         j                  j                  | d      }|dk(  s|dk(  ryt         j                  j                  | 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         j/var/www/teggl/fontify/venv/lib/python3.12/site-packages/scipy/optimize/_trustregion_constr/projections.pyr   r      s    $ YY^^AF{$$))!)7u- {fkyy~~aeeAh'Hvf}%DK    c                     	 t        dt              5  t               	ddd        	fd} 	fd} 	fd}|||fS # 1 sw Y   "xY w)zLReturn linear operators for matrix A using ``NormalEquation`` approach.
    ignore)actioncategoryNc                 8    j                  |             }| j                  j                  |      z
  }d}t        |      kD  rR|k\  r	 |S  j                  |            }|j                  j                  |      z
  }|dz  }t        |      kD  rR|S Nr      r   Tr   )xvzkr   factor	max_refinorth_tols       r   
null_spacez/normal_equation_projections.<locals>.null_spaceD   s    1558
N Aq!H,I~ 	 quuQx AACCGGAJAFA Aq!H, r   c                 2     j                  |             S Nr   r(   r   r,   s    r   least_squaresz2normal_equation_projections.<locals>.least_squaresV   s    aeeAhr   c                 F    j                   j                   |             S r1   r'   r   r3   s    r   	row_spacez.normal_equation_projections.<locals>.row_spaceZ   s    sswwvay!!r   )r   r	   r   )
r   mnr.   r-   tolr/   r4   r7   r,   s
   `  ``    @r   normal_equation_projectionsr;   9   sL     
x2N	O !a!$ " }i//;! !s   AAc           	      ^   	
 t        t               j                  g dggd      		 t        j                  j
                  j                  	      
 	
fd}
fd}
fd	}|||fS # t        $ r. t        dd       t         j                         |      cY S w xY w)
z;Return linear operators for matrix A - ``AugmentedSystem``.Ncsc)formatzVSingular Jacobian matrix. Using dense SVD decomposition to perform the factorizations.   
stacklevelc                    t        j                  | t        j                  	      g      } |      }|d  }d}t        |      kD  rC|
k\  r	 |S |j	                  |      z
  } |      }||z  }|d  }|dz  }t        |      kD  rC|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     IIq"((1+&' q2AJ Aq!H,I~  f%E eI iFr
AFA Aq!H,  r   c                 x    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   s;     IIq"((1+&' qa!}r   c                 r    t        j                  t        j                        | g      } |      }|d  S r1   rK   )r(   r)   rE   r9   rI   s      r   r7   z/augmented_system_projections.<locals>.row_space   s7     IIrxx{A&' qbqz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   `   s     	ilACC(1d)4UCA
=##..q1 D }i//M  = +	 -QYY[-.8-6= 	=	=s   )A5 54B,+B,c                 X   	
 t         j                  j                   j                  dd      \  
	t        j                  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.r?   r@   c                    j                   j                  |       }t        j                  j	                  	|d      }t        j                  
      }||<   | j                   j                  |      z
  }d}t        |      kD  r}|k\  r	 |S j                   j                  |      }t        j                  j	                  	|d      }||<   |j                   j                  |      z
  }|dz  }t        |      kD  r}|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    sswwqz||,,QE,BHHQK!
N Aq!H,I~  33771:D<<00D0FDAaDACCGGAJAFA Aq!H, r   c                     j                   j                  |       }t        j                  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   sJ    sswwqz||,,QE,BHHQK!r   c                 z    |    }t         j                  j                  |dd      }j                  |      }|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   sC    t||,,Q3836 - 8 EE$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     llooaccDzoBGAq!	yy~~aAh'#- +	 -Q1-5-6-02 	2 2 }i//r   c                     	
 t         j                  j                   d      \  	
	dd|kD  f   	
|kD  ddf   
|kD      	
fd}	
fd}	
fd}|||fS )zNReturn linear operators for matrix A using ``SVDFactorization`` approach.
    F)full_matricesNc                    j                  |       }dz  |z  }j                  |      }| j                  j                  |      z
  }d}t        |      
kD  re|	k\  r	 |S j                  |      }dz  |z  }j                  |      }|j                  j                  |      z
  }|dz  }t        |      
kD  re|S )Nr%   r   r&   )r(   r[   r\   r)   r*   r+   r   UVtr-   r.   ss         r   r/   z1svd_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                 \    j                  |       }dz  |z  }j                  |      }|S Nr%   r2   r(   r[   r\   r*   ri   rj   rk   s       r   r4   z4svd_factorization_projections.<locals>.least_squares  s/    vvays4xEE$Kr   c                     j                   j                  |       }dz  |z  }j                   j                  |      }|S rm   r6   rn   s       r   r7   z0svd_factorization_projections.<locals>.row_space  s7    sswwqzs4xDDHHTNr   )r   r   svd)r   r8   r9   r.   r-   r:   r/   r4   r7   ri   rj   rk   s   `  ``    @@@r   rO   rO      sx     ||7HAq" 	
!QW*A	AGQJB	!c'
A 0 }i//r   c                 :   t        j                  |       \  }}||z  dk(  rt        |       } t        |       r=|d}|dvrt	        d      |dk(  r8t
        s2t        j                  dt        d       d}n|d	}|d
vrt	        d      |dk(  rt        | |||||      \  }}}	nM|dk(  rt        | |||||      \  }}}	n3|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   AugmentedSystem)NormalEquationrr   z$Method not allowed for sparse array.rs   zmOnly accepts 'NormalEquation' option when scikit-sparse is available. Using 'AugmentedSystem' option instead.r?   r@   QRFactorization)rt   SVDFactorizationz#Method not allowed for dense array.ru   )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   '  ss   T 88A;DAq 	saxaL {>&F>>CDD%%.@MM > (A7 'F>&F@@BCC!!)!Q8YL 	-
M9	$	$*1aHiM 	-
M9	$	$*1aHiM 	-
M9	%	%+Aq!Xy#N 	-
M9 	1vz*A	A	.B1vy)Ab!8Or   )Ng-q=r?   gV瞯<)__doc__scipy.sparser   r   r   r   scipy.sparse.linalgr   scipy.linalgr   sksparse.cholmodr   r	   rx   ImportErrorry   numpyr   r
   r   __all__r   r;   rQ   re   rO   r    r   r   <module>r      s|    7 D D .  K  )  F$0NP0f;0|30lt{  s   
A AA