Ë
    7^(hQ9  ã                   óÂ   — d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 ddl
mZ ddlmZmZ dd	lmZ dd
lmZmZmZmZ ddlmZmZ  G d„ de«      Z G d„ dee«      ZeZy)é    )Ú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                   óœ  ‡ — e Zd ZdZeˆ fd„«       Zed„ «       Zd„ Zd„ Z	d„ Z
d„ Zd„ Zd	„ Zd
„ Zd„ Zdd„Zdd„Z eeddd«      Z eeddd«      Zd„ Zd„ Zdd„Zdd„Zd„ Zd„ Zej                  e_        ej                  e_        ej                  e_        ej                  e_        ej                  e_        ej                  e_        ˆ xZS )Ú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        |«      dk(  rIt        |d   t        «      r6|d   j                  }|d   j                  }|d   j                  «       Š||‰fS i Št        |«      dk(  r|d   €d d |d   g}t        |«      dk(  �rq|d d \  }}||cxu r€n nd x}}n-d ||fv rt        d«      ‚t        |d   «      t        |d   «      }}t        |d   t        «      r¶|d   }d ||fv rt        dj                  ||«      «      ‚t        |«      D �cg c]  }| j                  |«      ‘Œ }	}t        |«      D �
cg c]  }
| j                  |
«      ‘Œ }}
|	D ]8  }|D ]1  }
| j                   |||
«      «      }|| j                  k7  sŒ+|‰||
f<   Œ3 Œ: ||‰fS t        |d   t        t        f«      råˆfd„}|d   j                  «       D ]É  \  \  }}}t        |t        «      r:|j                  «       j                  «       D ]  \  \  }}
} |||z   ||
z   |«       Œ ŒSt        |t         t"        f«      r7 | j$                  |fi |¤Ž\  }}Š‰D ]  \  }}
 |||z   ||
z   ‰||
f   «       Œ Œ | j                  |«      } |||| j                  |«      «       ŒË nÏt'        |d   «      rÁt)        d„ |d   D «       «       }|s | j$                  |d   fi |¤Ž\  }}ŠnŽ|d   }t        |«      ||z  k7  r%t        d	j                  t        |«      ||«      «      ‚t        |«      D ]E  }t        |«      D ]5  }
|||z  |
z      }| j                  |«      }|| j                  k7  sŒ/|‰||
f<   Œ7 ŒG |€C‰j+                  «       }|rt-        d
„ |D «       «      dz   nd}|rt-        d„ |D «       «      dz   nd}nM‰j+                  «       D ]:  \  }}
|r||k\  s	|
sŒ|
|k\  sŒt        dj                  ||
fd|dz
  d|dz
  «      «      ‚ ||‰fS t        |«      dk(  r°t        |d   t         t"        f«      r—|d   }d}t/        |«      D ]l  \  }}t        |t         t"        f«      s|g}t/        |«      D ]+  \  }
}|| j                  k7  sŒ| j                  |«      ‰||
f<   Œ- t-        |t        |«      «      }Œn |rt        |«      nd}|}||‰fS t1        ‰| �H  |Ž \  }}}t        |«      D ]4  }t        |«      D ]$  }
|||z  |
z      }|| j                  k7  sŒ|‰||
f<   Œ& Œ6 ||‰fS c c}w c c}
w )Nr	   r   é   é   z*Pass rows=None and no cols for autosizing.z2{} and {} must be integers for this specification.c           	      ó€   •— |r;| |f‰v r-|‰| |f   k7  r#t        dj                  | |f|‰| |f   «      «      ‚|‰| |f<   y y )Nz)There is a collision at {} for {} and {}.)Ú
ValueErrorÚformat)ÚiÚjÚvÚsmats      €úS/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/matrices/sparse.pyÚupdatez7SparseRepMatrix._handle_creation_inputs.<locals>.update–   s]   ø€ áØ˜q˜6 T™>¨a°4¸¸1¸±:ªoÜ",Ø Kß!'¡¨¨A¨°°4¸¸1¸±:Ó!>ó#ð ð &'˜˜Q ˜Tš
ð ó    c              3   ó2   K  — | ]  }t        |«      –— Œ y ­w©Nr   )Ú.0r   s     r    ú	<genexpr>z:SparseRepMatrix._handle_creation_inputs.<locals>.<genexpr>®   s   è ø€ Ò?°!œ{¨1Ÿ~Ñ?ùs   ‚zMThe length of the flat list ({}) does not match the specified size ({} * {}).c              3   ó&   K  — | ]	  \  }}|–— Œ y ­wr$   © )r%   ÚrÚ_s      r    r&   z:SparseRepMatrix._handle_creation_inputs.<locals>.<genexpr>Ä   ó   è ø€ Ò.¡  Aœ1Ñ.ùó   ‚c              3   ó&   K  — | ]	  \  }}|–— Œ y ­wr$   r(   )r%   r*   Úcs      r    r&   z:SparseRepMatrix._handle_creation_inputs.<locals>.<genexpr>Å   r+   r,   z?The location {} is out of the designated range[{}, {}]x[{}, {}])ÚlenÚ
isinstancer
   ÚrowsÚcolsÚtodokr   r   r   r   ÚrangeÚ_sympifyÚzeroÚdictr   ÚitemsÚlistÚtupleÚ_handle_creation_inputsr   ÚanyÚkeysÚmaxÚ	enumerateÚsuper)ÚclsÚargsÚkwargsr1   r2   r)   r.   Úopr   Úrow_indicesr   Úcol_indicesÚvaluer!   r   Úvvr*   ÚflatÚ	flat_listr=   ÚrowÚmatr   Ú	__class__s                         @€r    r;   z'SparseRepMatrix._handle_creation_inputsk   s�  ù€ äˆt‹9˜Š>œj¨¨a©´*Ô=Ø˜‘7—<‘<ˆDØ˜‘7—<‘<ˆDØ˜‘7—=‘=“?ˆDØ˜˜tÐ#Ð#àˆäˆt‹9˜Š>˜d 1™g˜oØ˜$  Q¡Ð(ˆDäˆt‹9˜‹>Ø˜˜�8‰DˆAˆqØ�A�~Ø"Ð"�‘tØ˜!˜Q˜‘Ü Ø@óBð Bô $ D¨¡G›_¬f°T¸!±W«o�d�ä˜$˜q™'¤8Ô,Ø˜!‘W�à˜D $˜<Ñ'Ü$ð)ß)/©°°dÓ);ó=ð =ô 9>¸d»ÖD°1˜sŸ|™|¨A�ÐD�ÐDÜ8=¸d»ÖD°1˜sŸ|™|¨A�ÐD�ÐDà$ò /�AØ(ò /˜Ø #§¡©R°°1«XÓ 6˜Ø  C§H¡HÓ,Ø).˜D  A šJñ/ð/ð ˜T 4Ð'Ð'ä˜D ™G¤d¬D \Ô2ô'ð "& a¡§¡£ò 
