Skip to content

Commit

Permalink
ENH: Altered _binop and boolean comparisons to use the boolean data o…
Browse files Browse the repository at this point in the history
…utput from sparsetools routines.
  • Loading branch information
cowlicks committed Jun 23, 2013
1 parent 859b811 commit 2d075b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
12 changes: 8 additions & 4 deletions scipy/sparse/bsr.py
Expand Up @@ -502,10 +502,10 @@ def prune(self):

# utility functions
def _binopt(self, other, op, in_shape=None, out_shape=None):
"""apply the binary operation fn to two sparse matrices"""
"""Apply the binary operation fn to two sparse matrices."""

# ideally we'd take the GCDs of the blocksize dimensions
# and explode self and other to match
# Ideally we'd take the GCDs of the blocksize dimensions
# and explode self and other to match.
other = self.__class__(other, blocksize=self.blocksize)

# e.g. bsr_plus_bsr, etc.
Expand All @@ -516,7 +516,11 @@ def _binopt(self, other, op, in_shape=None, out_shape=None):
max_bnnz = len(self.data) + len(other.data)
indptr = np.empty_like(self.indptr)
indices = np.empty(max_bnnz, dtype=np.intc)
data = np.empty(R*C*max_bnnz, dtype=upcast(self.dtype,other.dtype))

if op == '_ne_':
data = np.empty(R*C*max_bnnz, dtype=np.bool_)
else:
data = np.empty(R*C*max_bnnz, dtype=upcast(self.dtype,other.dtype))

fn(self.shape[0]//R, self.shape[1]//C, R, C,
self.indptr, self.indices, np.ravel(self.data),
Expand Down
22 changes: 13 additions & 9 deletions scipy/sparse/compressed.py
Expand Up @@ -174,11 +174,11 @@ def __eq__(self, other):
if isscalarlike(other):
other_arr = self.copy()
other_arr.data[:] = other
res = self._binopt(other_arr,'_ne_').astype(bool)
res = self._binopt(other_arr,'_ne_')
if other == 0:
warn("Comparing a sparse matrix with 0 using == is inefficient"
", try using != instead.")
all_true = self.__class__(np.ones(self.shape).astype(bool))
all_true = self.__class__(np.ones(self.shape, dtype=np.bool_))
return all_true - res
else:
self_as_bool = self.astype(bool)
Expand All @@ -195,8 +195,8 @@ def __eq__(self, other):
return False
elif self.format != other.format:
other = other.asformat(self.format)
res = self._binopt(other,'_ne_').astype(bool)
all_true = self.__class__(np.ones(self.shape).astype(bool))
res = self._binopt(other,'_ne_')
all_true = self.__class__(np.ones(self.shape, dtype=np.bool_))
return all_true - res
else:
return False
Expand All @@ -207,13 +207,13 @@ def __ne__(self, other):
if other != 0:
warn("Comparing a sparse matrix with a nonzero scalar using !="
" is inefficient, try using == instead.")
all_true = self.__class__(np.ones(self.shape).astype(bool))
all_true = self.__class__(np.ones(self.shape), dtype=np.bool_)
res = (self == other)
return all_true - res
else:
other_arr = self.copy()
other_arr.data[:] = other
return self._binopt(other_arr,'_ne_').astype(bool)
return self._binopt(other_arr,'_ne_')
# Dense other.
elif isdense(other):
return self.todense() != other
Expand All @@ -224,7 +224,7 @@ def __ne__(self, other):
return True
elif self.format != other.format:
other = other.asformat(self.format)
return self._binopt(other,'_ne_').astype(bool)
return self._binopt(other,'_ne_')
else:
return True

Expand Down Expand Up @@ -753,7 +753,7 @@ def _with_data(self,data,copy=True):
shape=self.shape,dtype=data.dtype)

def _binopt(self, other, op):
"""apply the binary operation fn to two sparse matrices"""
"""apply the binary operation fn to two sparse matrices."""
other = self.__class__(other)

# e.g. csr_plus_csr, csr_minus_csr, etc.
Expand All @@ -762,7 +762,11 @@ def _binopt(self, other, op):
maxnnz = self.nnz + other.nnz
indptr = np.empty_like(self.indptr)
indices = np.empty(maxnnz, dtype=np.intc)
data = np.empty(maxnnz, dtype=upcast(self.dtype,other.dtype))

if op == '_ne_':
data = np.empty(maxnnz, dtype=np.bool_)
else:
data = np.empty(maxnnz, dtype=upcast(self.dtype,other.dtype))

fn(self.shape[0], self.shape[1],
self.indptr, self.indices, self.data,
Expand Down

0 comments on commit 2d075b4

Please sign in to comment.