
    ^MhQI                     r    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dS )zn
Routines for removing redundant (linearly dependent) equations from linear
programming equality constraints.
    N)svd)interp_decompdtrsmc                     d}t          j        t          |           |k                        d                                                    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

    gvIh%<=   axis)nparrayabssumflatten)Atols     a/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/optimize/_remove_redundancy.py
_row_countr      s@    " C8SVVc\&&A&..//77999    c                 N    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9Y-...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#   :   s    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    EBfIEaU!!,,,ADQqSD'Bt!tQwK	!A#$$A
QT(CqsttQwKKKAcEKKKq5Lr   c           
         d}d}d}d}d}t          | |          \  } }}}|dk    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k    rd||dz
  <   	 ||dz
           }t          ||| dd|f         |dz
            }n# t          $ ry t          j                            | dd|f                   }|\  }}t          t          |                    }t#          |          D ]\  }}||         ||         c||<   ||<   Y nw xY wt          j                            ||d
          }||         }d}t          dt'          |          |          D ]}||t)          ||z   t'          |                             }t+          | dd|f                                                             |                    }||k                                    r,||t	          j        |          z            }|||<   d|||z
  <    n|j                            |                    dd                    }t          j                            |          } t+          |          d| z   z  |k    rd}|}||||fc S |                    |           |t'          |          ||z
  k    r nbt=          t          |                    }!t          |!t=          |          z
            }!||!ddf         ||!         ||fS )  
    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   s   @ GIFG5L
 0377AsFG{{#vw&&7DAqU1XXAQA 	AFF
!QU3'''AQAaaaeH
AIa1C000Mgm)666G 
			ry||	+BUF * *!q55AacF	@!A#A VQqqq!tWac::BB 	@ 	@ 	@''!!!Q$00BEB%((^^F#A,, @ @B)/VBZ&r
F2JJ@ @		@ \""2q"227# QB// 	 	G7Cs2ww$?$??@IAaaalO--//33B7788AG  "" w1-.!$!	 DHHS[[Q//00EINN3''E5zz1U7#i//&sFG3333(SVVq9}-D-DEuQxx==Ds1vvD$'?CIvw66s    *E++B G.-G.c                    d}d}d}d}d}t          | |          \  } }}}|dk    r| |||fS | j        \  }}t          t          |                    }	t          |	          }
t	          t          |||z                       }g }| }t
          j                            t
          j                            |          | f          	                                } t          j        |          }|	D ]}| dd|
f         }d||<   |dk    rd||dz
  <   t
          j        j                            |                                |                              dd          }t          |t	          |
          z
            }t          j        | dd|f                                                             |                    |k                                    d         }t'          |          dk    r||d                  }||
|<   |j                            |                    dd                    }t          j                            |          }t!          |          d|z   z  |k    rd}|}||||fc S |                    |           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      s   B GIFG5L
 0377AsFG{{#vw&&7DAqU1XXAQA 	E!QqSMMA
AFU\--a00!455;;==A
A,  ( (aaadG!q55AacF\ ((::BB2qII!CFF(^^ VAaaaeH&&((,,R0011G;DDFFqIq66A::1Q4A AaDDDHHS[[Q//00EINN3''E5zz1u9%	11&sFG3333uQxx==Ds1vvD$'?CIvw66r   c                    t          | |          \  } }}}|dk    r| |||fS t          |           \  }}}t          j        t                    j        }|                                t          | j                  z  |z  }| j        \  }	}
|	|
k    r|d         nd}t          |          |k     r?|dddf         }t          j        |          |dz  k    }t          j	        |          r=t          j	        t          j        |
                    |                     |k              rd}d}nt          j	        t          j        |
                    |                    |dz  k              rd}d	}n}t          | |          }t          j        | |d
          } t          j        ||          }t          |           \  }}}| j        \  }	}
|	|
k    r|d         nd}t          |          |k     ?| |||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   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3Aq&'{{!VW$$1vvHAq"
(5//
C
%%''CLL
 3
&C7DAq!VVAbEEE" e**s

aaaeHvayy3:-vl## 	rvbfQUU1XX.>.>.D'E'E 	F%G 6"&q""S3Y.// 	F8G
 <00Ia***Ia""q661bw1a"Q7 e**s

: a  r   Tc                    d}d}d}t          | |          \  } }}}|dk    r| |||fS | j        \  }}|}	|t          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5L 0377AsFG{{#vw&&7DAqA|I!!!$$ac1:666IC ;s3rr7|d*CABBL99  RaR//C	
36Bs8DtVW$$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      s               4 4 4 4 4 4  # # # # # #: : :*/ / /.#! #! #!L	 	 	|7 |7 |7 |7~|7 |7 |7~Y! Y! Y!xD% D% D% D% D% D%r   