
    \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  oR                  U5      PM     n	n[        U5       V
s/ s H  oR                  U
5      PM     nn
U	 H:  nU H1  n
U R                  U" X5      5      nXR                  :w  d  M+  UTX4'   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  pn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  pU" XX-   Xj-   TX4   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OU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XR                  :w  d  M/  UTX4'   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  pU(       a  X:  d  U
(       d  M  X:  d  M!  [        SR                  X4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  pXR                  :w  d  M  U R                  U5      TX4'   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XR                  :w  d  M  UTX4'   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      M/var/www/auris/envauris/lib/python3.13/site-packages/sympy/matrices/sparse.pyupdate7SparseRepMatrix._handle_creation_inputs.<locals>.update   sS    6T>a4:o", K!'4:!>#  &'QT
     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~~ws   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   t9>ja*==7<<D7<<D7==?Dt##t9>d1go$Q(Dt9>8DA~~""t! @B B $DG_fT!Wod$q'8,,!WD<'$))/);= = 9>dD1||AD8=dD1||AD$A( #RX 6 HH,).DJ ) % 4''DGdD\22' "&aIFQA!!Z00*+'')//*;JFQB"15!%4 +<#Ae}55%(%@%@%Mf%M
1d$(DA"15!%ad< %)  LLOqS\\!_5 "1 T!W%%?tAw???33DGFvF Aq$ !%QI9~4(B#VC	ND?  #4[!&tA$-afqj$9E$'LL$7E$0-2QT
	 "- ) |yy{6:s...26:s...2 !IIKDAQY!!	(0#VQFAtaxD1HE  ( t##Y!^
47T5M B BQAA#A,3!#e}55%C&s^EAXX~%(\\"%5QT
 , 3s8$ ' 3q6ADDt## $g=tDOD4[tA
OE(%*QT
 % ! 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	
 zz|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    xxvzz(E:#)::lG#D'-zz2BE'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      $ )zApply 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
 JJL&&(DA1BQwA )
 yyDIIs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      $ )zReturns 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]^fgh^iYjr$   key)sortedr6   r@   r=   rV   rh   s     r!   col_listSparseRepMatrix.col_list5  sB    ( 06djjl6G6G6IOj/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 FF||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      $ )zReturn 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    
 ~~yy499$ !;<<TYY&  "N O O ' 8868*33C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1r9 
(D$(I	JB	(D$(I	JB6;C99 /5nnEM.N.V.V#+.>.F.FH.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$   