
    \h0                         S SK Jr  S SKJr  S SKJrJr  SSKJrJ	r	J
r
Jr  SSKJr   SS jr  SS	 jr\	4S
 jr\	SS4S jr\	S4S jrS rS r\	SSS4S jrg)    )FunctionType)CoercionFailed)ZZQQ   )_get_intermediate_simp_iszero_dotprodsimp	_simplify)_find_reasonable_pivotTc	                   ^ ^^ UU 4S jn	UU 4S jn
UUU 4S jn[        [        5      mSu  p/ n/ nUT:  Ga&  X:  Ga   [        U	" U5      US XE5      u  nnnnU H  u  nnUU-  nUT UT-  U-   '   M     Uc  US-  nMM  UR                  U5        US:w  a"  U
" UUU-   5        UR                  UUU-   45        USL aC  XnnUT UT-  U-   '   [	        UT-  U-   S-   US-   T-  5       H  nT" T U   U-  5      T U'   M     Un[	        U5       H=  nUU:X  a  M  USL a  UU:  a  M  T UT-  U-      nU" U5      (       a  M2  U" UUUU5        M?     US-  nUT:  a  X:  a  GM   US	L ac  US	L a^  [        U5       HO  u  nnT UT-  U-      nUT UT-  U-   '   [	        UT-  U-   S-   US-   T-  5       H  nT" T U   U-  5      T U'   M     MQ     T [        U5      [        U5      4$ )
a{  Row reduce a flat list representation of a matrix and return a tuple
(rref_matrix, pivot_cols, swaps) where ``rref_matrix`` is a flat list,
``pivot_cols`` are the pivot columns and ``swaps`` are any row swaps that
were used in the process of row reduction.

Parameters
==========

mat : list
    list of matrix elements, must be ``rows`` * ``cols`` in length

rows, cols : integer
    number of rows and columns in flat list representation

one : SymPy object
    represents the value one, from ``Matrix.one``

iszerofunc : determines if an entry can be used as a pivot

simpfunc : used to simplify elements and test if they are
    zero if ``iszerofunc`` returns `None`

normalize_last : indicates where all row reduction should
    happen in a fraction-free manner and then the rows are
    normalized (so that the pivots are 1), or whether
    rows should be normalized along the way (like the naive
    row reduction algorithm)

normalize : whether pivot rows should be normalized so that
    the pivot value is 1

zero_above : whether entries above the pivot should be zeroed.
    If ``zero_above=False``, an echelon matrix will be returned.
c                    > TU S T2   $ N )icolsmats    Q/var/www/auris/envauris/lib/python3.13/site-packages/sympy/matrices/reductions.pyget_col!_row_reduce_list.<locals>.get_col/   s    17d7|    c                 p   > TUT-  US-   T-   TU T-  U S-   T-   sTU T-  U S-   T-  & TUT-  US-   T-  & g )Nr   r   )r   jr   r   s     r   row_swap"_row_reduce_list.<locals>.row_swap2   s]    $At|$c!D&!a%&> 	;AdFAE4< #afa!eT\":r   c                    > X1-
  T-  n[        UT-  US-   T-  5       H  nT" U TU   -  UTXT-      -  -
  5      TU'   M!     g)z,Does the row op row[i] = a*row[i] - b*row[j]r   N)range)	ar   br   qpr   isimpr   s	         r   cross_cancel&_row_reduce_list.<locals>.cross_cancel6   sP    UDLqvAt|,A1SV8aAE
l23CF -r   r   r   Nr   r   FT)r   r
   r   appendr   	enumeratetuple)r   rowsr   one
iszerofuncsimpfuncnormalize_last	normalize
zero_abover   r   r#   piv_rowpiv_col
pivot_colsswapspivot_offset	pivot_valassumed_nonzeronewly_determinedoffsetvalr   r   r!   rowpiv_ipiv_jr"   s   ` `                         @r   _row_reduce_listr=   
   s\   J?4 #<0EGJE D.W^,B *J-B	*i)
 .MVSgF),Ctg%& . qLG'"1WlW45LL'<'#9:; U"qA!C$
