예제 #1
0
 def _add_dense(self, other):
     if other.shape != self.shape:
         raise ValueError('Incompatible shapes.')
     dtype = upcast_char(self.dtype.char, other.dtype.char)
     order = self._swap('CF')[0]
     result = np.array(other, dtype=dtype, order=order, copy=True)
     M, N = self._swap(self.shape)
     y = result if result.flags.c_contiguous else result.T
     csr_todense(M, N, self.indptr, self.indices, self.data, y)
     return matrix(result, copy=False)
예제 #2
0
 def toarray(self, order=None, out=None):
     if out is None and order is None:
         order = self._swap('cf')[0]
     out = self._process_toarray_args(order, out)
     if not (out.flags.c_contiguous or out.flags.f_contiguous):
         raise ValueError('Output array must be C or F contiguous')
     # align ideal order with output array order
     if out.flags.c_contiguous:
         x = self.tocsr()
         y = out
     else:
         x = self.tocsc()
         y = out.T
     M, N = x._swap(x.shape)
     csr_todense(M, N, x.indptr, x.indices, x.data, y)
     return out
def fast_in_place_add(ndarray, matrix):
    """
    Performs fast in-place addition of a sparse CSC matrix to an ndarray of the
    same size

    This is based on the code found in:
        scipy/sparse/compressed.py:_cs_matrix._add_dense
    """
    if ndarray.shape != matrix.shape:
        raise ValueError("Shapes do not match: {} vs {}".format(
            ndarray.shape, matrix.shape))
    if not ndarray.flags.f_contiguous:
        raise ValueError("ndarray must be in Fortran order")
    # In order to use Compressed Sparse Row (csr) operations with Compressed
    # Sparse Column (csc) matrices we need to transpose the matrix
    # we're adding into. This is a fast operation which just returns a new view
    # on the underlying data.
    transposed = ndarray.transpose()
    rows, columns = transposed.shape
    _sparsetools.csr_todense(rows, columns, matrix.indptr, matrix.indices,
                             matrix.data, transposed)