Ë
    7^(hÔ0  ã                   ó¦   — d dl mZ d dlmZ d dlmZmZ ddlmZm	Z	m
Z
mZ ddlmZ 	 dd„Z	 	 dd	„Ze	fd
„Ze	ddfd„Ze	dfd„Zd„ Zd„ Ze	dddfd„Zy)é    )ÚFunctionType)ÚCoercionFailed)ÚZZÚQQé   )Ú_get_intermediate_simpÚ_iszeroÚ_dotprodsimpÚ	_simplify)Ú_find_reasonable_pivotTc	                 ó~  ‡ ‡‡— ˆˆ fd„}	ˆˆ fd„}
ˆˆˆ fd„}t        t        «      Šd\  }}g }g }|‰k  �r||k  �rt         |	|«      |d ||«      \  }}}}|D ]  \  }}||z  }|‰ |‰z  |z   <   Œ |€|dz  }ŒI|j                  |«       |dk7  r" |
|||z   «       |j                  |||z   f«       |du rB||}}|‰ |‰z  |z   <   t	        |‰z  |z   dz   |dz   ‰z  «      D ]  } ‰‰ |   |z  «      ‰ |<   Œ |}t	        |«      D ]1  }||k(  rŒ	|du r||k  rŒ‰ |‰z  |z      } ||«      rŒ' |||||«       Œ3 |dz  }|‰k  r||k  r�Œ|d	u r^|d	u rZt        |«      D ]L  \  }}‰ |‰z  |z      }|‰ |‰z  |z   <   t	        |‰z  |z   dz   |dz   ‰z  «      D ]  } ‰‰ |   |z  «      ‰ |<   Œ ŒN ‰ t        |«      t        |«      fS )
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                 ó   •— ‰| d ‰…   S ©N© )ÚiÚcolsÚmats    €€úW/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/matrices/reductions.pyÚget_colz!_row_reduce_list.<locals>.get_col/   s   ø€ Ø�1�7�d�7‰|Ðó    c                 óp   •— ‰|‰z  |dz   ‰z   ‰| ‰z  | dz   ‰z   c‰| ‰z  | dz   ‰z   ‰|‰z  |dz   ‰z   y )Nr   r   )r   Újr   r   s     €€r   Úrow_swapz"_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                 ó„   •— ||z
  ‰z  }t        |‰z  |dz   ‰z  «      D ]  } ‰| ‰|   z  |‰||z      z  z
  «      ‰|<   Œ! y)z,Does the row op row[i] = a*row[i] - b*row[j]r   N)Úrange)	Úar   Úbr   ÚqÚpr   Úisimpr   s	         €€€r   Úcross_cancelz&_row_reduce_list.<locals>.cross_cancel6   sY   ø€ à�‰U�D‰LˆÜ�q˜‘v  A¡ t™|Ó,ò 	4ˆAÙ˜1˜S ™V™8 a¨¨A°©E©
