
    `iw                        d dl Z d dlZd dlZ	 d dlZdZn# e$ r dZY nw xY wd dlZd dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlm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        ej        ej                  ZdS )    NTF)_core)_scalar)basic)_base)_coo)_data)_sputils)_util)_indexc                      e Zd ZdZ ej         ej        e                              ddd          d          Z	 ej         ej        e                              ddd          d          Z
 ej         ej        e                              dd	d          d          Z ej         ej        e                              d
d	d          d
          ZdZ ej         ej        e                              dd          dg d          Z ej         ej        e                              dd	          dg d          Z ej        dddd          Z ej        dddd          ZdCdZdDdZd Zd Zd  Zd! Zd" Zd# Zd$ Zd% Zd& Zd' ZdEd)Z d* Z!d+ Z"d, Z#dFd-Z$dFd.Z%d/ Z&d0 Z'd1 Z(d2 Z)d3 Z*d4 Z+d5 Z,d6 Z-d7 Z.d8 Z/ e0e.e/9          Z1d: Z2d; Z3 e0e2e39          Z4d< Z5dGd=Z6d> Z7d? Z8d@ Z9dA Z:dB Z;dS )H_compressed_sparse_matrixa  
        extern "C" __global__
        void ${func}(double* data, int* x, int* y, int length,
                           double* z) {
            // Get the index of the block
            int tid = blockIdx.x * blockDim.x + threadIdx.x;

            // Calculate the block length
            int block_length = y[tid] - x[tid];

            // Select initial value based on the block density
            double running_value = 0;
            if (${cond}){
                running_value = data[x[tid]];
            } else {
                running_value = 0;
            }

            // Iterate over the block and update
            for (int entry = x[tid]; entry < y[tid]; entry++){
                if (data[entry] != data[entry]){
                    // Check for NaN
                    running_value = nan("");
                    break;
                } else {
                    // Check for a value update
                    if (data[entry] ${op} running_value){
                        running_value = data[entry];
                    }
                }
            }

            // Store in the return function
            z[tid] = running_value;
        }max_reduction>zblock_length == length)funcopcondmax_nonzero_reductionzblock_length > 0min_reduction<min_nonzero_reductiona  
        template<typename T1, typename T2> __global__ void
        ${func}_arg_reduction(T1* data, int* indices, int* x, int* y,
                              int length, T2* z) {
            // Get the index of the block
            int tid = blockIdx.x * blockDim.x + threadIdx.x;

            // Calculate the block length
            int block_length = y[tid] - x[tid];

            // Select initial value based on the block density
            int data_index = 0;
            double data_value = 0;

            if (block_length == length){
                // Block is dense. Fill the first value
                data_value = data[x[tid]];
                data_index = indices[x[tid]];
            } else if (block_length > 0)  {
                // Block has at least one zero. Assign first occurrence as the
                // starting reference
                data_value = 0;
                for (data_index = 0; data_index < length; data_index++){
                    if (data_index != indices[x[tid] + data_index] ||
                        x[tid] + data_index >= y[tid]){
                        break;
                    }
                }
            } else {
                // Zero valued array
                data_value = 0;
                data_index = 0;
            }

            // Iterate over the section of the sparse matrix
            for (int entry = x[tid]; entry < y[tid]; entry++){
                if (data[entry] != data[entry]){
                    // Check for NaN
                    data_value = nan("");
                    data_index = 0;
                    break;
                } else {
                    // Check for a value update
                    if (data[entry] ${op} data_value){
                        data_index = indices[entry];
                        data_value = data[entry];
                    }
                }
            }

            // Store in the return function
            z[tid] = data_index;
        }max)r   r   )z
-std=c++11)zmax_arg_reduction<float, int>z#max_arg_reduction<float, long long>zmax_arg_reduction<double, int>z$max_arg_reduction<double, long long>)codeoptionsname_expressionsmin)zmin_arg_reduction<float, int>z#min_arg_reduction<float, long long>zmin_arg_reduction<double, int>z$min_arg_reduction<double, long long>zraw T indptr, raw T indicesz	bool diffz
        bool diff_out = true;
        for (T jj = indptr[i]; jj < indptr[i+1] - 1; jj++) {
            if (indices[jj] > indices[jj+1]){
                diff_out = false;
            }
        }
        diff = diff_out;
        %cupyx_scipy_sparse_has_sorted_indicesaF  
        bool diff_out = true;
        if (indptr[i] > indptr[i+1]) {
            diff = false;
            return;
        }
        for (T jj = indptr[i]; jj < indptr[i+1] - 1; jj++) {
            if (indices[jj] >= indices[jj+1]) {
                diff_out = false;
            }
        }
        diff = diff_out;
        'cupyx_scipy_sparse_has_canonical_formatNFc                 &
   ddl m} |Mt          j        |          st	          d          t          |d                   t          |d                   f}t          j        |          rL|                    | j	                  }|j
        }|j        }|j        }	|j	        | j	        k    rd}||j        }n t          j        |          r|\  }
}t          |
          t          |          }}
t          j        d|r|nd          }t          j        dd          }t          j        |                     |
|          d         dz   d          }	|
|f}d}n`t"          rt$          j                            |          rv|                    | j	                  }t)          j        |j
                  }t)          j        |j        d          }t)          j        |j        d          }	d}||j        }nt-          |t.                    r\t1          |          d	k    rIt3          j        ||||
          }|                    | j	                  }|j
        }|j        }|j        }	nSt-          |t.                    rt1          |          dk    r|\  }}}	t          j        |          rI|j        dk    r>t          j        |          r*|j        dk    rt          j        |	          r|	j        dk    st	          d          t1          |          t1          |          k    rt	          d          nt          j        |          rf|j        d	k    rt;          d          |j        dk    r	|d          }n|j        dk    r|d         }|                     |          \  }}}	d}||j        }nt	          d          ||j        }ntA          j        |          }|j!        dvrt	          d          |"                    ||          }tF          j$        %                    | |           |"                    d|          | _        |	"                    d|          | _        |H|                     t1          |	          dz
  t          |&                                          dz             } | j        | \  }}t1          |	          |dz   k    r$t	          dt1          |	          |dz   fz            |j'        (                                | _)        || _*        d S )Nr   )cusparsez(invalid shape (must be a 2-tuple of int)   Fdidtype   )shaper$   copy   z'data, indices, and indptr should be 1-Dz*indices and data should have the same sizez'expected dimension <= 2 array or matrix)NNzUnsupported initializer formatz?fdFDzCOnly bool, float32, float64, complex64 and complex128 are supportedr'   z&index pointer size (%d) should be (%d))+cupyxr   r
   isshape
