
    CpjO                         S r Sr/ SQrSSKrSSKJr  SSKJrJ	r	  SSK
JrJrJrJrJr  SS	KJr  SS
KJr   " S S\5      rS r " S S\\	5      r " S S\\5      rg)z#Compressed Sparse Row matrix formatzrestructuredtext en)	csr_array
csr_matrixisspmatrix_csr    N   )spmatrix)_spbasesparray)	csr_tocsc	csr_tobsrcsr_count_blocksget_csr_submatrixcsr_sample_values)upcast)
_cs_matrixc                     ^  \ rS rSrSrSrSS jr\R                  R                  \l        SS jr	\R                  R                  \	l        SS jr
\R                  R                  \
l        SU 4S jjr\R                  R                  \l        SS jr\R                  R                  \l        SS	 jr\R                  R                  \l        \S
 5       rS rS rS rS rS rS rS rS rS rS rS rS rS rS rSrU =r$ )	_csr_base   csr)r      c                     Ub  US:w  a  [        S5      eU R                  S:X  a  U(       a  U R                  5       $ U $ U R                  u  p4U R	                  U R
                  U R                  U R                  4XC4US9$ )N)r   r   zvSparse arrays/matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.r   shapecopy)
ValueErrorndimr   r   _csc_containerdataindicesindptr)selfaxesr   MNs        M/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/scipy/sparse/_csr.py	transpose_csr_base.transpose   s     L M M 99>"&499;0D0zz""DIIt||$(KK$19:T # K 	K    c                    U R                   S:w  a  [        S5      eU R                  U R                  U R                  S9nU R                  5         U R                  U R                  U R                  pTnUR                  UR                  pv[        U R                  S   5       H6  nX8   n	X8S-      n
XIU
 R                  5       Xh'   XYU
 R                  5       Xx'   M8     U$ )Nr   z.Cannot convert a 1d sparse array to lil formatdtyper   r   )r   r   _lil_containerr   r*   sum_duplicatesr   r   r   rowsrangetolist)r    r   lilptrinddatr-   r   nstartends              r$   tolil_csr_base.tolil$   s    99>MNN!!$**DJJ!?kk$,,tyyXXsxxdtzz!}%AFEc(Cn++-DGn++-DG	 & 
r'   c                 4    U(       a  U R                  5       $ U $ Nr   )r    r   s     r$   tocsr_csr_base.tocsr7   s    99;Kr'   c                 D   > [         TU ]  US9nU R                  Ul        U$ )Nr;   )supertocoohas_canonical_format)r    r   A	__class__s      r$   r@   _csr_base.tocoo?   s(    GMtM$ "&!:!:r'   c           
      z   U R                   S:w  a  [        S5      eU R                  u  p#U R                  U R                  U R
                  4[        U R                  U5      S9n[        R                  " US-   US9n[        R                  " U R                  US9n[        R                  " U R                  [        U R                  5      S9n[        X#U R                  R                  USS9U R
                  R                  USS9U R                  UUU5        U R                  XvU4U R                  S9nS	Ul        U$ )
Nr   z.Cannot convert a 1d sparse array to csc formatmaxvalr   r)   Fr;   r   T)r   r   r   _get_index_dtyper   r   maxnnznpemptyr   r*   r
   astyper   r   has_sorted_indices)	r    r   r"   r#   	idx_dtyper   r   r   rB   s	            r$   tocsc_csr_base.tocscI   s   99>MNNzz))4;;*E+.txx+; * =	!a%y1((48895xxtzz(:;!++$$YU$;,,%%ie%<))	  7tzzJ#r'   c                    U R                   S:w  a  [        S5      eUc  SSKJn  U R	                  U" U 5      S9$ US:X  aN  U R
                  R                  SSS5      U R                  U R                  4nU R                  X@R                  US9$ Uu  pVU R                  u  pxUS:  d  US:  d  Xu-  S	:w  d  X-  S	:w  a  [        S
U 35      e[        XxXVU R                  U R                  5      n	U R                  U R                  U R                  4[        X-  U	5      S9n
[        R                  " Xu-  S-   U
S9n[        R                  " XS9n[        R                   " XU4U R"                  S9n[%        XxXVU R                  R'                  U
SS9U R                  R'                  U
SS9U R
                  XUR)                  5       5
        U R                  XU4U R                  S9$ )Nr   z.Cannot convert a 1d sparse array to bsr formatr   )estimate_blocksize)	blocksize)r   r   r   r   zinvalid blocksize rF   r)   Fr;   rH   )r   r   _spfuncsrT   tobsrr   reshaper   r   _bsr_containerr   r   rI   rJ   rL   rM   zerosr*   r   rN   ravel)r    rU   r   rT   arg1RCr"   r#   blksrP   r   r   r   s                 r$   rX   _csr_base.tobsra   s   99>MNN4::(:4(@:AA%II%%b1-dll4;;GD&&t::D&II CA**CA1uA!quz #5i[!ABB#ADKKED--t{{DLL.I/214 . @IXXad1fI6Fhht5G88TAJdjj9DaAkk(((?ll)))%)@iitzz|	5 &&'tzz '  r'   c                     U $ )zBswap the members of x if this is a column-oriented matrix
         xs    r$   _swap_csr_base._swap   s	     r'   c              #     #    U R                   S:X  a  U R                  R                  S5      nSn[        U R                  U R
                  5       H&  u  p4[        X2-
  5       H  nUv   M	     Uv   US-   nM(     [        U R                  S   U-
  5       H  nUv   M	     g [        R                  " SU R                  R                  S9n[        U [        5      (       a  U R                  SS  OSU R                  S   4nSnU R                  SS   H=  n	X-
  US'   U R                  X n
U R
                  X nU R                  XU4USS9v   U	nM?     g 7f)Nr   r   r   r)   Tr   )r   r*   typezipr   r   r.   r   rL   r[   r   
isinstancer	   rC   )r    zerouvd_r   r   i0i1r   r   s               r$   __iter___csr_base.__iter__   s/    99>::??1%DADLL$))4quAJ &E	 5
 4::a=1,-
 .!4;;#4#45",T7";";