¡lÑ2Ó3ˆC�ŠFñ	4r   ©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ˆWØ€JØ€Eð �D‹.˜W t›^ä,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Ñ"Ø˜GˆqˆAØ!ˆC��$‘˜‘
‰OÜ˜1˜T™6 A™:¨™>¨A°©E°4©<Ó8ò 3�Ù˜s 1™v¨	Ñ1Ó2��A’ð3ð ˆIô ˜“;ò 	7ˆCà�gŠ~Øà˜UÑ" s¨W¢}Øà�c˜$‘h Ñ(Ñ)ˆCÙ˜#ŒØá˜ C¨¨gÕ6ð	7ð 	�1‰ˆðY �DŠ.˜W tœ^ð^ ˜Ñ )¨tÑ"3Ü% jÓ1ò 	3‰LˆE�5Ø˜E $™J¨Ñ.Ñ/ˆIØ&)ˆC��d‘
˜UÑ"Ñ#Ü˜5 ™:¨Ñ-°Ñ1°E¸A±I¸tÑ3CÓDò 3�Ù˜s 1™v¨	Ñ1Ó2��A’ñ3ð	3ð ”�jÓ!¤5¨£<Ð/Ð/r   c                 óÔ   — t        t        | «      | j                  | j                  | j                  |||||¬«	      \  }}}| j                  | j                  | j                  |«      ||fS )N©r*   r+   r,   )r:   Úlistr&   r   r'   Ú_new)	ÚMr(   r)   r*   r+   r,   r   r/   r0   s	            r   Ú_row_reducer@   |   s]   € ô .¬d°1«g°q·v±v¸q¿v¹vÀqÇuÁuØ˜°Ø¨Jô8Ñ€Cˆ�Uð �6‰6�!—&‘&˜!Ÿ&™& #Ó&¨
°EÐ9Ð9r   c                 óò   ‡— | j                   dk  s| j                  dk  ryt        ˆfd„| dd…df   D «       «      } ‰| d   «      r|xr t        | dd…dd…f   ‰«      S |xr t        | dd…dd…f   ‰«      S )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   ó.   •K  — | ]  } ‰|«      –— Œ y ­wr   r   )Ú.0Útr(   s     €r   ú	<genexpr>z_is_echelon.<locals>.<genexpr>Ž   s   øè ø€ Ò6¨‘j —mÑ6ùs   ƒr   Nr"   )r&   r   ÚallÚ_is_echelon)r?   r(   Úzeros_belows    ` r   rG   rG   †   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                 ól   — t        |t        «      r|nt        }t        | ||ddd¬«      \  }}}|r||fS |S )an  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   r@   )r?   r(   ÚsimplifyÚwith_pivotsr)   r   ÚpivotsÚ_s           r   Ú_echelon_formrO   –   sF   € ô  & h´Ô=‰xÄ9€Hä   J°Ø¨5¸UôD�N€Cˆ�ñ Ø�Fˆ{Ðà€Jr   c                 ó  — d„ }t        |t        «      r|nt        }| j                  dk  s| j                  dk  ry| j                  dk  s| j                  dk  r| D �cg c]
  } ||«      ‘Œ }}d|v ry| j                  dk(  rU| j                  dk(  rF| D �cg c]
  } ||«      ‘Œ }}d|vrd|vry| j                  «       } ||«      rd|v ry ||«      du ry || |¬«      \  }}	t        |||ddd¬	«      \  }	}
}	t        |
«      S c c}w c c}w )
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                 óà   ‡ ‡— ˆ ˆfd„}t        ‰ j                  «      D �cg c]  } ||«      |f‘Œ }}t        |«      D ��cg c]  \  }}|‘Œ	 }}}‰ j                  |d¬«      |fS c c}w c c}}w )a€  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                 ó:   •— t        ˆfd„‰d d …| f   D «       «      S )Nc              3   ó6   •K  — | ]  } ‰|«      €dnd–— Œ y ­w)Nr   r   r   )rC   Úer(   s     €r   rE   zO_rank.<locals>._permute_complexity_right.<locals>.complexity.<locals>.<genexpr>Ï   s   øè ø€ ÒJ¸Q™J q›MÐ1‘q°qÓ8ÑJùs   ƒ)Úsum)r   r?   r(   s    €€r   Ú
complexityz<_rank.<locals>._permute_complexity_right.<locals>.complexityÌ   s   ø€ ô ÓJÀ!ÂAÀqÀDÁ'ÔJÓJÐJr   r   )Úorientation)r   r   ÚsortedÚpermute)r?   r(   rV   r   Úcomplexr   Úperms   ``     r   Ú_permute_complexity_rightz(_rank.<locals>._permute_complexity_rightÂ   sj   ù€ õ	Kô
 05°Q·V±V«}Ö=¨!‘J˜q“M 1Ò%Ð=ˆÐ=Ü#)¨'£?×3™˜!˜Q’1Ð3ˆÑ3à—	‘	˜$¨F�	Ó3°TÐ:Ð:ùò >ùÛ3s    A%ÁA*r   r   Fé   N)r(   Tr<   )rJ   r   r   r&   r   Údetr@   Úlen)r?   r(   rK   r\   r)   ÚxÚzerosÚdr   rN   rM   s              r   Ú_rankrc   ²   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 J°ÈØ¨ô/�L€A€vˆqô ˆv‹;Ðùò- +ùò +s   ÁDÂD	c                 óh  — t        | d«      sy | j                  }|j                  }|j                  r|S |j                  r	 |j                  t        «      S t        d„ | D «       «      sy 	 |j                  t        «      S # t        $ r |cY S w xY w# t        $ r |j                  t        «      cY S w xY w)NÚ_repc              3   ó4   K  — | ]  }|j                   –— Œ y ­wr   )Úis_Rational)rC   rT   s     r   rE   z_to_DM_ZZ_QQ.<locals>.<genexpr>  s   è ø€ Ò, Q�1—=•=Ñ,ùs   ‚)
Úhasattrre   ÚdomainÚis_ZZÚis_QQÚ
convert_tor   r   rF   r   )r?   ÚrepÚKs      r   Ú_to_DM_ZZ_QQro   ø   s¦   € ô �1�fÔØà
�&‰&€CØ�
‰
€Aà‡w‚wØˆ
Ø	
�Šð	Ø—>‘>¤"Ó%Ð%ô Ñ,¨!Ô,Ô,Øð	&Ø—>‘>¤"Ó%Ð%øô ò 	ØŠJð	ûô ò 	&Ø—>‘>¤"Ó%Ò%ð	&ús$   ÁA? Á*B Á?BÂBÂB1Â0B1c                 óò   — | j                   }|j                  r*| j                  d¬«      \  }}}|j                  «       |z  }n"|j                  r| j                  «       \  }}nJ ‚|j                  «       }||fS )z7Compute the reduced row echelon form of a DomainMatrix.F)Úkeep_domain)ri   rj   Úrref_denÚto_fieldrk   ÚrrefÚ	to_Matrix)ÚdMrn   ÚdM_rrefÚdenrM   ÚM_rrefs         r   Ú_rref_dmrz     so   € à
�	‰	€Aà‡w‚wØ!Ÿ{™{°u˜{Ó=Ñˆ��fØ×"Ñ"Ó$ sÑ*‰Ø	
�ŠØŸ'™'›)‰ˆ‘àˆuà×ÑÓ €Fà�6ˆ>Ðr   c                 ó¦   — t        | «      }|�t        |«      \  }}n.t        |t        «      r|}nt        }t        | |||dd¬«      \  }}}	|r||fS |S )a-	  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,   )ro   rz   rJ   r   r   r@   )
r?   r(   rK   rM   r*   rv   r   r/   r)   rN   s
             r   Ú_rrefr|   '  si   € ôT 
�a‹€Bà	€~ä" 2›,‰ˆ‰Zô �h¤Ô-Ø‰Hä ˆHä(¨¨J¸Ø¨$¸4ôAÑˆˆZ˜ñ Ø�JˆÐàˆ
r   N)TTT)Útypesr   Úsympy.polys.polyerrorsr   Úsympy.polys.domainsr   r   Ú	utilitiesr   r	   r
   r   Údeterminantr   r:   r@   rG   rO   rc   ro   rz   r|   r   r   r   ú<module>r‚      st   ðÝ å 1ß &ç OÓ OÝ /ð AEón0ðd 9=Ø+/ó:ð &ó >ð  !(°%ÀUó ð8  ¨%ó CòL&ò<ð"  ¨%¸Øô\r   