� ��gQ9����ddlmZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z mZdd lmZdd lmZmZmZmZdd lmZmZGd �d e��ZGd�dee ��ZeZdS)�)�Callable)�Dict)�sympy_deprecation_warning�� is_sequence)�as_int�)� MatrixBase)�MutableRepMatrix� RepMatrix)�_iszero)�_liupc� _row_structure_symbolic_cholesky�_cholesky_sparse�_LDLdecomposition_sparse)�_lower_triangular_solve_sparse�_upper_triangular_solve_sparsec�x��eZdZdZe�fd���Zed���Zd�Zd�Z d�Z d�Z d�Z d �Z d �Zd �Zdd �Zdd�Zeeddd��Zee ddd��Zd�Zd�Zdd�Zdd�Zd�Zd�Zeje_eje_eje_eje_eje_eje_�xZS)�SparseRepMatrixa A sparse matrix (a matrix with a large number of zero elements). Examples ======== >>> from sympy import SparseMatrix, ones >>> SparseMatrix(2, 2, range(4)) Matrix([ [0, 1], [2, 3]]) >>> SparseMatrix(2, 2, {(1, 1): 2}) Matrix([ [0, 0], [0, 2]]) A SparseMatrix can be instantiated from a ragged list of lists: >>> SparseMatrix([[1, 2, 3], [1, 2], [1]]) Matrix([ [1, 2, 3], [1, 2, 0], [1, 0, 0]]) For safety, one may include the expected size and then an error will be raised if the indices of any element are out of range or (for a flat list) if the total number of elements does not match the expected shape: >>> SparseMatrix(2, 2, [1, 2]) Traceback (most recent call last): ... ValueError: List length (2) != rows*columns (4) Here, an error is not raised because the list is not flat and no element is out of range: >>> SparseMatrix(2, 2, [[1, 2]]) Matrix([ [1, 2], [0, 0]]) But adding another element to the first (and only) row will cause an error to be raised: >>> SparseMatrix(2, 2, [[1, 2, 3]]) Traceback (most recent call last): ... ValueError: The location (0, 2) is out of designated range: (1, 1) To autosize the matrix, pass None for rows: >>> SparseMatrix(None, [[1, 2, 3]]) Matrix([[1, 2, 3]]) >>> SparseMatrix(None, {(1, 1): 1, (3, 3): 3}) Matrix([ [0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 3]]) Values that are themselves a Matrix are automatically expanded: >>> SparseMatrix(4, 4, {(1, 1): ones(2)}) Matrix([ [0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]) A ValueError is raised if the expanding matrix tries to overwrite a different element already present: >>> SparseMatrix(3, 3, {(0, 0): ones(2), (1, 1): 2}) Traceback (most recent call last): ... ValueError: collision at (1, 1) See Also ======== DenseMatrix MutableSparseMatrix ImmutableSparseMatrix c �� ����t|��dkrTt|dt��r9|dj}|dj}|d����||�fSi�t|��dkr|d� dd|dg}t|��dk�r�|dd�\}}||cxur�nndx}}n?d||fvrt d���t|d��t|d��}}t|dt��r�|d}d||fvr#t d� ||������fd�t|��D��}�fd�t|��D��} |D]8} | D]3} �� || | ����} | �j kr| �| | f<�4�9||�fSt|dttf���r�fd �} |d���D]�\\}}}t|t��rC|������D]\\} } }| || z|| z|����`t|t t"f��r6�j|fi|��\}}��D] \} } | || z|| z�| | f���!���� |��}| ||�� |������n�t'|d��r�t)d �|dD���� }|s�j|dfi|��\}}�n�|d}t|��||zkr1t d � t|��||�����t|��D]I} t|��D]7} || |z| z} �� | ��} | �j kr| �| | f<�8�J|�U����}|rt-d �|D����dznd}|rt-d �|D����dznd}nX����D]C\} } | r| |ks| r4| |kr.t d� | | fd|dz d|dz ������D||�fSt|��dkr�t|dt t"f��r�|d}d}t/|��D]{\} }t|t t"f��s|g}t/|��D]*\} }|�j kr�� |���| | f<�+t-|t|����}�||rt|��nd}|}||�fSt1��j|�\}}}t|��D]4} t|��D]"} ||| z| z} | �j kr| �| | f<�#�5||�fS)Nr r��z*Pass rows=None and no cols for autosizing.z2{} and {} must be integers for this specification.c�:��g|]}��|����S���_sympify)�.0�i�clss ��e/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/matrices/sparse.py� <listcomp>z;SparseRepMatrix._handle_creation_inputs.<locals>.<listcomp>��#���D�D�D�1�s�|�|�A���D�D�D�c�:��g|]}��|����Srr)r�jrs �r r!z;SparseRepMatrix._handle_creation_inputs.<locals>.<listcomp>�r"r#c ���|rK||f�vr<|�||fkr.td�||f|�||f�����|�||f<dSdS)Nz)There is a collision at {} for {} and {}.)� ValueError�format)rr%�v�smats �r �updatez7SparseRepMatrix._handle_creation_inputs.<locals>.update�sw����'��q�6�T�>�>�a�4��1��:�o�o�",� K�!'���A���4��1��:�!>�!>�#�#��&'��Q��T� � � � '�'r#c3�4K�|]}t|��V��dS�Nr)rrs r � <genexpr>z:SparseRepMatrix._handle_creation_inputs.<locals>.<genexpr>�s(����?�?�!�{�1�~�~�?�?�?�?�?�?r#zMThe length of the flat list ({}) does not match the specified size ({} * {}).c3� K�|] \}}|V�� dSr-r)r�r�_s r r.z:SparseRepMatrix._handle_creation_inputs.<locals>.<genexpr>��&����.�.���A�1�.�.�.�.�.�.r#c3� K�|] \}}|V�� dSr-r)rr1�cs r r.z:SparseRepMatrix._handle_creation_inputs.<locals>.<genexpr>�r2r#z?The location {} is out of the designated range[{}, {}]x[{}, {}])�len� isinstancer �rows�cols�todokr'rrr(�ranger�zero�dictr�items�list�tuple�_handle_creation_inputsr�any�keys�max� enumerate�super)r�args�kwargsr7r8r0r4�op� row_indices� col_indicesrr%�valuer+r)�vvr1�flat� flat_listrB�row�matr*� __class__s` @�r r@z'SparseRepMatrix._handle_creation_inputsks������ �t�9�9��>�>�j��a��*�=�=�>���7�<�D���7�<�D���7�=�=�?�?�D���t�#� #��� �t�9�9��>�>�d�1�g�o��$��Q��(�D� �t�9�9��>�>�����8�D�A�q��A�~�~�~�~�~�~�~�~�"�"��t�t��!�Q���� �@�B�B�B�$�D��G�_�_�f�T�!�W�o�o�d���$�q�'�8�,�,�> 3��!�W���D�$�<�'�'�$�)�)/���d�);�);�=�=�=�E�D�D�D��d� � �D�D�D� �D�D�D�D��d� � �D�D�D� �$�/�/�A�(�/�/�� #� � �R�R��1�X�X� 6� 6�� �C�H�,�,�).�D��A��J��/� �T�4�'�'��D��G�d�D�\�2�2�+ 3�'�'�'�'�'�"&�a������ 6� 6�I�F�Q��A�!�!�Z�0�0� 6�*+�'�'�)�)�/�/�*;�*;�5�5�J�F�Q��B�"�F�1�q�5�!�a�%��4�4�4�4�5�#�A��e�}�5�5�6�%@�S�%@��%M�%M�f�%M�%M� ��1�d�$(�=�=�D�A�q�"�F�1�q�5�!�a�%��a��d��<�<�<�<�=� �L�L��O�O����q�!�S�\�\�!�_�_�5�5�5�5� 6��T�!�W�%�%� 3��?�?�t�A�w�?�?�?�?�?�?���3�3��3�D��G�F�F�v�F�F��A�q�$�$�!%�Q��I��9�~�~����4�4�(�B�#�V�C� �N�N�D�$�?�?���� #�4�[�[�3�3��!&�t���3�3�A�$-�a��f�q�j�$9�E�$'�L�L��$7�$7�E�$���0�0�-2��Q��T� �� 3� �|��y�y�{�{��6:�A�s�.�.��.�.�.�.�.��2�2���6:�A�s�.�.��.�.�.�.�.��2�2����!�I�I�K�K���D�A�q���Q�$�Y�Y�!�Y��T� � �(�0�#�V�Q��F�A�t�a�x��D�1�H�E�E����� ��t�#� #� ��Y�Y�!�^�^� �4��7�T�5�M� B� B�^��Q��A��A�#�A�,�,� %� %���3�!�#��e�}�5�5� ��%�C�&�s�^�^�6�6�E�A�r��S�X�~�~�%(�\�\�"�%5�%5��Q��T� ����3�s�8�8�$�$����%�3�q�6�6�6�A�D��D���t�#� #�>�e�g�g�=�t�D�O�D�$���4�[�[� +� +���t���+�+�A���Q��� �O�E����(�(�%*��Q��T� ��+� ��t�#� #r#c�N�tddd���|���S)Nz� The private _smat attribute of SparseMatrix is deprecated. Use the .todok() method instead. z1.9z$deprecated-private-matrix-attributes)�deprecated_since_version�active_deprecations_target)rr9��selfs r �_smatzSparseRepMatrix._smat�s8�� "� �&+�'M�  � � � ��z�z�|�|�r#c ��|�|�dd��|�dt��|�dd�����S)N�method�LDL� iszerofunc�try_block_diagF)rYr[r\)�inv�getr )rVrGs r � _eval_inversezSparseRepMatrix._eval_inverse�sS���x�x�v�z�z�(�E�:�:�#)�:�:�l�G�#D�#D�'-�z�z�2B�E�'J�'J��L�L� Lr#c��t|��std���i}|������D]\}}||��}|dkr|||<�|�|j|j|��S)aXApply a function to each element of the matrix. Examples ======== >>> from sympy import SparseMatrix >>> m = SparseMatrix(2, 2, lambda i, j: i*2+j) >>> m Matrix([ [0, 1], [2, 3]]) >>> m.applyfunc(lambda i: 2*i) Matrix([ [0, 2], [4, 6]]) z`f` must be callable.r)�callable� TypeErrorr9r=�_newr7r8)rV�f�dok�kr)�fvs r � applyfunczSparseRepMatrix.applyfunc�s���$��{�{� 5��3�4�4� 4� ���J�J�L�L�&�&�(�(� � �D�A�q���1���B��Q�w�w���A����y�y���D�I�s�3�3�3r#c�$�ddlm}||��S)z,Returns an Immutable version of this Matrix.r )�ImmutableSparseMatrix)� immutablerj)rVrjs r � as_immutablezSparseRepMatrix.as_immutables%��4�4�4�4�4�4�$�$�T�*�*�*r#c� �t|��S)aCReturns a mutable version of this matrix. Examples ======== >>> from sympy import ImmutableMatrix >>> X = ImmutableMatrix([[1, 2], [3, 4]]) >>> Y = X.as_mutable() >>> Y[1, 1] = 5 # Can set values in Y >>> Y Matrix([ [1, 2], [3, 5]]) )�MutableSparseMatrixrUs r � as_mutablezSparseRepMatrix.as_mutable$s��#�4�(�(�(r#c����fd�t�������d����D��S)a�Returns a column-sorted list of non-zero elements of the matrix. Examples ======== >>> from sympy import SparseMatrix >>> a=SparseMatrix(((1, 2), (3, 4))) >>> a Matrix([ [1, 2], [3, 4]]) >>> a.CL [(0, 0, 1), (1, 0, 3), (0, 1, 2), (1, 1, 4)] See Also ======== sympy.matrices.sparse.SparseMatrix.row_list c�B��g|]}t|�|fz����Sr�r?�rrfrVs �r r!z,SparseRepMatrix.col_list.<locals>.<listcomp>Is+���l�l�l�!��a�4��7�*�n�%�%�l�l�lr#c�:�tt|����Sr-)r>�reversed)rfs r �<lambda>z*SparseRepMatrix.col_list.<locals>.<lambda>Is��Y]�^f�gh�^i�^i�Yj�Yj�r#��key)�sortedr9rBrUs`r �col_listzSparseRepMatrix.col_list5sE���(m�l�l�l�v�d�j�j�l�l�6G�6G�6I�6I�Oj�Oj�/k�/k�/k�l�l�l�lr#c�D�t|�����S)z2Returns the number of non-zero elements in Matrix.)r5r9rUs r �nnzzSparseRepMatrix.nnzKs���4�:�:�<�<� � � r#c����fd�t�������t���D��S)a�Returns a row-sorted list of non-zero elements of the matrix. Examples ======== >>> from sympy import SparseMatrix >>> a = SparseMatrix(((1, 2), (3, 4))) >>> a Matrix([ [1, 2], [3, 4]]) >>> a.RL [(0, 0, 1), (0, 1, 2), (1, 0, 3), (1, 1, 4)] See Also ======== sympy.matrices.sparse.SparseMatrix.col_list c�B��g|]}t|�|fz����Srrrrss �r r!z,SparseRepMatrix.row_list.<locals>.<listcomp>cs7���3�3�3�!��a�4��7�*�n�%�%�3�3�3r#rw)ryr9rBr>rUs`r �row_listzSparseRepMatrix.row_listOsO���(3�3�3�3� �4�:�:�<�<�$�$�&�&�D� 1� 1� 1�3�3�3� 3r#c� �||zS)z"Scalar element-wise multiplicationr)rV�scalars r �scalar_multiplyzSparseRepMatrix.scalar_multiplyfs ����}�r#rZc�N�|j}||z�|���|z|zS)a�Return the least-square fit to the data. By default the cholesky_solve routine is used (method='CH'); other methods of matrix inversion can be used. To find out which are available, see the docstring of the .inv() method. Examples ======== >>> from sympy import SparseMatrix, Matrix, ones >>> A = Matrix([1, 2, 3]) >>> B = Matrix([2, 3, 4]) >>> S = SparseMatrix(A.row_join(B)) >>> S Matrix([ [1, 2], [2, 3], [3, 4]]) If each line of S represent coefficients of Ax + By and x and y are [2, 3] then S*xy is: >>> r = S*Matrix([2, 3]); r Matrix([ [ 8], [13], [18]]) But let's add 1 to the middle value and then solve for the least-squares value of xy: >>> xy = S.solve_least_squares(Matrix([8, 14, 18])); xy Matrix([ [ 5/3], [10/3]]) The error is given by S*xy - r: >>> S*xy - r Matrix([ [1/3], [1/3], [1/3]]) >>> _.norm().n(2) 0.58 If a different xy is used, the norm will be higher: >>> xy += ones(2, 1)/10 >>> (S*xy - r).norm().n(2) 1.5 �rY)�Tr])rV�rhsrY�ts r �solve_least_squaresz#SparseRepMatrix.solve_least_squaresjs.��l �F���$��|�|�6�|�*�*�1�,�S�0�0r#c���|js@|j|jkrtd���|j|jkrtd���dS|�|����|��S)z�Return solution to self*soln = rhs using given inversion method. For a list of possible inversion methods, see the .inv() docstring. zUnder-determined system.z]For over-determined system, M, having more rows than columns, try M.solve_least_squares(rhs).r�N)� is_squarer7r8r'r]�multiply)rVr�rYs r �solvezSparseRepMatrix.solve�s��� �~� 9��y�4�9�$�$� �!;�<�<�<���T�Y�&�&� �"N�O�O�O�'�&��8�8�6�8�*�*�3�3�C�8�8� 8r#NzAlternate faster representationc� �t|��Sr-)rrUs r �liupczSparseRepMatrix.liupc�s���d�|�|�r#c� �t|��Sr-)rrUs r �row_structure_symbolic_choleskyz/SparseRepMatrix.row_structure_symbolic_cholesky�s��/��5�5�5r#Tc�$�t||���S�N)� hermitian)r�rVr�s r �choleskyzSparseRepMatrix.cholesky�s���� �:�:�:�:r#c�$�t||���Sr�)rr�s r �LDLdecompositionz SparseRepMatrix.LDLdecomposition�s��'�� �B�B�B�Br#c�"�t||��Sr-)r�rVr�s r �lower_triangular_solvez&SparseRepMatrix.lower_triangular_solve����-�d�C�8�8�8r#c�"�t||��Sr-)rr�s r �upper_triangular_solvez&SparseRepMatrix.upper_triangular_solve�r�r#)rZ)T)�__name__� __module__� __qualname__�__doc__� classmethodr@�propertyrWr_rhrlrorzr|rr�r�r��RL�CLr�r�r�r�r�r�rrrr� __classcell__)rQs@r rrs��������S�S�j�~$�~$�~$�~$��[�~$�@� � ��X� �L�L�L� 4�4�4�@+�+�+� )�)�)�"m�m�m�,!�!�!�3�3�3�.���71�71�71�71�r 9� 9� 9� 9� ��(�D�$�(I� J� J�B� ��(�D�$�(I� J� J�B����6�6�6�;�;�;�;�C�C�C�C�9�9�9�9�9�9�/5�n�E�M�.N�.V�#�+�.>�.F�H��.F�.N���.D�.L��"�.D�.L��"�"�"�"�"r#rc�$�eZdZed���ZdS)rnc�|�|j|i|��\}}}|�|||��}|�|��Sr-)r@�_smat_to_DomainMatrix�_fromrep)rrFrGr7r8r*�reps r rczMutableSparseMatrix._new�sI��6�3�6��G��G�G���d�D��'�'��d�D�9�9���|�|�C� � � r#N)r�r�r�r�rcrr#r rnrn�s-�������!�!��[�!�!�!r#rnN)�collections.abcr�sympy.core.containersr�sympy.utilities.exceptionsr�sympy.utilities.iterablesr�sympy.utilities.miscr� matrixbaser � repmatrixr r � utilitiesr �decompositionsrrrr�solversrrrrn� SparseMatrixrr#r �<module>r�sz��$�$�$�$�$�$�&�&�&�&�&�&�@�@�@�@�@�@�1�1�1�1�1�1�'�'�'�'�'�'�"�"�"�"�"�"�2�2�2�2�2�2�2�2�������������������D�D�D�D�D�D�D�D�vM�vM�vM�vM�vM�i�vM�vM�vM�r !�!�!�!�!�/�+;�!�!�!�#� � � r#
Memory