ValueErrorintr   issparseasformatformatdataindicesindptrr&   r   zeros_swapscipy_availablescipysparsecupyarray
isinstancetuplelenr   
coo_matrixisdensendim	TypeError_convert_denser$   numpycharastypesparse_data_data_matrix__init__r   MatDescriptorcreate_descr_shape)selfarg1r&   r$   r'   r   xr1   r2   r3   mnsp_coosp_compressedmajorminors                   r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/scipy/sparse/_compressed.pyrH   z"_compressed_sparse_matrix.__init__   s   """"""='' M !KLLLaMM3uQx==0E>$ E	2dk**A6DiGXF{dk))}
]4   8	2DAqq663q66qA;q5"9%%c::Dk!S))G[Aq!1!1!!4q!8DDDFFEDD .	2!6!6t!<!< .	2dk**A:af%%Dj#666GZ444FD}
e$$ #	2Ta _Te$OOOF"OODK88M %D#+G")FFe$$ 	2Ta$(!D'6M$'' ?DINNM'** -;/6|q/@/@M&)) 0A.4kQ.>.> =? ? ? 4yyCLL(( !MNNN ) ]4   	2y1}} IJJJaDzaJ'$($7$7$=$=!D'6D}
 02 2 2 =JEEK&&E:W$$ ! ! ! {{5t{,, ))$555~~c~55mmCdm33=JJs6{{QGKKMM0B0BQ0FGGE!tz5)uv;;%!)##E #FUQY78 9 9 9 ,3355    Tc                 
   |rS|                      || j                                        | j                                        f| j        |j                  S |                      || j        | j        f| j        |j                  S )N)r&   r$   )	__class__r2   r'   r3   r&   r$   )rM   r1   r'   s      rV   
