
    (ph                     J    S r Sr/ SQrSSKJrJr  SSKJr  S rS
S jr	S
S	 jr
g)z.Functions to extract parts of sparse matrices
zrestructuredtext en)findtriltriu   )
coo_matrix	coo_array)sparrayc                     [        U SS9n U R                  5         U R                  S:g  nU R                  U   U R                  U   U R                  U   4$ )a  Return the indices and values of the nonzero elements of a matrix

Parameters
----------
A : dense or sparse array or matrix
    Matrix whose nonzero elements are desired.

Returns
-------
(I,J,V) : tuple of arrays
    I,J, and V contain the row indices, column indices, and values
    of the nonzero entries.


Examples
--------
>>> from scipy.sparse import csr_array, find
>>> A = csr_array([[7.0, 8.0, 0],[0, 0, 9.0]])
>>> find(A)
(array([0, 0, 1], dtype=int32),
 array([0, 1, 2], dtype=int32),
 array([ 7.,  8.,  9.]))

Tcopy    )r   sum_duplicatesdatarowcol)Anz_masks     H/var/www/html/venv/lib/python3.13/site-packages/scipy/sparse/_extract.pyr   r      sN    4 	!$AffkG55>155>166'?::    Nc                 D   [        U [        5      (       a  [        O[        nU" U SS9n U R                  U-   U R
                  :  nU R                  U   nU R
                  U   nU R                  U   nU" XuU44U R                  U R                  S9nUR                  U5      $ )av  Return the lower triangular portion of a sparse array or matrix

Returns the elements on or below the k-th diagonal of A.
    - k = 0 corresponds to the main diagonal
    - k > 0 is above the main diagonal
    - k < 0 is below the main diagonal

Parameters
----------
A : dense or sparse array or matrix
    Matrix whose lower trianglar portion is desired.
k : integer : optional
    The top-most diagonal of the lower triangle.
format : string
    Sparse format of the result, e.g. format="csr", etc.

Returns
-------
L : sparse matrix
    Lower triangular portion of A in sparse format.

See Also
--------
triu : upper triangle in sparse format

Examples
--------
>>> from scipy.sparse import csr_array, tril
>>> A = csr_array([[1, 2, 0, 0, 3], [4, 5, 0, 6, 7], [0, 0, 8, 9, 0]],
...               dtype='int32')
>>> A.toarray()
array([[1, 2, 0, 0, 3],
       [4, 5, 0, 6, 7],
       [0, 0, 8, 9, 0]], dtype=int32)
>>> tril(A).toarray()
array([[1, 0, 0, 0, 0],
       [4, 5, 0, 0, 0],
       [0, 0, 8, 0, 0]], dtype=int32)
>>> tril(A).nnz
4
>>> tril(A, k=1).toarray()
array([[1, 2, 0, 0, 0],
       [4, 5, 0, 0, 0],
       [0, 0, 8, 9, 0]], dtype=int32)
>>> tril(A, k=-1).toarray()
array([[0, 0, 0, 0, 0],
       [4, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]], dtype=int32)
>>> tril(A, format='csc')
<Compressed Sparse Column sparse array of dtype 'int32'
    with 4 stored elements and shape (3, 5)>

Fr
   shapedtype

isinstancer   r   r   r   r   r   r   r   asformat	r   kformat
coo_sparsemaskr   r   r   new_coos	            r   r   r   .   s    l )G44*J 	15!A5519D
%%+C
%%+C66$<D$c
+177!''JGF##r   c                 D   [        U [        5      (       a  [        O[        nU" U SS9n U R                  U-   U R
                  :*  nU R                  U   nU R
                  U   nU R                  U   nU" XuU44U R                  U R                  S9nUR                  U5      $ )a  Return the upper triangular portion of a sparse array or matrix

Returns the elements on or above the k-th diagonal of A.
    - k = 0 corresponds to the main diagonal
    - k > 0 is above the main diagonal
    - k < 0 is below the main diagonal

Parameters
----------
A : dense or sparse array or matrix
    Matrix whose upper trianglar portion is desired.
k : integer : optional
    The bottom-most diagonal of the upper triangle.
format : string
    Sparse format of the result, e.g. format="csr", etc.

Returns
-------
L : sparse array or matrix
    Upper triangular portion of A in sparse format.
    Sparse array if A is a sparse array, otherwise matrix.

See Also
--------
tril : lower triangle in sparse format

Examples
--------
>>> from scipy.sparse import csr_array, triu
>>> A = csr_array([[1, 2, 0, 0, 3], [4, 5, 0, 6, 7], [0, 0, 8, 9, 0]],
...                dtype='int32')
>>> A.toarray()
array([[1, 2, 0, 0, 3],
       [4, 5, 0, 6, 7],
       [0, 0, 8, 9, 0]], dtype=int32)
>>> triu(A).toarray()
array([[1, 2, 0, 0, 3],
       [0, 5, 0, 6, 7],
       [0, 0, 8, 9, 0]], dtype=int32)
>>> triu(A).nnz
8
>>> triu(A, k=1).toarray()
array([[0, 2, 0, 0, 3],
       [0, 0, 0, 6, 7],
       [0, 0, 0, 9, 0]], dtype=int32)
>>> triu(A, k=-1).toarray()
array([[1, 2, 0, 0, 3],
       [4, 5, 0, 6, 7],
       [0, 0, 8, 9, 0]], dtype=int32)
>>> triu(A, format='csc')
<Compressed Sparse Column sparse array of dtype 'int32'
    with 8 stored elements and shape (3, 5)>

Fr
   r   r   r   s	            r   r   r   q   s    n )G44*J 	15!A5519D
%%+C
%%+C66$<D$c
+177!''JGF##r   )r   N)__doc____docformat____all___coor   r   _baser   r   r   r    r   r   <module>r)      s1    &
" ( ;B@$FA$r   