Ë
    âQ(hQI  ã                   óp   — d Z ddlZddlmZ ddlmZ ddlZddlm	Z	 d„ Z
d„ Zd„ Zd	„ Zdd
„Zd„ Zd„ Zdd„Zy)zn
Routines for removing redundant (linearly dependent) equations from linear
programming equality constraints.
é    N)Úsvd)Úinterp_decomp©Údtrsmc                 ó„   — d}t        j                  t        | «      |kD  j                  d¬«      «      j	                  «       S )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

    g‚vIhÂ%<=é   ©Úaxis)ÚnpÚarrayÚabsÚsumÚflatten)ÚAÚtols     ú_/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/scipy/optimize/_remove_redundancy.pyÚ
_row_countr      s7   € ð" €CÜ�8‰8”S˜“V˜c‘\×&Ñ&¨AÐ&Ó.Ó/×7Ñ7Ó9Ð9ó    c                 óH   — t        | «      }t        j                  ||z  «      S )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Ü�9‰9�Y Ñ-Ó.Ð.r   c                 óÔ   — d}d}t        | «      dk(  }| t        j                  |«      dd…f   } t        j                  ||   d«      sd}d}|t        j                  |«         }| |||fS )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#   :   st   € ð4 €FØ€GÜ˜‹]˜aÑ€FØ	Œ"�.‰.˜Ó
 ¢!Ð
#Ñ$€AÜ�;‰;�q˜‘y !Ô$Øˆð>ˆà	Œ"�.‰.˜Ó
 Ñ!€AØˆa�˜Ð Ð r   c                 ó®   — | \  }}||   }t        d||dd¬«      }|d |dz    |d |dz   …|f<   ||dz   d  }|||f   }	||dz   d …|fxx   ||	z  z  cc<   ||fS )Nr   )ÚlowerÚdiagr   )
