� ��g������ddlZddlmZddlmZddlmZmZddlm Z ddl m Z m Z ddl mZmZdd lmZed fd �Zd �Zd �Zdd�Zdd�Zdd�Zdd�Zedd fd�Zedd fd�Zd�Zd�Zdd�Zd�Zd�ZdS)�N)�S)� expand_mul)�Min�sqrt)�sign�)�NonSquareMatrixError�NonPositiveDefiniteMatrixError)�_get_intermediate_simp�_iszero)�_find_reasonable_pivot_naiveFc���|�||d���\}}t|��}|�t|j��|��}|d|�dd�f}||fS)a�Returns a pair of matrices (`C`, `F`) with matching rank such that `A = C F`. Parameters ========== iszerofunc : Function, optional A function used for detecting whether an element can act as a pivot. ``lambda x: x.is_zero`` is used by default. simplify : Bool or Function, optional A function used to simplify elements when looking for a pivot. By default SymPy's ``simplify`` is used. Returns ======= (C, F) : Matrices `C` and `F` are full-rank matrices with rank as same as `A`, whose product gives `A`. See Notes for additional mathematical details. Examples ======== >>> from sympy import Matrix >>> A = Matrix([ ... [1, 3, 1, 4], ... [2, 7, 3, 9], ... [1, 5, 3, 1], ... [1, 2, 0, 8] ... ]) >>> C, F = A.rank_decomposition() >>> C Matrix([ [1, 3, 4], [2, 7, 9], [1, 5, 1], [1, 2, 8]]) >>> F Matrix([ [1, 0, -2, 0], [0, 1, 1, 0], [0, 0, 0, 1]]) >>> C * F == A True Notes ===== Obtaining `F`, an RREF of `A`, is equivalent to creating a product .. math:: E_n E_{n-1} ... E_1 A = F where `E_n, E_{n-1}, \dots, E_1` are the elimination matrices or permutation matrices equivalent to each row-reduction step. The inverse of the same product of elimination matrices gives `C`: .. math:: C = \left(E_n E_{n-1} \dots E_1\right)^{-1} It is not necessary, however, to actually compute the inverse: the columns of `C` are those from the original matrix with the same column indices as the indices of the pivot columns of `F`. References ========== .. [1] https://en.wikipedia.org/wiki/Rank_factorization .. [2] Piziak, R.; Odell, P. L. (1 June 1999). "Full Rank Factorization of Matrices". Mathematics Magazine. 72 (3): 193. doi:10.2307/2690882 See Also ======== sympy.matrices.matrixbase.MatrixBase.rref T)�simplify� iszerofunc�pivotsN)�rref�len�extract�range�rows)�Mrr�F� pivot_cols�rank�Cs �m/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/matrices/decompositions.py�_rank_decompositionr sl��l�F�F�H������M�A�z� �z�?�?�D� � � �%���-�-��,�,�A� �%�4�%����(� �A� �a�4�K�c���d�t|j��D��}|���D]'\}}}||kr||�|���(t |��}|g|jz}|g|jz}t|j��D]R}||dd�D]?}|||kr||}|||<|}|||k�|||kr |x||<||<�@�S||fS)aaLiu's algorithm, for pre-determination of the Elimination Tree of the given matrix, used in row-based symbolic Cholesky factorization. Examples ======== >>> from sympy import SparseMatrix >>> S = SparseMatrix([ ... [1, 0, 3, 2], ... [0, 0, 1, 0], ... [4, 0, 0, 5], ... [0, 6, 7, 0]]) >>> S.liupc() ([[0], [], [0], [1, 2]], [4, 3, 4, 4]) References ========== .. [1] Symbolic Sparse Cholesky Factorization using Elimination Trees, Jeroen Van Grondelle (1999) https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.7582 c��g|]}g��S�r!)�.0�rs r� <listcomp>z_liupc.<locals>.<listcomp>�s��#�#�#���#�#�#rN�����)rr�row_list�appendr) r�Rr#�c�_�inf�parent�virtual�ts r�_liupcr/ms��4 $�#�U�1�6�]�]�#�#�#�A��:�:�<�<�����1�a� ��6�6� �a�D�K�K��N�N�N���!�f�f�C��e�A�F�l�F��e�A�F�l�G� �1�6�]�]�+�+���1��c�r�c�� +� +�A��!�*�q�.�.�$�Q�Z����� ����!�*�q�.�.� �q�z�S� � �)*�*��q� �G�A�J�� +� �f�9�rc�~�|���\}}t|��}tj|��}t |j��D]m}||D]=}||kr5||kr/||�|��||}||kr||k�/�>tt||����||<�n|S)aVSymbolic cholesky factorization, for pre-determination of the non-zero structure of the Cholesky factororization. Examples ======== >>> from sympy import SparseMatrix >>> S = SparseMatrix([ ... [1, 0, 3, 2], ... [0, 0, 1, 0], ... [4, 0, 0, 5], ... [0, 6, 7, 0]]) >>> S.row_structure_symbolic_cholesky() [[0], [], [0], [1, 2]] References ========== .. [1] Symbolic Sparse Cholesky Factorization using Elimination Trees, Jeroen Van Grondelle (1999) https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.7582 ) �liupcr�copy�deepcopyrrr'�sorted�set)rr(r,r+�Lrow�k�js r� _row_structure_symbolic_choleskyr9�s���0��� � �I�A�v��A���C�� �a� � �D� �1�6�]�]�'�'���1�� � �A��s�(�(�q�A�v�v��Q����q�!�!�!��1�I���s�(�(�q�A�v�v����T�!�W���&�&��Q��� �KrTc �:����ddlm}|jstd���|r|jst d���|s#|���st d���|�|j|j���|r�t|j��D]��t���D]J�d���fz |��ft���fd�t���D����z z���f<�K|��ft��fd�t���D����z }|j durtd ���t|�����f<��n�t|j��D]��t���D]J�d���fz |��ft���fd �t���D����z z���f<�Kt|��ft��fd �t���D����z �����f<��|����S) aReturns the Cholesky-type decomposition L of a matrix A such that L * L.H == A if hermitian flag is True, or L * L.T == A if hermitian is False. A must be a Hermitian positive-definite matrix if hermitian is True, or a symmetric matrix if it is False. Examples ======== >>> from sympy import Matrix >>> A = Matrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11))) >>> A.cholesky() Matrix([ [ 5, 0, 0], [ 3, 3, 0], [-1, 1, 3]]) >>> A.cholesky() * A.cholesky().T Matrix([ [25, 15, -5], [15, 18, 0], [-5, 0, 11]]) The matrix can have complex entries: >>> from sympy import I >>> A = Matrix(((9, 3*I), (-3*I, 5))) >>> A.cholesky() Matrix([ [ 3, 0], [-I, 2]]) >>> A.cholesky() * A.cholesky().H Matrix([ [ 9, 3*I], [-3*I, 5]]) Non-hermitian Cholesky-type decomposition may be useful when the matrix is not positive-definite. >>> A = Matrix([[1, 2], [2, 1]]) >>> L = A.cholesky(hermitian=False) >>> L Matrix([ [1, 0], [2, sqrt(3)*I]]) >>> L*L.T == A True See Also ======== sympy.matrices.dense.DenseMatrix.LDLdecomposition sympy.matrices.matrixbase.MatrixBase.LUdecomposition QRdecomposition r��MutableDenseMatrix�Matrix must be square.�Matrix must be Hermitian.�Matrix must be symmetric.c3�f�K�|]+}��|f��|f���zV��,dS�N�� conjugate�r"r7�L�ir8s ���r� <genexpr>z_cholesky.<locals>.<genexpr> sD�����F�F���!�Q�$���!�Q�$�� 1� 1� 3� 3�3�F�F�F�F�F�Frc3�f�K�|]+}��|f��|f���zV��,dSrArB�r"r7rErFs ��rrGz_cholesky.<locals>.<genexpr>sD�����B�B�A�A�a��d�G�A�a��d�G�-�-�/�/�/�B�B�B�B�B�BrF� Matrix must be positive-definitec3�B�K�|]}��|f��|fzV��dSrAr!rDs ���rrGz_cholesky.<locals>.<genexpr>s7�����:�:�A��!�Q�$���!�Q�$���:�:�:�:�:�:rc3�2�K�|]}��|fdzV��dS��Nr!rIs ��rrGz_cholesky.<locals>.<genexpr>s/�����1�1�1�A�a��d�G�Q�J�1�1�1�1�1�1r)�denser<� is_squarer � is_hermitian� ValueError� is_symmetric�zerosrr�sum� is_positiver r�_new)r� hermitianr<�Lii2rErFr8s @@@r� _choleskyrZ�s������r*�)�)�)�)�)� �;�=�"�#;�<�<�<��6���6��4�5�5�5� �6�Q�^�^�-�-�6��4�5�5�5� � "� "�1�6�1�6� 2� 2�A��3��q�v��� !� !�A��1�X�X� I� I����!�Q�$��K�!�A�q�D�'��F�F�F�F�F�F�U�1�X�X�F�F�F�F�F�+G�H��!�Q�$����a��d�G��B�B�B�B�B��q���B�B�B�B�B�C�D���5�(�(�4�6�8�8�8��4�j�j�A�a��d�G�G� !��q�v��� 3� 3�A��1�X�X� =� =����!�Q�$��K�!�A�q�D�'��:�:�:�:�:�:��q���:�:�:�:�:�+;�<��!�Q�$����1�Q��T�7��1�1�1�1�1��a���1�1�1�1�1�2�3�3�A�a��d�G�G� �6�6�!�9�9�rc �p�ddlm}|jstd���|r|jst d���|s#|���st d���ttt��}|� ��}|� |j ��}tt|����D�]c}||D�]V}||kr�|||f|||f<d}||D]h} | |kr`||D]U} | |krL| | krE|r+|||| f||| f���zz }�;|||| f||| fzz }�Tnn�i||||f|z |||fz ��|||f<��|||f|||f<d}||D]F} | |kr>|r+|||| f||| f���zz }�5|||| fdzz }�F||||f|z ��} |r| jdurt!d ���t#| ��|||f<��X��e|�|��S) as Returns the Cholesky decomposition L of a matrix A such that L * L.T = A A must be a square, symmetric, positive-definite and non-singular matrix Examples ======== >>> from sympy import SparseMatrix >>> A = SparseMatrix(((25,15,-5),(15,18,0),(-5,0,11))) >>> A.cholesky() Matrix([ [ 5, 0, 0], [ 3, 3, 0], [-1, 1, 3]]) >>> A.cholesky() * A.cholesky().T == A True The matrix can have complex entries: >>> from sympy import I >>> A = SparseMatrix(((9, 3*I), (-3*I, 5))) >>> A.cholesky() Matrix([ [ 3, 0], [-I, 2]]) >>> A.cholesky() * A.cholesky().H Matrix([ [ 9, 3*I], [-3*I, 5]]) Non-hermitian Cholesky-type decomposition may be useful when the matrix is not positive-definite. >>> A = SparseMatrix([[1, 2], [2, 1]]) >>> L = A.cholesky(hermitian=False) >>> L Matrix([ [1, 0], [2, sqrt(3)*I]]) >>> L*L.T == A True See Also ======== sympy.matrices.sparse.SparseMatrix.LDLdecomposition sympy.matrices.matrixbase.MatrixBase.LUdecomposition QRdecomposition rr;r=r>r?rrNFrJ)rOr<rPr rQrRrSr r�row_structure_symbolic_choleskyrTrrrrCrVr rrW) rrXr<�dps� CrowstrucrrFr8�summ�p1�p2r7�Cjj2s r�_cholesky_sparserc"s���l*�)�)�)�)�)� �;�=�"�#;�<�<�<��6���6��4�5�5�5� �6�Q�^�^�-�-�6��4�5�5�5�&�z�:�>�>�C��1�1�3�3�I�"�(�(���0�0�A� �3�y�>�>� "� "�)%�)%���1��( %�( %�A��A�v�v��A�q�D�'��!�Q�$����#�A�,� "� "�B��A�v�v�"+�A�,� "� "�B�!�A�v�v�#%��8�8�'0�%B�(,��!�R�%���1�b�5��9K�9K�9M�9M�0M�(M���(,��!�R�%���1�b�5��0A�(A��� %��!�E���#�q��A��w��~��1�a�4��8�9�9��!�Q�$����A�q�D�'��!�Q�$����"�1����A��1�u�u�$�/� �A�a��d�G�A�a��d�G�,=�,=�,?�,?�$?�?�D�D� �A�a��d�G�Q�J�.�D�D���s�1�Q��T�7�T�>�*�*���<��!1�U�!:�!:�8�:�<�<�<��t�*�*��!�Q�$���Q( %�T �6�6�!�9�9�rc �z�����ddlm}|jstd���|r|jst d���|s#|���st d���|�|j|j���|� |j���|r�t|j��D]��t���D]K�d���fz |��ft����fd�t���D����z z���f<�L|��ft���fd�t���D����z ���f<���fj durtd �����n�t|j��D]��t���D]K�d���fz |��ft����fd �t���D����z z���f<�L|��ft���fd �t���D����z ���f<��|����|����fS) a�Returns the LDL Decomposition (L, D) of matrix A, such that L * D * L.H == A if hermitian flag is True, or L * D * L.T == A if hermitian is False. This method eliminates the use of square root. Further this ensures that all the diagonal entries of L are 1. A must be a Hermitian positive-definite matrix if hermitian is True, or a symmetric matrix otherwise. Examples ======== >>> from sympy import Matrix, eye >>> A = Matrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11))) >>> L, D = A.LDLdecomposition() >>> L Matrix([ [ 1, 0, 0], [ 3/5, 1, 0], [-1/5, 1/3, 1]]) >>> D Matrix([ [25, 0, 0], [ 0, 9, 0], [ 0, 0, 9]]) >>> L * D * L.T * A.inv() == eye(A.rows) True The matrix can have complex entries: >>> from sympy import I >>> A = Matrix(((9, 3*I), (-3*I, 5))) >>> L, D = A.LDLdecomposition() >>> L Matrix([ [ 1, 0], [-I/3, 1]]) >>> D Matrix([ [9, 0], [0, 4]]) >>> L*D*L.H == A True See Also ======== sympy.matrices.dense.DenseMatrix.cholesky sympy.matrices.matrixbase.MatrixBase.LUdecomposition QRdecomposition rr;r=r>r?c3�|�K�|]6}��|f��|f���z�||fzV��7dSrArB�r"r7�DrErFr8s ����rrGz$_LDLdecomposition.<locals>.<genexpr>�sj�����7K�7K�<=�A�a��d�G�A�a��d�G�-�-�/�/�/��!�Q�$��7�7K�7K�7K�7K�7K�7Krc3�|�K�|]6}��|f��|f���z�||fzV��7dSrArB�r"r7rgrErFs ���rrGz$_LDLdecomposition.<locals>.<genexpr>�sQ�����J�J�A�A�a��d�G�A�a��d�G�-�-�/�/�/��!�Q�$��7�J�J�J�J�J�JrFrJc3�X�K�|]$}��|f��|fz�||fzV��%dSrAr!rfs ����rrGz$_LDLdecomposition.<locals>.<genexpr>�sU�����7?�7?�01�A�a��d�G�A�a��d�G�O�A�a��d�G�+�7?�7?�7?�7?�7?�7?rc3�H�K�|]}��|fdz�||fzV��dSrMr!ris ���rrGz$_LDLdecomposition.<locals>.<genexpr>�s<�����#I�#I�1�A�a��d�G�Q�J�q��A��w�$6�#I�#I�#I�#I�#I�#Ir)rOr<rPr rQrRrSrTr�eyerrUrVr rW)rrXr<rgrErFr8s @@@@r�_LDLdecompositionrm�s�������h*�)�)�)�)�)� �;�=�"�#;�<�<�<��6���6��4�5�5�5� �6�Q�^�^�-�-�6��4�5�5�5� � "� "�1�6�1�6� 2� 2�A� � � ��� (� (�A��J��q�v��� 8� 8�A��1�X�X� L� L���q��A��w�;��1�a�4��3�7K�7K�7K�7K�7K�7K�7K�AF�q���7K�7K�7K�4K�4K�*K�L��!�Q�$�����A��w��J�J�J�J�J�J��q���J�J�J�J�J�K�A�a��d�G���A��w�"�e�+�+�4�6�8�8�8�,� 8��q�v��� J� J�A��1�X�X� @� @���q��A��w�;��1�a�4��3�7?�7?�7?�7?�7?�7?�7?�5:�1�X�X�7?�7?�7?�4?�4?�*?�@��!�Q�$�����1��g��#I�#I�#I�#I�#I�#I��a���#I�#I�#I� I� I�I�A�a��d�G�G� �6�6�!�9�9�a�f�f�Q�i�i� �rc � �ddlm}|jstd���|r|jst d���|s#|���st d���ttt��}|� ��}|� |j ��}|� |j |j ��}tt|����D�]�}||D�]y}||kr�|||f|||f<d} ||D]|} | |krt||D]j} | |krb| | kr[|r6| ||| f||| f���z|| | fzz } �F| ||| f||| fz|| | fzz } �j�|||||f| z |||fz ��|||f<��|||f|||f<d} ||D]\} | |krT|r6| ||| f||| f���z|| | fzz } �@| ||| fdz|| | fzz } �\||||f| z ��|||f<|r |||fjdurt%d �����{���|�|��|�|��fS) a� Returns the LDL Decomposition (matrices ``L`` and ``D``) of matrix ``A``, such that ``L * D * L.T == A``. ``A`` must be a square, symmetric, positive-definite and non-singular. This method eliminates the use of square root and ensures that all the diagonal entries of L are 1. Examples ======== >>> from sympy import SparseMatrix >>> A = SparseMatrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11))) >>> L, D = A.LDLdecomposition() >>> L Matrix([ [ 1, 0, 0], [ 3/5, 1, 0], [-1/5, 1/3, 1]]) >>> D Matrix([ [25, 0, 0], [ 0, 9, 0], [ 0, 0, 9]]) >>> L * D * L.T == A True rr;r=r>r?rrNFrJ)rOr<rPr rQrRrSr rr\rlrrT�colsrrrCrVr rW) rrXr<r]� LrowstrucrErgrFr8r_r`rar7s r�_LDLdecomposition_sparserq�s%��<*�)�)�)�)�)� �;�=�"�#;�<�<�<��6���6��4�5�5�5� �6�Q�^�^�-�-�6��4�5�5�5�&�z�:�>�>�C��1�1�3�3�I�"�&�&�q�v�.�.�A�"�(�(�����8�8�A� �3�y�>�>� "� "�'<�'<���1��& <�& <�A��A�v�v��A�q�D�'��!�Q�$����#�A�,� � �B��A�v�v�"+�A�,�&�&�B�!�A�v�v�#%��8�8�'0�%L�(,��!�R�%���1�b�5��9K�9K�9M�9M�0M�a�PR�TV�PV�i�0W�(W���(,��!�R�%���1�b�5��0A�!�B��F�)�0K�(K��� %����#�q��A��w��~��1�a�4��8�9�9��!�Q�$����A�q�D�'��!�Q�$����"�1����A��1�u�u�$�7� �A�a��d�G�A�a��d�G�,=�,=�,?�,?�$?��!�Q�$��$G�G�D�D� �A�a��d�G�Q�J�q��A��w�$6�6�D�D���#�a��1��g��n�-�-��!�Q�$���<��1�a�4��!4��!=�!=�8�:�<�<�<��K& <�P �6�6�!�9�9�a�f�f�Q�i�i� �rc���� ���|||���\� }�� fd�}�� fd�}��� j� j|��}��� j� j|��}|||fS)ahReturns (L, U, perm) where L is a lower triangular matrix with unit diagonal, U is an upper triangular matrix, and perm is a list of row swap index pairs. If A is the original matrix, then ``A = (L*U).permuteBkwd(perm)``, and the row permutation matrix P such that $P A = L U$ can be computed by ``P = eye(A.rows).permuteFwd(perm)``. See documentation for LUCombined for details about the keyword argument rankcheck, iszerofunc, and simpfunc. Parameters ========== rankcheck : bool, optional Determines if this function should detect the rank deficiency of the matrixis and should raise a ``ValueError``. iszerofunc : function, optional A function which determines if a given expression is zero. The function should be a callable that takes a single SymPy expression and returns a 3-valued boolean value ``True``, ``False``, or ``None``. It is internally used by the pivot searching algorithm. See the notes section for a more information about the pivot searching algorithm. simpfunc : function or None, optional A function that simplifies the input. If this is specified as a function, this function should be a callable that takes a single SymPy expression and returns an another SymPy expression that is algebraically equivalent. If ``None``, it indicates that the pivot search algorithm should not attempt to simplify any candidate pivots. It is internally used by the pivot searching algorithm. See the notes section for a more information about the pivot searching algorithm. Examples ======== >>> from sympy import Matrix >>> a = Matrix([[4, 3], [6, 3]]) >>> L, U, _ = a.LUdecomposition() >>> L Matrix([ [ 1, 0], [3/2, 1]]) >>> U Matrix([ [4, 3], [0, -3/2]]) See Also ======== sympy.matrices.dense.DenseMatrix.cholesky sympy.matrices.dense.DenseMatrix.LDLdecomposition QRdecomposition LUdecomposition_Simple LUdecompositionFF LUsolve )r�simpfunc� rankcheckc�p��||kr�jS||kr�jS|�jkr �||fS�jSrA)�zero�onero�rFr8r�combineds ��r�entry_Lz!_LUdecomposition.<locals>.entry_L�sG��� �q�5�5��6�M� �!�V�V��5�L� ��� � ��A�q�D�>� !��v� rc�2��||kr�jn �||fSrA)rvrxs ��r�entry_Uz!_LUdecomposition.<locals>.entry_U�s����Q���q�v�v�H�Q��T�N�2r)�LUdecomposition_SimplerWrro) rrrsrt�przr|rE�Urys ` @r�_LUdecompositionr�Bs�����L�*�*�j��Y�+�0�0�K�H�a� � � � � � �3�3�3�3�3�3� ���x�}�h�m�W�5�5�A� ���x�}�h�m�W�5�5�A� �a��7�Nrc ����|r tj|jvr"|�|j|j��gfSt ��}|����g}d�td�jdz ��D�]�}d}�|jkrR|rP��fd�t||j��D��}t|||��\} } } } | du}|r�dz ��|jkr|�P|r�|krtd���| �dn|| z} | �|r�|fcS| D]\}}|�||z�f<�|| kry|� || g���| d|�f�|d|�fc�|d|�f<�| d|�f<�| ��j�f�|��j�fc�|��j�f<�| ��j�f<�dz}t|dz�j��D]k}|�|�f�|�fz ���||f<t|�j��D]0}|�||f�||f�||fzz ���||f<�1�l|�kr't|dz�j��D]}|j �|�f<��dz ���jkr�|fcS���|rX|�t�j�j��dz t�j�j��dz f��rtd����|fS)aq$Compute the PLU decomposition of the matrix. Parameters ========== rankcheck : bool, optional Determines if this function should detect the rank deficiency of the matrixis and should raise a ``ValueError``. iszerofunc : function, optional A function which determines if a given expression is zero. The function should be a callable that takes a single SymPy expression and returns a 3-valued boolean value ``True``, ``False``, or ``None``. It is internally used by the pivot searching algorithm. See the notes section for a more information about the pivot searching algorithm. simpfunc : function or None, optional A function that simplifies the input. If this is specified as a function, this function should be a callable that takes a single SymPy expression and returns an another SymPy expression that is algebraically equivalent. If ``None``, it indicates that the pivot search algorithm should not attempt to simplify any candidate pivots. It is internally used by the pivot searching algorithm. See the notes section for a more information about the pivot searching algorithm. Returns ======= (lu, row_swaps) : (Matrix, list) If the original matrix is a $m, n$ matrix: *lu* is a $m, n$ matrix, which contains result of the decomposition in a compressed form. See the notes section to see how the matrix is compressed. *row_swaps* is a $m$-element list where each element is a pair of row exchange indices. ``A = (L*U).permute_backward(perm)``, and the row permutation matrix $P$ from the formula $P A = L U$ can be computed by ``P=eye(A.row).permute_forward(perm)``. Raises ====== ValueError Raised if ``rankcheck=True`` and the matrix is found to be rank deficient during the computation. Notes ===== About the PLU decomposition: PLU decomposition is a generalization of a LU decomposition which can be extended for rank-deficient matrices. It can further be generalized for non-square matrices, and this is the notation that SymPy is using. PLU decomposition is a decomposition of a $m, n$ matrix $A$ in the form of $P A = L U$ where * $L$ is a $m, m$ lower triangular matrix with unit diagonal entries. * $U$ is a $m, n$ upper triangular matrix. * $P$ is a $m, m$ permutation matrix. So, for a square matrix, the decomposition would look like: .. math:: L = \begin{bmatrix} 1 & 0 & 0 & \cdots & 0 \\ L_{1, 0} & 1 & 0 & \cdots & 0 \\ L_{2, 0} & L_{2, 1} & 1 & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ L_{n-1, 0} & L_{n-1, 1} & L_{n-1, 2} & \cdots & 1 \end{bmatrix} .. math:: U = \begin{bmatrix} U_{0, 0} & U_{0, 1} & U_{0, 2} & \cdots & U_{0, n-1} \\ 0 & U_{1, 1} & U_{1, 2} & \cdots & U_{1, n-1} \\ 0 & 0 & U_{2, 2} & \cdots & U_{2, n-1} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & 0 & \cdots & U_{n-1, n-1} \end{bmatrix} And for a matrix with more rows than the columns, the decomposition would look like: .. math:: L = \begin{bmatrix} 1 & 0 & 0 & \cdots & 0 & 0 & \cdots & 0 \\ L_{1, 0} & 1 & 0 & \cdots & 0 & 0 & \cdots & 0 \\ L_{2, 0} & L_{2, 1} & 1 & \cdots & 0 & 0 & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots \\ L_{n-1, 0} & L_{n-1, 1} & L_{n-1, 2} & \cdots & 1 & 0 & \cdots & 0 \\ L_{n, 0} & L_{n, 1} & L_{n, 2} & \cdots & L_{n, n-1} & 1 & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots \\ L_{m-1, 0} & L_{m-1, 1} & L_{m-1, 2} & \cdots & L_{m-1, n-1} & 0 & \cdots & 1 \\ \end{bmatrix} .. math:: U = \begin{bmatrix} U_{0, 0} & U_{0, 1} & U_{0, 2} & \cdots & U_{0, n-1} \\ 0 & U_{1, 1} & U_{1, 2} & \cdots & U_{1, n-1} \\ 0 & 0 & U_{2, 2} & \cdots & U_{2, n-1} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & 0 & \cdots & U_{n-1, n-1} \\ 0 & 0 & 0 & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & 0 & \cdots & 0 \end{bmatrix} Finally, for a matrix with more columns than the rows, the decomposition would look like: .. math:: L = \begin{bmatrix} 1 & 0 & 0 & \cdots & 0 \\ L_{1, 0} & 1 & 0 & \cdots & 0 \\ L_{2, 0} & L_{2, 1} & 1 & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ L_{m-1, 0} & L_{m-1, 1} & L_{m-1, 2} & \cdots & 1 \end{bmatrix} .. math:: U = \begin{bmatrix} U_{0, 0} & U_{0, 1} & U_{0, 2} & \cdots & U_{0, m-1} & \cdots & U_{0, n-1} \\ 0 & U_{1, 1} & U_{1, 2} & \cdots & U_{1, m-1} & \cdots & U_{1, n-1} \\ 0 & 0 & U_{2, 2} & \cdots & U_{2, m-1} & \cdots & U_{2, n-1} \\ \vdots & \vdots & \vdots & \ddots & \vdots & \cdots & \vdots \\ 0 & 0 & 0 & \cdots & U_{m-1, m-1} & \cdots & U_{m-1, n-1} \\ \end{bmatrix} About the compressed LU storage: The results of the decomposition are often stored in compressed forms rather than returning $L$ and $U$ matrices individually. It may be less intiuitive, but it is commonly used for a lot of numeric libraries because of the efficiency. The storage matrix is defined as following for this specific method: * The subdiagonal elements of $L$ are stored in the subdiagonal portion of $LU$, that is $LU_{i, j} = L_{i, j}$ whenever $i > j$. * The elements on the diagonal of $L$ are all 1, and are not explicitly stored. * $U$ is stored in the upper triangular portion of $LU$, that is $LU_{i, j} = U_{i, j}$ whenever $i <= j$. * For a case of $m > n$, the right side of the $L$ matrix is trivial to store. * For a case of $m < n$, the below side of the $U$ matrix is trivial to store. So, for a square matrix, the compressed output matrix would be: .. math:: LU = \begin{bmatrix} U_{0, 0} & U_{0, 1} & U_{0, 2} & \cdots & U_{0, n-1} \\ L_{1, 0} & U_{1, 1} & U_{1, 2} & \cdots & U_{1, n-1} \\ L_{2, 0} & L_{2, 1} & U_{2, 2} & \cdots & U_{2, n-1} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ L_{n-1, 0} & L_{n-1, 1} & L_{n-1, 2} & \cdots & U_{n-1, n-1} \end{bmatrix} For a matrix with more rows than the columns, the compressed output matrix would be: .. math:: LU = \begin{bmatrix} U_{0, 0} & U_{0, 1} & U_{0, 2} & \cdots & U_{0, n-1} \\ L_{1, 0} & U_{1, 1} & U_{1, 2} & \cdots & U_{1, n-1} \\ L_{2, 0} & L_{2, 1} & U_{2, 2} & \cdots & U_{2, n-1} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ L_{n-1, 0} & L_{n-1, 1} & L_{n-1, 2} & \cdots & U_{n-1, n-1} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ L_{m-1, 0} & L_{m-1, 1} & L_{m-1, 2} & \cdots & L_{m-1, n-1} \\ \end{bmatrix} For a matrix with more columns than the rows, the compressed output matrix would be: .. math:: LU = \begin{bmatrix} U_{0, 0} & U_{0, 1} & U_{0, 2} & \cdots & U_{0, m-1} & \cdots & U_{0, n-1} \\ L_{1, 0} & U_{1, 1} & U_{1, 2} & \cdots & U_{1, m-1} & \cdots & U_{1, n-1} \\ L_{2, 0} & L_{2, 1} & U_{2, 2} & \cdots & U_{2, m-1} & \cdots & U_{2, n-1} \\ \vdots & \vdots & \vdots & \ddots & \vdots & \cdots & \vdots \\ L_{m-1, 0} & L_{m-1, 1} & L_{m-1, 2} & \cdots & U_{m-1, m-1} & \cdots & U_{m-1, n-1} \\ \end{bmatrix} About the pivot searching algorithm: When a matrix contains symbolic entries, the pivot search algorithm differs from the case where every entry can be categorized as zero or nonzero. The algorithm searches column by column through the submatrix whose top left entry coincides with the pivot position. If it exists, the pivot is the first entry in the current search column that iszerofunc guarantees is nonzero. If no such candidate exists, then each candidate pivot is simplified if simpfunc is not None. The search is repeated, with the difference that a candidate may be the pivot if ``iszerofunc()`` cannot guarantee that it is nonzero. In the second search the pivot is the first candidate that iszerofunc can guarantee is nonzero. If no such candidate exists, then the pivot is the first candidate for which iszerofunc returns None. If no such candidate exists, then the search is repeated in the next column to the right. The pivot search algorithm differs from the one in ``rref()``, which relies on ``_find_reasonable_pivot()``. Future versions of ``LUdecomposition_simple()`` may use ``_find_reasonable_pivot()``. See Also ======== sympy.matrices.matrixbase.MatrixBase.LUdecomposition LUdecompositionFF LUsolve rrTc3�,�K�|]}�|�fV��dSrAr!)r"r#�lu� pivot_cols ��rrGz*_LUdecomposition_Simple.<locals>.<genexpr>�s,�����J�J�A�r�!�Y�,�'�J�J�J�J�J�JrNz�Rank of matrix is strictly less than number of rows or columns. Pass keyword argument rankcheck=False to compute the LU decomposition of this matrix.)r�Zero�shaperTrror � as_mutablerr rRr'rvr)rrrsrtr]� row_swaps� pivot_row� iszeropivot�sub_col�pivot_row_offset� pivot_value�is_assumed_non_zero�ind_simplified_pairs�candidate_pivot_row�offset�val� start_col�rowr)r�r�s @@r�_LUdecomposition_Simpler��s&����D� � ��v������w�w�q�v�q�v�&�&��*�*�&�(�(�C�� � ���B��I��I��1�b�g��k�*�*�f!�f!� �� ��1�6�!�!�k�!�J�J�J�J�J��y�!�&�1I�1I�J�J�J�G�-�W�j�(�K�K� U� �k�+>�@T�&��-�K�� ��Q�� ��1�6�!�!�k�!� � I��i�/�/� �H�I�I� I� '7�&>�d�d�I�P`�D`�� � &�;� &� �y�=� � � �0� 4� 4�K�F�C�03�B�y�6�!�9�,� -� -� �+� +� +� � � �i�)<�=� >� >� >��&��)� �3�4�b��A�i�K�9O�6P� M�B�y�!�I�+�%� &��+>��)� �+K�(L� �&� �"�'�(9�9�:�B�y�)�TV�T[�J[�?[�<\� Y�B�y�)�B�G�+�+� ,�b�1D�i�PR�PW�FW�1W�.X���M� ���Q����0�0� S� S�C���B�s�I�~�&�r�)�Y�*>�'?�?�@�@� �s�I�~� ��9�b�g�.�.� S� S�� �S��C��F��b��i��.@��I�q�L�AQ�.Q�!Q�R�R��3��6� � � S� � � !� !� �Y��]�B�G�4�4� ,� ,��%&�V��3� �>�"�"��Q�� � ��� � ��y�=� � � � � �I� �:��3�r�w���(�(�1�,�c�"�'�2�7�.C�.C�a�.G�G�H� J� J� I��H�I�I� I� �y�=�rc�z�ddlm}|j}|j}|j|j}}|���||��||��}}}|||��} d} t|dz ��D�];} || | fdkr�t| dz|��D]} || | frn�td���|| | d�f|| | d�fc|| | d�f<|| | d�f<|| d| �f|| d| �fc|| d| �f<|| d| �f<|| dd�f|| dd�fc|| dd�f<|| dd�f<|| | fx|| | f<} | | z| | | f<t| dz|��D]S}||| fx||| f<}t| dz|��D]%}| |||fz|| |f|zz | z |||f<�&d||| f<�T| } ��=| | |dz |dz f<||| |fS)aICompute a fraction-free LU decomposition. Returns 4 matrices P, L, D, U such that PA = L D**-1 U. If the elements of the matrix belong to some integral domain I, then all elements of L, D and U are guaranteed to belong to I. See Also ======== sympy.matrices.matrixbase.MatrixBase.LUdecomposition LUdecomposition_Simple LUsolve References ========== .. [1] W. Zhou & D.J. Jeffrey, "Fraction-free matrix factors: new forms for LU and QR factors". Frontiers in Computer Science in China, Vol 2, no. 1, pp. 67-80, 2008. r)� SparseMatrixrzMatrix is not full rankN) �sympy.matricesr�rTrlrror�rrR)rr�rTrl�n�mrrE�P�DD�oldpivotr7�kpivot�UkkrF�Uikr8s r�_LUdecompositionFFr�+sx��,,�+�+�+�+�+��!�E���C��v�q�v�q�A��|�|�~�~�s�s�1�v�v�s�s�1�v�v�!�q�A��u�Q��{�{�B��H� �1�q�5�\�\���� �Q��T�7�a�<�<���A��q�/�/� <� <���V�Q�Y�<���E��!�!:�;�;�;�&'����� �m�Q�q�!�"�"�u�X� #�A�a����e�H�a����� �m�&'����� �m�Q�q�"�1�"�u�X� #�A�a��!��e�H�a����� �m�&'����� �l�A�a����d�G� !�A�a����d�G�Q�v�q�q�q�y�\��1�a�4�� ��1�a�4��3��c�>��1�a�4���q�1�u�a��� � �A��a��d�G� #�A�a��d�G�c��1�q�5�!�_�_� E� E����1�a�4��=�1�Q��T�7�S�=�8�H�D��!�Q�$����A�a��d�G�G�����B�q�1�u�a�!�e�|�� �a��Q�;�rc�4� � �|j}|j\}}||kr�||z���\}� g� t� j����D]!\}}|js� �|���"|dd�� f}� � fd�t� j��D��}� j |�� |� ��\}}||z� j ��z} n�||z���\} � g� t� j����D]!\}}|js� �|���"| dd�� f} � � fd�t� j��D��}� j |�� | � ��\} }|| z� j ��z}| � |fS)a�Returns a Condensed Singular Value decomposition. Explanation =========== A Singular Value decomposition is a decomposition in the form $A = U \Sigma V^H$ where - $U, V$ are column orthogonal matrix. - $\Sigma$ is a diagonal matrix, where the main diagonal contains singular values of matrix A. A column orthogonal matrix satisfies $\mathbb{I} = U^H U$ while a full orthogonal matrix satisfies relation $\mathbb{I} = U U^H = U^H U$ where $\mathbb{I}$ is an identity matrix with matching dimensions. For matrices which are not square or are rank-deficient, it is sufficient to return a column orthogonal matrix because augmenting them may introduce redundant computations. In condensed Singular Value Decomposition we only return column orthogonal matrices because of this reason If you want to augment the results to return a full orthogonal decomposition, you should use the following procedures. - Augment the $U, V$ matrices with columns that are orthogonal to every other columns and make it square. - Augment the $\Sigma$ matrix with zero rows to make it have the same shape as the original matrix. The procedure will be illustrated in the examples section. Examples ======== we take a full rank matrix first: >>> from sympy import Matrix >>> A = Matrix([[1, 2],[2,1]]) >>> U, S, V = A.singular_value_decomposition() >>> U Matrix([ [ sqrt(2)/2, sqrt(2)/2], [-sqrt(2)/2, sqrt(2)/2]]) >>> S Matrix([ [1, 0], [0, 3]]) >>> V Matrix([ [-sqrt(2)/2, sqrt(2)/2], [ sqrt(2)/2, sqrt(2)/2]]) If a matrix if square and full rank both U, V are orthogonal in both directions >>> U * U.H Matrix([ [1, 0], [0, 1]]) >>> U.H * U Matrix([ [1, 0], [0, 1]]) >>> V * V.H Matrix([ [1, 0], [0, 1]]) >>> V.H * V Matrix([ [1, 0], [0, 1]]) >>> A == U * S * V.H True >>> C = Matrix([ ... [1, 0, 0, 0, 2], ... [0, 0, 3, 0, 0], ... [0, 0, 0, 0, 0], ... [0, 2, 0, 0, 0], ... ]) >>> U, S, V = C.singular_value_decomposition() >>> V.H * V Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> V * V.H Matrix([ [1/5, 0, 0, 0, 2/5], [ 0, 1, 0, 0, 0], [ 0, 0, 1, 0, 0], [ 0, 0, 0, 0, 0], [2/5, 0, 0, 0, 4/5]]) If you want to augment the results to be a full orthogonal decomposition, you should augment $V$ with an another orthogonal column. You are able to append an arbitrary standard basis that are linearly independent to every other columns and you can run the Gram-Schmidt process to make them augmented as orthogonal basis. >>> V_aug = V.row_join(Matrix([[0,0,0,0,1], ... [0,0,0,1,0]]).H) >>> V_aug = V_aug.QRdecomposition()[0] >>> V_aug Matrix([ [0, sqrt(5)/5, 0, -2*sqrt(5)/5, 0], [1, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 1], [0, 2*sqrt(5)/5, 0, sqrt(5)/5, 0]]) >>> V_aug.H * V_aug Matrix([ [1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]) >>> V_aug * V_aug.H Matrix([ [1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]) Similarly we augment U >>> U_aug = U.row_join(Matrix([0,0,1,0])) >>> U_aug = U_aug.QRdecomposition()[0] >>> U_aug Matrix([ [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0]]) >>> U_aug.H * U_aug Matrix([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) >>> U_aug * U_aug.H Matrix([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) We add 2 zero columns and one row to S >>> S_aug = S.col_join(Matrix([[0,0,0]])) >>> S_aug = S_aug.row_join(Matrix([[0,0,0,0], ... [0,0,0,0]]).H) >>> S_aug Matrix([ [2, 0, 0, 0, 0], [0, sqrt(5), 0, 0, 0], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0]]) >>> U_aug * S_aug * V_aug.H == C True Nc�F��g|]}|�v�t�||f����Sr!�r�r"rFr�rankeds ��rr$z1_singular_value_decomposition.<locals>.<listcomp>"�,���M�M�M�1��f����a��1��g�����rc�F��g|]}|�v�t�||f����Sr!r�r�s ��rr$z1_singular_value_decomposition.<locals>.<listcomp>0r�r) �Hr�� diagonalize� enumerate�diagonal�is_zeror'rr�diag�QRdecomposition�inv) �A�AHr�r��VrF�x� Singular_valsr*rrr�s @@r�_singular_value_decompositionr�gs�����^ ��B� �7�D�A�q��A�v�v��Q��#�#�%�%���1����j�a�j�l�l�+�+� !� !�D�A�q��9� !�� � �a� � � �� �a�a�a��i�L��M�M�M�M�M��a�f� � �M�M�M� � �A�F�M� "��� � �"�"���1� ��E�E�A�E�G�G�O����B��#�#�%�%���1����j�a�j�l�l�+�+� !� !�D�A�q��9� !�� � �a� � � �� �a�a�a��i�L��M�M�M�M�M��a�f� � �M�M�M� � �A�F�M� "��� � �"�"���1� ��F�U�Q�U�W�W� �� �a��7�Nrc �v�d�}ttt��}|���}g}|}|�|j��}t |j��D]�}t |��D]�} |dd�| fjr�||dd�| f|dd�|f��||dd�| f|dd�| f��z || |f<||| |f��|| |f<|dd�|fxx|dd�| f|| |fzzcc<��||dd�|f��|dd�|f<|dd�|fjdur!|�|��|j|||f<��|� t |j ��|��}|� |t |j����}|r]t |j��D]H} |dd�| f� ��} |dd�| fxx| zcc<|| dd�fxx| zcc<�I|� |��|� |��fS)Nc�0�|�|d���S)NT)rX)�dot)�u�vs rr�z&_QRdecomposition_optional.<locals>.dot9s���u�u�Q�$�u�'�'�'rT) r rr�rTror�is_zero_matrixr'rwrr�norm� __class__) r� normalizer�r]r�r��Qr(r8rFr�s r�_QRdecomposition_optionalr�8sj��(�(�(� !��Z� 8� 8�C� � � ���A� �F� �A� �������A� �1�6�]�]� � ���q��� )� )�A�����A��w�%� ���c�!�A�A�A�q�D�'�1�Q�Q�Q��T�7�+�+�c�c�!�A�A�A�q�D�'�1�Q�Q�Q��T�7�.C�.C�C�A�a��d�G��c�!�A�q�D�'�l�l�A�a��d�G� �a�a�a��d�G�G�G�q����A��w��1�a�4��(� (�G�G�G�G��#�a����1��g�,�,��!�!�!�Q�$�� �Q�Q�Q��T�7� !�� -� -� �M�M�!� � � ��e�A�a��d�G�� � � �%���-�-��(�(�A� � � �&�%���-�-�(�(�A����q�v��� � �A��Q�Q�Q��T�7�<�<�>�>�D� �a�a�a��d�G�G�G�t�O�G�G�G� �a����d�G�G�G�t�O�G�G�G�G� �;�;�q�>�>�1�;�;�q�>�>� )�)rc�$�t|d���S)alReturns a QR decomposition. Explanation =========== A QR decomposition is a decomposition in the form $A = Q R$ where - $Q$ is a column orthogonal matrix. - $R$ is a upper triangular (trapezoidal) matrix. A column orthogonal matrix satisfies $\mathbb{I} = Q^H Q$ while a full orthogonal matrix satisfies relation $\mathbb{I} = Q Q^H = Q^H Q$ where $I$ is an identity matrix with matching dimensions. For matrices which are not square or are rank-deficient, it is sufficient to return a column orthogonal matrix because augmenting them may introduce redundant computations. And an another advantage of this is that you can easily inspect the matrix rank by counting the number of columns of $Q$. If you want to augment the results to return a full orthogonal decomposition, you should use the following procedures. - Augment the $Q$ matrix with columns that are orthogonal to every other columns and make it square. - Augment the $R$ matrix with zero rows to make it have the same shape as the original matrix. The procedure will be illustrated in the examples section. Examples ======== A full rank matrix example: >>> from sympy import Matrix >>> A = Matrix([[12, -51, 4], [6, 167, -68], [-4, 24, -41]]) >>> Q, R = A.QRdecomposition() >>> Q Matrix([ [ 6/7, -69/175, -58/175], [ 3/7, 158/175, 6/175], [-2/7, 6/35, -33/35]]) >>> R Matrix([ [14, 21, -14], [ 0, 175, -70], [ 0, 0, 35]]) If the matrix is square and full rank, the $Q$ matrix becomes orthogonal in both directions, and needs no augmentation. >>> Q * Q.H Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> Q.H * Q Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> A == Q*R True A rank deficient matrix example: >>> A = Matrix([[12, -51, 0], [6, 167, 0], [-4, 24, 0]]) >>> Q, R = A.QRdecomposition() >>> Q Matrix([ [ 6/7, -69/175], [ 3/7, 158/175], [-2/7, 6/35]]) >>> R Matrix([ [14, 21, 0], [ 0, 175, 0]]) QRdecomposition might return a matrix Q that is rectangular. In this case the orthogonality condition might be satisfied as $\mathbb{I} = Q.H*Q$ but not in the reversed product $\mathbb{I} = Q * Q.H$. >>> Q.H * Q Matrix([ [1, 0], [0, 1]]) >>> Q * Q.H Matrix([ [27261/30625, 348/30625, -1914/6125], [ 348/30625, 30589/30625, 198/6125], [ -1914/6125, 198/6125, 136/1225]]) If you want to augment the results to be a full orthogonal decomposition, you should augment $Q$ with an another orthogonal column. You are able to append an identity matrix, and you can run the Gram-Schmidt process to make them augmented as orthogonal basis. >>> Q_aug = Q.row_join(Matrix.eye(3)) >>> Q_aug = Q_aug.QRdecomposition()[0] >>> Q_aug Matrix([ [ 6/7, -69/175, 58/175], [ 3/7, 158/175, -6/175], [-2/7, 6/35, 33/35]]) >>> Q_aug.H * Q_aug Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> Q_aug * Q_aug.H Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) Augmenting the $R$ matrix with zero row is straightforward. >>> R_aug = R.col_join(Matrix([[0, 0, 0]])) >>> R_aug Matrix([ [14, 21, 0], [ 0, 175, 0], [ 0, 0, 0]]) >>> Q_aug * R_aug == A True A zero matrix example: >>> from sympy import Matrix >>> A = Matrix.zeros(3, 4) >>> Q, R = A.QRdecomposition() They may return matrices with zero rows and columns. >>> Q Matrix(3, 0, []) >>> R Matrix(0, 4, []) >>> Q*R Matrix([ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) As the same augmentation rule described above, $Q$ can be augmented with columns of an identity matrix and $R$ can be augmented with rows of a zero matrix. >>> Q_aug = Q.row_join(Matrix.eye(3)) >>> R_aug = R.col_join(Matrix.zeros(3, 4)) >>> Q_aug * Q_aug.T Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> R_aug Matrix([ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) >>> Q_aug * R_aug == A True See Also ======== sympy.matrices.dense.DenseMatrix.cholesky sympy.matrices.dense.DenseMatrix.LDLdecomposition sympy.matrices.matrixbase.MatrixBase.LUdecomposition QRsolve T)r�)r�)rs r�_QRdecompositionr�_s��h %�Q�$� 7� 7� 7�7rc��|���}|jstd���|j}|�|��}|}t |dz ��D�]d}||dzd�|f}|dd�dd�fjr�&t|d��dkr7|dt|d��|���zz|d<n |d|���z|d<||���z }||dzd�dd�fd|z|j ||dzd�dd�fzzz ||dzd�dd�f<|dd�|dzd�f|dd�|dzd�fd|zz|j zz |dd�|dzd�f<|dd�|dzd�f|dd�|dzd�fd|zz|j zz |dd�|dzd�f<��f||fS)a�Converts a matrix into Hessenberg matrix H. Returns 2 matrices H, P s.t. $P H P^{T} = A$, where H is an upper hessenberg matrix and P is an orthogonal matrix Examples ======== >>> from sympy import Matrix >>> A = Matrix([ ... [1,2,3], ... [-3,5,6], ... [4,-8,9], ... ]) >>> H, P = A.upper_hessenberg_decomposition() >>> H Matrix([ [1, 6/5, 17/5], [5, 213/25, -134/25], [0, 216/25, 137/25]]) >>> P Matrix([ [1, 0, 0], [0, -3/5, 4/5], [0, 4/5, 3/5]]) >>> P * H * P.H == A True References ========== .. [#] https://mathworld.wolfram.com/HessenbergDecomposition.html r=rNrNr) r�rPr rorlrr�rr�r�)r�rr�r�r�r8r�r�s r�_upper_hessenberg_decompositionr�s ��J � � ���A� �;�=�"�#;�<�<�<� ��A� ���a���A� �A� �1�q�5�\�\�E�E�� �a�!�e�f�f�a�i�L�� �Q�R�R����U�8� "� � � ��!��:�:��?�?��Q�4�$�q��t�*�*�q�v�v�x�x�/�/�A�a�D�D��Q�4�!�&�&�(�(�?�A�a�D� ������L����Q��������|�a�!�e�q�s�Q�q�1�u�v�v�q�q�q�y�\�/A�&B�B��!�a�%�&�&�!�!�!�)� �����A��E�F�F��|�q����A��E�F�F��|�q�1�u�'=���&D�D��!�!�!�Q��U�V�V�)� �����A��E�F�F��|�q����A��E�F�F��|�q�1�u�'=���&D�D��!�!�!�Q��U�V�V�)� � � �a�4�Kr)T) r2� sympy.corer�sympy.core.functionr�(sympy.functions.elementary.miscellaneousrr�$sympy.functions.elementary.complexesr� exceptionsr r � utilitiesr r � determinantr rr/r9rZrcrmrqr�r�r�r�r�r�r�r!rr�<module>r�s��� � � � �������*�*�*�*�*�*�>�>�>�>�>�>�>�>�5�5�5�5�5�5�L�L�L�L�L�L�L�L�6�6�6�6�6�6�6�6�5�5�5�5�5�5�'.��]�]�]�]�@.�.�.�`$�$�$�N\�\�\�\�|n�n�n�n�bU �U �U �U �nU �U �U �U �p$+�T�U�c�c�c�c�J+2�D��B�B�B�B�H :�:�:�xO�O�O�b$*�$*�$*�$*�Nt8�t8�t8�l@�@�@�@�@r
Memory