12!TZZPQ]AS++ab/BF1Ill2)G99R#D..$!8D.QQB "s   EEc                    U R                   S:X  a4  US;  a  [        SU S35      eU R                  SU R                  S   4SS9$ U R                  u  p#[	        U5      nUS:  a  X-  nUS:  d  X:  a  [        SU S35      e[        X#U R                  U R                  U R                  XS-   SU5	      u  pEnU R                  XeU4SU4U R                  SS	9$ )
zMReturns a copy of row i of the matrix, as a (1 x n)
CSR matrix (row vector).
r   )r   rV   index () out of ranger   Tr;   Fr   r*   r   )r   
IndexErrorrY   r   intr   r   r   r   rC   r*   r    ir"   r#   r   r   r   s          r$   _getrow_csr_base._getrow   s     99> 71#^!<==<<DJJqM 2<>>zzFq5FAq5AFwqc899 1$++t||TYYq5!Q!H~~tf5aV$(JJU  < 	<r'   c                 \   U R                   S:X  a  [        S5      eU R                  u  p#[        U5      nUS:  a  X-  nUS:  d  X:  a  [	        SU S35      e[        X#U R                  U R                  U R                  SX!US-   5	      u  pEnU R                  XeU4US4U R                  SS9$ )zLReturns a copy of column i. A (m x 1) sparse array (column vector).
        r   z4getcol not provided for 1d arrays. Use indexing A[j]r   rv   rw   Frx   )r   r   r   rz   ry   r   r   r   r   rC   r*   r{   s          r$   _getcol_csr_base._getcol   s     99>STTzzFq5FAq5AFwqc899 1$++t||TYY1Q!H~~tf5aV$(JJU  < 	<r'   c                     [         R                  " U R                  U:H  5      nUR                  (       a  U R                  US      $ U R                  R
                  R                  S5      $ Nr   )rL   flatnonzeror   sizer   r*   ri   )r    idxspots      r$   _get_int_csr_base._get_int   sL    ~~dllc129999T!W%%yy##A&&r'   c                     U[        S 5      :X  a  U R                  5       $ UR                  S;   a/  U R                  SUSS9nUR	                  UR
                  S   5      $ U R                  U5      $ )Nr   Nr   Tr;   rV   )slicer   step_get_submatrixrY   r   _minor_slice)r    r   rets      r$   
