
    \h<                         S SK Jr  S SKJrJrJr  S SKJr  S SKJ	r	J
r
  SS.S jrSSS	.S
 jrS rS rS rS rS rS rS rSS.S jrSS.S jrSS.S jrS rS rS rS rg)    )ZZ)SDM	sdm_irrefsdm_rref_den)DDM)	ddm_irrefddm_irref_denauto)methodc                \   [        XSS9u  p[        X5      u  pUS:X  a  [        U 5      n[        U5      u  pVOeUS:X  a  [	        U 5      u  pxn[        U5      U-  nOBUS:X  a.  U R                  SS9u  p[	        U
5      u  pxn[        U5      U-  nO[        SU 35      e[        XS5      u  pYXV4$ )	aZ  
Compute the reduced row echelon form of a ``DomainMatrix``.

This function is the implementation of :meth:`DomainMatrix.rref`.

Chooses the best algorithm depending on the domain, shape, and sparsity of
the matrix as well as things like the bit count in the case of :ref:`ZZ` or
:ref:`QQ`. The result is returned over the field associated with the domain
of the Matrix.

See Also
========

sympy.polys.matrices.domainmatrix.DomainMatrix.rref
    The ``DomainMatrix`` method that calls this function.
sympy.polys.matrices.rref._dm_rref_den
    Alternative function for computing RREF with denominator.
FdenominatorGJFFCDTconvertUnknown method for rref: )_dm_rref_choose_method
_dm_to_fmt	_to_field_dm_rref_GJ_dm_rref_den_FFclear_denoms_rowwise
ValueError)Mr   use_fmtold_fmtMfM_rrefpivotsM_rref_fden_Mrs              Q/var/www/auris/envauris/lib/python3.13/site-packages/sympy/polys/matrices/rref.py_dm_rrefr'   %   s    & -QEJOFA'JA~q\$R	4 / 2v8$s*	4&&t&4 / 3v8$s* 4VH=>>6+IF >    T)keep_domainr   c                   [        XSS9u  p#[        X5      u  pUS:X  a  [        U 5      u  pVnGOHUS:X  a  [        [	        U 5      5      u  pU(       a^  UR
                  U R
                  :w  aD  UR                  SS9u  pU(       a  USUS   4   R                  nOUR
                  R                  nOUnUR
                  R                  nOUS:X  a  U R                  SS9u  p[        U
5      u  pnU(       a?  UR
                  U R
                  :w  a%  [	        U5      U-  nU R
                  R                  nOCUnU(       a  USUS   4   R                  nO%UR
                  R                  nO[        SU 35      e[        XT5      u  pYXVU4$ )	a  
Compute the reduced row echelon form of a ``DomainMatrix`` with denominator.

This function is the implementation of :meth:`DomainMatrix.rref_den`.

Chooses the best algorithm depending on the domain, shape, and sparsity of
the matrix as well as things like the bit count in the case of :ref:`ZZ` or
:ref:`QQ`. The result is returned over the same domain as the input matrix
unless ``keep_domain=False`` in which case the result might be over an
associated ring or field domain.

See Also
========

sympy.polys.matrices.domainmatrix.DomainMatrix.rref_den
    The ``DomainMatrix`` method that calls this function.
sympy.polys.matrices.rref._dm_rref
    Alternative function for computing RREF without denominator.
Tr   r   r   r   r   r   r   )r   r   r   r   r   domainclear_denomselementoner   r   )r   r)   r   r   r   r    r#   r!   r"   r$   r%   M_rref_rs               r&   _dm_rref_denr0   Y   sk   ( -QDIOFA'JA~-a0V	4&y|4 8??ahh6 --d-;IAQq	\*22mm'' F--##C	4&&t&4 / 3v8??ahh6x(3.F((,,C FQq	\*22mm''4VH=>>6+IF r(   c                     U R                   R                  nX!:X  a   X4$ US:X  a  U R                  5       n X4$ US:X  a  U R                  5       n X4$ [	        SU 35      e)z?Convert a matrix to the given format and return the old format.densesparsezUnknown format: )repfmtto_dense	to_sparser   )r   r5   r   s      r&   r   r      sr    eeiiG~ : 
JJL
 :	 
KKM : +C5122r(   c                 b    U R                   R                  S:X  a  [        U 5      $ [        U 5      $ )z:Compute RREF using Gauss-Jordan elimination with division.r3   )r4   r5   _dm_rref_GJ_sparse_dm_rref_GJ_denser   s    r&   r   r      s(    uuyyH!!$$ ##r(   c                 b    U R                   R                  S:X  a  [        U 5      $ [        U 5      $ )z:Compute RREF using fraction-free Gauss-Jordan elimination.r3   )r4   r5   _dm_rref_den_FF_sparse_dm_rref_den_FF_denser;   s    r&   r   r      s(    uuyyH%a(($Q''r(   c                     [        U R                  5      u  pn[        XR                  U R                  5      n[        U5      nU R                  U5      U4$ )zACompute RREF using sparse Gauss-Jordan elimination with division.)r   r4   r   shaper+   tuplefrom_rep)r   M_rref_dr!   r$   
M_rref_sdms        r&   r9   r9      sF    #AEE*HaXww1J6]F::j!6))r(   c                 b   U R                   R                  =(       d    U R                   R                  nU R                  R	                  5       R                  5       n[        X!S9n[        X R                  U R                   5      n[        U5      nU R                  UR                  5       5      U4$ )z@Compute RREF using dense Gauss-Jordan elimination with division.)_partial_pivot)r+   is_RRis_CCr4   to_ddmcopyr   r   r@   rA   rB   to_dfm_or_ddm)r   partial_pivotddmr!   
M_rref_ddms        r&   r:   r:      sx    HHNN4ahhnnM
%%,,.


Cs9FS''188,J6]F::j..01699r(   c                     [        U R                  U R                  5      u  pn[        XR                  U R                  5      n[        U5      nU R                  U5      X#4$ zACompute RREF using sparse fraction-free Gauss-Jordan elimination.)r   r4   r+   r   r@   rA   rB   )r   rC   r#   r!   rD   s        r&   r=   r=      sL    (9H6Xww1J6]F::j!3..r(   c                    U R                   R                  5       R                  5       n[        XR                  5      u  p#[        XR                  U R                  5      n[        U5      nU R                  UR                  5       5      X#4$ rP   )
r4   rI   rJ   r	   r+   r   r@   rA   rB   rK   )r   rM   r#   r!   rN   s        r&   r>   r>      sd    
%%,,.


CXX.KCS''188,J6]F::j..013>>r(   Fr   c                   US:w  a0  UR                  S5      (       a  US[        S5      *  nSnX4$ Sn X4$ SnU R                  nUR                  (       a  [	        XS9nX4$ UR
                  (       a  [        XS9nX4$ UR                  (       d  UR                  (       a  SnSnX4$ UR                  (       a(  U R                  R                  S:X  a  U(       d  SnSnX4$ U(       a  SnX4$ SnX4$ )	z3Choose the fastest method for computing RREF for M.r
   _denseNr2   r3   r   r   r   )endswithlenr+   is_ZZ_dm_rref_choose_method_ZZis_QQ_dm_rref_choose_method_QQrG   rH   is_EXr4   r5   )r   r   r   r   Ks        r&   r   r      s    ??8$$Oc(m^,FGJ ?G GF ?? HH77.qJF4 ?3 WW.qJF0 ?/ WWFG( ?' WWg-k FG ?  ? ?r(   c                v   [        U 5      u  p#nU[        SUS-  5      :  a  g[        U 5      u  pV[        U Vs/ s H  owR	                  5       PM     snSS9n[
        R                  n	U H2  n
[
        R                  " X5      n	U	R	                  5       SU-  :  d  M2    g   U	R	                  5       S:  a  ggs  snf )	z5Choose the fastest method for computing RREF over QQ.      r      default2   r   r   )_dm_row_densitymin_dm_QQ_numers_denomsmax
bit_lengthr   r.   lcm)r   r   densityr$   ncolsnumersdenomsn
numer_bits	denom_lcmds              r&   rY   rY     s     (*G Qa  *!,NFf5fllnf5qAJIFF9(	!AjL0  ") 6s   B6c                *   Sn[        U 5      u  p4nUS:  a
  X5S-  :  a  ggUS:  a  gUSX$-  -   :  a  g[        U 5      n[        U Vs/ s H  owR                  5       PM     snSS9n[        SS	U-  U-  5      n	SX$US-  -  -  -   U	-  n
X::  a  ggs  snf )
z5Choose the fastest method for computing RREF over ZZ.i'  
   r^   r   r   r]   r_   r`   gUUUUUU?)rc   _dm_elementsrf   rg   )r   r   PARAMri   nrows_nzrj   elementsebitswidenessmax_densitys              r&   rW   rW   F  s    $ E
  /q1Gu "}1W {	1u~%	% AH1111=D 1c%i()HutQw.//8;K 2s   Bc                     U R                   S   nU R                  R                  5       R                  5       nU(       d  SSU4$ [	        U5      n[        [        [        U5      5      U-  nXCU4$ )a  Density measure for sparse matrices.

Defines the "density", ``d`` as the average number of non-zero entries per
row except ignoring rows that are fully zero. RREF can ignore fully zero
rows so they are excluded. By definition ``d >= 1`` except that we define
``d = 0`` for the zero matrix.

Returns ``(density, nrows_nz, ncols)`` where ``nrows_nz`` counts the number
of nonzero rows and ``ncols`` is the number of columns.
r_   r   )r@   r4   to_sdmvaluesrU   summap)r   rj   rows_nzru   ri   s        r&   rc   rc   |  sb     GGAJEeelln##%G!U{w<c#w'(83%''r(   c                 *    U R                  5       u  pU$ )z*Return nonzero elements of a DomainMatrix.)
to_flat_nz)r   rv   r$   s      r&   rs   rs     s    ,,.KHOr(   c                     [        U 5      nU Vs/ s H  o"R                  PM     nnU Vs/ s H  o"R                  PM     nnX44$ s  snf s  snf )zBReturns the numerators and denominators of a DomainMatrix over QQ.)rs   	numeratorr   )Mqrv   rw   rk   rl   s        r&   re   re     sH    BH#+,8akk8F,%-.XmmXF.> -.s
   AA
c                 `    U R                   nUR                  (       a  U R                  5       $ U $ )z.Convert a DomainMatrix to a field if possible.)r+   has_assoc_Fieldto_field)r   r[   s     r&   r   r     s%    	Azz|r(   N)sympy.polys.domainsr   sympy.polys.matrices.sdmr   r   r   sympy.polys.matrices.ddmr   sympy.polys.matrices.denser   r	   r'   r0   r   r   r   r9   r:   r=   r>   r   rY   rW   rc   rs   re   r    r(   r&   <module>r      s   < # A A ( ? ! 1h $( K\"$(*:/? 6; +\ 16 *Z 16 3l(,r(   