Example #1
0
    def _get_submatrix(self, major=None, minor=None, copy=False):
        """Return a submatrix of this matrix.
        major, minor: None or slice with step 1
        """
        M, N = self._swap(*self.shape)
        i0, i1 = self._process_slice(major, M)
        j0, j1 = self._process_slice(minor, N)

        is_major_full = i0 == 0 and i1 == M
        is_minor_full = j0 == 0 and j1 == N

        if is_major_full and is_minor_full:
            return self.copy() if copy else self

        indptr, indices, data = self.indptr, self.indices, self.data

        if not is_major_full:
            indptr, indices, data = _index._get_csr_submatrix_major_axis(
                indptr, indices, data, i0, i1)
        if not is_minor_full:
            indptr, indices, data = _index._get_csr_submatrix_minor_axis(
                indptr, indices, data, j0, j1)

        shape = self._swap(i1 - i0, j1 - j0)
        return self.__class__((data, indices, indptr),
                              shape=shape,
                              dtype=self.dtype,
                              copy=False)
Example #2
0
    def _major_slice(self, idx, copy=False):
        """Index along the major axis where idx is a slice object.
        """

        if idx == slice(None):
            return self.copy() if copy else self

        M, N = self._swap(*self.shape)
        start, stop, step = idx.indices(M)
        M = len(range(start, stop, step))
        new_shape = self._swap(M, N)
        if M == 0 or self.nnz == 0:
            return self.__class__(new_shape)

        if step == 1:
            indptr, indices, data = _index._get_csr_submatrix_major_axis(
                self.indptr, self.indices, self.data, start, stop)
        else:
            rows = cupy.arange(start,
                               start + M * step,
                               step,
                               dtype=self.indptr.dtype)
            indptr, indices, data = _index._csr_row_index(
                rows, self.indptr, self.indices, self.data)

        return self.__class__((data, indices, indptr),
                              shape=new_shape,
                              copy=False)
Example #3
0
    def _get_intXint(self, row, col):
        major, minor = self._swap(row, col)

        indptr, indices, data = _index._get_csr_submatrix_major_axis(
            self.indptr, self.indices, self.data, major, major + 1)
        indptr, indices, data = _index._get_csr_submatrix_minor_axis(
            indptr, indices, data, minor, minor + 1)
        return data.sum(dtype=self.dtype)
Example #4
0
 def _get_intXint(self, row, col):
     major, minor = self._swap(row, col)
     data, indices, _ = _index._get_csr_submatrix_major_axis(
         self.data, self.indices, self.indptr, major, major + 1)
     dtype = data.dtype
     res = cupy.zeros((), dtype=dtype)
     if dtype.kind == 'c':
         _index._compress_getitem_complex_kern(
             data.real, data.imag, indices, minor, res.real, res.imag)
     else:
         _index._compress_getitem_kern(data, indices, minor, res)
     return res
Example #5
0
File: csr.py Project: anaruse/cupy
    def getrow(self, i):
        """Returns a copy of row i of the matrix, as a (1 x n)
        CSR matrix (row vector).

        Args:
            i (integer): Row

        Returns:
            cupyx.scipy.sparse.csr_matrix: Sparse matrix with single row
        """
        M, N = self.shape
        i = _index._normalize_index(i, M, 'index')
        indptr, indices, data = _index._get_csr_submatrix_major_axis(
            self.indptr, self.indices, self.data, i, i + 1)
        return csr_matrix((data, indices, indptr),
                          shape=(1, N),
                          dtype=self.dtype,
                          copy=False)
Example #6
0
    def _major_slice(self, idx, copy=False):
        """Index along the major axis where idx is a slice object.
        """
        M, N = self._swap(*self.shape)
        start, stop, step = idx.indices(M)

        if start == 0 and stop == M and step == 1:
            return self.copy() if copy else self

        M = len(range(start, stop, step))
        new_shape = self._swap(M, N)

        if step == 1:
            if M == 0 or self.nnz == 0:
                return self.__class__(new_shape, dtype=self.dtype)
            return self.__class__(_index._get_csr_submatrix_major_axis(
                self.data, self.indices, self.indptr, start, stop),
                                  shape=new_shape,
                                  copy=copy)
        rows = cupy.arange(start, stop, step, dtype=self.indptr.dtype)
        return self._major_index_fancy(rows)