ó
    ‰*£hÔ0  ã                   ó²   • 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  XÍn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    €€ÚV/home/mande/repo/quber/.venv/lib/python3.13/site-packages/sympy/matrices/reductions.pyÚget_colÚ!_row_reduce_list.<locals>.get_col/   s   ø€ Ø�1�7�d�7‰|Ðó    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±%¸±Ð&>ð 	;ˆˆAˆd‰F�A˜‘E˜4‘<Ð  # a¨¡f¨a°!©e°T©\Ñ":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   ø€ à‰U�D‰LˆÜ�q˜‘v  A¡ t™|Ö,ˆAÙ˜1˜S ™V™8 a¨¨A©E©
¡lÑ2Ó3ˆC�‹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ô #¤<Ó0€EØÑ€GØ€JØ€Eð �DŒ.˜Wœ^ä,BÙ˜Ó   Ð*¨Jó-Bñ	*ˆ�iØÐ)ó
 .‰MˆV�SØ�gÑˆFØ),ˆC��t‘˜gÑ%Ó&ñ .ð ÑØ�q‰LˆGÙà×Ñ˜'Ô"Ø˜1ÓÙ�W˜l¨WÑ4Ô5Ø�L‰L˜' <°'Ñ#9Ð:Ô;ð ˜UÒ"ØˆqˆAØ!ˆC��$‘˜‘
‰OÜ˜1˜T™6 A™:¨™>¨A°©E°4©<Ö8�Ù˜s 1™v¨	Ñ1Ó2��A“ñ 9ð ˆIô ˜–;ˆCà�g‹~Ùà˜UÒ" s¨W£}Ùà�c˜$‘h Ñ(Ñ)ˆCÙ˜#�‰Ùá˜ C¨¨gÖ6ñ ð 	�1‰ˆðY �D‹.˜Wž^ð^ ˜Ò )¨tÒ"3Ü% jÖ1‰LˆE�5Ø˜E $™J¨Ñ.Ñ/ˆIØ&)ˆC��d‘
˜UÑ"Ñ#Ü˜5 ™:¨Ñ-°Ñ1°E¸A±I¸tÑ3CÖD�Ù˜s 1™v¨	Ñ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   € ô .¬d°1«g°q·v±v¸q¿v¹vÀqÇuÁuØ°Øñ8Ñ€C�Uð �6‰6�!—&‘&˜!Ÿ&™& #Ó&¨
Ð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      $ )zžReturns `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   øé € Ð6ªX¨‘j —m�mªXùs   ƒr   Nr%   )r)   r   ÚallÚ_is_echelon)rB   r+   Úzeros_belows    ` r   rK   rK   †   s€   ø€ ð
 	‡v�v�ƒ{�a—f‘f “kØäÔ6¨Q¨q©r°1¨uªXÓ6Ó6€Ká�!�D‘'×ÑØ×@œ{¨1ªQ°±¨U©8°ZÓ@Ð@à×=œ; q¨©¨Q©R¨¡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´×=Ñ=‰xÄ9€Hä  °Ø¨5¸UñD�N€C�ö Øˆ{Ðà€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‰[        X�USSSS	9u  pšn	[        U
5      $ s  snf s  snf )
zÓReturns 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Â'¸Q™J q›MÑ1‘q°qÔ8Â'ùs   ƒ)Úsum)r   rB   r+   s    €€r   Ú
complexityÚ<_rank.<locals>._permute_complexity_right.<locals>.complexityÌ   s   ø€ ô ÔJÀ!ÂAÀqÀDÂ'Ó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ô
 05°Q·V±V¬}Ó=ª}¨!�J˜q“M 1Ó%©}ˆÐ=Ü#)¨'¤?Ô3¢?™˜!“1¡?ˆÑ3à—	‘	˜$¨F�	Ð3°TÐ:Ð:ùò >ùÛ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´×=Ñ=‰xÄ9€Hð
 	‡v�v�ƒ{�a—f‘f “kØà‡v�v�ƒ{�a—f‘f “kÙ()Ó*ª 1�˜A–©ˆÐ*à�E‹>Øà‡v�v�ƒ{�q—v‘v “{Ù()Ó*ª 1�˜A–©ˆÐ*à˜Ó $¨eÓ"3Øà�E‰E‹Gˆá�a�=‰=˜U e›^ØÙ�a‹=˜EÒ!Øá,¨QÑF�F€CÜ˜s°ÈØ¨ñ/�L€Aˆqô ˆ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¬   € ô �1�f×ÑØà
�&‰&€CØ�
‰
€Aà‡w‡wØˆ
Ø	
��ð	Ø—>‘>¤"Ó%Ð%ô Ñ,©!Ó,×,Ñ,Øð	&Ø—>‘>¤"Ó%Ð%øô ó 	ØŠ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à‡w‡wØ!Ÿ{™{°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0ðd 9=Ø+/ô:ð &ô >ð  !(°%ÀUô ð8  ¨%ô CòL&ò<ð"  ¨%¸Øõ\r   