_with_dataz$_compressed_sparse_matrix._with_data,  s     		">>t|((**DK,<,<,>,>?jj " " " "
 >>t|T[1jj " " " "rW   c                     t           NNotImplementedError)rM   rO   s     rV   rB   z(_compressed_sparse_matrix._convert_dense8      !!rW   c                     t           r\   r]   )rM   rO   ys      rV   r5   z_compressed_sparse_matrix._swap;  r_   rW   c                     t           r\   r]   )rM   otheralphabetas       rV   _add_sparsez%_compressed_sparse_matrix._add_sparse>  r_   rW   c                    t          j        |          r.|dk    r|r|  S |                                 S t          d          t	          j        |          r#|rdnd}|rdnd}|                     |||          S t	          j        |          rc|r1|r|                                  |z
  S ||                                 z
  S |r|                                 |z
  S |                                 |z   S t          S )Nr   z;adding a nonzero scalar to a sparse matrix is not supportedr    )
r9   isscalarr'   r^   r   
isspmatrixrf   r?   todenseNotImplemented)rM   rc   lhs_negativerhs_negativerd   re   s         rV   _addz_compressed_sparse_matrix._addA  s   = 	"zz ' 5L99;;&) ! ! ! e$$ 	"&-BBAE%,221D##E5$777]5!! 	" 	2 2 LLNN?U22 4<<>>11 2<<>>E11<<>>E11!!rW   c                 0    |                      |dd          S NFro   rM   rc   s     rV   __add__z!_compressed_sparse_matrix.__add__^      yyu---rW   c                 0    |                      |dd          S rq   rr   rs   s     rV   __radd__z"_compressed_sparse_matrix.__radd__a  ru   rW   c                 0    |                      |dd          S )NFTrr   rs   s     rV   __sub__z!_compressed_sparse_matrix.__sub__d  s    yyt,,,rW   c                 0    |                      |dd          S )NTFrr   rs   s     rV   __rsub__z"_compressed_sparse_matrix.__rsub__g  s    yye,,,rW   c                 n   |                      ||          \  }}t          j        | j        | j        | j        ||dz             \  }}}|j        }t          j        d|          }	|j	        dk    r.t          j
        |j        |j        |||	j        |	j                   nt          j        ||||	           |	S )Nr     r#   c)r5   r   _get_csr_submatrix_major_axisr1   r2   r3   r$   r9   r4   kind_compress_getitem_complex_kernrealimag_compress_getitem_kern)
rM   rowcolrT   rU   r1   r2   _r$   ress
             rV   _get_intXintz&_compressed_sparse_matrix._get_intXintj  s    zz#s++u!?It|T[%D Dgq
j5))):1	49guchJ J J J )$DDD
rW   c                     |                      ||          \  }}|j        dv }|                     |                              ||          S )N)r    Nr)   )r5   step_major_slice_minor_slice)rM   r   r   rT   rU   r'   s         rV   _get_sliceXslicez*_compressed_sparse_matrix._get_sliceXslicew  sL    zz#s++uzY&  ''44U4FFFrW   r   c           
         | j         j        } | j        | j         \  }}|                     ||          \  }}|                    |d          }|                    |d          }t          j        ||| j        | j         | j        |	                                |	                                |          }	|j
        dk    rt          j        |	d          S |                     |	                    |j                            S )NFr)   r    r   )r2   r$   r5   r&   rE   r   _csr_sample_valuesr3   r1   ravelr@   r9   expand_dimsrY   reshape)
rM   r   r   not_found_val	idx_dtypeMNrT   rU   vals
             rV   _get_arrayXarrayz*_compressed_sparse_matrix._get_arrayXarray|  s    L&	tz4:&1zz#s++uYU33YU33'q$+t|TYKKMM5;;== 
 :??#C+++~~ckk%+66777rW   c                     |                      ||          \  }}|                     |                              |          S r\   )r5   _major_index_fancy_minor_index_fancy)rM   r   r   rT   rU   s        rV   _get_columnXarrayz+_compressed_sparse_matrix._get_columnXarray  s;    zz#s++u&&u--@@GGGrW   c                 2    | j         | j         \  }}|j        }|                      ||          }| j        dk    s|dk    r|                     || j                  S |                     t          j        | j        | j	        | j
        |          |d          S )zBIndex along the major axis where idx is an array of ints.
        r   r#   Fr&   r'   )r5   r&   sizennzrY   r$   r   _csr_row_indexr1   r2   r3   )rM   idxr   r   r   	new_shapes         rV   r   z,_compressed_sparse_matrix._major_index_fancy  s     tz4:&1HJJq!$$	8q==AFF>>)4:>>>>~~!$)T\4;LL%  ) ) 	)rW   c                 T    | j         | j         \  }}|j        }|                      ||          }| j        dk    s|dk    r|                     || j                  S |j        |z  | j        k     r	 |                                                     |                                          S )zBIndex along the minor axis where idx is an array of ints.
        r   r#   )r5   r&   r   r   rY   r$   _tocsxr   )rM   r   r   r   r   r   s         rV   r   z,_compressed_sparse_matrix._minor_index_fancy  s     tz4:&1HJJq!$$	8q==AFF>>)4:>>>>8a<$(""{{}}//44;;===rW   c           	      b    | j         | j         \  }}|                    |          \  }}}|dk    r$||k    r|dk    r|r|                                 n| S t	          t          |||                    }|                      ||          }|dk    rj|dk    s| j        dk    r|                     || j                  S |                     t          j
        | j        | j        | j        ||          ||          S t          j        |||| j        j                  }	|                     |	          S )z@Index along the major axis where idx is a slice object.
        r   r    r#   r   )r5   r&   r2   r'   r=   ranger   rY   r$   r   r   r1   r3   r9   aranger   )
