
    sg<                         d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	m
Z
 dddZddd	d
Zd Zd Zd Zd Zd Zd Zd ZdddZdddZdddZd Zd Zd Zd Zy)    )ZZ)SDM	sdm_irrefsdm_rref_den)DDM)	ddm_irrefddm_irref_denauto)methodc                x   t        | |d      \  }}t        | |      \  } }|dk(  rt        |       }t        |      \  }}ni|dk(  rt	        |       \  }}}t        |      |z  }nF|dk(  r3| j                  d      \  }	}
t	        |
      \  }}}t        |      |z  }nt        d|       t        ||      \  }}	||fS )	a  
    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              L/var/www/html/venv/lib/python3.12/site-packages/sympy/polys/matrices/rref.py_dm_rrefr'   %   s    & -QEJOFGAw'JAw~q\$R	4 / 2#v8$s*	4&&t&42 / 3#v8$s* 4VH=>>67+IFA 6>    T)keep_domainr   c                
   t        | |d      \  }}t        | |      \  } }|dk(  rt        |       \  }}}n:|dk(  rt        t	        |             \  }}|r\|j
                  | j
                  k7  rC|j                  d      \  }	}|r|d|d   f   j                  }n|j
                  j                  }n|}|j
                  j                  }n|dk(  r| j                  d      \  }	}
t        |
      \  }}}|r>|j
                  | j
                  k7  r%t	        |      |z  }| j
                  j                  }n>|}|r|d|d   f   j                  }n%|j
                  j                  }nt        d|       t        ||      \  }}	|||fS )	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   s   ( -QDIOFGAw'JAw~-a0V	4&y|4& 8??ahh6 --d-;IAvQq	\*22mm'' F--##C	4&&t&42 / 3#v8??ahh6x(3.F((,,C FQq	\*22mm''4VH=>>67+IFA 3r(   c                     | j                   j                  }||k(  r	 | |fS |dk(  r| j                         } | |fS |dk(  r| j                         } | |fS t	        d|       )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      sz    eeiiG#~ g: 
JJL
 g:	 
KKM g: +C5122r(   c                 `    | j                   j                  dk(  rt        |       S t        |       S )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                 `    | j                   j                  dk(  rt        |       S t        |       S )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                     t        | j                        \  }}}t        || j                  | j                        }t        |      }| j                  |      |fS )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      sJ    #AEE*HfaXqww1J6]F::j!6))r(   c                 b   | j                   j                  xs | j                   j                  }| j                  j	                         j                         }t        ||      }t        || j                  | j                         }t        |      }| j                  |j                               |fS )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:      s|    HHNN4ahhnnM
%%,,.


Cs=9FS!''188,J6]F::j..01699r(   c                     t        | j                  | j                        \  }}}t        || j                  | j                        }t        |      }| j                  |      ||fS 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=      sR    (9Hc6Xqww1J6]F::j!3..r(   c                     | j                   j                         j                         }t        || j                        \  }}t        || j                  | j                        }t        |      }| j                  |j                               ||fS rP   )
r4   rI   rJ   r	   r+   r   r@   rA   rB   rK   )r   rM   r#   r!   rN   s        r&   r>   r>      sl    
%%,,.


CQXX.KCS!''188,J6]F::j..013>>r(   Fr   c                   |dk7  r,|j                  d      r|dt        d        }d}||fS d}||fS d}| j                  }|j                  rt	        | |      }||fS |j
                  rt        | |      }||fS |j                  s|j                  rd}d}||fS |j                  r#| j                  j                  dk(  r
|sd}d}||fS |rd}||fS d}||fS )	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 7?G GF 7?? HH77.qkJF4 7?3 WW.qkJF0 7?/ WWFG( 7?' WWg-k FG 7?  7? 7?r(   c                r   t        |       \  }}}|t        d|dz        k  ryt        |       \  }}t        |D cg c]  }|j	                          c}d      }t
        j                  }	|D ]0  }
t        j                  |	|
      }	|	j	                         d|z  kD  s0 y |	j	                         dk  ryyc c}w )	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     (*GQ Qa  *!,NFFf5alln5qAJI FF9a(	!AjL0 ") 6s   B4c                ,   d}t        |       \  }}}|dk  r
||dz  k  ryy|dk  ry|d||z  z   kD  ryt        |       }t        |D cg c]  }|j                          c}d      }t        dd	|z  |z        }	d|||dz  z  z  z   |	z  }
||
k  ryyc c}w )
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GXu "}U1W {	1uX~%	% AH1111=D 1c%i()HuhtQw.//8;K 2s   Bc                     | j                   d   }| j                  j                         j                         }|sdd|fS t	        |      }t        t        t        |            |z  }|||fS )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   |  sd     GGAJEeelln##%G!U{w<c#w'(83%''r(   c                 ,    | j                         \  }}|S )z*Return nonzero elements of a DomainMatrix.)
to_flat_nz)r   rv   r$   s      r&   rs   rs     s    ,,.KHaOr(   c                     t        |       }|D cg c]  }|j                   }}|D cg c]  }|j                   }}||fS c c}w c c}w )zBReturns the numerators and denominators of a DomainMatrix over QQ.)rs   	numeratorr   )Mqrv   rw   rk   rl   s        r&   re   re     sL    BH#+,aakk,F,%-.amm.F.6> -.s
   AAc                 V    | j                   }|j                  r| j                         S | S )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(   