§
    OŠtjÔ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dS )é    )Ú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     �r7t           |	|¦  «        |d…         ||¦  «        \  }}}}|D ]\  }}||z  }|‰ |‰z  |z   <   Œ|€|dz  }ŒU|                     |¦  «         |dk    r) |
|||z   ¦  «         |                     |||z   f¦  «         |du rJ||}}|‰ |‰z  |z   <   t	          |‰z  |z   dz   |dz   ‰z  ¦  «        D ]} ‰‰ |         |z  ¦  «        ‰ |<   Œ|}t	          |¦  «        D ]<}||k    rŒ	|du r||k     rŒ‰ |‰z  |z            } ||¦  «        rŒ. |||||¦  «         Œ=|dz  }|‰k     r||k     �°7|d	u rk|d	u rgt          |¦  «        D ]W\  }}‰ |‰z  |z            }|‰ |‰z  |z   <   t	          |‰z  |z   dz   |dz   ‰z  ¦  «        D ]} ‰‰ |         |z  ¦  «        ‰ |<   ŒŒX‰ 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/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/matrices/reductions.pyÚget_colz!_row_reduce_list.<locals>.get_col/   s   ø€ Ø�1�7�d�7Œ|Ðó    c                 óŽ   •— ‰|‰z  |dz   ‰z  …         ‰| ‰z  | dz   ‰z  …         c‰| ‰z  | dz   ‰z  …<   ‰|‰z  |dz   ‰z  …<   d S )Nr   r   )r   Újr   r   s     €€r   Úrow_swapz"_row_reduce_list.<locals>.row_swap2   so   ø€ à��$‘˜˜A™˜t‘|Ð#Ô$ c¨!¨D©&°!°a±%¸±Ð*=Ô&>ð 	;ˆˆAˆd‰F�A˜‘E˜4‘<ÐÑ  # a¨¡f¨a°!©e°T©\Ð&9Ñ":Ð":Ð":r   c                 óœ   •— ||z
  ‰z  }t          |‰z  |dz   ‰z  ¦  «        D ](} ‰| ‰|         z  |‰||z            z  z
  ¦  «        ‰|<   Œ)dS )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   sm   ø€ à�‰U�D‰LˆÝ�q˜‘v  A¡ t™|Ñ,Ô,ð 	4ð 	4ˆAØ�U˜1˜S œV™8 a¨¨A°©E¬
¡lÑ2Ñ3Ô3ˆC�‰FˆFð	4ð 	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:   
   sA  øøø€ ðJð ð ð ð ð ð?ð ?ð ?ð ?ð ?ð ?ð4ð 4ð 4ð 4ð 4ð 4ð 4õ #¥<Ñ0Ô0€EØÑ€GˆWØ€JØ€Eð �DŠ.‰.˜W tš^™^å,BØ�˜Ñ Ô    Ô*¨J¸ñ-Bô -Bñ	*ˆ�iØÐ)ð
 .ð 	-ð 	-‰MˆV�SØ�gÑˆFØ),ˆC��t‘˜gÑ%Ñ&Ð&àÐØ�q‰LˆGØà×Ò˜'Ñ"Ô"Ð"Ø˜1ÒÐØˆH�W˜l¨WÑ4Ñ5Ô5Ð5Ø�LŠL˜' <°'Ñ#9Ð:Ñ;Ô;Ð;ð ˜UÐ"Ð"Ø˜GˆqˆAØ!ˆC��$‘˜‘
