
    `i                         	 d dl ZdZn# e$ r dZY nw xY wd dlZd dlmZ d dlmZ d dlmZ d dlm	Z	  G d d	ej
                  Zd
 ZdS )    NTF)_core)_csc)_data)_utilc                   \    e Zd ZdZdZddZddZddZd	 Zdd
Z	ddZ
ddZddZddZdS )
dia_matrixa  Sparse matrix with DIAgonal storage.

    Now it has only one initializer format below:

    ``dia_matrix((data, offsets))``

    Args:
        arg1: Arguments for the initializer.
        shape (tuple): Shape of a matrix. Its length must be two.
        dtype: Data type. It must be an argument of :class:`numpy.dtype`.
        copy (bool): If ``True``, copies of given arrays are always used.

    .. seealso::
       :class:`scipy.sparse.dia_matrix`

    diaNFc                 ,   t           rRt          j                            |          r3|                                }|j        }|j        }|j        }|j        }d}n;t          |t                    r|\  }}|t          d          nt          d          t          j        |||          }t          j        |          }t          j        |d|          }t          j        |          }|j        dk    rt          d          |j        dk    rt          d	          |j        d
         t#          |          k    r,t          d|j        d
         t#          |          fz            t          j        |          }|d d         |dd          k                                    rt          d          || _        || _        t)          j        |          st          d          t-          |d
                   t-          |d                   f| _        d S )NFzexpected a shape argumentz,unrecognized form for dia_matrix constructor)dtypecopyi   zoffsets array must have rank 1   zdata array must have rank 2r   zBnumber of diagonals (%d) does not match the number of offsets (%d)z&offset array contains duplicate valuesz(invalid shape (must be a 2-tuple of int))_scipy_availablescipysparseissparsetodiadataoffsetsshaper   
isinstancetuple
ValueErrorcupyarray
atleast_2d
atleast_1dndimlensortanyr   isshapeint_shape)	selfarg1r   r   r   xr   r   sorted_offsetss	            k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/scipy/sparse/_dia.py__init__zdia_matrix.__init__%   s    	@ 5 5d ; ; 	@

A6DiGGEGEDDe$$ 	@ MD'} !<===  >@ @ @ z$e$777t$$*WCd;;;/'**<1=>>>9>>:;;;:a=CLL((:a=#g,,/01 1 1
 7++3B3>!""#55::<< 	GEFFF	}U## 	IGHHH%(mmSq]]2    Tc                     |r/t          || j                                        f| j                  S t          || 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.
        r   )r   r   r   r   )r'   r   r   s      r+   
_with_datazdia_matrix._with_dataQ   sQ      	FtT\%6%6%8%89LLLLtT\2$*EEEEr-   c                     t           st          d          | j                            |          }| j                            |          }t
          j                            ||f| j                  S )a:  Returns a copy of the array on host memory.

        Args:
            stream (cupy.cuda.Stream): CUDA stream object. If it is given, the
                copy runs asynchronously. Otherwise, the copy is synchronous.

        Returns:
            scipy.sparse.dia_matrix: Copy of the array on host memory.

        zscipy is not availabler/   )	r   RuntimeErrorr   getr   r   r   r   r&   )r'   streamr   r   s       r+   r3   zdia_matrix.getZ   sd       	97888y}}V$$,""6**|&&gdk&JJJr-   c                     | j         S )zcReturns the shape of the matrix.

        Returns:
            tuple: Shape of the matrix.
        )r&   )r'   s    r+   	get_shapezdia_matrix.get_shapek   s     {r-   c           
          |t          d          | j        \  }} t          j        ddddddd	          | j        ||          }t          |          S )
zReturns the number of stored values, including explicit zeros.

        Args:
            axis: Not supported yet.

        Returns:
            int: The number of stored values.

        Nz5getnnz over an axis is not implemented for DIA formatzint32 offsets, int32 m, int32 nz	int32 nnzz7offsets > 0 ? min(m, n - offsets) : min(m + offsets, n)za + bznnz = a0dia_nnz)NotImplementedErrorr   r   ReductionKernelr   r%   )r'   axismnnnzs        r+   getnnzzdia_matrix.getnnzs   sz     %GI I I z10e#-{EYY0 0 15aD D 3xxr-   c                 T    |                                                      ||          S )z3Returns a dense matrix representing the same value.)orderout)tocsctoarray)r'   rB   rC   s      r+   rE   zdia_matrix.toarray   s#    zz||##%S#999r-   c                    | j         j        dk    r t          j        | j        | j                  S | j        \  }}| j         j        \  }} t          j        dddd          || j        dddf         ||| j                   \  }}t          j
        |dz   d	          }t          j        |                    d
                    |d|dz   <   ||         ||dz   d<   |j        |j                                     d	d          }	| j         j        |j                 }
t          j        |
|	|f| j        | j                  S )a{  Converts the matrix to Compressed Sparse Column format.

        Args:
            copy (bool): If ``False``, it shares data arrays as much as
                possible. Actually this option is ignored because all
                arrays in a matrix cannot be shared in dia to csc conversion.

        Returns:
            cupyx.scipy.sparse.csc_matrix: Converted matrix.

        r   r   zGint32 offset_len, int32 offsets, int32 num_rows, int32 num_cols, T datazint32 row, bool maskz
            int offset_inds = i % offset_len;
            row = offset_inds - offsets;
            mask = (row >= 0 && row < num_rows && offset_inds < num_cols
                    && data != T(0));
            cupyx_scipy_sparse_dia_tocscNr   r   )r<   F)r   )r   r   )r   sizer   
csc_matrixr   r   r   ElementwiseKernelr   r   zeroscumsumsumTastype)r'   r   num_rowsnum_colsnum_offsets
offset_lenrowmaskindptrindicesr   s              r+   rD   zdia_matrix.tocsc   sX    9>Q?4:TZ@@@@!Z("&)/Z
,E+%" +
, 
, -7QQQW8M,4h	K K	T HqL444$(Ka0@0@$A$Aq*q. !"("4zA~%-&&s&77y{46"7F#4:TZI I I 	Ir-   c                 N    |                                                                  S )ax  Converts the matrix to Compressed Sparse Row format.

        Args:
            copy (bool): If ``False``, it shares data arrays as much as
                possible. Actually this option is ignored because all
                arrays in a matrix cannot be shared in dia to csr conversion.

        Returns:
            cupyx.scipy.sparse.csc_matrix: Converted matrix.

        )rD   tocsr)r'   r   s     r+   rZ   zdia_matrix.tocsr   s     zz||!!###r-   r   c                 ~   | j         \  }}|| k    s||k    r t          j        d| j        j                  S t          j        | j        |k              \  }t          d|          t          ||z   |          }}|j	        dk    r#t          j
        ||z
  | j        j                  S | j        |d         ||f         S )a  Returns the k-th diagonal of the matrix.

        Args:
            k (int, optional): Which diagonal to get, corresponding to elements
            a[i, i+k]. Default: 0 (the main diagonal).

        Returns:
            cupy.ndarray : The k-th diagonal.
        r   rG   )r   r   emptyr   r   nonzeror   maxminrI   rL   )r'   krowscolsidx	first_collast_cols          r+   diagonalzdia_matrix.diagonal   s     Z
d::d:aty7777|DLA-..!!QiiTAXt)<)<8	8q==:h2$)/JJJJyQ8!3344r-   )NNF)T)N)NN)F)r   )__name__
__module____qualname____doc__formatr,   r0   r3   r6   r@   rE   rD   rZ   rf    r-   r+   r   r      s         " F*3 *3 *3 *3XF F F FK K K K"     *: : : :$I $I $I $IL$ $ $ $5 5 5 5 5 5r-   r   c                 ,    t          | t                    S )zChecks if a given matrix is of DIA format.

    Returns:
        bool: Returns if ``x`` is :class:`cupyx.scipy.sparse.dia_matrix`.

    )r   r   )r)   s    r+   isspmatrix_diarn      s     a$$$r-   )scipy.sparser   r   ImportErrorr   r   cupyx.scipy.sparser   r   r   _data_matrixr   rn   rl   r-   r+   <module>rs      s              # # # # # # $ $ $ $ $ $ $ $ $ $ $ $
A5 A5 A5 A5 A5# A5 A5 A5H% % % % %s   	 