_get_slice_csr_base._get_slice   se    %+99;88y %%a4%8C;;syy}--  %%r'   c                    U R                  U R                  5      n[        R                  " XS9nUR                  S:X  a  U R                  / U R                  S9$ SU R                  S   pC[        R                  " XS9n[        R                  " XS9n[        R                  " UR                  U R                  S9n[        X4U R                  U R                  U R                  UR                  XVU5	        UR                  S   S:  a  UR                  OUR                  S   4nU R                  UR                  U5      5      $ )Nr)   r   r   )rI   r   rL   asarrayr   rC   r*   r   
zeros_likerM   r   r   r   rY   )	r    r   rP   r"   r#   rowcolval	new_shapes	            r$   
_get_array_csr_base._get_array   s    ))$,,7	jj.88q=>>"DJJ>77$**Q-1mmC1jj.hhsxxtzz2!T\\499((Cc	3 "%1!1CII		!	~~ckk)455r'   c                 B    U R                  U5      R                  U5      $ r:   )r}   _minor_index_fancyr    r   r   s      r$   _get_intXarray_csr_base._get_intXarray   s    ||C 33C88r'   c                    UR                   S;   a  U R                  XSS9$ U R                  u  p4UR                  U5      u  pVnU R                  XS-    u  pU R                  X n
U R
                  X nUS:  a
  X:  X:  -  nO	X:*  X:  -  n[        U5      S:  a  XU-
  U-  S:H  -  nX   U-
  U-  n
X   n[        R                  " S[        U
5      /5      nUS:  a  US S S2   n[        U
S S S2   5      n
S[        S[        [        R                  " [        Xe-
  5      U-  5      5      5      4nU R                  XU4UU R                  SS	9$ )
Nr   Tr;   r   r   r   rV   Frx   )r   r   r   r   r   r   absrL   arraylenrJ   rz   ceilfloatrC   r*   )r    r   r   r"   r#   r5   stopstrideiijjrow_indicesrow_datar2   
row_indptrr   s                  r$   _get_intXslice_csr_base._get_intXslice   sg   88y &&sd&;; zz!kk!nVSQ'll2)99R#A:'K,>?C'K,>?Cv;?%'61Q66C"'%/F:=XXq#k"234
A:"~Hk$B$/0KC3rwwuT\':V'CDEFG~~xjA$(JJU  < 	<r'   c                 ~    UR                   S;   a  U R                  XSS9$ U R                  U5      R                  US9$ )Nr   Tr;   minor)r   r   _major_slicer   s      r$   _get_sliceXint_csr_base._get_sliceXint  sC    88y &&sd&;;  %4434??r'   c                 B    U R                  U5      R                  U5      $ r:   )r   r   r   s      r$   _get_sliceXarray_csr_base._get_sliceXarray  s      %88==r'   c                     U R                  U5      R                  US9nUR                  S:  a  UR                  UR                  5      $ U$ )Nr   r   )_major_index_fancyr   r   rY   r   )r    r   r   ress       r$   _get_arrayXint_csr_base._get_arrayXint  sC    %%c*999D88a<;;syy))
r'   c                     UR                   S;  a@  [        R                  " UR                  U R                  S   5      6 nU R                  X5      $ U R                  U5      R                  US9$ )Nr   r   r   )r   rL   aranger   r   _get_arrayXarrayr   r   r   s      r$   _get_arrayXslice_csr_base._get_arrayXslice  s]    889$))S[[A78C((22&&s+:::EEr'   c                 (    U R                  SX5        g r   )	_set_manyr    r   re   s      r$   _set_int_csr_base._set_int!  s    q#!r'   c                     [         R                  " X!R                  5      nU R                  [         R                  " U5      X5        g r:   )rL   broadcast_tor   r   r   r   s      r$   
_set_array_csr_base._set_array$  s+    OOAyy)r}}S)32r'   rc   )NF)F)NT) __name__
__module____qualname____firstlineno___format	_allow_ndr%   r   __doc__r7   r<   r@   rQ   rX   staticmethodrf   rs   r}   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes____classcell__)rC   s   @r$   r   r      s   GI
K  ))11I" MM))EM MM))EM MM))EM, MM))EM"H MM))EM  
0<(< '&6 9<B@
>F"3 3r'   r   c                 "    [        U [        5      $ )a0  Is `x` of csr_matrix type?

.. warning::

   SciPy sparse is shifting from a sparse matrix interface to a sparse
   array interface. In the next few releases we expect to deprecate the
   sparse matrix interface. For documentation of the matrix
   interface, see the :ref:`spmatrix interface docs <spmatrix_api>`.
   For guidance on converting existing code to sparse arrays, see
   :ref:`Migration from spmatrix to sparray <migration_to_sparray>`.

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

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

Examples
--------
>>> from scipy.sparse import csr_array, csr_matrix, coo_matrix, isspmatrix_csr
>>> isspmatrix_csr(csr_matrix([[5]]))
True
>>> isspmatrix_csr(csr_array([[5]]))
False
>>> isspmatrix_csr(coo_matrix([[5]]))
False
)rk   r   rd   s    r$   r   r   )  s    @ a$$r'   c                       \ rS rSrSrSrg)r   iM  a  
Compressed Sparse Row array.

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

    csr_array(S)
        with another sparse array or matrix S (equivalent to S.tocsr())

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

    csr_array((data, (row_ind, col_ind)), [shape=(M, N)])
        where ``data``, ``row_ind`` and ``col_ind`` satisfy the
        relationship ``a[row_ind[k], col_ind[k]] = data[k]``.

    csr_array((data, indices, indptr), [shape=(M, N)])
        is the standard CSR representation where the column indices for
        row i are stored in ``indices[indptr[i]:indptr[i+1]]`` and their
        corresponding values are stored in ``data[indptr[i]:indptr[i+1]]``.
        If the shape parameter is not supplied, the array dimensions
        are inferred from the index arrays.

