� ��gn3����ddlmZddlmZddlmZmZmZddlm Z d�Z d�Z d�Z dd �Z e fd �Ze fd �Ze fd �Ze fd�Ze fd�Ze fd�Ze fd�Zdd�Zd�Zdd�Ze fd�Zde dfd�ZdS)�)�DMNonInvertibleMatrixError)�EX�)� MatrixError�NonSquareMatrixError�NonInvertibleMatrixError��_iszeroc�N�|jr|jS|j|jkrD|j�|������|j��S|j�|�|j�������S)aSubroutine for full row or column rank matrices. For full row rank matrices, inverse of ``A * A.H`` Exists. For full column rank matrices, inverse of ``A.H * A`` Exists. This routine can apply for both cases by checking the shape and have small decision. )�is_zero_matrix�H�rows�cols�multiply�inv)�Ms �f/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/matrices/inverse.py�_pinv_full_rankrs�� ����s� ��v������s�|�|�A���"�"�$�$�-�-�a�c�2�2�2��s�|�|�A�J�J�q�s�O�O�/�/�1�1�2�2�2�c��|jr|jS|���\}}t|��}t|��}|�|��S)z�Subroutine for rank decomposition With rank decompositions, `A` can be decomposed into two full- rank matrices, and each matrix can take pseudoinverse individually. )r r �rank_decompositionrr)r�B�C�Bp�Cps r�_pinv_rank_decompositionrsU�� ����s� � � � � !� !�D�A�q� �� � �B� �� � �B� �;�;�r�?�?�rc��|jr|jS|}|j} |j|jkr�|�|���d���\}}|�d���}|�|���|j���|��S|�|���d���\}}|�d���}|�|���|���|j��S#t$rtd���wxYw)z�Subroutine using diagonalization This routine can sometimes fail if SymPy's eigenvalue computation is not reliable. T)� normalizec�.�t|��rdnd|z S�Nrrr ��xs r�<lambda>z'_pinv_diagonalization.<locals>.<lambda><����� � �+E�1�1��A��rc�.�t|��rdnd|z Sr r r!s rr#z'_pinv_diagonalization.<locals>.<lambda>Cr$rz[pinv for rank-deficient matrices where diagonalization of A.H*A fails is not supported yet.) r r rrr� diagonalize� applyfuncr�NotImplementedError)r�A�AH�P�D�D_pinvs r�_pinv_diagonalizationr.,sJ�� ����s� � �A� ��B�D� �6�Q�V� � ��[�[��^�^�/�/�$�/�?�?�D�A�q��[�[�!E�!E�F�F�F��:�:�f�%�%�.�.�q�s�3�3�<�<�R�@�@� @��Z�Z��^�^�/�/�"&�0�(�(�D�A�q��[�[�!E�!E�F�F�F��;�;�q�>�>�*�*�6�2�2�;�;�A�C�@�@� @�� �D�D�D�!� C�D�D� D�D���s�BD-�+BD-�-E�RDc��|jr|jS|dkrt|��S|dkrt|��St dt |��z���)aCalculate the Moore-Penrose pseudoinverse of the matrix. The Moore-Penrose pseudoinverse exists and is unique for any matrix. If the matrix is invertible, the pseudoinverse is the same as the inverse. Parameters ========== method : String, optional Specifies the method for computing the pseudoinverse. If ``'RD'``, Rank-Decomposition will be used. If ``'ED'``, Diagonalization will be used. Examples ======== Computing pseudoinverse by rank decomposition : >>> from sympy import Matrix >>> A = Matrix([[1, 2, 3], [4, 5, 6]]) >>> A.pinv() Matrix([ [-17/18, 4/9], [ -1/9, 1/9], [ 13/18, -2/9]]) Computing pseudoinverse by diagonalization : >>> B = A.pinv(method='ED') >>> B.simplify() >>> B Matrix([ [-17/18, 4/9], [ -1/9, 1/9], [ 13/18, -2/9]]) See Also ======== inv pinv_solve References ========== .. [1] https://en.wikipedia.org/wiki/Moore-Penrose_pseudoinverse r/�EDzinvalid pinv method %s)r r rr.� ValueError�repr)r�methods r�_pinvr5Ls_��l ����s� � ��~�~�'��*�*�*� �4���$�Q�'�'�'��1�D��L�L�@�A�A�Arc�F���|jstd���|�d���}|�d��}|�J|�d���d�t ��fd�t �j��D����}|rtd ���|S) zfInitial check to see if a matrix is invertible. Raises or returns determinant for use in _inv_ADJ.�"A Matrix must be square to invert.� berkowitz)r4rNT)�simplifyc3�>�K�|]}��||f��V��dS�N�)�.0�j� iszerofunc�oks ��r� <genexpr>z%_verify_invertible.<locals>.<genexpr>�s5�����@�@�A�:�:�b��A��h�'�'�@�@�@�@�@�@r� Matrix det == 0; not invertible.) � is_squarer�det�equals�rref�any�rangerr)rr?�d�zeror@s ` @r�_verify_invertiblerK�s����� �;�I�"�#G�H�H�H� �5�5� �5� $� $�A� �8�8�A�;�;�D� �|��v�v�t�v�$�$�Q�'���@�@�@�@�@��r�w���@�@�@�@�@�� �K�&�'I�J�J�J� �Hrc�R�t||���}|���|z S)z�Calculates the inverse using the adjugate matrix and a determinant. See Also ======== inv inverse_GE inverse_LU inverse_CH inverse_LDL �r?)rK�adjugate)rr?rIs r�_inv_ADJrO�s)�� �1��4�4�4�A� �:�:�<�<�!� �rc����ddlm}|jstd���|�|���|�|j����}|��d���d�t��fd�t�j��D����rtd���|� �d d �|jd �f��S) z�Calculates the inverse using Gaussian elimination. See Also ======== inv inverse_ADJ inverse_LU inverse_CH inverse_LDL r)�Matrixr7T)r?r9rc3�>�K�|]}��||f��V��dSr;r<)r=r>r?�reds ��rrAz_inv_GE.<locals>.<genexpr>�s5����� :� :�Q�:�:�c�!�Q�$�i� � � :� :� :� :� :� :rrBN) �denserQrCr�hstack� as_mutable�eyerrFrGrHr�_new)rr?rQ�bigrSs ` @r�_inv_GErZ�s����������� �;�I�"�#G�H�H�H� �-�-�� � ���� � �1�6�(:�(:� ;� ;�C� �(�(�j�4�(� 8� 8�� ;�C� � :� :� :� :� :�%���/�/� :� :� :�:�:�K�&�'I�J�J�J� �6�6�#�a�a�a�����l�#� $� $�$rc���|jstd���|jrt||���|�|�|j��t���S)z�Calculates the inverse using LU decomposition. See Also ======== inv inverse_ADJ inverse_GE inverse_CH inverse_LDL r7rM)rCr� free_symbolsrK�LUsolverWrr �rr?s r�_inv_LUr_�s]�� �;�I�"�#G�H�H�H��~�5��1��4�4�4�4� �9�9�Q�U�U�1�6�]�]�w�9� 7� 7�7rc�~�t||���|�|�|j����S)z�Calculates the inverse using cholesky decomposition. See Also ======== inv inverse_ADJ inverse_GE inverse_LU inverse_LDL rM)rK�cholesky_solverWrr^s r�_inv_CHrb�s7���q�Z�0�0�0�0� � � �A�E�E�!�&�M�M� *� *�*rc�~�t||���|�|�|j����S)z�Calculates the inverse using LDL decomposition. See Also ======== inv inverse_ADJ inverse_GE inverse_LU inverse_CH rM)rK�LDLsolverWrr^s r�_inv_LDLre�s5���q�Z�0�0�0�0� �:�:�a�e�e�A�F�m�m� $� $�$rc�~�t||���|�|�|j����S)z�Calculates the inverse using QR decomposition. See Also ======== inv inverse_ADJ inverse_GE inverse_CH inverse_LDL rM)rK�QRsolverWrr^s r�_inv_QRrhs5���q�Z�0�0�0�0� �9�9�Q�U�U�1�6�]�]� #� #�#rFc��|���}|j}|s |jrdS|jr|�t��S|S)z.Try to convert a matrix to a ``DomainMatrix``.N)�to_DM�domain�is_EXRAW� convert_tor)r�use_EX�dM�Ks r�_try_DMrqsO�� �����B� � �A� ��a�j���t� ����}�}�R� � � �� rc�2�|js|jrdS|j S)z,Check whether to convert to an exact domain.F)�is_RR�is_CC�is_Exact)�doms r�_use_exact_domainrw s'��  �y� �C�I� ��u��<��rTc�^�|j\}}|j}||krtd���t|��}|r)|���}|�|��} |���\}}n#t$rtd���wxYw|r+|�|��}|� ||��}|r8|jj s|� ��}||z � ��} n/|� ��|j� |��z } | S)z�Calculates the inverse using ``DomainMatrix``. See Also ======== inv inverse_ADJ inverse_GE inverse_CH inverse_LDL sympy.polys.matrices.domainmatrix.DomainMatrix.inv r7rB)�shaperkrrw� get_exactrm�inv_denrr� convert_from�is_Field�to_field� to_Matrix�to_sympy) ro�cancel�m�nrv� use_exact� dom_exact�dMi�den�Mis r�_inv_DMr�+s<�� �8�D�A�q� �)�C��A�v�v�"�#G�H�H�H�"�#�&�&�I��&��M�M�O�O� � �]�]�9� %� %��K��:�:�<�<���S�S�� %�K�K�K�&�'I�J�J�J�K�����/��n�n�S�!�!�����s�I�.�.�� �8��z�"� !��,�,�.�.�C��C�i� "� "� $� $����]�]�_�_�s�z�2�2�3�7�7� 7�� �Is �"A:�:Bc��ddlm}|jd}|dkr|�dt���S|d|dz�d|dz�f}|d|dz�|dzd�f}||dzd�d|dz�f}||dzd�|dzd�f} t |��}n,#t $r|�dt���cYSwxYw||z} | |z} || z } t | ��} n,#t $r|�dt���cYSwxYw| | z} ||z} | | z}|| | zz}|| | g||gg�����}|S)z�Calculates the inverse using BLOCKWISE inversion. See Also ======== inv inverse_ADJ inverse_GE inverse_CH inverse_LDL r)� BlockMatrix��LU�r4r?N�)�&sympy.matrices.expressions.blockmatrixr�ryrr � _inv_blockr� as_explicit)rr?r��ir)rrr,�D_inv�B_D_i�BDC�A_n�B_n�dc�C_n�D_n�nns rr�r�Ys���C�B�B�B�B�B� ��� �A��B�w�w��u�u�D�W�u�5�5�5� �'�1��6�'�6�A��E�6�/��A� �'�1��6�'�1��6�7�7� ��A� �!�q�&�'�'�7�A��F�7� ��A� �!�q�&�'�'�1��6�7�7� ��A�6��1� � ���� #�6�6�6��u�u�D�W�u�5�5�5�5�5�6���� �e�G�E� ��'�C� �c�'�C�6���o�o���� #�6�6�6��u�u�D�W�u�5�5�5�5�5�6���� �$�u�*�C� �q��B� �#�c�'�C� �"�c�T�'�/�C� ��s�C�j�3��*�-� .� .� :� :� <� <�B� �Is$�B�&C�?C�C#�#&D � D Nc��ddlm}m}|jst d���|rJ|���}g}|D],}|�|�||������-||�S|�|turt|d���} | �d}n|d vrt|d ���} |�t||��rd }nd }|dkrt| ��} n�|d krt| d���} n�|d kr|� |���} n�|dkr|� |���} n�|dkr|�|���} n�|dkr|�|���} nf|d kr|�|���} nI|dkr|�|���} n,|dkr|�|���} nt'd���|�| ��S)ac Return the inverse of a matrix using the method indicated. The default is DM if a suitable domain is found or otherwise GE for dense matrices LDL for sparse matrices. Parameters ========== method : ('DM', 'DMNC', 'GE', 'LU', 'ADJ', 'CH', 'LDL', 'QR') iszerofunc : function, optional Zero-testing function to use. try_block_diag : bool, optional If True then will try to form block diagonal matrices using the method get_diag_blocks(), invert these individually, and then reconstruct the full inverse matrix. Examples ======== >>> from sympy import SparseMatrix, Matrix >>> A = SparseMatrix([ ... [ 2, -1, 0], ... [-1, 2, -1], ... [ 0, 0, 2]]) >>> A.inv('CH') Matrix([ [2/3, 1/3, 1/6], [1/3, 2/3, 1/3], [ 0, 0, 1/2]]) >>> A.inv(method='LDL') # use of 'method=' is optional Matrix([ [2/3, 1/3, 1/6], [1/3, 2/3, 1/3], [ 0, 0, 1/2]]) >>> A * _ Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> A = Matrix(A) >>> A.inv('CH') Matrix([ [2/3, 1/3, 1/6], [1/3, 2/3, 1/3], [ 0, 0, 1/2]]) >>> A.inv('ADJ') == A.inv('GE') == A.inv('LU') == A.inv('CH') == A.inv('LDL') == A.inv('QR') True Notes ===== According to the ``method`` keyword, it calls the appropriate method: DM .... Use DomainMatrix ``inv_den`` method DMNC .... Use DomainMatrix ``inv_den`` method without cancellation GE .... inverse_GE(); default for dense matrices LU .... inverse_LU() ADJ ... inverse_ADJ() CH ... inverse_CH() LDL ... inverse_LDL(); default for sparse matrices QR ... inverse_QR() Note, the GE and LU methods may require the matrix to be simplified before it is inverted in order to properly detect zeros during pivoting. In difficult cases a custom zero detection function can be provided by setting the ``iszerofunc`` argument to a function that should return True if its argument is zero. The ADJ routine computes the determinant and uses that to detect singular matrices in addition to testing for zeros on the diagonal. See Also ======== inverse_ADJ inverse_GE inverse_LU inverse_CH inverse_LDL Raises ====== ValueError If the determinant of the matrix is zero. r)�diag� SparseMatrixr7r�NF)rn�DM)r��DMNCT�LDL�GEr�)r�rMr��ADJ�CH�QR�BLOCKzInversion method unrecognized)�sympy.matricesr�r�rCr�get_diag_blocks�appendrr rq� isinstancer�� inverse_GE� inverse_LU� inverse_ADJ� inverse_CH� inverse_LDL� inverse_QR� inverse_BLOCKr2rX) rr4r?�try_block_diagr�r��blocks�r�blockro�rvs r�_invr�s.��r2�1�1�1�1�1�1�1� �;�I�"�#G�H�H�H����"�"�$�$����� F� F�E� �H�H�U�Y�Y�f��Y�D�D� E� E� E� E��t�Q�x���~�*��/�/� �Q�u� %� %� %�� �>��F�� �>� !� !� �Q�t� $� $� $���~� �a�� &� &� ��F�F��F� ��~�~� �R�[�[��� �6� � � �R�� &� &� &��� �4��� �\�\�Z�\� 0� 0��� �4��� �\�\�Z�\� 0� 0��� �5��� �]�]�j�]� 1� 1��� �4��� �\�\�Z�\� 0� 0��� �5��� �]�]�j�]� 1� 1��� �4��� �\�\�Z�\� 0� 0��� �7� � � �_�_� �_� 3� 3����8�9�9�9� �6�6�"�:�:�r)r/)F)T)�sympy.polys.matrices.exceptionsr�sympy.polys.domainsr� exceptionsrrr� utilitiesr rrr.r5rKrOrZr_rbrerhrqrwr�r�r�r<rr�<module>r�s���F�F�F�F�F�F�"�"�"�"�"�"�S�S�S�S�S�S�S�S�S�S�������3�3�3�$���$D�D�D�@>B�>B�>B�>B�B&-� � � � �&#�����""�%�%�%�%�4"�8�8�8�8�("�+�+�+�+�"#�%�%�%�%�""�$�$�$�$�" � � � � � � �,�,�,�,�\%�$�$�$�$�L�G�E�M�M�M�M�M�Mr
Memory