
    bi[\                         d Z dZg dZddl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 ddlmZmZmZmZmZmZmZmZ ddlmZmZmZmZ  G d de      Zd Z d Z! G d dee      Z" G d de	e      Z#y)zSparse DIAgonal formatzrestructuredtext en)	dia_array
dia_matrixisspmatrix_dia    N   )_prune_arraycopy_if_needed   )spmatrix)issparse_formats_spbasesparray)_data_matrix)isdenseisscalarlikeisshapeupcast_chargetdtypeget_sum_dtypevalidateaxischeck_shape)
dia_matmat
dia_matvecdia_matvecs	dia_tocsrc                   h    e Zd ZdZddddZd Zd ZddZej                  j                  e_	        ddZ
ej                  j                  e
_	        dd	Zej                  j                  e_	        dd
Z fdZd Z fdZd Zd Z fdZddZddZej(                  j                  e_	        ddZej*                  j                  e_	        ddZej,                  j                  e_	        ddZej.                  j                  e_	        ddZd Zej2                  j                  e_	         xZS )	_dia_basediaNmaxprintc                   t        j                  | ||       t        |      r|j                  dk(  rP|r|j	                         }|j
                  | _        |j                  | _        t        |j                        | _	        nl|j                  | j                  k(  r|r|j	                         }n|j                         }|j
                  | _        |j                  | _        t        |j                        | _	        nt        |t              r5t        |      r}t        |      | _	        t        j                  dt!        |t"                    | _        | j%                  t'        | j                              }t        j                  d|      | _        nY	 |\  }}	|t)        d      |st*        }t        j,                  t        j.                  |d   ||	            | _        t        j.                  |d
   | j%                  t'        |            |	      }	t        j0                  |	      | _        t        |      | _	        n	 t        j4                  |      }t        | t6              r(|j8                  dk7  rt)        d|j8                   d      | j;                  |||      j                         }|j
                  | _        |j                  | _        t        |j                        | _	        |-t!        |      }| j
                  j=                  |d      | _        | j                  j8                  d
k7  rt)        d      | j
                  j8                  dk7  rt)        d      | j
                  j                  d   t?        | j                        k7  r<t)        d| j
                  j                  d    dt?        | j                         d      t?        t        j@                  | j                              t?        | j                        k7  rt)        d      y # t2        $ r}
d}t)        |      |
d }
~
ww xY w# t2        $ r}
t)        d| j                   d      |
d }
~
ww xY w)Nr   r   )r   r   )defaultmaxvalr   dtypezexpected a shape argument)r&   copyr	   z+unrecognized form for dia_array constructorzunrecognized form for z_matrix constructorr   zDIA arrays don't support zD input. Use 2D)r&   shapeFr'   zoffsets array must have rank 1zdata array must have rank 2znumber of diagonals (z() does not match the number of offsets ()z&offset array contains duplicate values)!r   __init__r   formatr'   dataoffsetsr   r(   _shapetodia
isinstancetupler   npzerosr   float_get_index_dtypemax
ValueErrorr   
atleast_2darray
atleast_1d	Exceptionasarrayr   ndim_coo_containerastypelenunique)selfarg1r(   r&   r'   r    A	idx_dtyper-   r.   emessagenewdtypes                L/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/scipy/sparse/_dia.pyr+   z_dia_base.__init__   s   dD8<D>{{e#99;D II	#||)$**5;;$++-$		A

AFF	 yy)!''2e$t} *$/HHUHUE,JK	 11TZZ1I	!xx9=5$(MD'
 }()DEE- "bhhtAweRV.W XDI hhtAw-1-B-B#e*-B-U,02G $&==#9DL"-e"4DKMzz$' $(TYY!^ #<TYYK!WXX##DU#CIIKADI99DL%agg.DKH		(((>DI <<!=>>99>>Q:;;99??1T\\!22'		(:'; <"4<<014  ryy&'3t||+<<EFF =Y ! 5KG$W-145$  M !9$(KK=0C"E FKLMMs0   P P: 	P7$P22P7:	Q"QQ"c                     t         | j                     \  }}t        | t              rdnd}| j                  j
                  d   }d| d| d| j                   d| j                   d| d	| j
                   d