Attributes
----------
data : ndarray
    CSR format data array of the array
indices : ndarray
    CSR format index array of the array
indptr : ndarray
    CSR format index pointer array of the array
has_sorted_indices : bool
    Whether indices are sorted
has_canonical_format : bool
    Whether indices are sorted and no duplicate entries exist
dtype : dtype
    Data type of the array
shape : 2-tuple
    Shape of the array
ndim : int
    Number of dimensions (this is always 2)
format : str
    Three letter code for the format of the array storage, e.g. 'csr'
nnz : int
    Number of values stored in the array
size : int
    Number of values stored in the array
T : csr_array
    The transpose of the array
mT : csr_array
    The matrix transpose of the array

Notes
-----

Sparse arrays can be used in arithmetic operations: they support
addition, subtraction, multiplication, division, and matrix power.

Advantages of the CSR format
  - efficient arithmetic operations CSR + CSR, CSR * CSR, etc.
  - efficient row slicing
  - fast matrix vector products

Disadvantages of the CSR format
  - slow column slicing operations (consider CSC)
  - changes to the sparsity structure are expensive (consider LIL or DOK)

Canonical Format
    - Within each row, indices are sorted by column.
    - There are no duplicate entries.

Examples
--------

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

>>> row = np.array([0, 0, 1, 2, 2, 2])
>>> col = np.array([0, 2, 2, 0, 1, 2])
>>> data = np.array([1, 2, 3, 4, 5, 6])
>>> csr_array((data, (row, col)), shape=(3, 3)).toarray()
array([[1, 0, 2],
       [0, 0, 3],
       [4, 5, 6]])

>>> indptr = np.array([0, 2, 3, 6])
>>> indices = np.array([0, 2, 2, 0, 1, 2])
>>> data = np.array([1, 2, 3, 4, 5, 6])
>>> csr_array((data, indices, indptr), shape=(3, 3)).toarray()
array([[1, 0, 2],
       [0, 0, 3],
       [4, 5, 6]])

Duplicate entries are summed together:

>>> row = np.array([0, 1, 2, 0])
>>> col = np.array([0, 1, 1, 0])
>>> data = np.array([1, 2, 4, 8])
>>> csr_array((data, (row, col)), shape=(3, 3)).toarray()
array([[9, 0, 0],
       [0, 2, 0],
       [0, 4, 0]])

As an example of how to construct a CSR array incrementally,
the following snippet builds a term-document array from texts:

>>> docs = [["hello", "world", "hello"], ["goodbye", "cruel", "world"]]
>>> indptr = [0]
>>> indices = []
>>> data = []
>>> vocabulary = {}
>>> for d in docs:
...     for term in d:
...         index = vocabulary.setdefault(term, len(vocabulary))
...         indices.append(index)
...         data.append(1)
...     indptr.append(len(indices))
...
>>> csr_array((data, indices, indptr), dtype=int).toarray()
array([[2, 1, 0, 0],
       [0, 1, 1, 1]])

rc   Nr   r   r   r   r   r   rc   r'   r$   r   r   M  s    @r'   r   c                       \ rS rSrSrSrg)r   i  ah  
Compressed Sparse Row matrix.