6‘I‘F�Q˜˜AÜ! !¤ZÔ0Ø*+¯'©'«)¯/©/Ó*;ò 5™J™F˜Q  BÙ" 1 q¡5¨!¨a©%°Õ4ñ5ä# A¬¬e }Ô5Ø%@ S×%@Ñ%@ÀÑ%MÀfÑ%M™
˜˜1˜dØ$(ò =™D˜A˜qÙ" 1 q¡5¨!¨a©%°°a¸°d±Õ<ñ=ð  ŸL™L¨›O˜Ù˜q ! S§\¡\°!£_Õ5ñ
6ô ˜T !™WÔ%ÜÑ?°t¸A±wÔ?Ó?Ð?�Ùà3˜×3Ñ3°D¸±GÑF¸vÑFñ �A�q™$ð !% Q¡�IÜ˜9“~¨°©Ò4Ü(ðBç#™V¤C¨	£N°D¸$Ó?óð ô # 4›[ò 3˜Ü!& t£ò 3˜AØ$-¨a°©f°q©jÑ$9˜EØ$'§L¡L°Ó$7˜EØ$¨¯©Ó0Ø-2  Q¨ T¢
ñ	3ð3ð ˆ|Ø—y‘y“{�Ù6:”sÑ.¨Ô.Ó.°Ò2À�Ù6:”sÑ.¨Ô.Ó.°Ò2À‘ð !ŸI™I›Kò ‘D�A�qÙ˜Q $šYª!°°T³	Ü(ð0ç#™V Q¨ F¨A¨t°a©x¸¸DÀ1¹HÓEóð ðð ˜˜tÐ#Ð#ä�‹Y˜!Š^¤
¨4°©7´T¼5°MÔ Bà�Q‘ˆAØˆAÜ# A›,ò %‘��3Ü! #¬¬e }Ô5Ø˜%�CÜ& s›^ò 6‘E�A�rØ˜SŸX™X“~Ø%(§\¡\°"Ó%5˜˜Q ˜Tš
ð6ô ˜œ3˜s›8Ó$‘ð%ñ ”3�q”6 AˆDØˆDØ˜˜tÐ#Ð#ô $™gÑ=¸tÐD‰OˆD�$˜Ü˜4“[ò +�Ü˜t›ò +�AØ  Q¡¨¡
™O�EØ §¡Ó(Ø%*˜˜Q ˜Tš
ñ+ð+ð ˜˜tÐ#Ð#ùòA EùÚDs   ÄTÄ7Tc                 ó>   — t        ddd¬«       | j                  «       S )Nz�
            The private _smat attribute of SparseMatrix is deprecated. Use the
            .todok() method instead.
            z1.9z$deprecated-private-matrix-attributes)Údeprecated_since_versionÚactive_deprecations_target)r   r3   ©Úselfs    r    Ú_smatzSparseRepMatrix._smatì   s'   € ô 	"ðð &+Ø'Mõ	
ð �z‰z‹|Ðr"   c                 ó’   — | j                  |j                  dd«      |j                  dt        «      |j                  dd«      ¬«      S )NÚmethodÚLDLÚ
iszerofuncÚtry_block_diagF)rU   rW   rX   )ÚinvÚgetr   )rR   rC   s     r    Ú_eval_inversezSparseRepMatrix._eval_inverseú   sD   € Ø�x‰x˜vŸz™z¨(°EÓ:Ø#)§:¡:¨l¼GÓ#DØ'-§z¡zÐ2BÀEÓ'Jð ó Lð 	Lr"   c                 óò   — t        |«      st        d«      ‚i }| j                  «       j                  «       D ]  \  }} ||«      }|dk7  sŒ|||<   Œ | j	                  | j
                  | j                  |«      S )aX  Apply 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Ú	TypeErrorr3   r8   Ú_newr1   r2   )rR   ÚfÚdokÚkr   Úfvs         r    Ú	applyfunczSparseRepMatrix.applyfuncÿ   sv   € ô$ ˜Œ{ÜÐ3Ó4Ð4ð
 ˆØ—J‘J“L×&Ñ&Ó(ò 	‰DˆAˆqÙ�1“ˆBØ�Q‹wØ��A’ð	ð
 �y‰y˜Ÿ™ D§I¡I¨sÓ3Ð3r"   c                 ó   — ddl m}  || «      S )z,Returns an Immutable version of this Matrix.r	   )ÚImmutableSparseMatrix)Ú	immutablerf   )rR   rf   s     r    Úas_immutablezSparseRepMatrix.as_immutable  s   € å4Ù$ TÓ*Ð*r"   c                 ó   — t        | «      S )aC  Returns 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]])
        )ÚMutableSparseMatrixrQ   s    r    Ú
