
    (phQI                     t    S r SSKrSSKJr  SSKJr  SSKrSSKJ	r	  S r
S rS rS	 rSS
 jrS rS rSS jrg)zn
Routines for removing redundant (linearly dependent) equations from linear
programming equality constraints.
    N)svd)interp_decompdtrsmc                     Sn[         R                  " [        U 5      U:  R                  SS95      R	                  5       $ )a^  
Counts the number of nonzeros in each row of input array A.
Nonzeros are defined as any element with absolute value greater than
tol = 1e-13. This value should probably be an input to the function.

Parameters
----------
A : 2-D array
    An array representing a matrix

Returns
-------
rowcount : 1-D array
    Number of nonzeros in each row of A

gvIh%<=   axis)nparrayabssumflatten)Atols     T/var/www/html/venv/lib/python3.13/site-packages/scipy/optimize/_remove_redundancy.py
_row_countr      s7    " C88SVc\&&A&./7799    c                 H    [        U 5      n[        R                  " X!-  5      $ )a  
Returns the index of the densest row of A. Ignores rows that are not
eligible for consideration.

Parameters
----------
A : 2-D array
    An array representing a matrix
eligibleRows : 1-D logical array
    Values indicate whether the corresponding row of A is eligible
    to be considered

Returns
-------
i_densest : int
    Index of the densest row in A eligible for consideration

)r   r   argmax)r   eligibleRows	rowCountss      r   _get_densestr   #   s    & 1I99Y-..r   c                     SnSn[        U 5      S:H  nU [        R                  " U5      SS24   n [        R                  " X   S5      (       d  SnSnU[        R                  " U5         nXX#4$ )a  
 Eliminates trivial equations from system of equations defined by Ax = b
and identifies trivial infeasibilities

 Parameters
 ----------
 A : 2-D array
     An array representing the left-hand side of a system of equations
 b : 1-D array
     An array representing the right-hand side of a system of equations

 Returns
 -------
 A : 2-D array
     An array representing the left-hand side of a system of equations
 b : 1-D array
     An array representing the right-hand side of a system of equations
 status: int
     An integer indicating the status of the removal operation
     0: No infeasibility identified
     2: Trivially infeasible
 message : str
     A string descriptor of the exit status of the optimization.

 r    N   zbThere is a zero row in A_eq with a nonzero corresponding entry in b_eq. The problem is infeasible.)r   r   logical_notallclose)r   bstatusmessagei_zeros        r   _remove_zero_rowsr#   :   sq    4 FG]aF	"..
 !
#$A;;qy!$$>	"..
 !A  r   c                     U u  pEX!   n[        SXFSSS9nUS US-    US US-   2U4'   XsS-   S  nXCU4   n	XCS-   S 2U4==   X-  -  ss'   XE4$ )Nr   )lowerdiagr   )
