Пример #1
0
 def test_sparseToDense(self, dtype):
     if not cusparse.check_availability('sparseToDense'):
         pytest.skip('sparseToDense is not available')
     m, n = self.shape
     x = scipy.sparse.random(m, n, density=self.density, format=self.format,
                             dtype=dtype)
     if self.format == 'csr':
         x = sparse.csr_matrix(x)
     elif self.format == 'csc':
         x = sparse.csc_matrix(x)
     elif self.format == 'coo':
         x = sparse.coo_matrix(x)
     y = cusparse.sparseToDense(x)
     testing.assert_array_equal(x.todense(), y)
Пример #2
0
    def toarray(self, order=None, out=None):
        """Returns a dense matrix representing the same value.

        Args:
            order ({'C', 'F', None}): Whether to store data in C (row-major)
                order or F (column-major) order. Default is C-order.
            out: Not supported.

        Returns:
            cupy.ndarray: Dense array representing the same matrix.

        .. seealso:: :meth:`scipy.sparse.csr_matrix.toarray`

        """
        order = 'C' if order is None else order.upper()
        if self.nnz == 0:
            return cupy.zeros(shape=self.shape, dtype=self.dtype, order=order)

        if self.dtype.char not in 'fdFD':
            return csr2dense(self, order)

        x = self.copy()
        x.has_canonical_format = False  # need to enforce sum_duplicates
        x.sum_duplicates()
        if (cusparse.check_availability('sparseToDense')
                and (not runtime.is_hip or (x.nnz > 0))):
            # On HIP, nnz=0 is problematic as of ROCm 4.2.0
            y = cusparse.sparseToDense(x)
            if order == 'F':
                return y
            elif order == 'C':
                return cupy.ascontiguousarray(y)
            else:
                raise ValueError('order not understood')
        else:
            # csr2dense returns F-contiguous array.
            if order == 'C':
                # To return C-contiguous array, it uses transpose.
                return cusparse.csc2dense(x.T).T
            elif order == 'F':
                return cusparse.csr2dense(x)
            else:
                raise ValueError('order not understood')