as_mutablezSparseRepMatrix.as_mutable$  s   € ô # 4Ó(Ð(r"   c                 óœ   — t        | j                  «       j                  «       d„ ¬«      D �cg c]  }t        || |   fz   «      ‘Œ c}S c c}w )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                 ó*   — t        t        | «      «      S r$   )r9   Úreversed)rb   s    r    ú<lambda>z*SparseRepMatrix.col_list.<locals>.<lambda>I  s   € ÔY]Ô^fÐghÓ^iÓYj€ r"   ©Úkey)Úsortedr3   r=   r:   ©rR   rb   s     r    Úcol_listzSparseRepMatrix.col_list5  s>   € ô( 06°d·j±j³l×6GÑ6GÓ6IÑOjÔ/kÖl¨!”�a˜4 ™7˜*‘nÕ%ÒlÐlùÒls   ­A	c                 ó4   — t        | j                  «       «      S )z2Returns the number of non-zero elements in Matrix.)r/   r3   rQ   s    r    ÚnnzzSparseRepMatrix.nnzK  s   € ä�4—:‘:“<Ó Ð r"   c                 ó¢   — t        | j                  «       j                  «       t        ¬«      D �cg c]  }t	        || |   fz   «      ‘Œ c}S c c}w )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
        rp   )rr   r3   r=   r9   r:   rs   s     r    Úrow_listzSparseRepMatrix.row_listO  sE   € ô* �4—:‘:“<×$Ñ$Ó&¬DÔ1ö3¨!”�a˜4 ™7˜*‘nÕ%ò 3ð 	3ùò 3s   °Ac                 ó   — || z  S )z"Scalar element-wise multiplicationr(   )rR   Úscalars     r    Úscalar_multiplyzSparseRepMatrix.scalar_multiplyf  s   € à˜‰}Ðr"   c                 óP   — | j                   }|| z  j                  |¬«      |z  |z  S )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

        ©rU   )ÚTrY   )rR   ÚrhsrU   Úts       r    Úsolve_least_squaresz#SparseRepMatrix.solve_least_squaresj  s-   € ðl �F‰FˆØ�$‘�|‰| 6ˆ|Ó*¨1Ñ,¨SÑ0Ð0r"   c                 óî   — | j                   sI| j                  | j                  k  rt        d«      ‚| j                  | j                  kD  rt        d«      ‚y| j	                  |¬«      j                  |«      S )z–Return solution to self*soln = rhs using given inversion method.

        For a list of possible inversion methods, see the .inv() docstring.
        zUnder-determined system.z]For over-determined system, M, having more rows than columns, try M.solve_least_squares(rhs).r}   N)Ú	is_squarer1   r2   r   rY   Úmultiply)rR   r   rU   s      r    ÚsolvezSparseRepMatrix.solve£  so   € ð
 �~Š~Ø�y‰y˜4Ÿ9™9Ò$Ü Ð!;Ó<Ð<Ø—‘˜TŸY™YÒ&Ü ð "Nó Oð Oð 'ð —8‘8 6�8Ó*×3Ñ3°CÓ8Ð8r"   NzAlternate faster representationc                 ó   — t        | «      S r$   )r   rQ   s    r    ÚliupczSparseRepMatrix.liupc´  s   € Ü�d‹|Ðr"   c                 ó   — t        | «      S r$   )r   rQ   s    r    Úrow_structure_symbolic_choleskyz/SparseRepMatrix.row_structure_symbolic_cholesky·  s   € Ü/°Ó5Ð5r"   c                 ó   — t        | |¬«      S ©N)Ú	hermitian)r   ©rR   rŒ   s     r    ÚcholeskyzSparseRepMatrix.choleskyº  s   € Ü °	Ô:Ð:r"   c                 ó   — t        | |¬«      S r‹   )r   r�   s     r    ÚLDLdecompositionz SparseRepMatrix.LDLdecomposition½  s   € Ü'¨¸	ÔBÐBr"   c                 ó   — t        | |«      S r$   )r   ©rR   r   s     r    Úlower_triangular_solvez&SparseRepMatrix.lower_triangular_solveÀ  ó   € Ü-¨d°CÓ8Ð8r"   c                 ó   — t        | |«      S r$   )r   r’   s     r    Úupper_triangular_solvez&SparseRepMatrix.upper_triangular_solveÃ  r”   r"   )rV   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr;   ÚpropertyrS   r[   rd   rh   rk   rt   rv   rx   r{   r�   r…   ÚRLÚCLr‡   r‰   rŽ   r�   r“   r–   r   r   r   r   Ú__classcell__)rM   s   @r    r   r      s  ø„ ñSðj ó~$ó ð~$ð@ ñó ðòLò
4ò@+ò
)ò"mò,!ò3ò.ó71ór9ñ 
�(˜D $Ð(IÓ	J€BÙ	�(˜D $Ð(IÓ	J€Bòò6ó;óCò9ò9ð /5¯n©n€E„MØ.N×.VÑ.VÐ#Ô+Ø.>×.FÑ.F€HÔØ.F×.NÑ.NÐÔØ.D×.LÑ.LÐÔ"Ø.D×.LÑ.LÐ×"Ð"r"   r   c                   ó   — e Zd Zed„ «       Zy)rj   c                 óv   —  | j                   |i |¤Ž\  }}}| j                  |||«      }| j                  |«      S r$   )r;   Ú_smat_to_DomainMatrixÚ_fromrep)rA   rB   rC   r1   r2   r   Úreps          r    r_   zMutableSparseMatrix._newÐ  sC   € à6˜3×6Ñ6¸ÐGÀÑGÑˆˆd�Dà×'Ñ'¨¨d°DÓ9ˆà�|‰|˜CÓ Ð r"   N)r—   r˜   r™   r›   r_   r(   r"   r    rj   rj   Î  s   „ àñ!ó ñ!r"   rj   N)Úcollections.abcr   Úsympy.core.containersr   Úsympy.utilities.exceptionsr   Úsympy.utilities.iterablesr   Úsympy.utilities.miscr   Ú
matrixbaser
   Ú	repmatrixr   r   Ú	utilitiesr   Údecompositionsr   r   r   r   Úsolversr   r   r   rj   ÚSparseMatrixr(   r"   r    ú<module>r°      sT   ðÝ $å &Ý @Ý 1Ý 'å "ß 2å ÷ó ÷DôvM�iô vMôr!˜/Ð+;ô !ð #�r"   