
    tKgq3                         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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y# e$ r	 ddlZdZY /w xY w)z1Basic linear factorizations needed by the solver.    )bmat
csc_matrixeyeissparse)LinearOperatorNcholesky_AAtTF)warn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         r/home/alanp/www/video.onchill/myenv/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                 R    	 t               	 	fd} 	fd} 	fd}|||fS )zLReturn linear operators for matrix A using ``NormalEquation`` approach.
    c                 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_space@   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_squaresR   s    aeeAhr   c                 F    j                   j                   |             S r-   r#   r   r/   s    r   	row_spacez.normal_equation_projections.<locals>.row_spaceV   s    sswwvay!!r   r   )
r   mnr*   r)   tolr+   r0   r3   r(   s
   `  ``    @r   normal_equation_projectionsr7   9   s,     !_F$ " }i//r   c           	      l   	
 t        t        t               j                  g dgg            		 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``.NzVSingular 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   Kr4   r)   r5   r*   solves          r   r+   z0augmented_system_projections.<locals>.null_spacer   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 r-   r   r=   r>   )r$   r%   r?   r4   r5   rC   s      r   r0   z3augmented_system_projections.<locals>.least_squares   s;     IIq"((1+&' qa!}r   c                 r    t        j                  t        j                        | g      } |      }|d  S r-   rE   )r$   r%   r?   r5   rC   s      r   r3   z/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   r4   r5   r*   r)   r6   r+   r0   r3   rB   rC   s   `````    @@r   augmented_system_projectionsrK   \   s     	4#a&!##D	234A
=##..q1 D }i//M  = +	 -QYY[-.8-6= 	=	=s   )A< <4B32B3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.r9   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   r>   r   )r$   aux1aux2r%   r&   r'   r   PQRr4   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 )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   sJ    sswwqz||,,QE,BHHQK!r   c                 z    |    }t         j                  j                  |dd      }j                  |      }|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   sC    t||,,Q3836 - 8 EE$K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     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$   rU   rV   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!   r.   r$   rU   rV   r&   rc   rd   re   s       r   r0   z4svd_factorization_projections.<locals>.least_squares  s/    vvays4xEE$Kr   c                     j                   j                  |       }dz  |z  }j                   j                  |      }|S rg   r2   rh   s       r   r3   z0svd_factorization_projections.<locals>.row_space  s7    sswwqzs4xDDHHTNr   )r   r   svd)r   r4   r5   r*   r)   r6   r+   r0   r3   rc   rd   re   s   `  ``    @@@r   rI   rI      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 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)NormalEquationrl   z%Method not allowed for sparse matrix.rm   zmOnly accepts 'NormalEquation' option when scikit-sparse is available. Using 'AugmentedSystem' option instead.r9   r:   QRFactorization)rn   SVDFactorizationz#Method not allowed for dense array.ro   )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   #  ss   T 88A;DAq 	saxqM {>&F>>DEE%%.@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=r9   gV瞯<)__doc__scipy.sparser   r   r   r   scipy.sparse.linalgr   scipy.linalgr   sksparse.cholmodr	   rr   ImportErrorrs   numpyr   r
   __all__r   r7   rK   r_   rI   r    r   r   <module>r      s|    7 : : .  -    F 0FP0f;0|30lts  s   A AA