rM   r   r'   r   r   startstopr   r   rowss
             rV   r   z&_compressed_sparse_matrix._major_slice  s4    tz4:&1KKNNtTA::$!))		"&0499;;;D0eT4(())JJq!$$	199AvvQ~~itz~BBB>>4It|T[%G Gd " , , , {5$DK4EFFF&&t,,,rW   c           	      b    | j         | j         \  }}|                    |          \  }}}|dk    r$||k    r|dk    r|r|                                 n| S t	          t          |||                    }|                      ||          }|dk    s| j        dk    r|                     || j                  S |dk    r=|                     t          j
        | j        | j        | j        ||          |d          S t          j        |||| j        j                  }	|                     |	          S )z@Index along the minor axis where idx is a slice object.
        r   r    r#   Fr   )r5   r&   r2   r'   r=   r   r   rY   r$   r   _get_csr_submatrix_minor_axisr1   r3   r9   r   r   )
rM   r   r'   r   r   r   r   r   r   colss
             rV   r   z&_compressed_sparse_matrix._minor_slice  s4    tz4:&1KKNNtTA::$!))		"&0499;;;D0eT4(())JJq!$$	66TX]]>>)4:>>>>199>>4It|T[%G Ge " - - - {5$DL4FGGG&&t,,,rW   c                 f    |                      ||          \  }}|                     |||           d S r\   r5   	_set_manyrM   r   r   rO   r"   js         rV   _set_intXintz&_compressed_sparse_matrix._set_intXint  5    zz#s##1q!QrW   c                 f    |                      ||          \  }}|                     |||           d S r\   r   r   s         rV   _set_arrayXarrayz*_compressed_sparse_matrix._set_arrayXarray  r   rW   c                     | j         |                     ||            |j        \  }}|dk    o|j        d         dk    }|dk    o|j        d         dk    }|j        |j        }	}t          j        |j        | j                  }|rVt          j	        t          j
        |          |j                  }t          j        |	|          }	t          j        ||          }|rVt          j	        ||          }t          j        t          j
        |          |	j                  }	t          j	        ||          }|                     |||	f         |||	f                   \  }
}|                     |
||           d S )Nr    r   r#   )
_zero_manyr5   r&   r   r   r9   asarrayr1   r$   repeatr   r   tiler   )rM   r   r   rO   r   r   broadcast_rowbroadcast_colrr~   r"   r   s               rV   _set_arrayXarray_sparsez1_compressed_sparse_matrix._set_arrayXarray_sparse  sH   C--..y1Q2171:?Q2171:?uae1Ltz222 	 DKNNAF33A	!QA	!QA 	"Aq!!A	$+a..!&11AAq!!Azz#ad)SAY//1q!QrW   c                 8    | j         | j         \  }}d }t          j        || j        j        dd                                          }t          j        || j        j        dd                                          } |||            |||           ||||fS )Nc                     |                                  }||k    rt          d||fz            |                                 }|| k     rt          d||fz            d S )Nzindex (%d) out of range (>= %d)zindex (%d) out of range (< -%d))r   
IndexErrorr   )r2   boundr   s      rV   check_boundsz@_compressed_sparse_matrix._prepare_indices.<locals>.check_bounds  sy    ++--Ce|| !B"%u". / / /++--CeV|| !B"%u". / / / |rW   Tr    r$   r'   ndmin)r5   r&   r9   r:   r3   r$   r   r2   )rM   r"   r   r   r   r   s         rV   _prepare_indicesz*_compressed_sparse_matrix._prepare_indices  s    tz4:&1	/ 	/ 	/ Jq 1 + + ++0577 	
Jq 2 + + ++0577 	
QQ!QzrW   c                 Z   |                      ||          \  }}}}t          j        || j        dd                                          }t
          j        j                            t          j	        | j
        t          j                  | j        | j        f||f          }|                    ||d                              t          j                                                  }|dk    }||         | j        ||         <   |                                rdS t'          j        d	                    | j                  t,          j                   | }||         }||d