S )Nr:   matrixr   <z sparse z of dtype 'z'
	with z stored elements (z diagonals) and shape >)r   r,   r1   r   r-   r(   r&   nnz)rC   _fmt
sparse_clsds        rJ   __repr__z_dia_base.__repr__d   s|    $++&3 *4 9Wx
IIOOAuHZLDJJ< @hhZ1!4J4::,VWY	
    c                     | j                   \  }}t        j                  | j                  j                   d         }|| j                  dddf   z
  }|dk\  }|||k  z  }|||k  z  }|S )z~Returns a mask of the same shape as self.data, where
        mask[i,j] is True when data[i,j] corresponds to a stored element.r	   Nr   )r(   r3   aranger-   r.   )rC   num_rowsnum_colsoffset_indsrowmasks         rJ   
_data_maskz_dia_base._data_maskm   so     "ZZ(ii		 23DLL400qx x'(rU   c                     |t        d      | j                         }t        j                  | j                  |         S )Nz<count_nonzero over an axis is not implemented for DIA format)NotImplementedErrorr]   r3   count_nonzeror-   )rC   axisr\   s      rJ   r`   z_dia_base.count_nonzerox   s?    %N   		$00rU   c           	      ^   |t        d      | j                  \  }}t        | j                  j                  d   |      }t	        t        j                  t        j                  || j                  z   |      t        j                  | j                  d      z
  d      j                               S )Nz6_getnnz over an axis is not implemented for DIA formatr	   r   )
r_   r(   minr-   intr3   maximumminimumr.   sum)rC   ra   MNLs        rJ   _getnnzz_dia_base._getnnz   s    % '7 8 8zz1		"A&2::bjjT\\)91= jjq9:!!$( 	(rU   c           
      :   t        |      }t        | j                        }| j                  \  }}d }|dk(  r| j	                         }| j
                  |z  j                  d      }	|	j                  d   |k(  r|	}
n3t        j                  ||	j                        }
|	|
d |	j                  d    | j                  |
|      }nt        j                  |df|      }t        j                  ||      }t        ||t        | j                        | j
                  j                  d   | j                  | j
                  ||       | j                  |      }||j                  ||      S | j                  |j                  |            }|j                  d||      S )	Nr   r   )ra   r%   r	   )r&   out )ra   r&   rn   )r   r   r&   r(   r]   r-   rg   r3   r4   _ascontaineronesr   rA   r.   )rC   ra   r&   rn   	res_dtyperX   rY   retr\   xresrow_sumsones                rJ   rg   z_dia_base.sum   sf   D!!$**-	!ZZ(4<??$DT!&&A&.AwwqzX%hhxqww7#$KQWWQZ ##Cy#9C xx1Y?H''()4Cx3t||+<yyq)4<<CS ((2H|||%S|99##HLLdL$;<CwwBew55rU   c                 F   t        |t              s|j                  |       S t        j                  | j
                  |j
                        rG| j                  |r| j                  |j                  z
        S | j                  |j                  z         S t        j                  | j
                  |j
                        }t        j                  || j
                        }t        j                  ||j
                        }| j                  j                  d   }|j                  j                  d   }||k(  rut        |      t        | j
                        k(  rT| j                  t        |         }|r||d d fxx   |j                  z  cc<   nx||d d fxx   |j                  z  cc<   n[||k(  rqt        |      t        |j
                        k(  rP|r|j                  t        |          }n|j                  t        |         }||d d fxx   | j                  z  cc<   nt        | j                  d   |d   z   | j                  d         }	t        j                  t        |      |	ft        j                  | j                  |j                              }||d |fxx   | j                  d d d |	f   z  cc<   |r%||d |fxx   |j                  d d d |	f   z  cc<   n$||d |fxx   |j                  d d d |	f   z  cc<   | j!                  ||f| j                        S )Nr	   r   r%   r(   )r1   r   _add_sparser3   array_equalr.   
