def tocsc(self, copy=False): """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. """ if self.data.size == 0: return csc.csc_matrix(self.shape, dtype=self.dtype) num_rows, num_cols = self.shape num_offsets, offset_len = self.data.shape row, mask = core.ElementwiseKernel( 'int32 offset_len, int32 offsets, int32 num_rows, ' 'int32 num_cols, T data', 'int32 row, bool mask', ''' int offset_inds = i % offset_len; row = offset_inds - offsets; mask = (row >= 0 && row < num_rows && offset_inds < num_cols && data != T(0)); ''', 'dia_tocsc')(offset_len, self.offsets[:, None], num_rows, num_cols, self.data) indptr = cupy.zeros(num_cols + 1, dtype='i') indptr[1:offset_len + 1] = cupy.cumsum(mask.sum(axis=0)) indptr[offset_len + 1:] = indptr[offset_len] indices = row.T[mask.T].astype('i', copy=False) data = self.data.T[mask.T] return csc.csc_matrix((data, indices, indptr), shape=self.shape, dtype=self.dtype)
def _compressed_sparse_stack(blocks, axis): """Fast path for stacking CSR/CSC matrices (i) vstack for CSR, (ii) hstack for CSC. """ other_axis = 1 if axis == 0 else 0 data = cupy.concatenate([b.data for b in blocks]) constant_dim = blocks[0].shape[other_axis] idx_dtype = sputils.get_index_dtype(arrays=[b.indptr for b in blocks], maxval=max(data.size, constant_dim)) indices = cupy.empty(data.size, dtype=idx_dtype) indptr = cupy.empty(sum(b.shape[axis] for b in blocks) + 1, dtype=idx_dtype) last_indptr = idx_dtype(0) sum_dim = 0 sum_indices = 0 for b in blocks: if b.shape[other_axis] != constant_dim: raise ValueError( 'incompatible dimensions for axis %d' % other_axis) indices[sum_indices:sum_indices+b.indices.size] = b.indices sum_indices += b.indices.size idxs = slice(sum_dim, sum_dim + b.shape[axis]) indptr[idxs] = b.indptr[:-1] indptr[idxs] += last_indptr sum_dim += b.shape[axis] last_indptr += b.indptr[-1] indptr[-1] = last_indptr if axis == 0: return csr.csr_matrix((data, indices, indptr), shape=(sum_dim, constant_dim)) else: return csc.csc_matrix((data, indices, indptr), shape=(constant_dim, sum_dim))
def transpose(self, axes=None, copy=False): """Returns a transpose matrix. Args: axes: This option is not supported. copy (bool): If ``True``, a returned matrix shares no data. Otherwise, it shared data arrays as much as possible. Returns: cupyx.scipy.sparse.spmatrix: Transpose matrix. """ if axes is not None: raise ValueError( 'Sparse matrices do not support an \'axes\' parameter because ' 'swapping dimensions is the only logical permutation.') shape = self.shape[1], self.shape[0] return csc.csc_matrix( (self.data, self.indices, self.indptr), shape=shape, copy=copy)
def tocsc(self, copy=False): """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 coo to csc conversion. Returns: cupyx.scipy.sparse.csc_matrix: Converted matrix. """ if self.nnz == 0: return csc.csc_matrix(self.shape, dtype=self.dtype) # copy is silently ignored (in line with SciPy) because both # sum_duplicates and coosort change the underlying data x = self.copy() x.sum_duplicates() cusparse.coosort(x, 'c') x = cusparse.coo2csc(x) x.has_canonical_format = True return x