ó
    ‰*£hQ9  ã                   óÂ   • S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	  SSK
Jr  SSKJrJr  SS	KJr  SS
KJrJrJrJr  SSKJrJr   " S S\5      r " S S\\5      r\rg)é    )Ú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                   ó®  ^ • \ rS rSrSr\U 4S j5       r\S 5       rS r	S r
S rS rS	 rS
 rS rS rSS jrSS jr\" \SSS5      r\" \SSS5      rS rS rSS jrSS jrS rS r\R                  \l        \R                  \l        \R                  \l        \R                  \l        \R                  \l        \R                  \l        SrU =r $ )ÚSparseRepMatrixé   a  
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                 óà
  >^• [        U5      S:X  aM  [        US   [        5      (       a5  US   R                  nUS   R                  nUS   R                  5       mX4T4$ 0 m[        U5      S:X  a  US   c  S S US   /n[        U5      S:X  Ga¯  US S u  pVXVs=L a  c  O  OS =p4O-S XV4;   a  [        S5      e[        US   5      [        US   5      pC[        US   [        5      (       a¼  US   nS X44;   a  [        SR                  X45      5      e[        U5       Vs/ s H  o€R                  U5      PM     n	n[        U5       V
s/ s H  o R                  U
5      PM     nn
U	 H:  nU H1  n
U R                  U" XŠ5      5      nXÀR                  :w  d  M+  UTXŠ4'   M3     M<     X4T4$ [        US   [        [        45      (       aï  U4S jnUS   R                  5        HÑ  u  u  pVn[        U[        5      (       a:  UR                  5       R                  5        H  u  u  pŠnU" XX-   Xj-   U5        M     MW  [        U[         ["        45      (       a6  U R$                  " U40 UD6u    nmT H  u  pŠU" XX-   Xj-   TXŠ4   5        M     M¨  U R                  U5      nU" XVU R                  U5      5        MÓ     Oß['        US   5      (       aÌ  [)        S US    5       5      (       + nU(       d  U R$                  " US   40 UD6u    nmO�US   n[        U5      X4-  :w  a$  [        S	R                  [        U5      X45      5      e[        U5       HG  n[        U5       H5  n
UX„-  U
-      nU R                  U5      nXÀR                  :w  d  M/  UTXŠ4'   M7     MI     UcM  TR+                  5       nU(       a  [-        S
 U 5       5      S-   OSnU(       a  [-        S U 5       5      S-   OSnOXTR+                  5        HD  u  pŠU(       a  Xƒ:¼  d  U
(       d  M  X¤:¼  d  M!  [        SR                  XŠ4SUS-
  SUS-
  5      5      e   X4T4$ [        U5      S:X  aÁ  [        US   [         ["        45      (       a£  US   nSn[/        U5       Hs  u  nn[        U[         ["        45      (       d  U/n[/        U5       H+  u  p¯XðR                  :w  d  M  U R                  U5      TXŠ4'   M-     [-        U[        U5      5      nMu     U(       a  [        U5      OSnUnX4T4$ [0        TU ]H  " U6 u  p4n[        U5       H6  n[        U5       H$  n
UXH-  U
-      nXÀR                  :w  d  M  UTXŠ4'   M&     M8     X4T4$ s  snf s  sn
f )Nr	   r   é   é   z*Pass rows=None and no cols for autosizing.z2{} and {} must be integers for this specification.c           	      ó†   >• U(       a9  X4T;   a+  UTX4   :w  a!  [        SR                  X4UTX4   5      5      eUTX4'   g g )Nz)There is a collision at {} for {} and {}.)Ú
ValueErrorÚformat)ÚiÚjÚvÚsmats      €ÚR/home/mande/repo/quber/.venv/lib/python3.13/site-packages/sympy/matrices/sparse.pyÚupdateÚ7SparseRepMatrix._handle_creation_inputs.<locals>.update–   sS   ø€ æØ˜6 T›>¨a°4¸¸±:«oÜ",Ø Kß!'¡¨¨°°4¸¸±:Ó!>ó#ð ð &'˜˜Q˜Tš
ð ó    c              3   ó8   #   • U  H  n[        U5      v •  M     g 7f©Nr   )Ú.0r   s     r!   Ú	<genexpr>Ú:SparseRepMatrix._handle_creation_inputs.<locals>.<genexpr>®   s   é € Ð?²w°!œ{¨1Ÿ~˜~²wùs   ‚zMThe length of the flat list ({}) does not match the specified size ({} * {}).c              3   ó*   #   • U  H	  u  pUv •  M     g 7fr&   © )r'   ÚrÚ_s      r!   r(   r)   Ä   ó   é € Ð.ª¡ �1ªùó   ‚c              3   ó*   #   • U  H	  u  pUv •  M     g 7fr&   r+   )r'   r-   Úcs      r!   r(   r)   Å   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Úkwargsr4   r5   r,   r1   Úopr   Úrow_indicesr   Úcol_indicesÚvaluer"   r   Úvvr-   ÚflatÚ	flat_listr@   ÚrowÚmatr    Ú	__class__s                         @€r!   r>   Ú'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Ø�~‰~Ø"Ð"��tØ˜!˜“Ü Ø@óBð Bô $ D¨¡G›_¬f°T¸!±W«o�dä˜$˜q™'¤8×,Ñ,Ø˜!‘W�à˜D˜<Ó'Ü$ð)ß)/©°Ó);ó=ð =ô 9>¸d¼ÓDº°1Ÿ|™|¨Až¹�ÐDÜ8=¸d¼ÓDº°1Ÿ|™|¨Až¹�ÐDã$�AÛ(˜Ø #§¡©R°«XÓ 6˜Ø §H¡HÕ,Ø).˜D  ›Jó )ñ %ð  4Ð'Ð'ä˜D ™G¤d¬D \×2Ñ2õ'ð "& a¡§¡¦‘I‘F�Q˜AÜ! !¤Z×0Ñ0Ø*+¯'©'«)¯/©/Ö*;™J™F˜Q BÙ" 1¡5¨!©%°Ö4ó +<ä# A¬¬e }×5Ñ5Ø%(×%@Ò%@ÀÑ%MÀfÑ%M™
˜˜1˜dÛ$(™D˜AÙ" 1¡5¨!©%°°a°d±Ö<ó %)ð  ŸL™L¨›O˜Ù˜q S§\¡\°!£_Ö5ò "1ô ˜T !™W×%Ñ%ÜÑ?°t¸A²wÓ?Ó?Ô?�Þà×3Ò3°D¸±GÑF¸vÑFñ �A�q™$ð !% Q¡�IÜ˜9“~¨©Ó4Ü(ðBç#™V¤C¨	£N°DÓ?óð ô # 4ž[˜Ü!& t¦˜AØ$-¨a©f°q©jÑ$9˜EØ$'§L¡L°Ó$7˜EØ$¯©Õ0Ø-2  Q T£
ó	 "-ñ )ð ‰|Ø—y‘y“{�Þ6:”sÑ.©Ó.Ó.°Ò2À�Þ6:”sÑ.©Ó.Ó.°Ò2À‘ð !ŸI™IžK‘D�AÞ˜Q›Y¯!¨!°µ	Ü(ð0ç#™V Q F¨A¨t°a©x¸¸DÀ1¹HÓEóð ñ (ð ˜tÐ#Ð#ä�‹Y˜!‹^¤
¨4°©7´T¼5°M× BÑ Bà�Q‘ˆAØˆAÜ# Až,‘��3Ü! #¬¬e }×5Ñ5Ø˜%�CÜ& sž^‘E�AØŸX™X•~Ø%(§\¡\°"Ó%5˜˜Q˜T›
ñ ,ô ˜œ3˜s›8Ó$’ñ 'ö ”3�q”6 AˆDØˆDØ˜tÐ#Ð#ô $™gÒ=¸tÐD‰OˆD˜Ü˜4–[�Ü˜tž�AØ ¡¨¡
™O�EØ§¡Õ(Ø%*˜˜Q˜T›
ó %ñ !ð ˜tÐ#Ð#ùòA EùÚDs   ÄU&ÅU+c                 ó8   • [        SSSS9  U R                  5       $ )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   r6   ©Úselfs    r!   Ú_smatÚSparseRepMatrix._smatì   s'   € ô 	"ðð &+Ø'Mò	
ð �z‰z‹|Ðr$   c                 óŒ   • U R                  UR                  SS5      UR                  S[        5      UR                  SS5      S9$ )NÚmethodÚLDLÚ
iszerofuncÚtry_block_diagF)rZ   r\   r]   )ÚinvÚgetr   )rV   rF   s     r!   Ú_eval_inverseÚSparseRepMatrix._eval_inverseú   sD   € Ø�x‰x˜vŸz™z¨(°EÓ:Ø#)§:¡:¨l¼GÓ#DØ'-§z¡zÐ2BÀEÓ'Jð ð Lð 	Lr$   c                 ó   • [        U5      (       d  [        S5      e0 nU R                  5       R                  5        H  u  p4U" U5      nUS:w  d  M  XRU'   M     U R	                  U R
                  U R                  U5      $ )zð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Ú	TypeErrorr6   r;   Ú_newr4   r5   )rV   ÚfÚdokÚkr   Úfvs         r!   Ú	applyfuncÚSparseRepMatrix.applyfuncÿ   so   € ô$ ˜�{‰{ÜÐ3Ó4Ð4ð
 ˆØ—J‘J“L×&Ñ&Ö(‰DˆAÙ�1“ˆBØ�Q�wØ�A“ñ )ð
 �y‰y˜Ÿ™ D§I¡I¨sÓ3Ð3r$   c                 ó   • SSK Jn  U" U 5      $ )z,Returns an Immutable version of this Matrix.r	   )ÚImmutableSparseMatrix)Ú	immutablerm   )rV   rm   s     r!   Úas_immutableÚSparseRepMatrix.as_immutable  s   € å4Ù$ TÓ*Ð*r$   c                 ó   • [        U 5      $ )zë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]])
)ÚMutableSparseMatrixrU   s    r!   Ú
as_mutableÚSparseRepMatrix.as_mutable$  s   € ô # 4Ó(Ð(r$   c                 óš   • [        U R                  5       R                  5       S S9 Vs/ s H  n[        XU   4-   5      PM     sn$ s  snf )a3  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                 ó*   • [        [        U 5      5      $ r&   )r<   Úreversed)rh   s    r!   Ú<lambda>Ú*SparseRepMatrix.col_list.<locals>.<lambda>I  s   € ÔY]Ô^fÐghÓ^iÔYjr$   ©Úkey)Úsortedr6   r@   r=   ©rV   rh   s     r!   Úcol_listÚSparseRepMatrix.col_list5  sB   € ô( 06°d·j±j³l×6GÑ6GÓ6IÑOjÒ/kÓlÒ/k¨!”�a ™7˜*‘nÖ%Ñ/kÑlÐlùÒls   ªAc                 ó4   • [        U R                  5       5      $ )z2Returns the number of non-zero elements in Matrix.)r2   r6   rU   s    r!   ÚnnzÚSparseRepMatrix.nnzK  s   € ä�4—:‘:“<Ó Ð r$   c                 ó    • [        U R                  5       R                  5       [        S9 Vs/ s H  n[	        XU   4-   5      PM     sn$ s  snf )a2  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
rz   )r|   r6   r@   r<   r=   r}   s     r!   Úrow_listÚSparseRepMatrix.row_listO  sL   € ô* �4—:‘:“<×$Ñ$Ó&¬DÒ1ó3Ú1ð +,”�a ™7˜*‘nÖ%Ù1ñ3ð 	3ùò 3s   ­Ac                 ó
   • X-  $ )z"Scalar element-wise multiplicationr+   )rV   Úscalars     r!   Úscalar_multiplyÚSparseRepMatrix.scalar_multiplyf  s
   € à‰}Ðr$   c                 óH   • U R                   nX0-  R                  US9U-  U-  $ )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

©rZ   )ÚTr^   )rV   ÚrhsrZ   Úts       r!   Úsolve_least_squaresÚ#SparseRepMatrix.solve_least_squaresj  s+   € ðl �F‰FˆØ‘�|‰| 6ˆ|Ð*¨1Ñ,¨SÑ0Ð0r$   c                 óö   • U R                   (       dK  U R                  U R                  :  a  [        S5      eU R                  U R                  :”  a  [        S5      egU R	                  US9R                  U5      $ )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_squarer4   r5   r   r^   Úmultiply)rV   r�   rZ   s      r!   ÚsolveÚ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                 ó   • [        U 5      $ r&   )r   rU   s    r!   ÚliupcÚSparseRepMatrix.liupc´  s   € Ü�d‹|Ðr$   c                 ó   • [        U 5      $ r&   )r   rU   s    r!   Úrow_structure_symbolic_choleskyÚ/SparseRepMatrix.row_structure_symbolic_cholesky·  s   € Ü/°Ó5Ð5r$   c                 ó   • [        XS9$ ©N)Ú	hermitian)r   ©rV   rž   s     r!   ÚcholeskyÚSparseRepMatrix.choleskyº  s   € Ü Ñ:Ð:r$   c                 ó   • [        XS9$ r�   )r   rŸ   s     r!   ÚLDLdecompositionÚ SparseRepMatrix.LDLdecomposition½  s   € Ü'¨ÑBÐBr$   c                 ó   • [        X5      $ r&   )r   ©rV   r�   s     r!   Úlower_triangular_solveÚ&SparseRepMatrix.lower_triangular_solveÀ  ó   € Ü-¨dÓ8Ð8r$   c                 ó   • [        X5      $ r&   )r   r¦   s     r!   Úupper_triangular_solveÚ&SparseRepMatrix.upper_triangular_solveÃ  r©   r$   r+   )r[   )T)!Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úclassmethodr>   ÚpropertyrW   r`   rj   ro   rs   r~   r�   r„   rˆ   r�   r”   ÚRLÚCLr—   rš   r    r£   r§   r«   r   r   r   r   Ú__static_attributes__Ú__classcell__)rP   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                   ó$   • \ rS rSr\S 5       rSrg)rr   iÎ  c                 ór   • U R                   " U0 UD6u  p4nU R                  X4U5      nU R                  U5      $ r&   )r>   Ú_smat_to_DomainMatrixÚ_fromrep)rD   rE   rF   r4   r5   r    Úreps          r!   re   ÚMutableSparseMatrix._newÐ  s=   € à×6Ò6¸ÐGÀÑGÑˆ�Dà×'Ñ'¨°DÓ9ˆà�|‰|˜CÓ Ð r$   r+   N)r­   r®   r¯   r°   r²   re   r¶   r+   r$   r!   rr   rr   Î  s   † àñ!ó ó!r$   rr   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   rr   ÚSparseMatrixr+   r$   r!   Ú<module>rÉ      sT   ðÝ $å &Ý @Ý 1Ý 'å "ß 2å ÷ó ÷DôvM�iô vMôr!˜/Ð+;ô !ð #�r$   