_with_datar-   union1dsearchsortedr(   rA   _invert_indexrc   r4   result_type_dia_container)
rC   othersubnew_offsetsself_idx	other_idxself_dother_dnew_datarS   s
             rJ   r{   z_dia_base._add_sparse   s   %+$$T** >>$,,6??S499uzz#9 ; ;#'99uzz#9; ; jju}}=??;=OOK?	#**""1% W[!1S5F!Fyyx!89HA&%**4&A&%**4&w3{#3s5==7I#I!JJ}Y'?@@ ::mI&>?Xq[!TYY.! DJJqMKO3TZZ]CA xx[!1%nnTYY

;H Xww&'499QU+;;'HWH,-ArrE1BB-HWH,-ArrE1BB-""Hk#:$**"MMrU   c                 h    t        |t              st        |   |      S | j	                  |d      S )NT)r   )r1   r   super_sub_sparser{   )rC   r   	__class__s     rJ   r   z_dia_base._sub_sparse   s3    %+7&u--400rU   c                 >    | j                  | j                  |z        S N)r}   r-   )rC   r   s     rJ   _mul_scalarz_dia_base._mul_scalar   s    tyy5011rU   c                 P   t        |      r| j                  |      S t        |      r|j                  dkD  r| j	                         |z  S d| j
                  v sd| j
                  v sd|j
                  v rt        |   |      S t        j                  |      }|j
                  \  }}| j
                  \  }}t        | j                  j
                  d   |      }| j                  d d d |f   j                  t        j                  | j                  |            }|dk(  r||dd |f   z  }n||k7  rt        d      t        j                  |      }||kD  r|| j                   d d d f   z
  |z  }	n|| j                   d d d f   |z  z
  }	|dk(  rd}n||k7  rt        d      |||	|f   z  }| j#                  |      S t%        |t&              r|j
                  | j
                  k7  rt        |   |      S t        j(                  | j                   |j                   dd      \  }
}}t        | j                  j
                  d   |j                  j
                  d         }| j                  |d |f   |j                  |d |f   z  }| j+                  ||
f| j
                        S )Nr   r   r	   zinconsistent shapesT)assume_uniquereturn_indicesrz   )r   r   r   r>   toarrayr(   r   multiplyr3   r9   rc   r-   r@   r   r8   rW   r.   r}   r1   r   intersect1dr   )rC   r   
other_rows
other_colsrowscolsrj   r-   jir.   r   r   r   s                rJ   r   z_dia_base.multiply   sk   ##E**5>zzA~||~-- DJJ!tzz/Q%++5Ew'..MM%(E%*[["J
JD$DIIOOA&-A99QU#**2>>$))U+KLDQa!e$t# !677IIaLt8T\\!T'22d:ADLLD1D88A?A4'$%:;;ad#??4(( %+u{{djj/H7#E**
 NN4<<)-dD 	%9 		"EJJ$4$4Q$78yy2A2&IrrM)BB""D'?$**"EErU   c                    |}t        j                  | j                  d   t        | j                  j
                  |j                  j
                              }| j                  j                  d   }| j                  \  }}t        ||t        | j                        || j                  | j                  |j                         |j                                |S )Nr   r%   r	   )r3   r4   r(   r   r&   charr-   r   rA   r.   ravel)rC   r   rt   yrj   rh   ri   s          rJ   _matmul_vectorz_dia_base._matmul_vector  s    HHTZZ]+djjoo78ww||+E F IIOOAjj!1QDLL)1dllDII779aggi	) rU   c                 T   t        j                  | j                  d   |j                  d   ft        j                  | j                  |            }t        g | j                  | j                  j                  | j                  | j                  |j                  d   ||  |S )Nr   r	   r%   )r3   r4   r(   r   r-   r   r.   )rC   r   ru   s      rJ   _matmul_multivectorz_dia_base._matmul_multivector)  s    hh

1u{{1~6^^DIIu=? 	0TZZ 	0$))// 	04<< 	0 	0KKN	0$)	0+.	0
rU   c                 l   t        |t              st        |   |      S d| j                  v sd|j                  v r-| j                  | j                  d   |j                  d   f      S t        g | j                  | j                  j                  | j                  | j                  |j                  d   |j                  j                  |j                  |j                   \  }}| j                  |j                  t        |      d      |f| j                  d   |j                  d   f      S )Nr   r	   ry   )r1   r   r   _matmul_sparser(   r   r   r-   r.   reshaperA   )rC   r   r.   r-   r   s       rJ   r   z_dia_base._matmul_sparse0  s   %+7)%00 

