� ��g����D�ddlmZddlmZddlmZmZddlmZddl m Z ddl m Z m Z ddlmZmZddlmZdd lmZdd lmZdd lmZmZmZmZmZdd lmZdd lm Z m!Z!ddl"m#Z#ddl$m%Z%m&Z&ddl'm(Z(ddl)m*Z*m+Z+ddgiZ,d�Z-d:d�Z.d�Z/ d;dddd�d�Z0dZ1 d<d�Z2 d<d�Z3e*dfd�Z4d �Z5d;d!�Z6de*fdd"�d#�Z7d:d$�Z8d:d%�Z9d&�Z:d'�Z;d(�Z<d;d)�Z=d;d*�Z>d=d+�Z?d,�Z@d-�ZAd.�ZBd/�ZCd0�ZDd1�ZEd2�ZFd3�ZGd4�ZHd5ZIeIeB_JeIeC_JeIeD_JeIeE_JeIeF_Jd;dd"�d6�ZKd7�ZLd8�ZMd9S)>�)� FunctionType)�Counter)�mp�workprec)� prec_to_dps��default_sort_key)�DEFAULT_MAXPREC�PrecisionExhausted)� fuzzy_and�fuzzy_or)�Float��_sympify)�sqrt)�roots�CRootOf�ZZ�QQ�EX)� DomainMatrix)�dom_eigenvects�dom_eigenvects_to_sympy)�gcd�)� MatrixError�NonSquareMatrixError)�_find_reasonable_pivot)�_iszero� _simplify)�_is_indefinite�_is_negative_definite�_is_negative_semidefinite�_is_positive_definite�_is_positive_semidefinite� matplotlibc�,�d�}d}td�|�t��D����}d| z}|tkr�t |��5t j|�t|�������}t j |��\}}|d�|D����}|�+t j ||z ��|kr||fcddd��S|}ddd��n #1swxYwY|dz}|tk��t�)Nc�X�tjtd�|D������S)Nc3� K�|] }|dzV�� dS)�N�)�.0�is �d/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/matrices/eigen.py� <genexpr>zA_eigenvals_eigenvects_mpmath.<locals>.<lambda>.<locals>.<genexpr>"s&����!2�!2�1�!�Q�$�!2�!2�!2�!2�!2�!2�)rr�sum)�vs r.�<lambda>z._eigenvals_eigenvects_mpmath.<locals>.<lambda>"s'��b�g�c�!2�!2��!2�!2�!2�2�2�3�3�r0c3�$K�|] }|jV�� dS�N)�_prec�r,�xs r.r/z/_eigenvals_eigenvects_mpmath.<locals>.<genexpr>%s$����/�/�1�q�w�/�/�/�/�/�/r0r*)�nc�h�g|]/}tj|��tj|��fD]}|���0Sr+)r�re�im)r,�er-s r.� <listcomp>z0_eigenvals_eigenvects_mpmath.<locals>.<listcomp>,s:��C�C�C�a�r�u�Q�x�x���q���.B�C�C���C�C�C�Cr0) �max�atomsrr rr�matrix�evalfr�eig�fabsr ) �M�norm2�v1�prec�eps�A�E�ER�v2s r.�_eigenvals_eigenvects_mpmathrN!ss�� 3� 3�E� �B� �/�/�������/�/�/� /� /�D� �d�U�(�C� �� � � �d�^�^� � �� �!�'�'�K��$5�$5�'�6�6�7�7�A��F�1�I�I�E�A�r���C�C�1�C�C�C�D�D�B��~�"�'�"�r�'�"2�"2�S�"8�"8��"�u�  � � � � � � � � �B�  � � � � � � � � � � ���� � � � � �� �� �� � � �s�BC4�&C4�4C8�;C8Fc�~�t|��\}}d�|D��}|r|Stt|����S)z Compute eigenvalues using mpmathc�,�g|]}t|����Sr+rr7s r.r>z%_eigenvals_mpmath.<locals>.<listcomp>>s�� %� %� %�a�h�q�k�k� %� %� %r0)rN�dictr)rE�multiplerK�_�results r.�_eigenvals_mpmathrU;sG�� '�� *� *�D�A�q� %� %�1� %� %� %�F���� � ����� � � r0c���t|��\}}g}t|j��D]I}t||��}t|dd�|f��}|�|d|gf���J|S)Nr)rN�range�rowsr�append)rErKrLrTr-�eigenval� eigenvects r.�_eigenvects_mpmathr\Dsz�� (�� +� +�E�A�r� �F� �1�6�]�]�2�2���A�a�D�>�>���R����1��X�&�&� �� � �x��Y�K�0�1�1�1�1� �Mr0T)�simplifyrR�rationalc ���|s|rgSiS|js"td�|�����|jjt t fvrDtd�|D����r+|�t��rt||���S|rddl m �|� �fd���}|rt|f||d�|��St|f||d�|��S)a�Compute eigenvalues of the matrix. Parameters ========== error_when_incomplete : bool, optional If it is set to ``True``, it will raise an error if not all eigenvalues are computed. This is caused by ``roots`` not returning a full list of eigenvalues. simplify : bool or function, optional If it is set to ``True``, it attempts to return the most simplified form of expressions returned by applying default simplification method in every routine. If it is set to ``False``, it will skip simplification in this particular routine to save computation resources. If a function is passed to, it will attempt to apply the particular function as simplification method. rational : bool, optional If it is set to ``True``, every floating point numbers would be replaced with rationals before computation. It can solve some issues of ``roots`` routine not working well with floats. multiple : bool, optional If it is set to ``True``, the result will be in the form of a list. If it is set to ``False``, the result will be in the form of a dictionary. Returns ======= eigs : list or dict Eigenvalues of a matrix. The return format would be specified by the key ``multiple``. Raises ====== MatrixError If not enough roots had got computed. NonSquareMatrixError If attempted to compute eigenvalues from a non-square matrix. Examples ======== >>> from sympy import Matrix >>> M = Matrix(3, 3, [0, 1, 1, 1, 0, 0, 1, 1, 1]) >>> M.eigenvals() {-1: 1, 0: 1, 2: 1} See Also ======== MatrixBase.charpoly eigenvects Notes ===== Eigenvalues of a matrix $A$ can be computed by solving a matrix equation $\det(A - \lambda I) = 0$ It's not always possible to return radical solutions for eigenvalues for matrices larger than $4, 4$ shape due to Abel-Ruffini theorem. If there is no radical solution is found for the eigenvalue, it may return eigenvalues in the form of :class:`sympy.polys.rootoftools.ComplexRootOf`. z{} must be a square matrix.c3�$K�|] }|jV�� dSr5�� is_numberr7s r.r/z_eigenvals.<locals>.<genexpr>��$����&�&�q�q�{�&�&�&�&�&�&r0�rRr�� nsimplifyc�V��|�t��r �|d���n|S�NT)r^)�hasr�r8rfs �r.r3z_eigenvals.<locals>.<lambda>�s*���Q�U�U�5�\�\�H�i�i��D�1�1�1�1�q�r0)�error_when_incompleter])� is_squarer�format�_rep�domainrr�allrirrU�sympy.simplifyrf� applyfunc�_eigenvals_list�_eigenvals_dict)rErkr]rRr^�flagsrfs @r.� _eigenvalsrvPs>���` �� � ��I�� � �;�L�"�#@�#G�#G��#J�#J�K�K�K��v�}�R��H�$�$� �&�&�A�&�&�&� &� &� ;�1�5�5��<�<� ;�$�Q��:�:�:� :��J�,�,�,�,�,�,� �K�K� H� H� H� H� J� J����� ��%:�X������ � � � �!6�� � � � � �r0adIt is not always possible to express the eigenvalues of a matrix of size 5x5 or higher in radicals. We have CRootOf, but domains other than the rationals are not currently supported. If there are no symbols in the matrix, it should still be possible to compute numeric approximations of the eigenvalues using M.evalf().eigenvals() or M.charpoly().nroots().c ���|���}g}|jjttfv}|D]�}|r;t |��dkr(|d}|||f} |�| ���?|||f} t�t��r| � ����} n| � ��} t| fddi|��} t | ��| j kr@ | � d���} n(#t$r|rtt���g} YnwxYw|| z }���s|St�t��st ��fd�|D��S)Nrr�r]rRTrdc�&��g|] }�|����Sr+r+)r,�valuer]s �r.r>z#_eigenvals_list.<locals>.<listcomp>�s!��� 2� 2� 2��H�H�U�O�O� 2� 2� 2r0)�strongly_connected_componentsrnrorr�lenrY� isinstancer�charpolyrrX� all_roots�NotImplementedErrorr�eigenvals_error_messager ) rErkr]ru�iblocks�all_eigs�is_dom�b�index�val�blockr~�eigss ` r.rsrs�s�����-�-�/�/�G��H� �V�]�r�2�h� &�F� ���� � �c�!�f�f��k�k��a�D�E��E�5�L�/�C� �O�O�C� � � � ��!�Q�$��� �h� � -� -� (��~�~�x�~�8�8�H�H��~�~�'�'�H��X�6�6��6��6�6�� �t�9�9�� � "� "� ��)�)�4�)�8�8����&� � � �(��%�&=�>�>�>��D�D�D�  ���� �D���� ���� �h� � -� -���� 2� 2� 2� 2�� 2� 2� 2�2s�%C<�<"D!� D!c ���|���}i}|jjttfv}|D�]K}|rBt |��dkr/|d}|||f} |�| d��dz|| <�G|||f} t�t��r| � ����} n| � ��} t| fddi|��} t| � ����| j krM t| �d�����} n(#t $r|rt#t$���i} YnwxYw| ���D]\} }| |vr|| xx|z cc<�||| <� ��M�s|St�t��st(��fd�|���D��S)NrrrxrRFrdc�.��i|]\}}�|��|��Sr+r+)r,�keyrzr]s �r.� <dictcomp>z#_eigenvals_dict.<locals>.<dictcomp>s'��� D� D� D�Z�S�%�H�H�S�M�M�5� D� D� Dr0)r{rnrorrr|�getr}rr~rr1�valuesrXrQrr�rr��itemsr )rErkr]rur�r�r�r�r�r�r�r~r��kr2s ` r.rtrt�s�����-�-�/�/�G��H� �V�]�r�2�h� &�F� � � �� � �c�!�f�f��k�k��a�D�E��E�5�L�/�C�$�L�L��a�0�0�1�4�H�S�M� ��!�Q�$��� �h� � -� -� (��~�~�x�~�8�8�H�H��~�~�'�'�H��X�7�7��7��7�7�� �t�{�{�}�}� � ��� +� +� ��H�.�.��.�>�>�?�?����&� � � �(��%�&=�>�>�>��D�D�D�  ���� �J�J�L�L� � �D�A�q��H�}�}��� � � �q� � � � � ���� � �  � ���� �h� � -� -���� D� D� D� D�8�>�>�3C�3C� D� D� D�Ds�?#D#�#"E�Ec�4�||�|j��|zz }|�|���}t|��dkr|r|�|d���}t|��dkr"t d�|�����|S)z:Get a basis for the eigenspace for a particular eigenvalue)� iszerofuncrT�r�r]z,Can't evaluate eigenvector for eigenvalue {})�eyerX� nullspacer|r�rm)rErZr�r]�m�rets r.� _eigenspacer�s��� �a�e�e�A�F�m�m�h�&� &�A� �+�+��+� ,� ,�C� �3�x�x�1�}�}��}��k�k�Z�$�k�?�?�� �3�x�x�1�}�}�!� :� A� A�(� K� K�M�M� M� �Jr0c ���tj|dd���}|���}|jtkr:t |��\}}t |||jfi|��}t|d����}|SdS)NT)�field� extensionc�,�t|d��S)Nrr�r8s r.r3z!_eigenvects_DOM.<locals>.<lambda>3s��6F�q��t�6L�6L�r0�r�) r� from_Matrix�to_denserorrr� __class__�sorted)rE�kwargs�DOMr^� algebraic� eigenvectss r.�_eigenvects_DOMr�+s��� � "�1�D�D� A� A� A�C� �,�,�.�.�C� �z�R���,�S�1�1���)�,� �i���8�8�06�8�8� ��J�,L�,L�M�M�M� ��� �4r0c �:�|jdddi|��}|D]+}|�t��rtd����,t |���t ���}g}|D]0\}}t||||���} |�||| f���1|S)Nr^FzYEigenvector computation is not implemented if the matrix have eigenvalues in CRootOf formr�r�r+) � eigenvalsrirrr�r�r r�rY) rEr�r]rur�r8r�r��mult�vectss r.�_eigenvects_sympyr�9s����� �4�4�U�4�e�4�4�I��/�/�� �5�5��>�>� /��.�/�/� /� /� �y���(�(�.>�?�?�?�I� �C��'�'� ��T��A�s�z�H�M�M�M�� � � �C��u�%�&�&�&�&� �Jr0��chopc �P�� � � �|�dd��}|�dd��}|�dd��|�dd��t|t��s |rtnd�� |�t ��}|rFtd�|D����rt|��Sdd l m � |� � fd ���}t|��}|�t||fd|i|��}|r� fd �� � fd �|D��}|r�fd �|D��}|S)a�Compute eigenvectors of the matrix. Parameters ========== error_when_incomplete : bool, optional Raise an error when not all eigenvalues are computed. This is caused by ``roots`` not returning a full list of eigenvalues. iszerofunc : function, optional Specifies a zero testing function to be used in ``rref``. Default value is ``_iszero``, which uses SymPy's naive and fast default assumption handler. It can also accept any user-specified zero testing function, if it is formatted as a function which accepts a single symbolic argument and returns ``True`` if it is tested as zero and ``False`` if it is tested as non-zero, and ``None`` if it is undecidable. simplify : bool or function, optional If ``True``, ``as_content_primitive()`` will be used to tidy up normalization artifacts. It will also be used by the ``nullspace`` routine. chop : bool or positive number, optional If the matrix contains any Floats, they will be changed to Rationals for computation purposes, but the answers will be returned after being evaluated with evalf. The ``chop`` flag is passed to ``evalf``. When ``chop=True`` a default precision will be used; a number will be interpreted as the desired level of precision. Returns ======= ret : [(eigenval, multiplicity, eigenspace), ...] A ragged list containing tuples of data obtained by ``eigenvals`` and ``nullspace``. ``eigenspace`` is a list containing the ``eigenvector`` for each eigenvalue. ``eigenvector`` is a vector in the form of a ``Matrix``. e.g. a vector of length 3 is returned as ``Matrix([a_1, a_2, a_3])``. Raises ====== NotImplementedError If failed to compute nullspace. Examples ======== >>> from sympy import Matrix >>> M = Matrix(3, 3, [0, 1, 1, 1, 0, 0, 1, 1, 1]) >>> M.eigenvects() [(-1, 1, [Matrix([ [-1], [ 1], [ 0]])]), (0, 1, [Matrix([ [ 0], [-1], [ 1]])]), (2, 1, [Matrix([ [2/3], [1/3], [ 1]])])] See Also ======== eigenvals MatrixBase.nullspace r]TFNrRc��|Sr5r+r�s r.r3z_eigenvects.<locals>.<lambda>�s���r0c3�$K�|] }|jV�� dSr5rar7s r.r/z_eigenvects.<locals>.<genexpr>�rcr0rrec����|d���Srhr+rjs �r.r3z_eigenvects.<locals>.<lambda>�s���)�)�A��"=�"=�"=�r0c� ���fd�|D��S)Nc�t��g|]4}|tt|����z ������5Sr+)r�listrr)r,r2�simpfuncs �r.r>z4_eigenvects.<locals>.denom_clean.<locals>.<listcomp>�s8���F�F�F�q�Q��T�!�W�W���%�0�0��:�:�F�F�Fr0r+)�lr�s �r.� denom_cleanz _eigenvects.<locals>.denom_clean�s���F�F�F�F�A�F�F�F� Fr0c�4��g|]\}}}||�|��f��Sr+r+)r,r�r��esr�s �r.r>z_eigenvects.<locals>.<listcomp>�s.���E�E�E� ��T�2��T�;�;�r�?�?�+�E�E�Er0c�b��g|]+\}}}|�����|�fd�|D��f��,S)r�c�<��g|]}|�������S)r��rB)r,r2r�s �r.r>z*_eigenvects.<locals>.<listcomp>.<listcomp>�s'���,L�,L�,L�A�Q�W�W�$�W�-?�-?�,L�,L�,Lr0r�)r,r�r�r�r�s �r.r>z_eigenvects.<locals>.<listcomp>�sY���*�*�*�!�C��r�� � �t� �$�$�d�,L�,L�,L�,L��,L�,L�,L�M�*�*�*r0)r��popr}rr rirrpr\rqrfrrr�r�) rErkr�r�rur]� primitive� has_floatsr�r�rfr�s ` @@@r.� _eigenvectsr�Ls�������X�y�y��T�*�*�H�� � �*�e�,�,�I� �I�I�j�$���� �I�I�j�$���� �h� � -� -�:� (�9�9�9�k�k�����u���J��?� �&�&�A�&�&�&� &� &� )�%�a�(�(� (�,�,�,�,�,�,� �K�K�=�=�=�=� >� >�� �!� � �C� �{���:�J�J��J�E�J�J���F� G� G� G� G� G�F�E�E�E��E�E�E���*�*�*�*�*�%(�*�*�*�� �Jr0c��|jsdgfS|�d���}|D].\}}}|r |jsd|fcS|t|��krd|fcS�/d|fS)z�See _is_diagonalizable. This function returns the bool along with the eigenvectors to avoid calculating them again in functions like ``diagonalize``.FTrx)rlr��is_realr|)rE� reals_only� eigenvecsr�r��basiss r.�_is_diagonalizable_with_eigenr��s��� �;���b�y��� � �d� �+�+�I�%�$�$���T�5� � $�c�k� $��)�#� #� #� #� �3�u�:�:� � ��)�#� #� #� #� � ��?�r0c ���|jsdStd�|D����r|���rdStd�|D����r |jrdSt ||���dS)a�Returns ``True`` if a matrix is diagonalizable. Parameters ========== reals_only : bool, optional If ``True``, it tests whether the matrix can be diagonalized to contain only real numbers on the diagonal. If ``False``, it tests whether the matrix can be diagonalized at all, even with numbers that may not be real. Examples ======== Example of a diagonalizable matrix: >>> from sympy import Matrix >>> M = Matrix([[1, 2, 0], [0, 3, 0], [2, -4, 2]]) >>> M.is_diagonalizable() True Example of a non-diagonalizable matrix: >>> M = Matrix([[0, 1], [0, 0]]) >>> M.is_diagonalizable() False Example of a matrix that is diagonalized in terms of non-real entries: >>> M = Matrix([[0, 1], [-1, 0]]) >>> M.is_diagonalizable(reals_only=False) True >>> M.is_diagonalizable(reals_only=True) False See Also ======== sympy.matrices.matrixbase.MatrixBase.is_diagonal diagonalize Fc3�$K�|] }|jV�� dSr5)r��r,r=s r.r/z%_is_diagonalizable.<locals>.<genexpr>�s$���� � ��1�9� � � � � � r0Tc3�$K�|] }|jV�� dSr5)� is_complexr�s r.r/z%_is_diagonalizable.<locals>.<genexpr>s$���� #� #�A�1�<� #� #� #� #� #� #r0�r�r)rlrp� is_symmetric� is_hermitianr�)rEr�r�s r.�_is_diagonalizabler��s���X �;���u� � � �a� � � � � ��Q�^�^�%5�%5���t� � #� #�� #� #� #�#�#������t� (��z� B� B� B�1� E�Er0c�Z�|jdkstd���|���}|���}|���}|dt|d��z }|���}|d||zz|d<|d||zz |d<|dd�df���dkrd}d|d<nT|���|���kr|}n|}||dz }d|���dzz }||fS)NrzInput must be a column matrix)rrrr*)�cols� ValueError�copy�abs�norm)r8r2�v_plus�v_minus�q�norm_x�bets r.�_householder_vectorr�s�� �6�Q�;�;��8�9�9�9� �����A� �V�V�X�X�F��f�f�h�h�G� �$��#�a��g�,�,��A� �V�V�X�X�F��T�7�Q��Z�'�F�4�L��d�G�a�&�j�(�G�D�M�����Q��x�}�}���!�������$��� �;�;�=�=�G�L�L�N�N� *� *��A�A��A� ��!��H���1�6�6�8�8�q�=�!�� �c�6�Mr0c�2�|j}|j}|���}|�|��|�|��}}t t ||����D�](}t ||d�|f��\}}|�||z ��||z|jzz } | ||d�|d�fz||d�|d�f<|�|��} | | |d�|d�f<|| z}|dz|dz kr�t |||dzd�fj��\}}|�||z dz ��||z|jzz } ||d�|dzd�f| z||d�|dzd�f<|�|��} | | |dzd�|dzd�f<| |z}��*|||fS�Nrr*) rXr�� as_mutabler�rW�minr��H�T) rEr�r9rJ�U�Vr-r2r��hh_mat�temps r.�_bidiagonal_decmp_hholderr�s��� ��A� ��A� � � ���A� �5�5��8�8�Q�U�U�1�X�X�q�A� �3�q�!�9�9� � � � ��$�Q�q�r�r�1�u�X�.�.���3����q�1�u����a��!�#� �-���Q�q�r�r�1�2�2�v�Y�&��!�"�"�a�b�b�&� ��u�u�Q�x�x����Q�R�R����V� � ��H�� �q�5�A��E�>�>�(��1�a��c�d�d�7���6�6�F�A�s��U�U�1�q�5�1�9�%�%��a��!�#� �5�F��A�B�B��!����H�+��.�A�a�b�b�!�A�#�$�$�h�K��5�5��8�8�D�%�D��1����q��s�t�t�� ��q��A�� �a��7�Nr0c�4�|j}|j}|���}tt ||����D]�}t ||d�|f��\}}|�||z ��||z|jzz }|||d�|d�fz||d�|d�f<|dz|dz krmt |||dzd�fj��\}}|�||z dz ��||z|jzz }||d�|dzd�f|z||d�|dzd�f<��|Sr�) rXr�r�rWr�r�r�r�r�)rEr�r9rJr-r2r�r�s r.�_eval_bidiag_hholderr�4s9�� ��A� ��A� � � ���A� �3�q�!�9�9� � �/�/��$�Q�q�r�r�1�u�X�.�.���3����q��s���c�A�g���m�+���Q�q�r�r�1�2�2�v�Y�&��!�"�"�a�b�b�&� � �q�5�A��E�>�>�(��1�a��c�d�d�7���6�6�F�A�s��U�U�1�q�5�1�9�%�%��a��!�#� �5�F��A�B�B��!����H�+��.�A�a�b�b�!�A�#�$�$�h�K�� �Hr0c���t|t��std���|rt|��St|j��}|dj|dj|djfS)a/ Returns $(U,B,V.H)$ for $$A = UBV^{H}$$ where $A$ is the input matrix, and $B$ is its Bidiagonalized form Note: Bidiagonal Computation can hang for symbolic matrices. Parameters ========== upper : bool. Whether to do upper bidiagnalization or lower. True for upper and False for lower. References ========== .. [1] Algorithm 5.4.2, Matrix computations by Golub and Van Loan, 4th edition .. [2] Complex Matrix Bidiagonalization, https://github.com/vslobody/Householder-Bidiagonalization �upper must be a booleanr*rr)r}�boolr�r�r�)rE�upper�Xs r.�_bidiagonal_decompositionr�Csi��0 �e�T� "� "�4��2�3�3�3� �,�(��+�+�+�!�!�#�&�&�A� �Q�4�6�1�Q�4�6�1�Q�4�6� !�!r0c��t|t��std���|rt|��St|j��jS)a� Returns $B$, the Bidiagonalized form of the input matrix. Note: Bidiagonal Computation can hang for symbolic matrices. Parameters ========== upper : bool. Whether to do upper bidiagnalization or lower. True for upper and False for lower. References ========== .. [1] Algorithm 5.4.2, Matrix computations by Golub and Van Loan, 4th edition .. [2] Complex Matrix Bidiagonalization : https://github.com/vslobody/Householder-Bidiagonalization r�)r}r�r�r�r�)rEr�s r.�_bidiagonalizer�esN��( �e�T� "� "�4��2�3�3�3� �'�#�A�&�&�&� ��� $� $� &�&r0c� �|jst���t||���\}}|std���|rt |t ���}gg}}|D]\}} } ||g| zz }|| z }�|r d�|D��}|j|�|j|�fS)a� Return (P, D), where D is diagonal and D = P^-1 * M * P where M is current matrix. Parameters ========== reals_only : bool. Whether to throw an error if complex numbers are need to diagonalize. (Default: False) sort : bool. Sort the eigenvalues along the diagonal. (Default: False) normalize : bool. If True, normalize the columns of P. (Default: False) Examples ======== >>> from sympy import Matrix >>> M = Matrix(3, 3, [1, 2, 0, 0, 3, 0, 2, -4, 2]) >>> M Matrix([ [1, 2, 0], [0, 3, 0], [2, -4, 2]]) >>> (P, D) = M.diagonalize() >>> D Matrix([ [1, 0, 0], [0, 2, 0], [0, 0, 3]]) >>> P Matrix([ [-1, 0, -1], [ 0, 0, -1], [ 2, 1, 2]]) >>> P.inv() * M * P Matrix([ [1, 0, 0], [0, 2, 0], [0, 0, 3]]) See Also ======== sympy.matrices.matrixbase.MatrixBase.is_diagonal is_diagonalizable r�zMatrix is not diagonalizabler�c�<�g|]}||���z ��Sr+)r�)r,r2s r.r>z _diagonalize.<locals>.<listcomp>�s$��/�/�/�1�!�a�f�f�h�h�,�/�/�/r0)rlrr�rr�r �hstack�diag) rEr��sort� normalize�is_diagonalizabler��p_colsr�r�r�r�s r.� _diagonalizer��s���h �;�%�"�$�$�$�#@��%�$'�$'�$'� ��y� �:��8�9�9�9� �<��9�*:�;�;�;� ��r�D�F�%�����T�5� �3�%�$�,����%�����0�/�/��/�/�/�� �1�8�V� �f�a�f�d�m� +�+r0c�P�|���}|durdS|r |jrdSdS�NFT)�_has_positive_diagonals�is_strongly_diagonally_dominant)rE�positive_diagonalss r.�_fuzzy_positive_definiter�s@���2�2�4�4���U�"�"��u���a�?���t� �4r0c�P�|���}|durdS|r |jrdSdSr�)�_has_nonnegative_diagonals�is_weakly_diagonally_dominant)rE�nonnegative_diagonalss r.�_fuzzy_positive_semidefiniter�s@���8�8�:�:����%�%��u����!@���t� �4r0c�z�|js|jsdS||jz}t|��}|�|St |��S�NF)r�rlr�r�_is_positive_definite_GE�rE�fuzzys r.r$r$�sL�� �>���{� ��5� ���G�� $�Q� '� '�E� ��� � #�A� &� &�&r0c�z�|js|jsdS||jz}t|��}|�|St |��Sr)r�rlr�r�"_is_positive_semidefinite_choleskyr s r.r%r%�sL�� �>���{� ��5� ���G�� (�� +� +�E� ��� � -�a� 0� 0�0r0c�"�t| ��Sr5)r$�rEs r.r"r"�s�� �!�� $� $�$r0c�"�t| ��Sr5)r%rs r.r#r#s�� $�a�R� (� (�(r0c�>�|jr|���}d�|���D��}t|��}d�|���D��}t|��}t ||g��S|jr||jzjSdS)Nc��g|] }|j�� Sr+)� is_positiver7s r.r>z"_is_indefinite.<locals>.<listcomp>���<�<�<�!�� �<�<�<r0c��g|] }|j�� Sr+)� is_negativer7s r.r>z"_is_indefinite.<locals>.<listcomp> rr0F)r�r��keysr r rlr�� is_indefinite)rE�eigen�args1� any_positive�args2� any_negatives r.r!r!s����~� '�� � � � ��<�<�u�z�z�|�|�<�<�<������ �<�<�u�z�z�|�|�<�<�<������ ��,� �5�6�6�6� ��'��A�C��&�&� �5r0c�8�|���}|j}t|��D]n}|||fj}|dur|cSt|dz|��D]A}|||f|||dzd�fz|||f|||dzd�fzz |||dzd�f<�B�odS)zRA division-free gaussian elimination method for testing positive-definiteness.TrN)r�rXrWr)rE�sizer-r�js r.r r s��� � � ���A� �6�D� �4�[�[�E�E����1��g�)� � �d� "� "�� � � ��q��s�D�!�!� E� E�A��1�a�4��1�Q��!����W�:�-��!�Q�$��!�A�q��s�t�t�G�*�0D�D�A�a��1����g�J�J� E� �4r0c �`�������t�j��D�]w}�fd�t|�j��D��}t|��\}}}}|rdS|�Ut|dz�j��D]9}t|�j��D]!}�||fj} | �dS| durdS�"�:dS�||fjs|jrdS�||fjr|jsdS|dkr2��|||z���� |||z��t�||f���||f<�||dzd�fxx�||fzcc<�|dzd�|dzd�fxx�||dzd�fj �||dzd�fzzcc<��y�djS)z�Uses Cholesky factorization with complete pivoting References ========== .. [1] http://eprints.ma.man.ac.uk/1199/1/covered/MIMS_ep2008_116.pdf .. [2] https://www.value-at-risk.net/cholesky-factorization/ c�$��g|] }�||f�� Sr+r+)r,r-rEs �r.r>z6_is_positive_semidefinite_cholesky.<locals>.<listcomp>0s!���3�3�3�Q��1�a�4��3�3�3r0NrFTr)�����r#) r�rWrXrr��is_zeror�is_nonnegative�col_swap�row_swaprr�) rEr��diags�pivot� pivot_val�nonzerorSr-r �iszeros ` r.r r $s��� � � ���A� �1�6�]�]�3�3��3�3�3�3�%��1�6�"2�"2�3�3�3��'=�e�'D�'D�$��y�'�1� � ��4�4� �=��1�Q�3���'�'� %� %���q�!�&�)�)�%�%�A��q�!�t�W�_�F��~�#�t�t�t�t��5���$�u�u�u�u�)� %� �4�4� �Q��T�7� � �)�"7� ��5�5��A�q�D�'�(� �Y�-E� ��4�4� �1�9�9� �J�J�q�!�E�'� "� "� "� �J�J�q�!�E�'� "� "� "��q��A��w�-�-��!�Q�$�� �!�Q�q�S�T�T�'� � � �a��1��g�� � � � �!�A�#�$�$��!����*� � � ��1�a��c�d�d�7����!�Q�q�S�T�T�'� �2�2� � � � � �V�9� #�#r0aPFinds out the definiteness of a matrix. Explanation =========== A square real matrix $A$ is: - A positive definite matrix if $x^T A x > 0$ for all non-zero real vectors $x$. - A positive semidefinite matrix if $x^T A x \geq 0$ for all non-zero real vectors $x$. - A negative definite matrix if $x^T A x < 0$ for all non-zero real vectors $x$. - A negative semidefinite matrix if $x^T A x \leq 0$ for all non-zero real vectors $x$. - An indefinite matrix if there exists non-zero real vectors $x, y$ with $x^T A x > 0 > y^T A y$. A square complex matrix $A$ is: - A positive definite matrix if $\text{re}(x^H A x) > 0$ for all non-zero complex vectors $x$. - A positive semidefinite matrix if $\text{re}(x^H A x) \geq 0$ for all non-zero complex vectors $x$. - A negative definite matrix if $\text{re}(x^H A x) < 0$ for all non-zero complex vectors $x$. - A negative semidefinite matrix if $\text{re}(x^H A x) \leq 0$ for all non-zero complex vectors $x$. - An indefinite matrix if there exists non-zero complex vectors $x, y$ with $\text{re}(x^H A x) > 0 > \text{re}(y^H A y)$. A matrix need not be symmetric or hermitian to be positive definite. - A real non-symmetric matrix is positive definite if and only if $\frac{A + A^T}{2}$ is positive definite. - A complex non-hermitian matrix is positive definite if and only if $\frac{A + A^H}{2}$ is positive definite. And this extension can apply for all the definitions above. However, for complex cases, you can restrict the definition of $\text{re}(x^H A x) > 0$ to $x^H A x > 0$ and require the matrix to be hermitian. But we do not present this restriction for computation because you can check ``M.is_hermitian`` independently with this and use the same procedure. Examples ======== An example of symmetric positive definite matrix: .. plot:: :context: reset :format: doctest :include-source: True >>> from sympy import Matrix, symbols >>> from sympy.plotting import plot3d >>> a, b = symbols('a b') >>> x = Matrix([a, b]) >>> A = Matrix([[1, 0], [0, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1)) An example of symmetric positive semidefinite matrix: .. plot:: :context: close-figs :format: doctest :include-source: True >>> A = Matrix([[1, -1], [-1, 1]]) >>> A.is_positive_definite False >>> A.is_positive_semidefinite True >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1)) An example of symmetric negative definite matrix: .. plot:: :context: close-figs :format: doctest :include-source: True >>> A = Matrix([[-1, 0], [0, -1]]) >>> A.is_negative_definite True >>> A.is_negative_semidefinite True >>> A.is_indefinite False >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1)) An example of symmetric indefinite matrix: .. plot:: :context: close-figs :format: doctest :include-source: True >>> A = Matrix([[1, 2], [2, -1]]) >>> A.is_indefinite True >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1)) An example of non-symmetric positive definite matrix. .. plot:: :context: close-figs :format: doctest :include-source: True >>> A = Matrix([[1, 2], [-2, 1]]) >>> A.is_positive_definite True >>> A.is_positive_semidefinite True >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1)) Notes ===== Although some people trivialize the definition of positive definite matrices only for symmetric or hermitian matrices, this restriction is not correct because it does not classify all instances of positive definite matrices from the definition $x^T A x > 0$ or $\text{re}(x^H A x) > 0$. For instance, ``Matrix([[1, 2], [-2, 1]])`` presented in the example above is an example of real positive definite matrix that is not symmetric. However, since the following formula holds true; .. math:: \text{re}(x^H A x) > 0 \iff \text{re}(x^H \frac{A + A^H}{2} x) > 0 We can classify all positive definite matrices that may or may not be symmetric or hermitian by transforming the matrix to $\frac{A + A^T}{2}$ or $\frac{A + A^H}{2}$ (which is guaranteed to be always real symmetric or complex hermitian) and we can defer most of the studies to symmetric or hermitian positive definite matrices. But it is a different problem for the existence of Cholesky decomposition. Because even though a non symmetric or a non hermitian matrix can be positive definite, Cholesky or LDL decomposition does not exist because the decompositions require the matrix to be symmetric or hermitian. References ========== .. [1] https://en.wikipedia.org/wiki/Definiteness_of_a_matrix#Eigenvalues .. [2] https://mathworld.wolfram.com/PositiveDefiniteMatrix.html .. [3] Johnson, C. R. "Positive Definite Matrices." Amer. Math. Monthly 77, 259-264 1970. c�F��������� �!��jstd�������t����r\ t d�����D����}n#t $rd}YnwxYwt t|��d������fd�}i����fd����fd�}d�}�fd �}�rd d lm � �� � fd ������ ��}|D]+} | �t��rtd ����,t|������jkrct#|���t$���} �j| �} |s || ��S�fd�| D��} �j| �} || | ��Sg}t#|���t$���D]o�|�}|�|��}||��}d�t+|��D��}|���|��fd�|D�����pt1d�|D����}|�jkr"td��������fd�|D��} �j| �} |s || ��Sg} t#|���t$���D]��g}|D]�\}}|�kr� ��|�����}��|dz �����}|||z|���!���!fd�t9|��D��}|�|��| �t;|���������j| �} || | ��S)aReturn $(P, J)$ where $J$ is a Jordan block matrix and $P$ is a matrix such that $M = P J P^{-1}$ Parameters ========== calc_transform : bool If ``False``, then only $J$ is returned. chop : bool All matrices are converted to exact types when computing eigenvalues and eigenvectors. As a result, there may be approximation errors. If ``chop==True``, these errors will be truncated. Examples ======== >>> from sympy import Matrix >>> M = Matrix([[ 6, 5, -2, -3], [-3, -1, 3, 3], [ 2, 1, -2, -3], [-1, 1, 5, 5]]) >>> P, J = M.jordan_form() >>> J Matrix([ [2, 1, 0, 0], [0, 2, 0, 0], [0, 0, 2, 1], [0, 0, 0, 2]]) See Also ======== jordan_block z&Only square matrices have Jordan formsc3�NK�|] }t|t���|jV��!dSr5)r}rr6)r,�terms r.r/z_jordan_form.<locals>.<genexpr>0s4����X�X�$� �4�QV�@W�@W�X�4�:�X�X�X�X�X�Xr0�5�c�`���r��fd�|D��}t|��dkr|dS|S)zMIf ``has_floats`` is `True`, cast all ``args`` as matrices of floats.c�>��g|]}|��������S))r9r�r�)r,r�r��max_dpss ��r.r>z8_jordan_form.<locals>.restore_floats.<locals>.<listcomp>?s)���@�@�@�a�A�G�G�g�D�G�1�1�@�@�@r0rr)r|)�argsr�r�r4s ���r.�restore_floatsz$_jordan_form.<locals>.restore_floats:sG��� � A�@�@�@�@�@�4�@�@�@�D� �t�9�9��>�>���7�N�� r0c���||f�vr �||fS||dz f�vr0�||dz f��|dfd����||f<n8�|���j��zz �|���||f<�||fS)zFCache computations of ``(M - val*I)**pow`` for quick retrievalrN�� dotprodsimp)�multiplyr�rX�pow)r�r;rE�mat� mat_caches ���r.�eig_matz_jordan_form.<locals>.eig_matHs���� ��:�� "� "��c�3�Z�(� (� ��q��>�Y� &� &�$-�s�C�!�G�n�$=�$F�$F��s�A�h�'�T�%G�%;�%;�I�s�C�j� !� !�&)�3�q�u�u�Q�V�}�}�+<�%<�$A�$A�#�$F�$F�I�s�C�j� !��#�s��$�$r0c�~���j}dg}|�|d�����z }d}||dkr�|�|��||krnf|�||�����z }|dz }||dks||kr"td�������||dk��|S)zpCalculate the sequence [0, nullity(E), nullity(E**2), ...] until it is constant where ``E = M - val*I``rrr*r#zMSymPy had encountered an inconsistent result while computing Jordan block: {})r��rankrYrrm)r��algebraic_multiplicityr�r��nullityr-rEr>s ��r.� nullity_chainz#_jordan_form.<locals>.nullity_chainXs���� �&���#������a���-�-�/�/�/������R�� � � �J�J�w� � � ��0�0�0���g�g�c�1�o�o�2�2�4�4�4�G� �q�L�A� ��R�� � �G�.D�$D�$D�!�������$�$�$���R�� � �$� r0c�����fd�tdt���dz ��D��}t���dkr�d�dz gn�dg}||zS)z�Return a list of the size of each Jordan block. If d_n is the nullity of E**n, then the number of Jordan blocks of size n is 2*d_n - d_(n-1) - d_(n+1)c�V��g|]%}d�|z�|dz z �|dzz ��&S)r*rr+)r,r9�ds �r.r>zC_jordan_form.<locals>.blocks_from_nullity_chain.<locals>.<listcomp>s;���J�J�J��q��1��v��!�a�%�� �1�Q��U�8�+�J�J�Jr0rr#�����r)rWr|)rF�mid�ends` r.�blocks_from_nullity_chainz/_jordan_form.<locals>.blocks_from_nullity_chainwsj���K�J�J�J�U�1�c�!�f�f�q�j�5I�5I�J�J�J��"%�Q���!���q��u�q��u�}�o�o�!�A�$����S�y�r0c����t|��dkr|dS|D]D}�j||gz��d���\}}|dt|��kr|cS�EdS)zWPicks a vector from big_basis that isn't in the subspace spanned by small_basisrT)� with_pivotsr#N)r|r�� echelon_form)� small_basis� big_basisr2rS�pivotsrEs �r.�pick_vecz_jordan_form.<locals>.pick_vec�s���� �{� � �q� � ��Q�<� �� � �A� ���;�!��#4�6�C�C� $�D�&�&�I�A�v��b�z�S��-�-�-�-�����.�  � r0rrec����|d���Srhr+rjs �r.r3z_jordan_form.<locals>.<lambda>�s���i�i��D�&A�&A�&A�r0zTJordan normal form is not implemented if the matrix have eigenvalues in CRootOf formr�c�X��g|]&}�|d�����d��'S)rr)r�)r,rCr>s �r.r>z _jordan_form.<locals>.<listcomp>�sD���#�#�#�� ���Q���1�1�3�3�A�6�#�#�#r0c�"�g|] \}}|dz|f�� S)rr+)r,r-�nums r.r>z _jordan_form.<locals>.<listcomp>�s$��E�E�E�F�A�s�a��c�3�Z�E�E�Er0c�B��g|]\}}t|��D]}�|f���Sr+)rW)r,rrUrSrCs �r.r>z _jordan_form.<locals>.<listcomp>�s4��� H� H� H�Y�T�3�U�3�Z�Z� H� H��c�4�[� H� H� H� Hr0c3� K�|] \}}|V�� dSr5r+)r,rCrs r.r/z_jordan_form.<locals>.<genexpr>�s&����A�A�I�C��4�A�A�A�A�A�Ar0zOSymPy had encountered an inconsistent result while computing Jordan block. : {}c3�L�K�|]\}}��||���V��dS))r� eigenvalueN)� jordan_block)r,rCrr<s �r.r/z_jordan_form.<locals>.<genexpr>�s:�����_�_�)�#�t�#�"�"���"�=�=�_�_�_�_�_�_r0rc�R��g|]#}��|����d�����$S)Nr8)r:)r,r-rCr>�vecs ���r.r>z _jordan_form.<locals>.<listcomp>�sF���*�*�*�� ���Q���0�0��$�0�G�G�*�*�*r0)rlrrirr?r�r�rrqrfrrr�rrr|rr�r�r r�r�� enumerate�reverse�extendr1rXrmr�rW�reversed)"rE�calc_transformr��max_precr6rCrJrQr�r8�blocks� jordan_mat� jordan_basis� basis_mat�block_structurerA�chain� block_sizes� size_nums�jordan_form_size� eig_basis� block_eigr�null_big� null_small�new_vecsrCr>r�r<r=r4rfr\s"` ` @@@@@@@@r.� _jordan_formrqs�������������F �;�M�"�#K�L�L�L��C����u���J�� 1� ��X�X�!�(�(�*�*�X�X�X�X�X�H�H��� � � ��H�H�H� �����k�(�+�+�R�0�0�� � � � � � � ��I� %� %� %� %� %� %� %� ������> � � � � � � � ��C�,�,�,�,�,�,��m�m�A�A�A�A�B�B�� �=�=�?�?�D��/�/�� �5�5��>�>� /��.�/�/� /� /� �4�9�9�;�;���3�8�#�#��D�I�I�K�K�-=�>�>�>���S�X�v�&� �� .�!�>�*�-�-� -�#�#�#�#�!�#�#�#� �!�s�z�<�0� ��~�i��4�4�4��O��d�i�i�k�k�'7�8�8�8�J�J��!%�c���� �c�#9�:�:��/�/��6�6� � F�E�i� �.D�.D�E�E�E� � ��������� H� H� H� H�)� H� H� H� J� J� J� J��A�A��A�A�A�A�A���1�6�!�!�� +�+1�6�!�9�9�6�6� 6�`�_�_�_��_�_�_�F����6�"�J� �*��~�j�)�)�)��L��d�i�i�k�k�'7�8�8�8�4�4��� �.� 4� 4�O�I�t��C����!�'�#�t�,�,�7�7�9�9�H�!�'�#�t�a�x�0�0�;�;�=�=�J�  �x� �Y� 6��A�A�C�*�*�*�*�*�*�"�4�[�[�*�*�*�H� � � �X� &� &� &� � � ��� 2� 2� 3� 3� 3� 3�! 4�$�� �L�)�I� �>�)�Z� 0� 0�0s�+A,�, A;�:A;c �X�|���jdi|��}d�|D��S)a�Returns left eigenvectors and eigenvalues. This function returns the list of triples (eigenval, multiplicity, basis) for the left eigenvectors. Options are the same as for eigenvects(), i.e. the ``**flags`` arguments gets passed directly to eigenvects(). Examples ======== >>> from sympy import Matrix >>> M = Matrix([[0, 1, 1], [1, 0, 0], [1, 1, 1]]) >>> M.eigenvects() [(-1, 1, [Matrix([ [-1], [ 1], [ 0]])]), (0, 1, [Matrix([ [ 0], [-1], [ 1]])]), (2, 1, [Matrix([ [2/3], [1/3], [ 1]])])] >>> M.left_eigenvects() [(-1, 1, [Matrix([[-2, 1, 1]])]), (0, 1, [Matrix([[-1, -1, 1]])]), (2, 1, [Matrix([[1, 1, 1]])])] c�4�g|]\}}}||d�|D��f��S)c�6�g|]}|�����Sr+)� transpose)r,r�s r.r>z/_left_eigenvects.<locals>.<listcomp>.<listcomp>s ��6�6�6�1������6�6�6r0r+)r,r�r�r�s r.r>z$_left_eigenvects.<locals>.<listcomp>s5�� U� U� U�<L�C��u�S�$�6�6��6�6�6� 7� U� U� Ur0r+)rur�)rErur�s r.�_left_eigenvectsrv�s9��< $�1�;�;�=�=� #� ,� ,�e� ,� ,�D� U� U�PT� U� U� U�Ur0c���|j|jkr-|j�|�����}n,|�|j�����}g}|���D]\}}|t |��g|zz }�t|��|jkr#||jg|jt|��z zz }|� dt���|S)a3Compute the singular values of a Matrix Examples ======== >>> from sympy import Matrix, Symbol >>> x = Symbol('x', real=True) >>> M = Matrix([[0, 1, 0], [0, x, 0], [-1, 0, 0]]) >>> M.singular_values() [sqrt(x**2 + 1), 1, 0] See Also ======== condition_number T)r^r�) rXr�r�r:r�r�rr|�zeror�r )rE� valmultpairs�valsr�r2s r.�_singular_valuesr{s���$ �v������s�|�|�A���0�0�2�2� � ��z�z�!�#���0�0�2�2� � �D��"�"�$�$�����1� ��a��� �A� ���� �4�y�y�1�6��� ����A�F�S��Y�Y�.�/�/�� �I�I�d� 0�I�1�1�1� �Kr0N)F)T)TF)FFF)N�typesr� collectionsr�mpmathrr�mpmath.libmp.libmpfr�sympy.core.sortingr �sympy.core.evalfr r �sympy.core.logicr r �sympy.core.numbersr�sympy.core.sympifyr�(sympy.functions.elementary.miscellaneousr� sympy.polysrrrrr�sympy.polys.matricesr�sympy.polys.matrices.eigenrr�sympy.polys.polytoolsr� exceptionsrr� determinantr� utilitiesrr �__doctest_requires__rNrUr\rvr�rsrtr�r�r�r�r�r�r�r�r�r�r�r�rrr$r%r"r#r!r r �_doc_positive_definite�__doc__rqrvr{r+r0r.�<module>r�sJ����������������������+�+�+�+�+�+�/�/�/�/�/�/�@�@�@�@�@�@�@�@�0�0�0�0�0�0�0�0�$�$�$�$�$�$�'�'�'�'�'�'�9�9�9�9�9�9�2�2�2�2�2�2�2�2�2�2�2�2�2�2�-�-�-�-�-�-�N�N�N�N�N�N�N�N�%�%�%�%�%�%�9�9�9�9�9�9�9�9�/�/�/�/�/�/�)�)�)�)�)�)�)�)�"�%1�>� �����4!�!�!�!����"�h�/4�u� �h�h�h�h�h�X2��-2�&3�&3�&3�&3�T-2�*E�*E�*E�*E�Z)0�%� � � � � � � �����&*.�'�l�5�l�l�l�l�l�^����&5F�5F�5F�5F�r���.���,  �  �  �"�"�"�"�D'�'�'�'�8I,�I,�I,�I,�X������ '� '� '� 1� 1� 1�%�%�%�)�)�)� � � � � � �)$�)$�)$�Zk��\%;���$:��!�$:���$:��!�$:���r1��r1�r1�r1�r1�r1�j V� V� V�F%�%�%�%�%r0
Memory