� ��g�0���ddlmZddlmZddlmZmZddlmZm Z m Z m Z ddl m Z  dd�Z dd �Ze fd �Ze d d fd �Ze d fd �Zd�Zd�Ze d ddfd�ZdS)�)� FunctionType)�CoercionFailed)�ZZ�QQ�)�_get_intermediate_simp�_iszero� _dotprodsimp� _simplify)�_find_reasonable_pivotTc �������fd�} ��fd�} ���fd�} tt���d\} } g}g}| �k�r>| |k�r7t| | ��| d�||��\}}}}|D]\}}|| z }|�|�z| z<�|�| dz } �U|�| ��|dkr)| | || z��|�| || zf��|durJ| | }}|�|�z|z<t |�z|zdz|dz�z��D]}��||z ���|<�|}t |��D]<}|| kr� |dur|| kr��|�z| z}||��r�.| |||| ���=| dz } | �kr| |k��7|d urk|d urgt |��D]W\}}�|�z|z}|�|�z|z<t |�z|zdz|dz�z��D]}��||z ���|<��X�t |��t |��fS) 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 ���i/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/matrices/reductions.py�get_colz!_row_reduce_list.<locals>.get_col/s����1�7�d�7�|��c����|�z|dz�z��|�z|dz�z�c�|�z|dz�z�<�|�z|dz�z�<dS)Nrr)r�jrrs ��r�row_swapz"_row_reduce_list.<locals>.row_swap2so��� ��$���A��t�|�#� $�c�!�D�&�!�a�%���*=�&>� ;��A�d�F�A��E�4�<� � �#�a��f�a�!�e�T�\�&9�":�":�":rc���||z �z}t|�z|dz�z��D](}�|�|z|�||zzz ���|<�)dS)z,Does the row op row[i] = a*row[i] - b*row[j]rN)�range) �ar�br�q�pr�isimprs ���r� cross_cancelz&_row_reduce_list.<locals>.cross_cancel6sm��� ��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�rrNrrFT)rr r �appendr� enumerate�tuple)r�rowsr�one� iszerofunc�simpfunc�normalize_last� normalize� zero_aboverrr!�piv_row�piv_col� pivot_cols�swaps� pivot_offset� pivot_val�assumed_nonzero�newly_determined�offset�valrrr�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��<�<� /�/rc ��tt|��|j|j|j|||||�� � \}}}|�|j|j|��||fS)N�r*r+r,)r:�listr&rr'�_new) �Mr(r)r*r+r,rr/r0s r� _row_reducer@|sb��.�d�1�g�g�q�v�q�v�q�u� ����J�8�8�8��C��U� �6�6�!�&�!�&�#� &� &� �E� 9�9rc���|jdks |jdkrdSt�fd�|dd�dfD����}�|d��r|ot|dd�dd�f���S|ot|dd�dd�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.rTc3�.�K�|]}�|��V��dSrr)�.0�tr(s �r� <genexpr>z_is_echelon.<locals>.<genexpr>�s+�����6�6��j�j��m�m�6�6�6�6�6�6rrNr")r&r�all� _is_echelon)r?r(� zeros_belows ` rrGrG�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�*�=�=�=rFc��t|t��r|nt}t|||ddd���\}}}|r||fS|S)anReturns 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<)� isinstancerr 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�{�� �Jrc����d�}t|t��r|nt}|jdks |jdkrdS|jdks |jdkr�fd�|D��}d|vrdS|jdkrW|jdkrL�fd�|D��}d|vrd|vrdS|���}�|��rd|vrdS�|��durdS||�� ��\}}t |�|d dd� ��\}} }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�������fd���fd�t�j��D��}d�t|��D��}��|d���|fS)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�fd��dd�|fD����S)Nc3�6�K�|]}�|���dndV��dS)Nrrr)rC�er(s �rrEzO_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)rr?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� Jrc�*��g|]}�|��|f��Srr)rCrrVs �r� <listcomp>z<_rank.<locals>._permute_complexity_right.<locals>.<listcomp>�s&���=�=�=�!�J�J�q�M�M�1�%�=�=�=rc��g|]\}}|��Srr)rCrrs rrXz<_rank.<locals>._permute_complexity_right.<locals>.<listcomp>�s��3�3�3��!�Q�1�3�3�3rr)� orientation)rr�sorted�permute)r?r(�complex�permrVs`` @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�:�:rrrc�&��g|] }�|����Srr�rC�xr(s �rrXz_rank.<locals>.<listcomp>��!���*�*�*�1���A���*�*�*rF�c�&��g|] }�|����Srrras �rrXz_rank.<locals>.<listcomp>�rcrN)r(Tr<)rJrr r&r�detr@�len) r?r(rKr_r)�zeros�drrNrMs ` 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�;�;�rc�|�t|d��sdS|j}|j}|jr|S|jr- |�t ��S#t$r|cYSwxYwtd�|D����sdS |�t ��S#t$r|�t��cYSwxYw)N�_repc3�$K�|] }|jV�� dSr)� is_Rational)rCrTs rrEz_to_DM_ZZ_QQ.<locals>.<genexpr>s$����,�,�Q�1�=�,�,�,�,�,�,r) �hasattrrl�domain�is_ZZ�is_QQ� convert_torrrFr)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}|jr2|�d���\}}}|���|z }n!|jr|���\}}nJ�|���}||fS)z7Compute the reduced row echelon form of a DomainMatrix.F)� keep_domain)rprq�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�>�rc���t|��}|�t|��\}}n8t|t��r|}nt}t ||||dd���\}}} |r||fS|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,)rvr�rJrr r@) r?r(rKrMr*r}rr/r)rNs r�_rrefr�'s���T �a���B� �~�"�2�,�,���Z�Z� �h� � -� -� !��H�H� �H�(��J���$�4�A�A�A���Z�����J���� rN)TTT)�typesr�sympy.polys.polyerrorsr�sympy.polys.domainsrr� utilitiesrr r r � determinantr r:r@rGrOrjrvr�r�rrr�<module>r�sW��������1�1�1�1�1�1�&�&�&�&�&�&�&�&�O�O�O�O�O�O�O�O�O�O�O�O�/�/�/�/�/�/�AE�n0�n0�n0�n0�d9=�+/�:�:�:�:�&� >� >� >� >� !(�%�U�����8 �%�C�C�C�C�L&�&�&�<���" �%���\�\�\�\�\�\r
Memory