k     xx         |z  cc<   ||         }||d
k     xx         |z  cc<   |                     ||||                    dS )zSets value at each (i, j) to x
        Here (i,j) index major and minor respectively, and must not contain
        duplicate entries.
        Tr    r   r#   r&   rh   r   Nz<Changing the sparsity structure of a {}_matrix is expensive.r   )r   r9   r:   r$   r   r*   r7   r8   
csr_matrixr   r   float32r2   r3   r   rE   int32r1   allwarningswarnr0   r   SparseEfficiencyWarning_insert_many)	rM   r"   r   rO   r   r   new_spoffsetsmasks	            rV   r   z#_compressed_sparse_matrix._set_many  s   
 **1a00
1aJq
Q???EEGG#..[666\4;(011v / 7 7 ))q * $ $$*F4:$6$6uuww 	 |#$T7	'$- 88:: 	F 	 006t{0C0C3	5 	5 	5 udG	!a%AdG	!a%A!Q$(((((rW   c                    |                      ||          \  }}}}t          j        j                            t          j        | j        t
          j                  | j	        | j
        f||f          }|                    ||d                              t
          j                                                  }d| j        ||dk             <   dS )zSets value at each (i, j) to zero, preserving sparsity structure.
        Here (i,j) index major and minor respectively.
        r#   r   rh   r   r   N)r   r*   r7   r8   r   r9   r   r   r   r2   r3   r   rE   r   r   r1   )rM   r"   r   r   r   r   r   s          rV   r   z$_compressed_sparse_matrix._zero_many.  s     **1a00
1a#..[666\4;(011v / 7 7 ))q * $ $$*F4:$6$6uuww 	 ,-	''B,'(((rW   c                    t          j        | j                  }||xx         |z  cc<   t          j        | j        j        |          } |d          |d<   ||dd<   t          j        ||           t          |d                   }t          j        ||          }	t          j        || j        j                  }
t          j	        |j
        |          }||dd         |<   t          j        ||           t          j        |||| j        | j        | j        ||	|
| j        j
        dz
  
  
         || _        |	| _        |
| _        dS )z>Insert new elements into current sparse matrix in sorted orderr#   r   r    N)outrh   r   )r9   diffr3   emptyr&   cumsumr-   r1   r$   r4   r   r   _insert_many_populate_arraysr2   )rM   indices_insertsdata_insertsr   
row_countsr   indptr_diff
new_indptrout_nnznew_indicesnew_datanew_indptr_lookups               rV   _perform_insertz)_compressed_sparse_matrix._perform_insert>  s]    i,,DZ'Z 1CCC
!	!
1$
122 	JJ////jn%%j	::::gTY_=== !JziHHH&0!""d#%+<====+\+<Kty*k4;+A-	/ 	/ 	/ 	/
 !"			rW   c                 v   t          j        |          }|                    |          }|                    |          }|                    |          }t          j        | j        | j        f| j        |j        z             }| j        	                    |          | _        | j        	                    |          | _        | j
        	                    | j                  | _
        t          j        ||||          \  }}}t          j        |d          \  }	}
t          j        |
j        dz   |
j                  }|j        |d<   |
|dd<   |}
t          j        |
j        dz
  |          }t           j                            |t          j        |	|          d           |                     |||	||           dS )a9  Inserts new nonzero at each (i, j) with value x
        Here (i,j) index major and minor respectively.
        i, j and x must be non-empty, 1d arrays.
        Inserts each major group (e.g. all entries per row) at a time.
        Maintains has_sorted_indices property.
        Modifies i, j, x in place.
        )maxvalT)return_indexr    rh   Nr#   )r9   argsorttaker	   get_index_dtyper2   r3   r   r   rE   r1   r$   r   _select_last_indicesuniquer   r4   addatsearchsortedr   )rM   r"   r   rO   orderr   indptr_insertsr   r   r   	ui_indptrto_addr   s                rV   r   z&_compressed_sparse_matrix._insert_many^  s    QFF5MMFF5MMFF5MM ,\4;'16!$ $ $	 k((33|**955I$$TZ00	 '1a;; 	6 +n4HHHiIN1,io>>Vr