pluperm_rvjLUpvpermulpivs
             r   bg_update_denser1   `   sv    EBIEa!!,ADQqS'Bt!tQwK	A#$A
T(CstQwKAEK5Lr   c           
         SnSnSnSnSn[        X5      u  ppVUS:w  a  XXV4$ U R                  u  p[        [        U5      5      n
[        U
5      n/ nSnU n[        R
                  " XU	-   4SS9n [        R                  " U S5        XSS2US24'   [        R
                  " U5      n[        R                  " XU	-   [        S	9n[        R                  " UR                  [        S	9n[        R                  " USS9[        R                  " U5      4nUS   nU
 GH  nSUU'   US:  a  SUUS-
  '    UUS-
     n[        UXSS2U4   US-
  5      n[        R                  R%                  UUSS
9nUU   nSn[        S['        U5      U5       H  nUU[)        UU-   ['        U5      5       n[+        U SS2U4   R-                  5       R/                  U5      5      nUU:  R1                  5       (       d  Mg  UU[        R2                  " U5      -      nUUU'   SUUU-
  '     GM     UR4                  R/                  UR7                  SS5      5      n[        R                  R9                  U5      n [+        U5      SU -   -  U:  a
  SnUnXXV4s  $ UR;                  U5        Uc  GM  ['        U5      X-
  :X  d  GM    O   [=        [        U5      5      n![        U![=        U5      -
  5      n!UU!SS24   UU!   XV4$ ! [         ai    [        R                  R!                  U SS2U4   5      nUu  nn[        [        U5      5      n[#        U5       H  u  nnUU   UU   sUU'   UU'   M      GNf = f)  
Eliminates redundant equations from system of equations defined by Ax = b
and identifies infeasibilities.

Parameters
----------
A : 2-D sparse matrix
    An matrix representing the left-hand side of a system of equations
rhs : 1-D array
    An array representing the right-hand side of a system of equations

Returns
-------
A : 2-D sparse matrix
    A matrix representing the left-hand side of a system of equations
rhs : 1-D array
    An array representing the right-hand side of a system of equations
status: int
    An integer indicating the status of the system
    0: No infeasibility identified
    2: Trivially infeasible
message : str
    A string descriptor of the exit status of the optimization.

References
----------
.. [2] Andersen, Erling D. "Finding all linearly dependent rows in
       large-scale linear programming." Optimization Methods and Software
       6.3 (1995): 219-227.

:0yE>r   r   There is a linear combination of rows of A_eq that results in zero, suggesting a redundant constraint. However the same linear combination of b_eq is nonzero, suggesting that the constraints conflict and the problem is infeasible.NF)orderr   )dtype)trans2   Fr   )r#   shapelistranger   zerosfill_diagonalarangeintonesbooleyer1   	Exceptionscipylinalg	lu_factor	enumeratelu_solvelenminr   	transposedotanyr   Treshapenormappendset)"r   rhs	true_ranktolapiv	tolprimalr    r!   inconsistentmnr)   r   dr(   A_origejs_candidatesjs_maskluir*   r+   r,   i1i2pijsbatchj_index	j_indicescbibarbnormkeeps"                                     r   _remove_redundancy_pivot_densero   l   sP   @ GIFG5L
 07AF{v&&77DAU1XAQA 	AFF
!U3'AQaeH
AIIa1C0Mggm))6G 
	ryy|	+BUF!q5AacF	@!A#A Vq!tWac:B \\""2q"27# QB/G7Cs2w$?@IAalO--/33B78AG  ""w1-.!$! 0 DDHHS[[Q/0EIINN3'E5z1U7#i/&F33(SVq}-DU X uQx=Ds1vD$'?CIv66K  	@''!Q$0BEB%(^F#A,B)/VBZ&r
F2J '		@s   /K22A/M%$M%c                    SnSnSnSnSn[        X5      u  ppEUS:w  a  XXE4$ U R                  u  px[        [        U5      5      n	[        U	5      n
[	        [        XwU-   5      5      n/ nU n[
        R                  R                  [
        R                  R                  U5      U 45      R                  5       n [        R                  " U5      nU	 GHb  nU SS2U
4   nSX'   US:  a  SXS-
  '   [
        R                  R                  R                  UR                  5       U5      R                  SS5      n[        U[	        U
5      -
  5      n[        R                   " U SS2U4   R                  5       R#                  U5      5      U:  R%                  5       S   n['        U5      S:  a  UUS      nUX'   M  UR(                  R#                  UR                  SS5      5      n[        R                  R+                  U5      n[!        U5      SU-   -  U:  a
  SnUnXXE4s  $ UR-                  U5        GMe     [	        [        U5      5      n[        U[	        U5      -
  5      nUUSS24   UU   XE4$ )	r3   r4   r   r   r5   Nr   r;   r   )r#   r<   r=   r>   rU   rG   sparsehstackrE   tocscr   r?   rH   spsolverN   rR   r   rO   nonzerorL   rQ   rS   rT   )r   rV   rX   rY   r    r!   rZ   r[   r\   r)   r   kr]   r^   r_   rc   Brf   rg   rk   r*   rl   rm   rn   s                           r   _remove_redundancy_pivot_sparserx      s2   B GIFG5L
 07AF{v&&77DAU1XAQA 	E!qSMA
AFU\\--a0!45;;=A
A, adGq5AcF\\  ((:BB2qI!CF(^ VVAaeH&&(,,R01G;DDFqIq6A:1Q4A ADDDHHS[[Q/0EIINN3'E5z1u9%	1&F33Q T uQx=Ds1vD$'?CIv66r   c                    [        X5      u  pp#US:w  a  XX#4$ [        U 5      u  pEn[        R                  " [        5      R
                  nUR                  5       [        U R                  5      -  U-  nU R                  u  pX::  a  US   OSn[        U5      U:  Ga?  USS2S4   n[        R                  " U5      US-  :  n[        R                  " U5      (       aA  [        R                  " [        R                  " UR                  U 5      5      U:  5      (       a  SnSnO[        R                  " [        R                  " UR                  U5      5      US-  :  5      (       a  SnS	nOp[        X5      n[        R                  " XSS
9n [        R                  " X5      n[        U 5      u  pEnU R                  u  pX::  a  US   OSn[        U5      U:  a  GM?  XX#4$ )ar  
Eliminates redundant equations from system of equations defined by Ax = b
and identifies infeasibilities.

Parameters
----------
A : 2-D array
    An array representing the left-hand side of a system of equations
b : 1-D array
    An array representing the right-hand side of a system of equations

Returns
-------
A : 2-D array
    An array representing the left-hand side of a system of equations
b : 1-D array
    An array representing the right-hand side of a system of equations
status: int
    An integer indicating the status of the system
    0: No infeasibility identified
    2: Trivially infeasible
message : str
    A string descriptor of the exit status of the optimization.

References
----------
.. [2] Andersen, Erling D. "Finding all linearly dependent rows in
       large-scale linear programming." Optimization Methods and Software
       6.3 (1995): 219-227.

r   r;   Ng    cA   zDue to numerical issues, redundant equality constraints could not be removed automatically. Try providing your constraint matrices as sparse matrices to activate sparse presolve, try turning off redundancy removal, or try turning off presolve altogether.d   r   r5   r	   )r#   r   r   finfofloatepsmaxr<   r   rP   rO   r   delete)r   r   r    r!   UsVhr~   r   r[   r\   s_minr)   r   i_removes                  r   _remove_redundancy_svdr   j  s   B .a3A&{V$$1vHA"
((5/

C
%%'CL
 3
&C77DAVAbEE" e*s
aeHvvay3:-vvl##rvvbffQUU1X.>.D'E'EF%G 66"&&q"S3Y.//F8G
 0IIa*IIa"q6bww"Q7 e*s
:   r   c                 d   SnSnSn[        X5      u  ppEUS:w  a  XXE4$ U R                  u  pxUn	Uc  [        R                  R	                  U 5      n	[        U R                  XS9u  p[        R                  " XSU	    U-  XU	S    5      (       d  SnUn[        U
SU	 5      n
X
SS24   nX   nXXE4$ )ag  Eliminates redundant equations from a system of equations.

Eliminates redundant equations from system of equations defined by Ax = b
and identifies infeasibilities.

Parameters
----------
A : 2-D array
    An array representing the left-hand side of a system of equations
rhs : 1-D array
    An array representing the right-hand side of a system of equations
rank : int, optional
    The rank of A
randomized: bool, optional
    True for randomized interpolative decomposition

Returns
-------
A : 2-D array
    An array representing the left-hand side of a system of equations
rhs : 1-D array
    An array representing the right-hand side of a system of equations
status: int
    An integer indicating the status of the system
    0: No infeasibility identified
    2: Trivially infeasible
message : str
    A string descriptor of the exit status of the optimization.

r   r   r5   N)randr   )	r#   r<   r   rH   matrix_rankr   rQ   r   sorted)r   rV   rank
randomizedr    r!   rZ   r[   r\   rv   idxprojA2rhs2s                 r   _remove_redundancy_idr     s    @ FG5L 07AF{v&&77DAA|II!!!$acc16IC ;;sr7|d*CABL99 Ra/C	
6B8DV$$r   )N)NT)__doc__numpyr   scipy.linalgr   scipy.linalg.interpolativer   rG   scipy.linalg.blasr   r   r   r#   r1   ro   rx   r   r    r   r   <module>r      sK      4  #:*/.#!L	|7~|7~Y!xD%r   