?a5;;.&&

1u{{1~'FGG" >DJJ > >#'<<>15>#(;;q>>49JJ4D4D> $)==> 38**> ""DLLWr$BG#L$(JJqM5;;q>#BD 	DrU   c                    | j                   \  }}|j                  dk(  rt        j                  }nt	        |      }|dk  rt        ||z   ||      }d}|}nt        |||z
  |      }|}||z   }|j                  dk7  r|d | }| j                  j                   \  }	}
|| j                  v ro||
kD  rIt        j                  |	|f| j                  j                        }| j                  |d d d |
f<   || _        || j                  | j                  |k(  ||f<   y t        j                  | j                  | j                  j                  j                  |            | _        t        ||
      }t        j                  |	dz   |f| j                  j                        }| j                  |d dd |
f<   ||d||f<   || _        y )Nr   r%   r	   ry   )r(   r>   r3   infrA   rc   r-   r.   r4   r&   appendtyper7   )rC   valueskrh   ri   values_nn	min_index	max_index	data_rows	data_colsr-   ms                rJ   _setdiagz_dia_base._setdiag@  s   zz1;;!vvH6{Hq5AE1h'AIIAq1uh'AIAI;;!BQZF#yy	99$xxI 6diiooN&*iiQ

]# 	@FDIIdlla'9)<<=99T\\4<<3E3E3J3J13MNDLIy)A88Y]A.diiooFD$(IID"jyj!,2DYy(()DIrU   c                 *    |r| j                         S | S r   r)   )rC   r'   s     rJ   r0   z_dia_base.todiae  s    99;KrU   c                    ||dk7  rt        d      | j                  \  }}t        | j                        }| j                   }t	        j
                  t        |      t        j                        d d d f   }t	        j
                  |t        j                        ||z  d d d f   z
  }t        d|| j                  j                  d   z
        }	t	        j                  | j                  t	        j                  | j                  j                  d   |	f| j                  j                        f      }
|
||f   }
| j                  |
|f||f|      S )N)r	   r   zvSparse arrays/matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.r%   r   r	   )r(   r'   )r8   r(   r7   r.   r3   rW   rA   intcr-   hstackr4   r&   r   )rC   axesr'   rX   rY   max_dimr.   rc
pad_amountr-   s              rJ   	transposez_dia_base.transposem  s3    L M M "ZZ(djj/ <<- IIc'l"''21d7;IIhbgg.'G2CQW1MMGDIIOOA$667
yy$))RXXtyyq/A:.N48IIOO&E F GAqDz""D'?h; &* # , 	,rU   c                    | j                   \  }}|| k  s||k\  r+t        j                  d| j                  j                        S t        j
                  | j                  |k(        \  }t        d|      }t        ||z   |      }||z
  }|j                  dk(  r+t        j                  || j                  j                        S | j                  |d   ||f   }|t        |      z
  }	|	dkD  rt        j                  |d|	fd      }|S )Nr   r%   constant)mode)r(   r3   emptyr-   r&   nonzeror.   r7   rc   sizer4   rA   pad)
rC   r   r   r   idx	first_collast_colresult_sizeresultpaddings
             rJ   diagonalz_dia_base.diagonal  s    ZZ
d:d88ATYY__55zz$,,!+,1I	tax&*88q=88Ktyy??3q69X#556F+Q;VVFQLzBFrU   c                    d| j                   v st        | j                        dk(  r'| j                  | j                   | j                        S | j                   \  }}| j
                  }| j                  t        |||            }t        j                  | j                        j                  |d      }t        j                  || j                        }t        j                  ||      }t        j                  d|z   |      }	t        ||g| j                  j                   | j                  j                  |d      | j                  ||||	 }