ss	 Z	q 0	BBB
J 1$ G GKKK_l!:y	: 	: 	: 	: 	:rW   c                 <   | j         j        dk    rd| _        n~t          | dd          sd| _        net	          | d          sU|                     | j        | j        | j        j        dz
            }t          |	                                          | _        | j        S )a  Determine whether the matrix has sorted indices and no duplicates.

        Returns
            bool: ``True`` if the above applies, otherwise ``False``.

        .. note::
            :attr:`has_canonical_format` implies :attr:`has_sorted_indices`, so
            if the latter flag is ``False``, so will the former be; if the
            former is found ``True``, the latter flag is also set.

        .. warning::
            Getting this property might synchronize the device.

        r   T_has_sorted_indicesF_has_canonical_formatr    r   )
r1   r   r   getattrhasattr_has_canonical_format_kernr3   r2   boolr   )rM   is_canonicals     rV   __get_has_canonical_formatz4_compressed_sparse_matrix.__get_has_canonical_format  s    ( 9>Q)-D&&4d;; 	B).D&&677 	B::T\0@0B ; D DL)-l.>.>.@.@)A)AD&))rW   c                 D    t          |          | _        |r	d| _        dS dS )zTaken from SciPy as is.TN)r   r   has_sorted_indicesrM   r   s     rV   __set_has_canonical_formatz4_compressed_sparse_matrix.__set_has_canonical_format  s0    %)#YY" 	+&*D###	+ 	+rW   )fgetfsetc                 
   | j         j        dk    rd| _        net          | d          sU|                     | j        | j        | j        j        dz
            }t          |                                          | _        | j        S )a#  Determine whether the matrix has sorted indices.

        Returns
            bool:
                ``True`` if the indices of the matrix are in sorted order,
                otherwise ``False``.

        .. warning::
            Getting this property might synchronize the device.

        r   Tr   r    r   )	r1   r   r   r   _has_sorted_indices_kernr3   r2   r   r   )rM   	is_sorteds     rV   __get_sortedz&_compressed_sparse_matrix.__get_sorted  s    " 9>Q'+D$$455 	=55T\0@0B 6 D DI'+IMMOO'<'<D$''rW   c                 .    t          |          | _        d S r\   )r   r   r  s     rV   __set_sortedz&_compressed_sparse_matrix.__set_sorted  s    #'99   rW   c                     | j         S )zdReturns the shape of the matrix.

        Returns:
            tuple: Shape of the matrix.

        )rL   rM   s    rV   	get_shapez#_compressed_sparse_matrix.get_shape  s     {rW   c                 ,    || j         j        S t          )zReturns the number of stored values, including explicit zeros.

        Args:
            axis: Not supported yet.

        Returns:
            int: The number of stored values.

        )r1   r   r,   )rM   axiss     rV   getnnzz _compressed_sparse_matrix.getnnz  s     <9>!rW   c                 V    |                                  }|                                 |S )zReturn a copy of this matrix with sorted indices

        .. warning::
            Calling this function might synchronize the device.
        )r'   sort_indices)rM   As     rV   sorted_indicesz(_compressed_sparse_matrix.sorted_indices  s&     IIKK	rW   c                     t           r\   r]   r  s    rV   r  z&_compressed_sparse_matrix.sort_indices  s
     "!rW   c                     | j         rdS |                                 }|                                 |                     |                    | j                             d| _         dS )ae  Eliminate duplicate matrix entries by adding them together.

        .. note::
            This is an *in place* operation.

        .. warning::
            Calling this function might synchronize the device.

        .. seealso::
           :meth:`scipy.sparse.csr_matrix.sum_duplicates`,
           :meth:`scipy.sparse.csc_matrix.sum_duplicates`
        NT)has_canonical_formattocoosum_duplicatesrH   r/   r0   )rM   coos     rV   r  z(_compressed_sparse_matrix.sum_duplicates  sc     $ 	F jjllcll4;//000$(!!!rW   c                 ,   | j         d|z
           }t          j        |                              t          j                  }|r%t          j        | j        t          j        | j        i}n$t          j        | j	        t          j        | j
        i} ||         |fd| j                            t          j                  | j        dt          | j                  dz
           | j        dd         t          j        | j         |                   |f           |S )a  Reduce nonzeros with a ufunc over the minor axis when non-empty

        Can be applied to a function of self.data by supplying data parameter.
        Warning: this does not call sum_duplicates()

        Args:
            ufunc (object): Function handle giving the operation to be
                conducted.
            axis (int): Matrix over which the reduction should be
                conducted.

        Returns:
            (cupy.ndarray): Reduce result for nonzeros in each
            major_index.

        r    r    N)r&   r9   r4   rE   float64amax_max_nonzero_reduction_kernamin_min_nonzero_reduction_kern_max_reduction_kern_min_reduction_kernr1   r3   r=   int64)rM   ufuncr  nonzero	out_shaper   kernss          rV   _minor_reducez'_compressed_sparse_matrix._minor_reduce  s    " Jq4x(	j##**4<88 	:Y @Y @BEE Y 8Y 8:E 	ei\4i&&t|44k"73t{#3#3a#7"78k!""otz$*T2B'C'C	 	 	 