ÚpluÚperm_rÚvÚjÚLUÚpÚvpermÚuÚlÚpivs
             r   Úbg_update_denser1   `   s†   € Ø�E€Bˆàˆf‰I€EÜˆa��U !¨!Ô,€AØ�D�Q�q‘S�'€B€tˆˆ!‰€tˆQ€w�KØ	ˆ!ˆA‰#ˆ$ˆ€AØ
ˆQ�ˆT‰(€CØ€qˆ�s�tˆQ€wƒK�A�c‘EÑƒKØˆqˆ5€Lr   c           
      óÔ  — d}d}d}d}d}t        | |«      \  } }}}|dk7  r| |||fS | j                  \  }}	t        t        |«      «      }
t        |
«      }g }d}| }t	        j
                  |||	z   fd¬«      } t	        j                  | d«       || dd…|d…f<   t	        j
                  |«      }t	        j                  |||	z   t        ¬	«      }t	        j                  |j                  t        ¬	«      }t	        j                  |d¬«      t	        j                  |«      f}|d   }|
D �]”  }d||<   |dkD  rd||dz
  <   	 ||dz
     }t        ||| dd…|f   |dz
  «      }t        j                  j%                  ||d¬
«      }||   }d}t        dt'        |«      |«      D ]Š  }||t)        ||z   t'        |«      «       }t+        | dd…|f   j-                  «       j/                  |«      «      }||kD  j1                  «       sŒa||t	        j2                  |«      z      }|||<   d|||z
  <    �Œ |j4                  j/                  |j7                  dd«      «      }t        j                  j9                  |«      } t+        |«      d| z   z  |kD  rd}|}||||fc S |j;                  |«       |€�Œ‚t'        |«      ||z
  k(  s�Œ• n t=        t        |«      «      }!t        |!t=        |«      z
  «      }!||!dd…f   ||!   ||fS # t        $ rg t        j                  j!                  | dd…|f   «      }|\  }}t        t        |«      «      }t#        |«      D ]  \  }}||   ||   c||<   ||<   Œ Y �Œw xY w)áï  
    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.

    ç:Œ0âŽyE>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)Útransé2   Féÿÿÿÿr   )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   sz  € ð@ €GØ€IØ€FØ€Gð5€Lô
 0°°3Ó7Ñ€A€sˆF�Gà�‚{Ø�#�v˜wÐ&Ð&à�7‰7�D€A€qäŒU�1‹X‹€AÜˆQ‹€Að 	€AØ€Fà€FÜ
�‰�!�Q˜‘U� 3Ô'€AÜ×Ñ�Q˜ÔØ€A‚aˆ‰€e�HÜ
�‰�‹€Aä—I‘I˜a  1¡¬CÔ0€Mä�g‰g�m×)Ñ)´Ô6€Gô 
�‰�˜Ô	œrŸy™y¨›|Ð	+€BØ�‰U€FØó *ˆàˆˆ!‰ØˆqŠ5ØˆAˆa�‰c‰Fð	@Ø�!�A‘#‘ˆAÜ   V¨Qªq°!¨t©W°a¸±cÓ:ˆBô �\‰\×"Ñ" 2 q°Ð"Ó2ˆà˜7Ñ#ˆØˆô ˜Q¤ B£¨Ó/ò 	ˆGØ˜7¤C¨°©´s¸2³wÓ$?Ð@ˆIä�A’a˜�l‘O×-Ñ-Ó/×3Ñ3°BÓ7Ó8ˆAØ�G‘× Ñ Õ"Ø�w¤§¡¨1£Ñ-Ñ.�Ø��!‘Ø$�˜˜!™‘Úð	ð —D‘D—H‘H˜SŸ[™[¨¨QÓ/Ó0ˆEÜ—I‘I—N‘N 3Ó'ˆEÜ�5‹z˜1˜U™7Ñ# iÒ/Ø�Ø&�Ø˜s F¨GÐ3Ò3à—‘˜”ØÒ(¬S°«V°q¸9±}Ô-DÙðU*ôX Œu�Q‹x‹=€DÜ�”s˜1“v‘Ó€DØ�$š�'‰?˜C ™I v¨wÐ6Ð6øôK ò 	@Ü—‘×'Ñ'¨ª!¨Q¨$©Ó0ˆBØ‰EˆB�Üœ% ›(“^ˆFÜ# A›,ò @‘��BØ)/°©°V¸B±ZÐ&��r‘
˜F 2šJó@ð		@ús   Ä; K7Ë7A,M'Í&M'c                 óè  — d}d}d}d}d}t        | |«      \  } }}}|dk7  r| |||fS | j                  \  }}t        t        |«      «      }	t        |	«      }
t	        t        |||z   «      «      }g }| }t
        j                  j                  t
        j                  j                  |«      | f«      j                  «       } t        j                  |«      }|	D �]a  }| dd…|
f   }d||<   |dkD  rd||dz
  <   t
        j                  j                  j                  |j                  «       |«      j                  dd«      }t        |t	        |
«      z
  «      }t        j                   | dd…|f   j                  «       j#                  |«      «      |kD  j%                  «       d   }t'        |«      dkD  r||d      }||
|<   Œç|j(                  j#                  |j                  dd«      «      }t        j                  j+                  |«      }t!        |«      d|z   z  |kD  rd}|}||||fc S |j-                  |«       �Œd t	        t        |«      «      }t        |t	        |«      z
  «      }||dd…f   ||   ||fS )	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   ë   sQ  € ðB €GØ€IØ€FØ€Gð5€Lô
 0°°3Ó7Ñ€A€sˆF�Gà�‚{Ø�#�v˜wÐ&Ð&à�7‰7�D€A€qäŒU�1‹X‹€AÜˆQ‹€Aô 	ŒE�!�Q�q‘S‹MÓ€AØ
€Aà€FÜ�‰×ÑœUŸ\™\×-Ñ-¨aÓ0°!Ð4Ó5×;Ñ;Ó=€AÜ
�‰�‹€Að, ó (ˆØŠa�ˆd‰Gˆàˆˆ!‰ØˆqŠ5ØˆAˆa�‰c‰Fä�\‰\× Ñ ×(Ñ(¨¯©«¸Ó:×BÑBÀ2ÀqÓIˆä�!”C˜“F‘(‹^ˆô �V‰V�A’a˜�e‘H×&Ñ&Ó(×,Ñ,¨RÓ0Ó1°GÑ;×DÑDÓFÀqÑIˆÜˆq‹6�AŠ:Ø�1�Q‘4‘ˆAð ˆAˆaŠDà—D‘D—H‘H˜SŸ[™[¨¨QÓ/Ó0ˆEÜ—I‘I—N‘N 3Ó'ˆEÜ�5‹z˜1˜u™9Ñ%¨	Ò1Ø�Ø&�Ø˜s F¨GÐ3Ò3à—‘˜–ðQ(ôT Œu�Q‹x‹=€DÜ�”s˜1“v‘Ó€DØ�$š�'‰?˜C ™I v¨wÐ6Ð6r   c                 ó¬  — t        | |«      \  } }}}|dk7  r| |||fS t        | «      \  }}}t        j                  t        «      j
                  }|j                  «       t        | j                  «      z  |z  }| j                  \  }	}
|	|
k  r|d   nd}t        |«      |k  �r.|dd…df   }t        j                  |«      |dz  kD  }t        j                  |«      r:t        j                  t        j                  |j                  | «      «      |kD  «      rd}d}n¶t        j                  t        j                  |j                  |«      «      |dz  kD  «      rd}d	}ntt        | |«      }t        j                  | |d¬
«      } t        j                  ||«      }t        | «      \  }}}| j                  \  }	}
|	|
k  r|d   nd}t        |«      |k  r�Œ.| |||fS )aÚ  
    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é   zþDue 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€qˆ&�'à�‚{Ø�!�V˜WÐ$Ð$ä�1‹v�H€A€qˆ"Ü
�(‰(”5‹/×
Ñ
€CØ
�%‰%‹'”C˜Ÿ™“LÑ
  3Ñ
&€Cà�7‰7�D€A€qØ˜!’VˆAˆbŠE €Eô" ˆe‹*�sÓ
ØŠa�ˆe‰Hˆä—v‘v˜a“y 3¨¡:Ñ-ˆÜ�v‰v�lÔ#¤r§v¡v¬b¯f©f°Q·U±U¸1³XÓ.>ÀÑ.DÔ'EØˆFð%ˆGð Ü�6‰6”"—&‘&˜Ÿ™˜q›Ó" S¨3¡YÑ.Ô/ØˆFð8ˆGð
 ä  <Ó0ˆÜ�I‰I�a˜¨Ô*ˆÜ�I‰I�a˜Ó"ˆÜ�q“6‰ˆˆ1ˆbØ�w‰w‰ˆˆ1Ø˜aš��"’ Qˆô7 ˆe‹*�sÔ
ð: ˆa�˜Ð Ð r   c                 óv  — d}d}d}t        | |«      \  } }}}|dk7  r| |||fS | j                  \  }}|}	|€t        j                  j	                  | «      }	t        | j                  |	|¬«      \  }
}t        j                  ||
d|	    |z  ||
|	d    «      sd}|}t        |
d|	 «      }
| |
dd…f   }||
   }||||fS )aË  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ð5€Lô 0°°3Ó7Ñ€A€sˆF�Gà�‚{Ø�#�v˜wÐ&Ð&à�7‰7�D€A€qà€AØ€|Ü�I‰I×!Ñ! !Ó$ˆä˜aŸc™c 1¨:Ô6�I€Cˆô �;‰;�s˜3˜r ˜7‘| dÑ*¨C°°A°B°©LÔ9ØˆØˆô ��R�a�‹/€CØ	
ˆ3’ˆ6‰€BØˆs‰8€DØˆt�V˜WÐ$Ð$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   