O1T6A:>AE4<8s1v	12A 9 I ;Cg~U"sW}c$h()C#Cg6  	1Y D.W^^ )t"3%j1LE5E$J./I&)Cd
U"#5:-1EAIt3CDs1v	12A E 2 j!5<//r   c                     [        [        U 5      U R                  U R                  U R                  XUXES9	u  pgnU R                  U R                  U R                  U5      Xx4$ )Nr-   r.   r/   )r=   listr)   r   r*   _new)	Mr+   r,   r-   r.   r/   r   r2   r3   s	            r   _row_reducerC   |   sU     .d1gqvvqvvquu8CU 66!&&!&&#&
99r   c                   ^ U R                   S::  d  U R                  S::  a  g[        U4S jU SS2S4    5       5      nT" U S   5      (       a  U=(       a    [        U SS2SS24   T5      $ U=(       a    [        U SS2SS24   T5      $ )zReturns `True` if the matrix is in echelon form. That is, all rows of
zeros are at the bottom, and below each leading non-zero in a row are
exclusively zeros.r   Tc              3   4   >#    U  H  nT" U5      v   M     g 7fr   r   ).0tr+   s     r   	<genexpr>_is_echelon.<locals>.<genexpr>   s     6XjmmXs   r   Nr%   )r)   r   all_is_echelon)rB   r+   zeros_belows    ` r   rK   rK      s    
 	vv{affk6Qqr1uX66K!D'@{1QU8Z@@=;qQRy*==r   Fc           	      t    [        U[        5      (       a  UO[        n[        XUSSSS9u  pVnU(       a  XV4$ U$ )aB  Returns a matrix row-equivalent to ``M`` that is in echelon form. Note
that echelon form of a matrix is *not* unique, however, properties like the
row space and the null space are preserved.

Examples
========

>>> from sympy import Matrix
>>> M = Matrix([[1, 2], [3, 4]])
>>> M.echelon_form()
Matrix([
[1,  2],
[0, -2]])
TFr?   )
isinstancer   r   rC   )rB   r+   simplifywith_pivotsr,   r   pivots_s           r   _echelon_formrS      sC      &h==x9H 5UDNC {Jr   c           	      B   S n[        U[        5      (       a  UO[        nU R                  S::  d  U R                  S::  a  gU R                  S::  d  U R                  S::  a  U  Vs/ s H
  oQ" U5      PM     nnSU;   a  gU R                  S:X  ad  U R                  S:X  aT  U  Vs/ s H
  oQ" U5      PM     nnSU;  a  SU;  a  gU R                  5       nU" U5      (       a  SU;   a  gU" U5      SL a  gU" XS9u  p[        XUSSSS	9u  pn	[        U
5      $ s  snf s  snf )
zReturns the rank of a matrix.

Examples
========

>>> from sympy import Matrix
>>> from sympy.abc import x
>>> m = Matrix([[1, 2], [x, 1 - 1/x]])
>>> m.rank()
2
>>> n = Matrix(3, 3, range(1, 10))
>>> n.rank()
2
c                    ^ ^ U U4S jn[        T R                  5       Vs/ s H  o2" U5      U4PM     nn[        U5       VVs/ s H  u  p5UPM	     nnnT R                  USS9U4$ s  snf s  snnf )aP  Permute columns with complicated elements as
far right as they can go.  Since the ``sympy`` row reduction
algorithms start on the left, having complexity right-shifted
speeds things up.

Returns a tuple (mat, perm) where perm is a permutation
of the columns to perform to shift the complex columns right, and mat
is the permuted matrix.c                 <   > [        U4S jTS S 2U 4    5       5      $ )Nc              3   >   >#    U  H  nT" U5      c  SOSv   M     g 7f)Nr   r   r   )rF   er+   s     r   rH   O_rank.<locals>._permute_complexity_right.<locals>.complexity.<locals>.<genexpr>   s     J'QJqM1qq8's   )sum)r   rB   r+   s    r   
complexity<_rank.<locals>._permute_complexity_right.<locals>.complexity   s     J!AqD'JJJr   r   )orientation)r   r   sortedpermute)rB   r+   r[   r   complexr   perms   ``     r   _permute_complexity_right(_rank.<locals>._permute_complexity_right   sj    	K
 05QVV}=}!JqM1%}=#)'?3?!1?3		$F	3T:: >3s   A'A,r   r   F   N)r+   Tr?   )rN   r   r   r)   r   detrC   len)rB   r+   rO   rb   r,   xzerosdr   rR   rQ   s              r   _rankrj      s    ;( &h==x9H
 	vv{affkvv{affk()*1A*E>vv{qvv{()*1A*$e"3EEGa==Ue^a=E!,QFFCs/LAq v;- + +s   &D$Dc                    [        U S5      (       d  g U R                  nUR                  nUR                  (       a  U$ UR                  (       a   UR                  [        5      $ [        S U  5       5      (       d  g  UR                  [        5      $ ! [         a    Us $ f = f! [         a    UR                  [        5      s $ f = f)N_repc              3   8   #    U  H  oR                   v   M     g 7fr   )is_Rational)rF   rX   s     r   rH   _to_DM_ZZ_QQ.<locals>.<genexpr>  s     ,!Q==!s   )
hasattrrl   domainis_ZZis_QQ
convert_tor   r   rJ   r   )rB   repKs      r   _to_DM_ZZ_QQrw      s     1f
&&C

Aww
	
	>>"%% ,!,,,	&>>"%%  	J	  	&>>"%%	&s$   B >B% B"!B"%CCc                     U R                   nUR                  (       a&  U R                  SS9u  p#nUR                  5       U-  nO&UR                  (       a  U R                  5       u  p$O eUR                  5       nXT4$ )z7Compute the reduced row echelon form of a DomainMatrix.F)keep_domain)rq   rr   rref_dento_fieldrs   rref	to_Matrix)dMrv   dM_rrefdenrQ   M_rrefs         r   _rref_dmr     sk    
		Aww!{{u{=f""$s*	
'')u F>r   c           	          [        U 5      nUb  [        U5      u  pgO.[        U[        5      (       a  UnO[        n[        XUUSSS9u  pgn	U(       a  Xg4$ U$ )aA  Return reduced row-echelon form of matrix and indices
of pivot vars.

Parameters
==========

iszerofunc : Function
    A function used for detecting whether an element can
    act as a pivot.  ``lambda x: x.is_zero`` is used by default.

simplify : Function
    A function used to simplify elements when looking for a pivot.
    By default SymPy's ``simplify`` is used.

pivots : True or False
    If ``True``, a tuple containing the row-reduced matrix and a tuple
    of pivot columns is returned.  If ``False`` just the row-reduced
    matrix is returned.

normalize_last : True or False
    If ``True``, no pivots are normalized to `1` until after all
    entries above and below each pivot are zeroed.  This means the row
    reduction algorithm is fraction free until the very last step.
    If ``False``, the naive row reduction procedure is used where
    each pivot is normalized to be `1` before row operations are
    used to zero above and below the pivot.

Examples
========

>>> from sympy import Matrix
>>> from sympy.abc import x
>>> m = Matrix([[1, 2], [x, 1 - 1/x]])
>>> m.rref()
(Matrix([
[1, 0],
[0, 1]]), (0, 1))
>>> rref_matrix, rref_pivots = m.rref()
>>> rref_matrix
Matrix([
[1, 0],
[0, 1]])
>>> rref_pivots
(0, 1)

``iszerofunc`` can correct rounding errors in matrices with float
values. In the following example, calling ``rref()`` leads to
floating point errors, incorrectly row reducing the matrix.
``iszerofunc= lambda x: abs(x) < 1e-9`` sets sufficiently small numbers
to zero, avoiding this error.

>>> m = Matrix([[0.9, -0.1, -0.2, 0], [-0.8, 0.9, -0.4, 0], [-0.1, -0.8, 0.6, 0]])
>>> m.rref()
(Matrix([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0]]), (0, 1, 2))
>>> m.rref(iszerofunc=lambda x:abs(x)<1e-9)
(Matrix([
[1, 0, -0.301369863013699, 0],
[0, 1, -0.712328767123288, 0],
[0, 0,         0,          0]]), (0, 1))

Notes
=====

The default value of ``normalize_last=True`` can provide significant
speedup to row reduction, especially on matrices with symbols.  However,
if you depend on the form row reduction algorithm leaves entries
of the matrix, set ``normalize_last=False``
T)r.   r/   )rw   r   rN   r   r   rC   )
rB   r+   rO   rQ   r-   r~   r   r2   r,   rR   s
             r   _rrefr   '  sf    T 
aB	~"2,Z h--H H($4A 
r   N)TTT)typesr   sympy.polys.polyerrorsr   sympy.polys.domainsr   r   	utilitiesr   r	   r
   r   determinantr   r=   rC   rK   rS   rj   rw   r   r   r   r   r   <module>r      st     1 & O O / AEn0d 9=+/: & >  !(%U 8  % CL&<"  %\r   