rW   c                 x   | j         d|z
           }t          j        |t                    }d                    t          j        | j        j                  t          j        |j                            }|t          j	        k    r| j
                            d|z             }n-|t          j        k    r| j                            d|z             } ||fd| j        | j        | j        dt!          | j                  dz
           | j        dd         t          j        | j         |                   |f           |S )a  Reduce nonzeros with a ufunc over the minor axis when non-empty

        Can be applied to a function of self.data by supplying data parameter.
        Warning: this does not call sum_duplicates()

        Args:
            ufunc (object): Function handle giving the operation to be
                conducted.
            axis (int): Maxtrix over which the reduction should be conducted

        Returns:
            (cupy.ndarray): Reduce result for nonzeros in each
            major_index

        r    r#   z_arg_reduction<{}, {}>r   r   r  N)r&   r9   r4   r-   r0   r   get_typenamer1   r$   argmax_max_arg_reduction_modget_functionargmin_min_arg_reduction_modr2   r3   r=   r'  )rM   r(  r  r*  r   ker_namekers          rV   _arg_minor_reducez+_compressed_sparse_matrix._arg_minor_reduce4  s&   * Jq4x(	j#... ,22 11 ++- - DK-::58;KLLCCdk!!-::58;KLLCYL$Y[.#dk**Q../[_djD)9::	 	 	 
rW   )NNF)T)r   )Fr\   )<__name__
__module____qualname___max_min_reduction_coder   	RawKernelstringTemplate
substituter%  r"  r&  r$  _argmax_argmin_code	RawModuler0  r3  ElementwiseKernelr	  r   rH   rZ   rB   r5   rf   ro   rt   rw   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   3_compressed_sparse_matrix__get_has_canonical_format3_compressed_sparse_matrix__set_has_canonical_formatpropertyr  %_compressed_sparse_matrix__get_sorted%_compressed_sparse_matrix__set_sortedr  r  r  r  r  r  r,  r6  r}   rW   rV   r   r      s       "H *%//00;; S/G 	< 	I 	I 
 #2%//00;;(S7I 	< 	K 	K#! #!
 *%//00;; S/G 	< 	I 	I 
 #2%//00;;(S7I 	< 	K 	K#! #!4l -U_V_011<<3 =    B B B	C C C -U_V_011<<3 =    B B B	C C C  7u6%	 5 6  6 "9!8%	 7"8 "8"h h h hT
" 
" 
" 
"" " "" " "" " "" " ":. . .. . .- - -- - -  G G G
8 8 8 8$H H H
) ) )> > >- - - -,- - - -,               *  *) ) )B- - -   @': ': ':R* * *@+ + + $8)C)CE E E( ( (4- - - "|,GGG     	 	 	" " "
) ) )4! ! !F( ( ( ( (rW   r   )r<  r   rC   scipy.sparser7   r6   ImportErrorr9   r*   r   
cupy._corer   cupy._creationr   cupyx.scipy.sparser   r   r   rF   r	   r
   r   rG   _minmax_mixin
IndexMixinr   r}   rW   rV   <module>rN     s_     OO   OOO                           $ $ $ $ $ $ # # # # # # 3 3 3 3 3 3 ' ' ' ' ' ' $ $ $ $ $ $ % % % % % %B B B B B 8 + 9 & 1B B B B Bs    