| j                  t        |
||            }t        |d |
       }t        |d |
 j                  |d            }|	j                  |d      }	| j                  |||	f| j                   | j                        }d|_        |S )	Nr   r%   r#   Fr)   r	   )r(   r&   T)r(   rA   r.   _csr_containerr&   rO   r6   r7   r3   argsortr@   r   r   r-   r   has_canonical_format)rC   r'   n_rowsn_colsmax_nnzrF   ordercsr_dataindicesindptrrO   rn   s               rJ   tocsrz_dia_base.tocsr  s   

?c$,,/14&&tzz&DD((
 ))Wff1M)N	

4<<(//	/F88G4::6((7)4!f*I6 : :++IE+B:DHII::':)0:28: ))S&&1I)J	#/wt}33IE3JKyu5!!8Wf"=(,

$** " F#' 
rU   c                     |r7| j                  || j                  j                         f| j                        S | j                  || j                  f| j                        S )zReturns a matrix with the same sparsity structure as self,
        but with different data.  By default the structure arrays are copied.
        rz   )r   r.   r'   r(   )rC   r-   r'   s      rJ   r}   z_dia_base._with_data  sf     &&t||((*+4:: '   &&t||$DJJ '  rU   c                    t        |      }|\  }}| j                  d d d |f   | _        || j                  d   kD  rt        j                  | j
                  | j                  d   z   | j                  j                  d   k        r_| j
                  d d d f   | j                  d   z   t        j                  | j                  j                  d         k  }d| j                  |<   || _        y )Nr   r	   )r   r-   r(   r3   anyr.   rW   r/   )rC   r(   rh   ri   r\   s        rJ   resizez_dia_base.resize  s    E"1IIa!e$	

1t||djjm3diiooa6HHILLD)DJJqM9IIdiiooa012DDIIdOrU   )NNFr   )NNN)Frm   )NF)T)__name__
__module____qualname___formatr+   rT   r]   r`   r   __doc__rk   rg   r{   r   r   r   r   r   r   r   r0   r   r   r   r}   r   __classcell__)r   s   @rJ   r   r      s"   GKGT KGZ
	1 $1199M( oo--GO6@ ++%%CK-N^12/FbD #J MM))EM,,  ))11I  ''//H: MM))EM ^^++FNNrU   r   c                 r    t        j                  |       }t        j                  t        |             || <   |S )z)Helper function to invert an index array.)r3   
zeros_likerW   rA   )r   invs     rJ   r   r     s+    
--
CyyS"CHJrU   c                 "    t        | t              S )a  Is `x` of dia_matrix type?

    Parameters
    ----------
    x
        object to check for being a dia matrix

    Returns
    -------
    bool
        True if `x` is a dia matrix, False otherwise

    Examples
    --------
    >>> from scipy.sparse import dia_array, dia_matrix, coo_matrix, isspmatrix_dia
    >>> isspmatrix_dia(dia_matrix([[5]]))
    True
    >>> isspmatrix_dia(dia_array([[5]]))
    False
    >>> isspmatrix_dia(coo_matrix([[5]]))
    False
    )r1   r   )rt   s    rJ   r   r     s    . a$$rU   c                       e Zd ZdZy)r   a<  
    Sparse array with DIAgonal storage.

    This can be instantiated in several ways:
        dia_array(D)
            where D is a 2-D ndarray

        dia_array(S)
            with another sparse array or matrix S (equivalent to S.todia())

        dia_array((M, N), [dtype])
            to construct an empty array with shape (M, N),
            dtype is optional, defaulting to dtype='d'.

        dia_array((data, offsets), shape=(M, N))
            where the ``data[k,:]`` stores the diagonal entries for
            diagonal ``offsets[k]`` (See example below)

    Attributes
    ----------
    dtype : dtype
        Data type of the array
    shape : 2-tuple
        Shape of the array
    ndim : int
        Number of dimensions (this is always 2)
    nnz
    size
    data
        DIA format data array of the array
    offsets
        DIA format offset array of the array
    T

    Notes
    -----

    Sparse arrays can be used in arithmetic operations: they support
    addition, subtraction, multiplication, division, and matrix power.
    Sparse arrays with DIAgonal storage do not support slicing.

    Examples
    --------

    >>> import numpy as np
    >>> from scipy.sparse import dia_array
    >>> dia_array((3, 4), dtype=np.int8).toarray()
    array([[0, 0, 0, 0],
           [0, 0, 0, 0],
           [0, 0, 0, 0]], dtype=int8)

    >>> data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0)
    >>> offsets = np.array([0, -1, 2])
    >>> dia_array((data, offsets), shape=(4, 4)).toarray()
    array([[1, 0, 3, 0],
           [1, 2, 0, 4],
           [0, 2, 3, 0],
           [0, 0, 3, 4]])

    >>> from scipy.sparse import dia_array
    >>> n = 10
    >>> ex = np.ones(n)
    >>> data = np.array([ex, 2 * ex, ex])
    >>> offsets = np.array([-1, 0, 1])
    >>> dia_array((data, offsets), shape=(n, n)).toarray()
    array([[2., 1., 0., ..., 0., 0., 0.],
           [1., 2., 1., ..., 0., 0., 0.],
           [0., 1., 2., ..., 0., 0., 0.],
           ...,
           [0., 0., 0., ..., 2., 1., 0.],
           [0., 0., 0., ..., 1., 2., 1.],
           [0., 0., 0., ..., 0., 1., 2.]])
    Nr   r   r   r   ro   rU   rJ   r   r         HrU   r   c                       e Zd ZdZy)r   aO  
    Sparse matrix with DIAgonal storage.

    This can be instantiated in several ways:
        dia_matrix(D)
            where D is a 2-D ndarray

        dia_matrix(S)
            with another sparse array or matrix S (equivalent to S.todia())

        dia_matrix((M, N), [dtype])
            to construct an empty matrix with shape (M, N),
            dtype is optional, defaulting to dtype='d'.

        dia_matrix((data, offsets), shape=(M, N))
            where the ``data[k,:]`` stores the diagonal entries for
            diagonal ``offsets[k]`` (See example below)

    Attributes
    ----------
    dtype : dtype
        Data type of the matrix
    shape : 2-tuple
        Shape of the matrix
    ndim : int
        Number of dimensions (this is always 2)
    nnz
    size
    data
        DIA format data array of the matrix
    offsets
        DIA format offset array of the matrix
    T

    Notes
    -----

    Sparse matrices can be used in arithmetic operations: they support
    addition, subtraction, multiplication, division, and matrix power.
    Sparse matrices with DIAgonal storage do not support slicing.

    Examples
    --------

    >>> import numpy as np
    >>> from scipy.sparse import dia_matrix
    >>> dia_matrix((3, 4), dtype=np.int8).toarray()
    array([[0, 0, 0, 0],
           [0, 0, 0, 0],
           [0, 0, 0, 0]], dtype=int8)

    >>> data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0)
    >>> offsets = np.array([0, -1, 2])
    >>> dia_matrix((data, offsets), shape=(4, 4)).toarray()
    array([[1, 0, 3, 0],
           [1, 2, 0, 4],
           [0, 2, 3, 0],
           [0, 0, 3, 4]])

    >>> from scipy.sparse import dia_matrix
    >>> n = 10
    >>> ex = np.ones(n)
    >>> data = np.array([ex, 2 * ex, ex])
    >>> offsets = np.array([-1, 0, 1])
    >>> dia_matrix((data, offsets), shape=(n, n)).toarray()
    array([[2., 1., 0., ..., 0., 0., 0.],
           [1., 2., 1., ..., 0., 0., 0.],
           [0., 1., 2., ..., 0., 0., 0.],
           ...,
           [0., 0., 0., ..., 2., 1., 0.],
           [0., 0., 0., ..., 1., 2., 1.],
           [0., 0., 0., ..., 0., 1., 2.]])
    Nr   ro   rU   rJ   r   r   D  r   rU   r   )$r   __docformat____all__numpyr3   
_lib._utilr   r   _matrixr
   _baser   r   r   r   _datar   _sputilsr   r   r   r   r   r   r   r   _sparsetoolsr   r   r   r   r   r   r   r   r   ro   rU   rJ   <module>r      sv    %
7  5  7 7    I H, ,D%6I	7 IXI9 IrU   