� ��g����Z�ddlmZddlmZddlmZmZddlmZddl m Z ddl m Z ddl mZmZddlmZdd lmZdd lmZd d lmZd d lmZmZmZmZmZmZeefd�Zedfd�Z d�Z!d�Z"d%d�Z#defd�Z$d%d�Z%d%d�Z&d�Z'd�Z(d&d�Z)efd�Z*d�Z+edfd�Z,ed ���Z-d!�Z.d"�Z/d%d#�Z0d$�Z1dS)'�)� FunctionType)�cacheit)�Float�Integer)�S)�uniquely_named_symbol)�Mul)�PurePoly�cancel)�nC)� DomainMatrix)�DDM�)�NonSquareMatrixError)�_get_intermediate_simp�_get_intermediate_simp_bool�_iszero�_is_zero_after_expand_mul� _dotprodsimp� _simplifyc�4�g}t|��}td�|D����r�td�|D����rld�|D��}t|��}||��r%|dkrd�t |��D��}ddd|fS|�|��}|||d|fSg}t |��D]3\}} || ��} | dkr|| d|fcS|�| ���4t|��rddd|fSt |��D]R\}} ||��|| ��} || ��} | dvr|�|| f��| dkr|| d|fcS| ||<�St|��rddd|fSt |��D]S\}} ||��| �tj ��r&d ||<|�|tj f���Tt|��rddd|fS|�d��}|||d |fS) a Find the lowest index of an item in ``col`` that is suitable for a pivot. If ``col`` consists only of Floats, the pivot with the largest norm is returned. Otherwise, the first element where ``iszerofunc`` returns False is used. If ``iszerofunc`` does not return false, items are simplified and retested until a suitable pivot is found. Returns a 4-tuple (pivot_offset, pivot_val, assumed_nonzero, newly_determined) where pivot_offset is the index of the pivot, pivot_val is the (possibly simplified) value of the pivot, assumed_nonzero is True if an assumption that the pivot was non-zero was made without being proved, and newly_determined are elements that were simplified during the process of pivot finding.c3�NK�|] }t|ttf��V��!dS�N)� isinstancerr��.0�xs �j/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/matrices/determinant.py� <genexpr>z)_find_reasonable_pivot.<locals>.<genexpr>*s1���� 8� 8�q�:�a�%��)� *� *� 8� 8� 8� 8� 8� 8�c3�@K�|]}t|t��V��dSr)rrrs rrz)_find_reasonable_pivot.<locals>.<genexpr>*sE����A/�A/�%&�J�q�%� � �A/�A/�A/�A/�A/�A/r c�,�g|]}t|����S�)�absrs r� <listcomp>z*_find_reasonable_pivot.<locals>.<listcomp>,s��'�'�'�a�3�q�6�6�'�'�'r rc�(�g|]\}}|dk� |df��S)rr#)r�irs rr%z*_find_reasonable_pivot.<locals>.<listcomp>3s%��#P�#P�#P�t�q�!��a���Q��F���r NF)TFT) �list�all�any�max� enumerate�index�append�equalsr�Zero) �col� iszerofunc�simpfunc�newly_determined�col_abs� max_valuer-�possible_zerosr'r�is_zero�simpeds r�_find_reasonable_pivotr:s���$�� �s�)�)�C� � 8� 8�C� 8� 8� 8�8�8� <�S�A/�A/�*-�A/�A/�A/�>/�>/� <�'�'�3�'�'�'����L�L� � �:�i� � � 9��A�~�~�#P�#P�y��~�~�#P�#P�#P� ��$��'7�8� 8�� � �i�(�(���s�5�z�5�*:�;�;��N��#���'�'���1��*�Q�-�-�� �e� � ��q�%�!1�2� 2� 2� 2����g�&�&�&�&� �>���5��d�E�#3�4�4��#��� $� $���1� �!� � (� ���!�����*�V�$�$�� �m� #� #� � #� #�Q��K� 0� 0� 0� �e� � ��v�u�&6�7� 7� 7� 7�#��q��� �>���5��d�E�#3�4�4� �#��� 1� 1���1� �!� � (� � �8�8�A�F� � � 1� !%�N�1� � � #� #�Q���K� 0� 0� 0�� �>���5��d�E�#3�4�4� ���T�"�"�A� �s�1�v�t�-� .�.r Nc��g}t|��D]7\}}||��}|dkr||dgfcS|�|�||f���8t|��dkrdddgfS|�|dd|dddgfSg}|D]^\}}||��}t|��t|��kr.|�||f��||��dkr||d|fcS�_|dd|ddd|fS)a� Helper that computes the pivot value and location from a sequence of contiguous matrix column elements. As a side effect of the pivot search, this function may simplify some of the elements of the input column. A list of these simplified entries and their indices are also returned. This function mimics the behavior of _find_reasonable_pivot(), but does less work trying to determine if an indeterminate candidate pivot simplifies to zero. This more naive approach can be much faster, with the trade-off that it may erroneously return a pivot that is zero. ``col`` is a sequence of contiguous column entries to be searched for a suitable pivot. ``iszerofunc`` is a callable that returns a Boolean that indicates if its input is zero, or None if no such determination can be made. ``simpfunc`` is a callable that simplifies its input. It must return its input if it does not simplify its input. Passing in ``simpfunc=None`` indicates that the pivot search should not attempt to simplify any candidate pivots. Returns a 4-tuple: (pivot_offset, pivot_val, assumed_nonzero, newly_determined) ``pivot_offset`` is the sequence index of the pivot. ``pivot_val`` is the value of the pivot. pivot_val and col[pivot_index] are equivalent, but will be different when col[pivot_index] was simplified during the pivot search. ``assumed_nonzero`` is a boolean indicating if the pivot cannot be guaranteed to be zero. If assumed_nonzero is true, then the pivot may or may not be non-zero. If assumed_nonzero is false, then the pivot is non-zero. ``newly_determined`` is a list of index-value pairs of pivot candidates that were simplified during the pivot search. FNrrT)r,r.�len�id) r1r2r3�indeterminatesr'�col_val�col_val_is_zeror4� tmp_col_vals r�_find_reasonable_pivot_naiverB{sl��V�N���n�n�0�0� ��7�$�*�W�-�-�� �e� #� #��g�u�b�(� (� (� (� � $� � !� !�1�g�,� /� /� /�� �>���a����T�5�"�$�$��� �a� ��#�^�A�%6�q�%9�4��C�C���$�?�?� ��7��h�w�'�'� � �g�;�;�"�[�/�/� )� )� � #� #�Q� �$4� 5� 5� 5��z�+�&�&�%�/�/��+�u�.>�>�>�>�>�� �!� �Q� ���!2�1�!5�t�=M� M�Mr c�������jdkr(�jdkr��dd�jg��S�d�ddd�fc}��dd�df�dd�dd�f}}|g�t �jdz ��D]2}��|��|d������3�fd��D����j| g�z���fd�}���jdz�j|��}||fS) z�Return (A,T) where T the Toeplitz matrix used in the Berkowitz algorithm corresponding to ``M`` and A is the first principal submatrix. rr�rrN��� dotprodsimpc�L��g|] }� �|d���d��!S)NrFrD)�multiply)r�d�Rs �rr%z._berkowitz_toeplitz_matrix.<locals>.<listcomp>�s0��� E� E� E�!�q�b�]�]�1�$�]� /� /�� 5� E� E� Er c�4��||kr�jS�||z Sr)�zero)r'�j�M�diagss ��r�entryz)_berkowitz_toeplitz_matrix.<locals>.entry�s!��� �q�5�5��6�M��Q��U�|�r )�rows�cols�_new�one�ranger.rI) rO�a�C�Ar'rQ�toeplitzrKrPs ` @@r�_berkowitz_toeplitz_matrixr[�s9�����  �v��{�{�q�v��{�{��v�v�a��A�E�7�#�#�#� �S�6�Q�q�!�"�"�u�X�D�A�q� �Q�R�R��U�8�Q�q�r�r�!�"�"�u�X�q�A� �C�E� �1�6�A�:� � �=�=�� � � �Q�Z�Z��a��d�Z�;�;�<�<�<�<� E� E� E� E�u� E� E� E�E� �U�Q�B�K�%� �E������� �v�v�a�f�q�j�!�&�%�0�0�H� �x�=�r c�J�|jdkr(|jdkr|�dd|jg��S|jdkr0|jdkr%|�dd|j|d g��St |��\}}|�t |��d���S)ap Run the Berkowitz algorithm and return a vector whose entries are the coefficients of the characteristic polynomial of ``M``. Given N x N matrix, efficiently compute coefficients of characteristic polynomials of ``M`` without division in the ground domain. This method is particularly useful for computing determinant, principal minors and characteristic polynomial when ``M`` has complicated coefficients e.g. polynomials. Semi-direct usage of this algorithm is also important in computing efficiently sub-resultant PRS. Assuming that M is a square matrix of dimension N x N and I is N x N identity matrix, then the Berkowitz vector is an N x 1 vector whose entries are coefficients of the polynomial charpoly(M) = det(t*I - M) As a consequence, all polynomials generated by Berkowitz algorithm are monic. For more information on the implemented algorithm refer to: [1] S.J. Berkowitz, On computing the determinant in small parallel time using a small number of processors, ACM, Information Processing Letters 18, 1984, pp. 147-150 [2] M. Keber, Division-Free computation of sub-resultants using Bezout matrices, Tech. Report MPI-I-2006-1-006, Saarbrucken, 2006 rrrErDNrF)rRrSrTrUr[rI�_berkowitz_vector)rO�submatrZs rr]r]�s���H �v��{�{�q�v��{�{��v�v�a��Q�U�G�$�$�$� ��1�����1����v�v�a��Q�U�Q�s�V�G�,�-�-�-�1�!�4�4��F�H� � � �.�v�6�6�D� � I� I�Ir � berkowitzc�R�|�|������S)aRReturns the adjugate, or classical adjoint, of a matrix. That is, the transpose of the matrix of cofactors. https://en.wikipedia.org/wiki/Adjugate Parameters ========== method : string, optional Method to use to find the cofactors, can be "bareiss", "berkowitz", "bird", "laplace" or "lu". Examples ======== >>> from sympy import Matrix >>> M = Matrix([[1, 2], [3, 4]]) >>> M.adjugate() Matrix([ [ 4, -2], [-3, 1]]) See Also ======== cofactor_matrix sympy.matrices.matrixbase.MatrixBase.transpose ��method)�cofactor_matrix� transpose�rOrbs r� _adjugaterf*s'��< � � �F� � +� +� 5� 5� 7� 7�7r �lambdac�X���|jst���|���}|j�|���}t ||gd����}�js �tur-�fd�|D��}�fd�|D��}t||��}nt||����}|S)a�Computes characteristic polynomial det(x*I - M) where I is the identity matrix. A PurePoly is returned, so using different variables for ``x`` does not affect the comparison or the polynomials: Parameters ========== x : string, optional Name for the "lambda" variable, defaults to "lambda". simplify : function, optional Simplification function to use on the characteristic polynomial calculated. Defaults to ``simplify``. Examples ======== >>> from sympy import Matrix >>> from sympy.abc import x, y >>> M = Matrix([[1, 3], [2, 0]]) >>> M.charpoly() PurePoly(lambda**2 - lambda - 6, lambda, domain='ZZ') >>> M.charpoly(x) == M.charpoly(y) True >>> M.charpoly(x) == M.charpoly(y) True Specifying ``x`` is optional; a symbol named ``lambda`` is used by default (which looks good when pretty-printed in unicode): >>> M.charpoly().as_expr() lambda**2 - lambda - 6 And if ``x`` clashes with an existing symbol, underscores will be prepended to the name to make it unique: >>> M = Matrix([[1, 2], [x, 0]]) >>> M.charpoly(x).as_expr() _x**2 - _x - 2*x Whether you pass a symbol or not, the generator can be obtained with the gen attribute since it may not be the same as the symbol that was passed: >>> M.charpoly(x).gen _x >>> M.charpoly(x).gen == x False Notes ===== The Samuelson-Berkowitz algorithm is used to compute the characteristic polynomial efficiently and without any division operations. Thus the characteristic polynomial over any commutative ring without zero divisors can be computed. If the determinant det(x*I - M) can be found out easily as in the case of an upper or a lower triangular matrix, then instead of Samuelson-Berkowitz algorithm, eigenvalues are computed and the characteristic polynomial with their help. See Also ======== det c� �d|zS)N�_r#)�ss r�<lambda>z_charpoly.<locals>.<lambda>�s ��s�Q�w�r )�modifyc�:��g|]}��|����Sr#)�to_sympy)r�c�Ks �rr%z_charpoly.<locals>.<listcomp>�s#���1�1�1��q�z�z�!�}�}�1�1�1r c�&��g|] }�|����Sr#r#)rrW�simplifys �rr%z_charpoly.<locals>.<listcomp>�s!���8�8�8�q�x�x��{�{�8�8�8r )�domain) � is_squarer�to_DMrt�charpolyr�is_EXRAWrr )rOrrs�dM�cp� berk_vector�prqs ` @r� _charpolyr}Ls�����N �;�%�"�$�$�$� �����B� � �A� �����B��a�!��->�->�?�?�?�A��z� &�X�Y�.�.� 2�1�1�1�b�1�1�1� �8�8�8�8�K�8�8�8� � �[�!� $� $��� �R��1� %� %� %�� �Hr c��|jr |jdkrt���tj||zdzz|�|||��zS)a�Calculate the cofactor of an element. Parameters ========== method : string, optional Method to use to find the cofactors, can be "bareiss", "berkowitz", "bird", "laplace" or "lu". Examples ======== >>> from sympy import Matrix >>> M = Matrix([[1, 2], [3, 4]]) >>> M.cofactor(0, 1) -3 See Also ======== cofactor_matrix minor minor_submatrix rrE)rurRrr� NegativeOne�minor�rOr'rNrbs r� _cofactorr��sN��4 �;�%�!�&�1�*�*�"�$�$�$� �=�A��E�Q�;� '�!�'�'�!�Q��*?�*?� ?�?r c�z����jst������j�j��fd���S)a�Return a matrix containing the cofactor of each element. Parameters ========== method : string, optional Method to use to find the cofactors, can be "bareiss", "berkowitz", "bird", "laplace" or "lu". Examples ======== >>> from sympy import Matrix >>> M = Matrix([[1, 2], [3, 4]]) >>> M.cofactor_matrix() Matrix([ [ 4, -3], [-2, 1]]) See Also ======== cofactor minor minor_submatrix c�2����||���Sr)�cofactor)r'rNrOrbs ��rrlz"_cofactor_matrix.<locals>.<lambda>�s������A�q�&�1�1�r )rurrTrRrSres``r�_cofactor_matrixr��sI����8 �;�%�"�$�$�$� �6�6�!�&�!�&� 1� 1� 1� 1� 1� 3� 3�3r c �T�� �ddl}�j\}}||kr �j�||}}tt |����}g}t d|dz��D];� |tt t|�|� ������z }�<d}|D]p}d}t|��} t |��D]!� |t�� fd�|D����z}�"||tj | zzt|| z || z ��zz }�q|tj |zz}|� ��S)a�Returns the permanent of a matrix. Unlike determinant, permanent is defined for both square and non-square matrices. For an m x n matrix, with m less than or equal to n, it is given as the sum over the permutations s of size less than or equal to m on [1, 2, . . . n] of the product from i = 1 to m of M[i, s[i]]. Taking the transpose will not affect the value of the permanent. In the case of a square matrix, this is the same as the permutation definition of the determinant, but it does not take the sign of the permutation into account. Computing the permanent with this definition is quite inefficient, so here the Ryser formula is used. Examples ======== >>> from sympy import Matrix >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M.per() 450 >>> M = Matrix([1, 5, 7]) >>> M.per() 13 References ========== .. [1] Prof. Frank Ben's notes: https://math.berkeley.edu/~bernd/ban275.pdf .. [2] Wikipedia article on Permanent: https://en.wikipedia.org/wiki/Permanent_%28mathematics%29 .. [3] https://reference.wolfram.com/language/ref/Permanent.html .. [4] Permanent of a rectangular matrix : https://arxiv.org/pdf/0904.3251.pdf rNrc3�,�K�|]}��|fV��dSrr#)rrNrOr's ��rrz_per.<locals>.<genexpr>.s+�����1�1�Q��1�a�4��1�1�1�1�1�1r ) � itertools�shape�Tr(rV�map� combinationsr<�sumrrr rs) rOr��m�nrk�subsets�perm�subset�prod�sub_lenr's ` @r�_perr��sM����D���� �7�D�A�q��1�u�u� �C���!�1�� �U�1�X�X���A��G� �1�a�!�e�_�_�A�A���4��D�)�"8�"8��A�">�">�?�?�@�@�@��� �D��M�M�����f�+�+���q��� 2� 2�A� �S�1�1�1�1�1�&�1�1�1�1�1� 1�T�T� ��q�}�g�-�-��1�w�;��G� �0L�0L�L�L����A�M�1� ��D� �=�=�?�?�r c��tj|dd���}|j}|�|�����S)NT)�field� extension)r � from_Matrixrtro�det)rO�DOMrqs r�_det_DOMr�3s:�� � "�1�D�D� A� A� A�C� � �A� �:�:�c�g�g�i�i� � � r �bareissc��|���}|dkrd}n|dkrd}|dvrtd|z���|�|dkrt}n5|dkrt}n't |t ��std|z���|j}||jk�r%|d kr|jS|d kr|d S|d kr@|d |d z|d|dzz }tt��|��S|dkr�|d |d z|dz|d|dz|dzz|d|dz|dzz|d|d z|dzz |d |dz|dzz |d|dz|dzz }tt��|��Sg}|� ��D]�}|dkrt|||f��}n�|dkr|||f� |���}n�|dkr|||f���}nj|dkr|||f�|���}nE|dkr|||f���}n"|dkr|||f���}|�|����t'|�S)a� Computes the determinant of a matrix if ``M`` is a concrete matrix object otherwise return an expressions ``Determinant(M)`` if ``M`` is a ``MatrixSymbol`` or other expression. Parameters ========== method : string, optional Specifies the algorithm used for computing the matrix determinant. If the matrix is at most 3x3, a hard-coded formula is used and the specified method is ignored. Otherwise, it defaults to ``'bareiss'``. Also, if the matrix is an upper or a lower triangular matrix, determinant is computed by simple multiplication of diagonal elements, and the specified method is ignored. If it is set to ``'domain-ge'``, then Gaussian elimination method will be used via using DomainMatrix. If it is set to ``'bareiss'``, Bareiss' fraction-free algorithm will be used. If it is set to ``'berkowitz'``, Berkowitz' algorithm will be used. If it is set to ``'bird'``, Bird's algorithm will be used [1]_. If it is set to ``'laplace'``, Laplace's algorithm will be used. Otherwise, if it is set to ``'lu'``, LU decomposition will be used. .. note:: For backward compatibility, legacy keys like "bareis" and "det_lu" can still be used to indicate the corresponding methods. And the keys are also case-insensitive for now. However, it is suggested to use the precise keys for specifying the method. iszerofunc : FunctionType or None, optional If it is set to ``None``, it will be defaulted to ``_iszero`` if the method is set to ``'bareiss'``, and ``_is_zero_after_expand_mul`` if the method is set to ``'lu'``. 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 tested as non-zero, and also ``None`` if it is undecidable. Returns ======= det : Basic Result of determinant. Raises ====== ValueError If unrecognized keys are given for ``method`` or ``iszerofunc``. NonSquareMatrixError If attempted to calculate determinant from a non-square matrix. Examples ======== >>> from sympy import Matrix, eye, det >>> I3 = eye(3) >>> det(I3) 1 >>> M = Matrix([[1, 2], [3, 4]]) >>> det(M) -2 >>> det(M) == M.det() True >>> M.det(method="domain-ge") -2 References ========== .. [1] Bird, R. S. (2011). A simple division-free algorithm for computing determinants. Inf. Process. Lett., 111(21), 1072-1074. doi: 10.1016/j.ipl.2011.08.006 �bareisr��det_lu�lu)r�r_r�� domain-ge�bird�laplacez$Determinant method '%s' unrecognizedNz%Zero testing method '%s' unrecognizedrrrDrE�rr�rr�rr�)rErE)rrE)rEr)rrE)rErr��r2r_r�r�)�lower� ValueErrorrrrrrRrSrUrr�strongly_connected_componentsr��_eval_det_bareiss�_eval_det_berkowitz� _eval_det_lu�_eval_det_bird�_eval_det_laplacer.r )rOrbr2r�r��dets�br�s r�_detr�9s"��r�\�\�^�^�F� ������� �8� � ��� �!�!�!��?�&�H�I�I�I��� �Y� � �2�J�J� �t�^�^� �J�� � �L� 1� 1�O��@�:�M�N�N�N� ��A��A�F�{�{� ��6�6��5�L� �!�V�V��T�7�N� �!�V�V��$��!�D�'�!�A�d�G�a��g�$5�5�A�7�)�,�7�7��:�:� :� �!�V�V��D�'�A�d�G�#�a��g�-��D�'�A�d�G�#�a��g�-�.��D�'�A�d�G�#�a��g�-�.��D�'�A�d�G�#�a��g�-�.��D�'�A�d�G�#�a��g�-� .� �D�'�A�d�G�#�a��g�-� .�A� 8�)�,�7�7��:�:� :� �D� � ,� ,� .� .� � �� �[� � ��1�Q��T�7�#�#�C�C� �y� � ��A�q�D�'�+�+�z�+�B�B�C�C� �{� "� "��A�q�D�'�-�-�/�/�C�C� �t�^�^��A�q�D�'�&�&�*�&�=�=�C�C� �v� � ��A�q�D�'�(�(�*�*�C�C� �y� � ��A�q�D�'�+�+�-�-�C� � � �C����� ��:�r c�|����d���fd� ��jst����jdkr�jS����S)a�Compute matrix determinant using Bareiss' fraction-free algorithm which is an extension of the well known Gaussian elimination method. This approach is best suited for dense symbolic matrices and will result in a determinant with minimal number of fractions. It means that less term rewriting is needed on resulting formulae. Parameters ========== iszerofunc : function, optional The function to use to determine zeros when doing an LU decomposition. Defaults to ``lambda x: x.is_zero``. TODO: Implement algorithm for sparse matrices (SFF), http://www.eecis.udel.edu/~saunders/papers/sffge/it5.ps. rc�������� ��jdkr�jS�jdkr�dSt�dd�df� ���\��}}���jSd�dzz}�fd�t �j��D��}t t �j����}��||��� ����� fd�}|� � ��jdz �jdz |�����zS) NrrrDr������rEc� ��g|] }|�k�|�� Sr#r#)rr'� pivot_poss �rr%z1_det_bareiss.<locals>.bareiss.<locals>.<listcomp>�s���=�=�=�a�a�9�n�n��n�n�nr c������||dzfz��|dzf�|dfzz �z }td��rt|��S|jst|��S|S)NrrT)rr�is_Atomr )r'rN�ret�cumm�matr�� pivot_val�tmp_mats �����rrQz,_det_bareiss.<locals>.bareiss.<locals>.entry�sz����W�Q��A��X�.�.��Y��A��5E�1F�w�q�RS�t�}�1T�T�X\�\�C�*�4�0�0� #�#�C�(�(�(��[� #��c�{�{�"��Jr ) rRrUr:rMrVr(rS�extractrT) r�r�rj�signrRrSrQr�r�r�rOr�r2s `` @@@���rr�z_det_bareiss.<locals>.bareiss�s#�������� �8�q�=�=��7�N� �X��]�]��t�9� � &<�C����1��I�R\�%]�%]�%]�"� �9�a�� � ��8�O�� �A� �&��>�=�=�=�5���?�?�=�=�=���E�#�(�O�O�$�$���+�+�d�D�)�)�� � � � � � � � � ��G�G�A�F�F�3�8�a�<���A��u�E�E�y�Q�Q�Q�Qr r)r)rurrRrU)rOr2r�s``@r� _det_bareissr��sp�����*R�R�R�R�R�R�R�R�B �;�%�"�$�$�$��v��{�{��u� � �7�1�:�:�r c��|jst���|jdkr|jSt |��}dt |��dz z|dzS)z8 Use the Berkowitz algorithm to compute the determinant.rr�r)rurrRrUr]r<)rOr{s r�_det_berkowitzr�s\�� �;�%�"�$�$�$��v��{�{��u� � $�A�&�&�K� �#�k�"�"�Q�&� '�+�b�/� 9�9r c�f�|jst���|jdkr|jS|�||���\}}|||jdz |jdz f��r|jSt |��dzr|j n|j}t|j��D]}||||fz}�|S)a� Computes the determinant of a matrix from its LU decomposition. This function uses the LU decomposition computed by LUDecomposition_Simple(). The keyword arguments iszerofunc and simpfunc are passed to LUDecomposition_Simple(). iszerofunc is a callable that returns a boolean indicating if its input is zero, or None if it cannot make the determination. simpfunc is a callable that simplifies its input. The default is simpfunc=None, which indicate that the pivot search algorithm should not attempt to simplify any candidate pivots. If simpfunc fails to simplify its input, then it must return its input instead of a copy. Parameters ========== iszerofunc : function, optional The function to use to determine zeros when doing an LU decomposition. Defaults to ``lambda x: x.is_zero``. simpfunc : function, optional The simplification function to use when looking for zeros for pivots. r)r2r3rrE)rurrRrU�LUdecomposition_SimplerMr<rV)rOr2r3r�� row_swapsr��ks r�_det_LUr�s���4 �;�%�"�$�$�$��v��{�{��u� � �,�,� ��-���M�B� ��z�"�R�W�Q�Y���� �)�*�+�+���v� �� �N�N�1�$� /�1�5�&�&�!�%�C� �2�7�^�^���� �r�!�Q�$�x���� �Jr c�����jd}|dkr�dS|dkr#�d�dz�d�dzz St�fd�t|��D����S) a�Compute the determinant of a matrix using Laplace expansion. This is a recursive function, and it should not be called directly. Use _det_laplace() instead. The reason for splitting this function into two is to allow caching of determinants of submatrices. While one could also define this function inside _det_laplace(), that would remove the advantage of using caching in Cramer Solve. rrrErDr�r�r�c3��K�|]8}d|z�d|fzt��d|����zV��9dS)r�rN)� __det_laplace�minor_submatrix)rr'rOs �rrz __det_laplace.<locals>.<genexpr>nsp�����M�M�>?��1�9�q��A��w�&� ��!2�!2�1�a�!8�!8�9�9�:�M�M�M�M�M�Mr )r�r�rV)rOr�s` rr�r�^s���� ��� �A��A�v�v���t� � �a�����w��4�� �1�T�7�Q�t�W�#4�4�4��M�M�M�M�CH��8�8�M�M�M�M�M� Mr c��|jst���|jddkr|jSt |�����S)aPCompute the determinant of a matrix using Laplace expansion. While Laplace expansion is not the most efficient method of computing a determinant, it is a simple one, and it has the advantage of being division free. To improve efficiency, this function uses caching to avoid recomputing determinants of submatrices. r)rurr�rUr�� as_immutable)rOs r� _det_laplacer�rsJ�� �;�%�"�$�$�$��w�q�z�Q����u� � ����)�)� *� *�*r c�@�d�}|j���}|jd}|dkr|jS|}t |dz ��D] }||���|��}�!|dd}|dzdkr| }|j�|��S)a�Compute the determinant of a matrix using Bird's algorithm. Bird's algorithm is a simple division-free algorithm for computing, which is of lower order than the Laplace's algorithm. It is described in [1]_. References ========== .. [1] Bird, R. S. (2011). A simple division-free algorithm for computing determinants. Inf. Process. Lett., 111(21), 1072-1074. doi: 10.1016/j.ipl.2011.08.006 c�R���|jd}|jj��}�g�tt d|����D](}||||z}��|���)�ddd����fd�t |��D��}t||j|j��S)Nrrr�c�R��g|]#\}}�g|z�|gz||dzd�z��$S)rNr#)rr'�X_i� diag_sumsrMs ��rr%z)_det_bird.<locals>.mu.<locals>.<listcomp>�sK������v�q�#�$��!��y��|�n�,�s�1�q�5�6�6�{�:���r )r�rtrM�reversedrVr.r,r)�Xr��totalr'�elemsr�rMs @@r�muz_det_bird.<locals>.mu�s����� �G�A�J���x�}�����F� ��%��1�+�+�&�&� $� $�A� �Q�q�T�!�W� �E� � � �U� #� #� #� #��d�d��d�O� �������1��������5�!�'�1�8�,�,�,r rrrE)�_rep�to_ddmr�rUrV�matmulrtro)rOr��Mddmr��Fn1rj�detAs r� _det_birdr��s��� -� -� -� �6�=�=�?�?�D� ��� �A��A�v�v��u� � �C� �1�q�5�\�\�#�#���b��g�g�n�n�T�"�"��� �q�6�!�9�D��1�u��z�z��u�� �;� � �� %� %�%r c��|jst���|�||���|���S)a�Return the (i,j) minor of ``M``. That is, return the determinant of the matrix obtained by deleting the `i`th row and `j`th column from ``M``. Parameters ========== i, j : int The row and column to exclude to obtain the submatrix. method : string, optional Method to use to find the determinant of the submatrix, can be "bareiss", "berkowitz", "bird", "laplace" or "lu". Examples ======== >>> from sympy import Matrix >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M.minor(1, 1) -12 See Also ======== minor_submatrix cofactor det ra)rurr�r�r�s r�_minorr��sA��> �;�%�"�$�$�$� � � �Q�� "� "� &� &�f� &� 5� 5�5r c�����dkr �|jz ��dkr �|jz �d�cxkr |jkrnnd�cxkr |jks$ntd|jzd|jzz����fd�t|j��D��}�fd�t|j��D��}|�||��S)a�Return the submatrix obtained by removing the `i`th row and `j`th column from ``M`` (works with Pythonic negative indices). Parameters ========== i, j : int The row and column to exclude to obtain the submatrix. Examples ======== >>> from sympy import Matrix >>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> M.minor_submatrix(1, 1) Matrix([ [1, 3], [7, 9]]) See Also ======== minor cofactor rz1`i` and `j` must satisfy 0 <= i < ``M.rows`` (%d)zand 0 <= j < ``M.cols`` (%d).c� ��g|] }|�k�|�� Sr#r#)rrWr's �rr%z$_minor_submatrix.<locals>.<listcomp>����� /� /� /�!��Q���A���r c� ��g|] }|�k�|�� Sr#r#)rrWrNs �rr%z$_minor_submatrix.<locals>.<listcomp>�r�r )rRrSr�rVr�)rOr'rNrRrSs `` r�_minor_submatrixr��s�����6 �1�u�u� �Q�V� ���1�u�u� �Q�V� �� ��?�?�?�?�A�F�?�?�?�?�?�!�q�/�/�/�/�1�6�/�/�/�/��#�%&�V�,�.M�PQ�PV�.V�W�X�X� X� 0� /� /� /�u�Q�V�}�}� /� /� /�D� /� /� /� /�u�Q�V�}�}� /� /� /�D� �9�9�T�4� � � r )r_)r�N)2�typesr�sympy.core.cacher�sympy.core.numbersrr�sympy.core.singletonr�sympy.core.symbolr�sympy.core.mulr � sympy.polysr r �%sympy.functions.combinatorial.numbersr �!sympy.polys.matrices.domainmatrixr �sympy.polys.matrices.ddmr� exceptionsr� utilitiesrrrrrrr:rBr[r]rfr}r�r�r�r�r�r�r�r�r�r�r�r�r�r#r r�<module>r�s���������$�$�$�$�$�$�-�-�-�-�-�-�-�-�"�"�"�"�"�"�3�3�3�3�3�3�������(�(�(�(�(�(�(�(�4�4�4�4�4�4�:�:�:�:�:�:�(�(�(�(�(�(�,�,�,�,�,�,�A�A�A�A�A�A�A�A�A�A�A�A�A�A�A�A� ,3�Y�e/�e/�e/�e/�P29�4�NN�NN�NN�NN�d+�+�+�^+J�+J�+J�\8�8�8�8�D�i�j �j �j �j �Z@�@�@�@�@ 3� 3� 3� 3�D6�6�6�p!�!�!� O�O�O�O�f 9�?�?�?�?�D :� :� :�""�D�<�<�<�<�~ �M�M� ��M�&+�+�+�$*&�*&�*&�Z"6�"6�"6�"6�J'!�'!�'!�'!�'!r
Memory