.. warning::

   SciPy sparse is shifting from a sparse matrix interface to a sparse
   array interface. In the next few releases we expect to deprecate the
   sparse matrix interface. For documentation of the matrix
   interface, see the :ref:`spmatrix interface docs <spmatrix_api>`.
   For guidance on converting existing code to sparse arrays, see
   :ref:`Migration from spmatrix to sparray <migration_to_sparray>`.

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

    csr_matrix(S)
        with another sparse array or matrix S (equivalent to S.tocsr())

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

    csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
        where ``data``, ``row_ind`` and ``col_ind`` satisfy the
        relationship ``a[row_ind[k], col_ind[k]] = data[k]``.

    csr_matrix((data, indices, indptr), [shape=(M, N)])
        is the standard CSR representation where the column indices for
        row i are stored in ``indices[indptr[i]:indptr[i+1]]`` and their
        corresponding values are stored in ``data[indptr[i]:indptr[i+1]]``.
        If the shape parameter is not supplied, the matrix dimensions
        are inferred from the index arrays.

Attributes
----------
data : ndarray
    CSR format data array of the matrix
indices : ndarray
    CSR format index array of the matrix
indptr : ndarray
    CSR format index pointer array of the matrix
has_sorted_indices : bool
    Whether indices are sorted
has_canonical_format : bool
    Whether indices are sorted and no duplicate entries exist
dtype : dtype
    Data type of the matrix
shape : 2-tuple
    Shape of the matrix
ndim : int
    Number of dimensions (this is always 2)
format : str
    Three letter code for the format of the matrix storage, e.g. 'csr'
nnz : int
    Number of values stored in the matrix
size : int
    Number of values stored in the matrix
T : csr_matrix
    The transpose of the matrix
mT : csr_matrix
    The matrix transpose

Notes
-----

Sparse matrices can be used in arithmetic operations: they support
addition, subtraction, multiplication, division, and matrix power.

Advantages of the CSR format
  - efficient arithmetic operations CSR + CSR, CSR * CSR, etc.
  - efficient row slicing
  - fast matrix vector products

Disadvantages of the CSR format
  - slow column slicing operations (consider CSC)
  - changes to the sparsity structure are expensive (consider LIL or DOK)

Canonical Format
    - Within each row, indices are sorted by column.
    - There are no duplicate entries.

Examples
--------

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

>>> row = np.array([0, 0, 1, 2, 2, 2])
>>> col = np.array([0, 2, 2, 0, 1, 2])
>>> data = np.array([1, 2, 3, 4, 5, 6])
>>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray()
array([[1, 0, 2],
       [0, 0, 3],
       [4, 5, 6]])

>>> indptr = np.array([0, 2, 3, 6])
>>> indices = np.array([0, 2, 2, 0, 1, 2])
>>> data = np.array([1, 2, 3, 4, 5, 6])
>>> csr_matrix((data, indices, indptr), shape=(3, 3)).toarray()
array([[1, 0, 2],
       [0, 0, 3],
       [4, 5, 6]])

Duplicate entries are summed together:

>>> row = np.array([0, 1, 2, 0])
>>> col = np.array([0, 1, 1, 0])
>>> data = np.array([1, 2, 4, 8])
>>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray()
array([[9, 0, 0],
       [0, 2, 0],
       [0, 4, 0]])

As an example of how to construct a CSR matrix incrementally,
the following snippet builds a term-document matrix from texts:

>>> docs = [["hello", "world", "hello"], ["goodbye", "cruel", "world"]]
>>> indptr = [0]
>>> indices = []
>>> data = []
>>> vocabulary = {}
>>> for d in docs:
...     for term in d:
...         index = vocabulary.setdefault(term, len(vocabulary))
...         indices.append(index)
...         data.append(1)
...     indptr.append(len(indices))
...
>>> csr_matrix((data, indices, indptr), dtype=int).toarray()
array([[2, 1, 0, 0],
       [0, 1, 1, 1]])

rc   Nr   rc   r'   r$   r   r     s    Ir'   r   )r   __docformat____all__numpyrL   _matrixr   _baser   r	   _sparsetoolsr
   r   r   r   r   _sputilsr   _compressedr   r   r   r   r   rc   r'   r$   <module>r      sc    )%
7   #A A  #T3
 T3n %HA	7 AHJ9 Jr'   