+
    i0                         ^ RI Ht ^ RIHt ^ RIHtHt ^RIHtH	t	H
t
Ht ^RIHt RR ltRR lt]	3R	 lt]	R
R
3R lt]	R
3R ltR tR t]	R
RR3R ltR# )    )FunctionType)CoercionFailed)ZZQQ)_get_intermediate_simp_iszero_dotprodsimp	_simplify)_find_reasonable_pivotTc	                  a aa VV 3R lp	VV 3R lp
VVV 3R lp\        \        4      o^ ^ r. p. pVS8  Edp   W8  Edi   \        V	! V4      VR WE4      w  ppppV F"  w  ppVV,          pVS VS,          V,           &   K$  	  Vf   V^,          pK`  VP                  V4       V^ 8w  d+   V
! VVV,           4       VP                  VVV,           34       VRJ dh   YppVS VS,          V,           &   \	        VS,          V,           ^,           V^,           S,          4       F  pS! S V,          V,          4      S V&   K  	  Tp\	        V4       FM  pVV8X  d   K  VRJ d
   VV8  d   K  S VS,          V,           ,          pV! V4      '       d   KB  V! VVVV4       KO  	  V^,          pEKw  VRJ d   VRJ d   \        V4       F  w  ppS VS,          V,           ,          pVS VS,          V,           &   \	        VS,          V,           ^,           V^,           S,          4       F  pS! S V,          V,          4      S V&   K  	  K  	  S \        V4      \        V4      3# )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                    < SV R S1,          # N )icolsmats   &y/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/matrices/reductions.pyget_col!_row_reduce_list.<locals>.get_col/   s    17d7|    c                    < SVS,          V^,           S,           SV S,          V ^,           S,           uSV S,          V ^,           S,          % SVS,          V^,           S,          % R# )   Nr   )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                   < W1,
          S,          p\        VS,          V^,           S,          4       F7  pS! V SV,          ,          VSWT,           ,          ,          ,
          4      SV&   K9  	  R# )z,Does the row op row[i] = a*row[i] - b*row[j]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   NFT)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   f&f&&&&&&                   @r   _row_reduce_listr<   
   sL   J?4 #<0E!W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 )t"3%j1LE5E$J./I&)Cd
U"#5:-1EAIt3CDs1v	12A E 2 j!5<//r   c                     \        \        V 4      V P                  V P                  V P                  WVWER 7	      w  rgpV P                  V P                  V P                  V4      Wx3# )r,   r-   r.   )r<   listr(   r   r)   _new)	Mr*   r+   r,   r-   r.   r   r1   r2   s	   &&&&&&   r   _row_reducerB   |   sU     .d1gqvvqvvquu8CU 66!&&!&&#&
99r   c                z  a V P                   ^ 8:  g   V P                  ^ 8:  d   R# \        ;QJ d&    V3R lV R,           4       F  '       d   K   RM	  RM! V3R lV R,           4       4      pS! V R,          4      '       d   T;'       d    \        V R,          S4      # T;'       d    \        V R,          S4      # )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.Tc              3   4   <"   T F  pS! V4      x  K  	  R # 5ir   r   ).0tr*   s   & r   	<genexpr>_is_echelon.<locals>.<genexpr>   s     6XjmmXs   F)r   NNr   )r   r   )NNNrI   )rI   rI   )r(   r   all_is_echelon)rA   r*   zeros_belows   &f r   rL   rL      s    
 	vv{affk#6QuX6###6QuX66K!D'@@{1U8Z@@==;qy*==r   Fc           	     ~    \        V\        4      '       d   TM\        p\        WVRRRR7      w  rVpV'       d   WV3# V# )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
   rB   )rA   r*   simplifywith_pivotsr+   r   pivots_s   &&&&    r   _echelon_formrT      sC      &h==x9H 5UDNC {Jr   c           	     n   R p\        V\        4      '       d   TM\        pV P                  ^ 8:  g   V P                  ^ 8:  d   ^ # V P                  ^8:  g   V P                  ^8:  d    V  Uu. uF
  qQ! V4      NK  	  ppRV9   d   ^# V P                  ^8X  dm   V P                  ^8X  d\   V  Uu. uF
  qQ! V4      NK  	  ppRV9  d
   RV9  d   ^ # V P                  4       pV! V4      '       d
   RV9   d   ^# V! V4      RJ d   ^# V! WR7      w  r\        WVRRRR7      w  rp	\        V
4      # u upi u upi )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                   a a V V3R lp\        S P                  4       Uu. uF  q2! V4      V3NK  	  pp\        V4       UUu. uF  w  r5VNK	  	  pppS P                  VRR7      V3# u upi u uppi )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                 @   < \        V3R  lSRV 3,           4       4      # )c              3   @   <"   T F  pS! V4      f   ^M^ x  K  	  R # 5ir   r   )rE   er*   s   & r   rG   O_rank.<locals>._permute_complexity_right.<locals>.complexity.<locals>.<genexpr>   s     J'QJqM1qq8's   rJ   )sum)r   rA   r*   s   &r   
complexity<_rank.<locals>._permute_complexity_right.<locals>.complexity   s     J!AqD'JJJr   r   )orientation)r   r   sortedpermute)rA   r*   r\   r   complexr   perms   ff     r   _permute_complexity_right(_rank.<locals>._permute_complexity_right   sj    	K
 05QVV}=}!JqM1%}=#)'?3?!1?3		$F	3T:: >3s   A(A-FN)r*   Tr>   )rO   r   r
   r(   r   detrB   len)rA   r*   rP   rc   r+   xzerosdr   rS   rR   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--D2c                    \        V R 4      '       g   R# V P                  pVP                  pVP                  '       d   V# VP                  '       d    VP                  \        4      # \        ;QJ d    R V  4       F  '       d   K   RM	  RM! R V  4       4      '       g   R#  VP                  \        4      #   \         d    Tu # i ; i  \         d    TP                  \        4      u # i ; i)_repNc              3   8   "   T F  qP                   x  K  	  R # 5ir   )is_Rational)rE   rY   s   & r   rG   _to_DM_ZZ_QQ.<locals>.<genexpr>  s     ,!Q==!s   FT)
hasattrrl   domainis_ZZis_QQ
convert_tor   r   rK   r   )rA   repKs   &  r   _to_DM_ZZ_QQrw      s     1f
&&C

Awww
	
	>>"%% s,!,sss,!,,,	&>>"%%  	J	  	&>>"%%	&s$   B; &C ;C
C C10C1c                   V P                   pVP                  '       d.   V P                  RR7      w  r#pVP                  4       V,          pM'VP                  '       d   V P                  4       w  r$MQ hVP                  4       pWT3# )z7Compute the reduced row echelon form of a DomainMatrix.F)keep_domain)rq   rr   rref_dento_fieldrs   rref	to_Matrix)dMrv   dM_rrefdenrR   M_rrefs   &     r   _rref_dmr     so    
		Awww!{{u{=f""$s*	
'')u F>r   c           	         \        V 4      pVe   \        V4      w  rgM2\        V\        4      '       d   TpM\        p\        WVVRRR7      w  rgp	V'       d   Wg3# V# )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   rO   r   r
   rB   )
rA   r*   rP   rR   r,   r~   r   r1   r+   rS   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<   rB   rL   rT   rj   rw   r   r   r   r   r   <module>r      se     1 & O O /n0d: & >  !(%U 8  % CL&<"  %\r   