‰OÝ˜1˜T™6 A™:¨™>¨A°©E°4©<Ñ8Ô8ð 3ð 3�Ø˜˜s 1œv¨	Ñ1Ñ2Ô2��A‘�àˆIõ ˜‘;”;ð 	7ð 	7ˆCà�gŠ~ˆ~Øà˜UÐ"Ð" s¨W¢} }Øà�c˜$‘h Ñ(Ô)ˆCØˆz˜#‰Œð ØàˆL˜ C¨¨gÑ6Ô6Ð6Ð6Ø�1‰ˆðY �DŠ.ˆ.˜W tš^™^ð^ ˜ÐÐ )¨tÐ"3Ð"3Ý% jÑ1Ô1ð 	3ð 	3‰LˆE�5Ø˜E $™J¨Ñ.Ô/ˆIØ&)ˆC��d‘
˜UÑ"Ñ#Ý˜5 ™:¨Ñ-°Ñ1°E¸A±I¸tÑ3CÑDÔDð 3ð 3�Ø˜˜s 1œv¨	Ñ1Ñ2Ô2��A‘�ð3ð •�jÑ!Ô!¥5¨¡<¤<Ð/Ð/r   c                 óº   — t          t          | ¦  «        | 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@   |   sb   € õ .­d°1©g¬g°q´v¸q¼vÀqÄuØ˜°Ø¨Jð8ñ 8ô 8Ñ€Cˆ�Uð �6Š6�!”&˜!œ& #Ñ&Ô&¨
°EÐ9Ð9r   c                 ó  ‡— | j         dk    s| j        dk    rdS t          ˆfd„| dd…df         D ¦   «         ¦  «        } ‰| d         ¦  «        r|ot          | dd…dd…f         ‰¦  «        S |o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  — | ]} ‰|¦  «        V — Œd S r   r   )Ú.0Útr(   s     €r   ú	<genexpr>z_is_echelon.<locals>.<genexpr>Ž   s+   øè è € Ð6Ð6¨�j�j ‘m”mÐ6Ð6Ð6Ð6Ð6Ð6r   r   Nr"   )r&   r   ÚallÚ_is_echelon)r?   r(   Úzeros_belows    ` r   rG   rG   †   s®   ø€ ð
 	„v�‚{€{�a”f ’k�kØˆtåÐ6Ð6Ð6Ð6¨Q¨q¨r¨r°1¨u¬XÐ6Ñ6Ô6Ñ6Ô6€Kà€z�!�D”'ÑÔð AØÐ@�{¨1¨Q¨Q¨Q°°°¨U¬8°ZÑ@Ô@Ð@àÐ=�; q¨¨¨¨Q¨R¨R¨¤y°*Ñ=Ô=Ð=r   Fc                 ó€   — 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   –   s_   € õ  & hµÑ=Ô=ÐLˆxˆxÅ9€Hå   J°Ø¨5¸UðDñ Dô D�N€Cˆ�ð ð Ø�Fˆ{Ðà€Jr   c                 óþ  ‡— d„ }t          |t          ¦  «        r|nt          }| j        dk    s| j        dk    rdS | j        dk    s| j        dk    rˆfd„| D ¦   «         }d|v rdS | j        dk    rW| j        dk    rLˆfd„| D ¦   «         }d|vrd|vrdS |                      ¦   «         } ‰|¦  «        rd|v rdS  ‰|¦  «        du rdS  || ‰¬	¦  «        \  }}t          |‰|d
dd¬¦  «        \  }}	}t          |	¦  «        S )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„Šˆfd„t          ‰ j        ¦  «        D ¦   «         }d„ t          |¦  «        D ¦   «         }‰                      |d¬¦  «        |fS )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                 óN   •— t          ˆfd„‰d d …| f         D ¦   «         ¦  «        S )Nc              3   ó6   •K  — | ]} ‰|¦  «        €dndV — Œd S )Nr   r   r   )rC   Úer(   s     €r   rE   zO_rank.<locals>._permute_complexity_right.<locals>.complexity.<locals>.<genexpr>Ï   s4   øè è € ÐJÐJ¸Q˜J˜J q™MœMÐ1�q�q°qÐJÐJÐJÐJÐJÐJr   )Úsum)r   r?   r(   s    €€r   Ú
complexityz<_rank.<locals>._permute_complexity_right.<locals>.complexityÌ   s4   ø€ õ ÐJÐJÐJÐJÀ!ÀAÀAÀAÀqÀDÄ'ÐJÑJÔJÑJÔJÐJr   c                 ó*   •— g | ]} ‰|¦  «        |f‘ŒS r   r   )rC   r   rV   s     €r   ú
<listcomp>z<_rank.<locals>._permute_complexity_right.<locals>.<listcomp>Ñ   s&   ø€ Ð=Ð=Ð=¨!�J�J˜q‘M”M 1Ð%Ð=Ð=Ð=r   c                 ó   — g | ]\  }}|‘ŒS r   r   )rC   r   r   s      r   rX   z<_rank.<locals>._permute_complexity_right.<locals>.<listcomp>Ò   s   € Ð3Ð3Ð3™˜!˜Q�1Ð3Ð3Ð3r   r   )Úorientation)r   r   ÚsortedÚpermute)r?   r(   ÚcomplexÚpermrV   s   ``  @r   Ú_permute_complexity_rightz(_rank.<locals>._permute_complexity_rightÂ   s€   øøø€ ð	Kð 	Kð 	Kð 	Kð 	Kð 	Kð
 >Ð=Ð=Ð=­u°Q´V©}¬}Ð=Ñ=Ô=ˆØ3Ð3¥6¨'¡?¤?Ð3Ñ3Ô3ˆà—	’	˜$¨F�	Ñ3Ô3°TÐ:Ð:r   r   r   c                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r   r   ©rC   Úxr(   s     €r   rX   z_rank.<locals>.<listcomp>ß   ó!   ø€ Ð*Ð*Ð* 1��˜A‘”Ð*Ð*Ð*r   Fé   c                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r   r   ra   s     €r   rX   z_rank.<locals>.<listcomp>å   rc   r   N)r(   Tr<   )rJ   r   r   r&   r   Údetr@   Úlen)
r?   r(   rK   r_   r)   ÚzerosÚdr   rN   rM   s
    `        r   Ú_rankrj   ²   s\  ø€ ð ;ð ;ð ;õ( & hµÑ=Ô=ÐLˆxˆxÅ9€Hð
 	„v�‚{€{�a”f ’k�kØˆqà„v�‚{€{�a”f ’k�kØ*Ð*Ð*Ð*¨Ð*Ñ*Ô*ˆà�Eˆ>ˆ>Ø�1à„v�‚{€{�q”v ’{�{Ø*Ð*Ð*Ð*¨Ð*Ñ*Ô*ˆà˜ÐÐ $¨eÐ"3Ð"3Ø�1à�EŠE‰GŒGˆàˆ:�a‰=Œ=ð 	˜U e˜^˜^Ø�1Øˆ:�a‰=Œ=˜EÐ!Ð!Ø�1à,Ð,¨Q¸:ÐFÑFÔF�F€CˆÝ˜s J°ÈØ¨ð/ñ /ô /�L€A€vˆqõ ˆv‰;Œ;Ðr   c                 ó|  — t          | d¦  «        sd S | j        }|j        }|j        r|S |j        r-	 |                     t          ¦  «        S # t          $ r |cY S w xY wt          d„ | D ¦   «         ¦  «        sd S 	 |                     t          ¦  «        S # t          $ r |                     t          ¦  «        cY S w xY w)NÚ_repc              3   ó$   K  — | ]}|j         V — Œd S r   )Úis_Rational)rC   rT   s     r   rE   z_to_DM_ZZ_QQ.<locals>.<genexpr>  s$   è è € Ð,Ð, Q�1”=Ð,Ð,Ð,Ð,Ð,Ð,r   )
Úhasattrrl   ÚdomainÚis_ZZÚis_QQÚ
convert_tor   r   rF   r   )r?   ÚrepÚKs      r   Ú_to_DM_ZZ_QQrv   ø   sí   € õ �1�fÑÔð Øˆtà
Œ&€CØŒ
€Aà„wð &Øˆ
Ø	
Œð &ð	Ø—>’>¥"Ñ%Ô%Ð%øÝð 	ð 	ð 	ØˆJˆJˆJð	øøøõ Ð,Ð,¨!Ð,Ñ,Ô,Ñ,Ô,ð 	Ø�4ð	&Ø—>’>¥"Ñ%Ô%Ð%øÝð 	&ð 	&ð 	&Ø—>’>¥"Ñ%Ô%Ð%Ð%Ð%ð	&øøøs#   ²A ÁAÁAÁ:B Â$B;Â:B;c                 óô   — | j         }|j        r2|                      d¬¦  «        \  }}}|                     ¦   «         |z  }n!|j        r|                      ¦   «         \  }}nJ ‚|                     ¦   «         }||fS )z7Compute the reduced row echelon form of a DomainMatrix.F)Úkeep_domain)rp   rq   Úrref_denÚto_fieldrr   ÚrrefÚ	to_Matrix)ÚdMru   ÚdM_rrefÚdenrM   ÚM_rrefs         r   Ú_rref_dmr�     s‚   € à
Œ	€Aà„wð Ø!Ÿ{š{°u˜{Ñ=Ô=Ñˆ��fØ×"Ò"Ñ$Ô$ sÑ*ˆˆØ	
Œð ØŸ'š'™)œ)‰ˆ��àˆuà×ÒÑ Ô €Fà�6ˆ>Ðr   c                 óÊ   — t          | ¦  «        }|�t          |¦  «        \  }}n8t          |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``
    NT)r+   r,   )rv   r�   rJ   r   r   r@   )
r?   r(   rK   rM   r*   r}   r   r/   r)   rN   s
             r   Ú_rrefrƒ   '  sŠ   € õT 
�a‰Œ€Bà	€~å" 2™,œ,‰ˆˆZˆZõ �h¥Ñ-Ô-ð 	!ØˆHˆHå ˆHå(¨¨J¸Ø¨$¸4ðAñ Aô AÑˆˆZ˜ð ð Ø�JˆÐàˆ
r   N)TTT)Útypesr   Úsympy.polys.polyerrorsr   Úsympy.polys.domainsr   r   Ú	utilitiesr   r	   r
   r   Údeterminantr   r:   r@   rG   rO   rj   rv   r�   rƒ   r   r   r   ú<module>r‰      sW  ðØ Ð Ð Ð Ð Ð à 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø &Ð &Ð &Ð &Ð &Ð &Ð &Ð &à OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OØ /Ð /Ð /Ð /Ð /Ð /ð AEðn0ð n0ð n0ð n0ðd 9=Ø+/ð:ð :ð :ð :ð &ð >ð >ð >ð >ð  !(°%ÀUð ð ð ð ð8  ¨%ð Cð Cð Cð CðL&ð &ð &ð<ð ð ð"  ¨%¸Øð\ð \ð \ð \ð \ð \r   